19. Writing HTML
The last step turns the term into HTML. You already know the tool for it. A grammar can write text as well as read it.
Writing text with a grammar
phrase/2 with an unknown list writes a sentence, as you saw in chapter 14. So the HTML writer is a grammar whose output is a list of characters.
% Writes the characters of an atom.
word(A) --> { atom_chars(A, Cs) }, chars(Cs).
chars([]) --> [].
chars([C | Cs]) --> [C], chars(Cs).
% Writes an element, with its content between the tags.
element(Tag, Content) --> ['<'], word(Tag), ['>'], Content, ['<', '/'], word(Tag), ['>'].element//2 takes the content as an argument. Content is a grammar rule, such as word(hello), and element//2 runs it between the tags. Passing a rule to another rule is like passing a callback to a function in JavaScript.
Escaping
Text such as a < b must be written as a < b in HTML, or the browser reads < as the start of a tag.
escaped(A) --> { atom_chars(A, Cs) }, escaped_chars(Cs).
escaped_chars([]) --> [].
escaped_chars([C | Cs]) --> escape(C), escaped_chars(Cs).
escape('<') --> !, word('<').
escape('>') --> !, word('>').
escape('&') --> !, word('&').
escape(C) --> [C].The cuts make sure that < is written only as <, and never as itself through the last clause.
Blocks and inlines
The writer follows the shape of the term, with one rule for each kind of node. Compare it with plain_text/2 in chapter 16. It is the same walk over the tree, but it writes HTML instead of plain text.
html_blocks([]) --> [].
html_blocks([B | Bs]) --> html_block(B), ['\n'], html_blocks(Bs).
html_block(h(N, I)) --> { heading_tag(N, Tag) }, element(Tag, html_inlines(I)).
html_block(p(I)) --> element(p, html_inlines(I)).
html_block(ul(Items)) --> element(ul, html_items(Items)).
heading_tag(1, h1).
heading_tag(2, h2).
heading_tag(3, h3).
heading_tag(4, h4).
heading_tag(5, h5).
heading_tag(6, h6).
html_items([]) --> [].
html_items([I | Is]) --> element(li, html_inlines(I)), html_items(Is).
html_inlines([]) --> [].
html_inlines([I | Is]) --> html_inline(I), html_inlines(Is).
html_inline(text(A)) --> escaped(A).
html_inline(code(A)) --> element(code, escaped(A)).
html_inline(strong(I)) --> element(strong, html_inlines(I)).
html_inline(em(I)) --> element(em, html_inlines(I)).The converter
Here is the whole converter, the reader of chapters 17 and 18 and the writer of this chapter. md_html/2 reads the Markdown into a term, then writes the term as HTML. Change the sample and run it again.
What you built
The converter is about 80 lines of Prolog, and it has no loops and no string indexes. Look back at what each part used.
- The reader is a grammar of Markdown (chapters 14 and 15), with cuts where Markdown takes the longest reading (chapters 9 and 17).
- The term in the middle is plain data, so you can ask questions about it, as in chapter 16.
- The writer is a grammar of HTML that follows the shape of the term. It is recursion over a tree (chapters 4 and 6).
Try it
- Add a block for code, three backticks on a line, then lines of code, then three backticks. Write it as
<pre><code>...</code></pre>. - Add ordered lists, lines that start with
1., with the termol(Items)and the tagol. - If you added links in chapter 18, write them as
<a href="url">text</a>. You will need a version ofelement//2that writes an attribute.
You have reached the end of the tutorial. The appendix lists what is different in C#Prolog from other Prolog systems, and what to read next.