squeeze

A static site generator that can put the toothpaste back in the tube.
git clone https://git.mulligrubs.me/squeeze
Log | Files | Refs | README | LICENSE

helpers.pl (1954B)


      1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      2 % File: helpers.pl
      3 % Description: Misc. utility predicates.
      4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      5 
      6 % read_file(+Stream, -Codes).
      7 %	Read a file to a list of character codes.
      8 read_file(Stream, Codes):-
      9 	get_code(Stream, Code),
     10 	read_file_next(Code, Stream, Codes).
     11 
     12 read_file_next(-1, _, []).
     13 
     14 read_file_next(Code, Stream, [Code|Rest]):-
     15 	read_file(Stream, Rest).
     16 
     17 
     18 % replace(+FindCodes, +ReplaceCodes, +Haystack, -Result).
     19 %	Find instances of FindCodes in Haystack and replace with ReplaceCodes.
     20 %	All four arguments are lists of character codes.
     21 replace(FindCodes, ReplaceCodes, Haystack, Result):-
     22 	substrings(FindCodes, Substrings, Haystack, []),
     23 	substrings(ReplaceCodes, Substrings, Result, []).
     24 
     25 substrings(Delimiter, [Substring|Substrings]) -->
     26 	anything(Substring),
     27 	Delimiter,
     28 	substrings(Delimiter, Substrings).
     29 
     30 substrings(_, [Substring]) --> anything(Substring).
     31 
     32 
     33 % write_codes(+CodesList).
     34 %   Loop through a list of character codes, convert each one to a
     35 %   character, and write them to the current output stream one at
     36 %   a time. This is better than converting the whole list to an atom
     37 %   with atom_codes/2, which can trigger a segfault if the atom is too long.
     38 write_codes(_, []).
     39 
     40 write_codes(Stream, [X|Rest]):-
     41 	char_code(Char, X),
     42 	write(Stream, Char),
     43 	write_codes(Stream, Rest).
     44 
     45 
     46 % join(?List, +Separator, ?Atom).
     47 %   Join elements of a list into an atom separated by a separator.
     48 %   Written specifically as a join predicate, but should work as a split.
     49 join([], _, '').
     50 
     51 join([A], _, A).
     52 
     53 join([First|Rest], Separator, Result):-
     54 	join(Rest, Separator, End),
     55 	atom_concat(First, Separator, FirstPlusSeparator),
     56 	atom_concat(FirstPlusSeparator, End, Result).
     57 
     58 
     59 anything([]) --> [].
     60 
     61 anything([X|Rest]) --> [X], anything(Rest).
     62 
     63 
     64 whitespace --> [].
     65 
     66 whitespace --> newline, whitespace.
     67 
     68 whitespace --> tab, whitespace.
     69 
     70 whitespace --> " ", whitespace.
     71 
     72 newline --> "\n".
     73 
     74 tab --> "\t".