commit 2920262d3d1cf88083a26c963bf02824ee5af1f7
parent eed7a473d73c4c4e614c9692ef708ae6f222b8a4
Author: St John Karp <contact@stjo.hn>
Date: Tue, 30 Jun 2020 05:58:32 -0500
Remove obsolete helper predicates & factor out append_lists
Removed some helper predicates that are no longer used.
Factored out the append_lists predicate, which is typically
a sign in Prolog that you're doing something wrong.
Diffstat:
3 files changed, 36 insertions(+), 56 deletions(-)
diff --git a/generate_rss.pl b/generate_rss.pl
@@ -41,24 +41,25 @@ files_to_articles([Filename|Filenames], [article(Date, Title, Link, Description)
files_to_articles(Filenames, Articles).
-% get_link(?Filename, ?Link).
+% get_link(?Filename, ?Path).
% Calculate a file's URL, given its current path.
-get_link(Filename, Link):-
+get_link(Filename, LinkPath):-
atom_codes(Filename, FilenameCodes),
- % Just assert that this is an index file before we go further.
- % Backtracking after this point will take us down a rabbit hole.
- append_lists(_, "index.html", FilenameCodes),
- site_url(URL, []),
- append_lists(_, "/output", StartPath),
- append_lists(StartPath, Path, FilenameCodes),
- append_lists(PathWithoutFile, "index.html", Path),
- append_lists(URL, PathWithoutFile, Link).
+ file_path(RelativePath, FilenameCodes, []),
+ link_path(RelativePath, LinkPath, []).
-get_link(Filename, Link):-
- atom_codes(Filename, FilenameCodes),
- site_url(URL, []),
- append_lists(_, "/output", StartPath),
- append_lists(StartPath, Path, FilenameCodes),
- append_lists(PathWithoutExtension, ".html", Path),
- append_lists(PathWithoutExtension, "/", PathWithSlash),
- append_lists(URL, PathWithSlash, Link).
+file_path(Path) -->
+ anything(_),
+ "/output",
+ anything(Path),
+ "/index.html".
+
+file_path(Path) -->
+ anything(_),
+ "/output",
+ anything(Path),
+ ".html".
+
+link_path(RelativePath) -->
+ anything(RelativePath),
+ "/".
diff --git a/helpers.pl b/helpers.pl
@@ -14,41 +14,19 @@ read_file(Stream, [Code|Rest]):-
read_file(Stream, Rest).
-% take_last(+Max, +List, -Results).
-% Return the last Max elements of List.
-take_last(_, [], []).
-
-take_last(Max, [First|Rest], Result):-
- take_last(Max, Rest, ResultSoFar),
- take_append(Max, First, ResultSoFar, Result).
-
-take_append(Max, _, ResultSoFar, ResultSoFar):-
- length(ResultSoFar, Max).
-
-take_append(_, Item, ResultSoFar, [Item|ResultSoFar]).
-
-
% replace(+FindCodes, +ReplaceCodes, +Haystack, -Result).
% Find instances of FindCodes in Haystack and replace with ReplaceCodes.
% All four arguments are lists of character codes.
-replace(_, _, [], []).
-
replace(FindCodes, ReplaceCodes, Haystack, Result):-
- append_lists(FindCodes, HaystackMinusMatch, Haystack),
- replace(FindCodes, ReplaceCodes, HaystackMinusMatch, ReplacedHaystackMinusMatch),
- append_lists(ReplaceCodes, ReplacedHaystackMinusMatch, Result).
-
-replace(FindCodes, ReplaceCodes, [Code|Haystack], [Code|Result]):-
- replace(FindCodes, ReplaceCodes, Haystack, Result).
-
+ substrings(FindCodes, Substrings, Haystack, []),
+ substrings(ReplaceCodes, Substrings, Result, []).
-% append_lists(?List1, ?List2, ?Result).
-% Append two lists.
-% This is not an ISO predicate, so I've definded it here for portability.
-append_lists([], List2, List2).
+substrings(Delimiter, [Substring|Substrings]) -->
+ anything(Substring),
+ Delimiter,
+ substrings(Delimiter, Substrings).
-append_lists([First|List1], List2, [First|Result]):-
- append_lists(List1, List2, Result).
+substrings(_, [Substring]) --> anything(Substring).
% write_codes(+CodesList).
@@ -92,4 +70,4 @@ whitespace --> " ", whitespace.
newline --> "\n".
-tab --> "\t".
-\ No newline at end of file
+tab --> "\t".
diff --git a/rss.pl b/rss.pl
@@ -49,14 +49,16 @@ item_description(Description) -->
Description,
"</description>".
-item_link(Link) -->
+item_link(Path) -->
"<link>",
- Link,
+ site_url,
+ Path,
"</link>".
-item_guid(Link) -->
+item_guid(Path) -->
"<guid>",
- Link,
+ site_url,
+ Path,
"</guid>".
language -->
@@ -83,15 +85,15 @@ items([]) --> [].
items([First|Rest]) --> item(First), items(Rest).
-item(article(Date, Title, Link, Description)) -->
+item(article(Date, Title, Path, Description)) -->
newline, tab, tab,
item_open,
newline, tab, tab, tab,
item_title(Title),
newline, tab, tab, tab,
- item_link(Link),
+ item_link(Path),
newline, tab, tab, tab,
- item_guid(Link),
+ item_guid(Path),
newline, tab, tab, tab,
item_description(Description),
newline, tab, tab, tab,