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