17. Reading blocks
In this chapter, you write the grammar that splits Markdown text into blocks, headings, paragraphs, and lists. To keep it simple, the content of each block stays a plain atom for now. Chapter 18 reads the inside of the lines.
Reading one line
Almost everything in Markdown is about lines. line//1 reads the characters up to the end of a line, and gives them as a list.
line([]) --> ['\n'], !.
line([]) --> eos, !.
line([C | Cs]) --> [C], line(Cs).
eos([], []).- A line ends at a new line character, which is read and dropped.
- A line also ends at the end of the text.
eosmeans "end of sequence". It is written as a plain predicate with the two extra arguments of chapter 14. It is true when nothing is left. - Otherwise, read one character and the rest of the line.
The cuts say "if the line ends here, don't try to read more". Without them, Prolog could later backtrack and read the new line as a normal character.
Headings
A heading is one to six # characters, a space, and the rest of the line.
block(h(N, Text)) --> hashes(N), { N >= 1, N =< 6 }, [' '], line(Cs), { atom_chars(Text, Cs) }.
hashes(N) --> ['#'], hashes(N0), { N is N0 + 1 }.
hashes(0) --> [].hashes//1 counts the # characters. The first clause tries to read as many as it can, so ### gives 3.
Lists
A list is one or more lines that start with - .
block(ul(Items)) --> items(Items), { Items \= [] }.
items([I | Is]) --> ['-', ' '], line(Cs), { atom_chars(I, Cs) }, items(Is).
items([]) --> [].Paragraphs
A paragraph is one or more lines that are not empty, and that don't start a heading or a list. The lines are joined with a space.
block(p(Text)) --> para_lines(Cs), { atom_chars(Text, Cs) }.
para_lines(Cs) --> plain_line(L), para_rest(L, Cs).
para_rest(L, Cs) --> plain_line(L2), !, { append(L, [' ' | L2], L3) }, para_rest(L3, Cs).
para_rest(L, L) --> [].
plain_line(L) --> line(L), { L \= [], L \= ['#' | _], L \= ['-', ' ' | _] }.para_rest//2 carries the text so far in its first argument, like the accumulators of chapter 7.
The whole document
A document is a list of blocks, with empty lines between them.
blocks(Bs) --> blank_lines, blocks1(Bs).
blocks1([]) --> eos.
blocks1([B | Bs]) --> block(B), blank_lines, blocks1(Bs).
blank_lines --> ['\n'], !, blank_lines.
blank_lines --> [].Here is everything together. Run it, and press Next answer.
The first answer is right. But the second answer splits the list into two lists, one item each. The text has only one list, so this answer is wrong.
One text, two readings
Why does Prolog give a second answer? The grammar allows it. items//1 can stop after - facts. Then blank_lines reads nothing, and the next block starts a new list with - rules. Nothing in the grammar says that a list must take all the items it can.
This is the backtracking of chapter 8 at work. Prolog finds every way to read the text. For a programming language, more than one reading is usually a bug in the grammar.
The fix is a cut. Once a line starts with - , it belongs to the current list.
items([I | Is]) --> ['-', ' '], !, line(Cs), { atom_chars(I, Cs) }, items(Is).
items([]) --> [].Now there is only one answer. Note that the price of the cuts is the same as in chapter 9. This grammar can only read. It can't write Markdown from a term. For a converter, that is fine, because chapter 19 writes the output with a second grammar.
Try it
Use the last runnable box above, the one with the cut in items//1.
- Add a block for a quote, a line that starts with
>. Give it the termquote(Text). You need a new clause ofblock//1. Also make sure thatplain_line//1doesn't take a quote line into a paragraph. - Add a line
#hashtagto the sample, with no space after the#, and run the box. What happens? Change the grammar so that such a line becomes a paragraph.