playfair.pl (1702B)
1 %-------------------------------------------------------- 2 % 3 % Filename: playfair.pl 4 % Author: St John Karp 5 % Date: 13 December 2021 6 % Version: 3.0 7 % 8 % Purpose: 9 % A program to format stage play scripts. 10 % 11 % Copyright: 12 % Playfair Script Formatter by St John Karp is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. 13 % 14 %------------------------------------------------------- 15 16 :- include('html.pl'). 17 :- include('script.pl'). 18 19 % parse_entry. 20 % Read in a script file from stdin. 21 play_to_html:- 22 read_file(user_input, RawScript), 23 script(ParsedScript, RawScript, []), 24 html(ParsedScript, HTML, []), 25 write_codes(user_output, HTML), 26 halt. 27 28 29 % read_file(+Stream, -Codes). 30 % Read a file to a list of character codes. 31 read_file(Stream, Codes):- 32 get_code(Stream, Code), 33 read_file_next(Code, Stream, Codes). 34 35 read_file_next(-1, _, []). 36 37 read_file_next(Code, Stream, [Code|Rest]):- 38 read_file(Stream, Rest). 39 40 41 % write_codes(+CodesList). 42 % Loop through a list of character codes, convert each one to a 43 % character, and write them to the current output stream one at 44 % a time. This is better than converting the whole list to an atom 45 % with atom_codes/2, which can trigger a segfault if the atom is too long. 46 write_codes(_, []). 47 48 write_codes(Stream, [X|Rest]):- 49 char_code(Char, X), 50 write(Stream, Char), 51 write_codes(Stream, Rest). 52 53 54 text(Forbidden, Text) --> 55 anything(Forbidden, Text), 56 { Text \= [] }. 57 58 59 anything(_, []) --> []. 60 61 anything(Forbidden, [X|Rest]) --> 62 [X], 63 anything(Forbidden, Rest), 64 { not_forbidden(Forbidden, X) }. 65 66 67 not_forbidden([], _). 68 69 not_forbidden([A|Rest], X):- 70 A \= X, 71 not_forbidden(Rest, X). 72 73 74 newline --> "\n". 75 76 tab --> "\t".