generate_rss.pl (2813B)
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 % File: generate_rss.pl 3 % Description: Predicates to generate an RSS file. 4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 5 6 :- include('helpers.pl'). 7 :- include('rss.pl'). 8 9 % generate_rss(+Filenames). 10 % Filenames is a list of atoms containing paths to all Markdown files with a date. 11 % These files will be read and used to generate an RSS of the most 12 % recent posts. 13 generate_rss(Filenames, BuildDate):- 14 % Read in all the files so we have their dates and contents. 15 files_to_articles(Filenames, Articles), 16 % Convert to RSS and write to stdout. 17 rss(BuildDate, Articles, RSSCodes, []), 18 write_codes(user_output, RSSCodes), 19 halt. 20 21 % generate_rss. 22 % Alternative interface to generate_rss(+Filenames) that reads 23 % the list of files from stdin. This allows the filenames to be piped 24 % from the output of another command like grep. 25 generate_rss(BuildDate):- 26 read_file(user_input, FileListCodes), 27 file_list(FileList, FileListCodes, []), 28 generate_rss(FileList, BuildDate). 29 30 31 file_list([]) --> []. 32 33 file_list([File|FileList]) --> 34 anything(FileCodes), 35 newline, 36 file_list(FileList), 37 { atom_codes(File, FileCodes) }. 38 39 40 % files_to_articles(+Filenames, -Articles). 41 % Read in each file as an article predicate. 42 files_to_articles([], []). 43 44 files_to_articles([Filename|Filenames], [article(FormattedDate, FormattedTitle, Link, Description)|Articles]):- 45 open(Filename, read, Stream), 46 read_file(Stream, HTML), 47 close(Stream), 48 % Grab the link. 49 get_link(Filename, Link), 50 % Extract the title, entry, etc. from the HTML. 51 page(Entry, _, _, Date, Title, HTML, []), 52 % Format the date according to RFC 822. 53 format_date(FormattedDate, Date), 54 % XML escape the description. 55 replace("&", "&", Entry, EntryAmp), 56 replace("<", "<", EntryAmp, EntryLT), 57 replace(">", ">", EntryLT, Description), 58 % Convert named HTML entities to numeric entities in the title. 59 % Named entities don't work because they're not defined, 60 % but numeric ones are fine. 61 replace("&", "&", Title, TitleAmp), 62 replace("‘", "‘", TitleAmp, TitleLSQuo), 63 replace("’", "’", TitleLSQuo, TitleRSQuo), 64 replace("“", "“", TitleRSQuo, TitleLDQuo), 65 replace("”", "”", TitleLDQuo, TitleRDQuo), 66 replace("…", "…", TitleRDQuo, FormattedTitle), 67 files_to_articles(Filenames, Articles). 68 69 70 % get_link(?Filename, ?Path). 71 % Calculate a file's URL, given its current path. 72 get_link(Filename, LinkPath):- 73 atom_codes(Filename, FilenameCodes), 74 file_path(RelativePath, FilenameCodes, []), 75 link_path(RelativePath, LinkPath, []). 76 77 file_path(Path) --> 78 anything(_), 79 "/output", 80 anything(Path), 81 "/index.html". 82 83 file_path(Path) --> 84 anything(_), 85 "/output", 86 anything(Path), 87 ".html". 88 89 link_path(RelativePath) --> 90 anything(RelativePath), 91 "/".