Fix list item parsing and headline token lvls

As headlines are always lvl (indent) 0 I thought it would be clever to abuse
the lvl field to store the headline lvl. Well, here we are - it wasn't clever.

List items only end when their parent ends or they run into something that's
not indented enough - everything else becomes part of the list item. Abusing
the token.lvl field (indent) for the headline lvl means headlines look indented
to the list item parsing logic - i.e. they become part of the list item if the
headline has a high enough lvl. That should never happen - so let's get rid of
the hack and (re-)calculate the headline lvl when we need it.
This commit is contained in:
Niklas Fasching 2020-07-22 13:49:58 +02:00
parent add727c011
commit 64b2b22270
4 changed files with 44 additions and 6 deletions

View file

@ -35,14 +35,14 @@ var tagRegexp = regexp.MustCompile(`(.*?)\s+(:[A-Za-z0-9_@#%:]+:\s*$)`)
func lexHeadline(line string) (token, bool) { func lexHeadline(line string) (token, bool) {
if m := headlineRegexp.FindStringSubmatch(line); m != nil { if m := headlineRegexp.FindStringSubmatch(line); m != nil {
return token{"headline", len(m[1]), m[2], m}, true return token{"headline", 0, m[2], m}, true
} }
return nilToken, false return nilToken, false
} }
func (d *Document) parseHeadline(i int, parentStop stopFn) (int, Node) { func (d *Document) parseHeadline(i int, parentStop stopFn) (int, Node) {
t, headline := d.tokens[i], Headline{} t, headline := d.tokens[i], Headline{}
headline.Lvl = t.lvl headline.Lvl = len(t.matches[1])
headline.Index = d.addHeadline(&headline) headline.Index = d.addHeadline(&headline)
@ -69,7 +69,7 @@ func (d *Document) parseHeadline(i int, parentStop stopFn) (int, Node) {
headline.Title = d.parseInline(text) headline.Title = d.parseInline(text)
stop := func(d *Document, i int) bool { stop := func(d *Document, i int) bool {
return parentStop(d, i) || d.tokens[i].kind == "headline" && d.tokens[i].lvl <= headline.Lvl return parentStop(d, i) || d.tokens[i].kind == "headline" && len(d.tokens[i].matches[1]) <= headline.Lvl
} }
consumed, nodes := d.parseMany(i+1, stop) consumed, nodes := d.parseMany(i+1, stop)
if len(nodes) > 0 { if len(nodes) > 0 {

View file

@ -72,9 +72,15 @@
</li> </li>
<li><a href="#headline-30">index out of range in explicit line break parsing</a> <li><a href="#headline-30">index out of range in explicit line break parsing</a>
</li> </li>
<li><a href="#headline-31">list items don&#39;t end on child headline</a>
<ul>
<li><a href="#headline-32">followed by a child headline</a>
</li>
</ul> </ul>
</li> </li>
<li><a href="#headline-31">Footnotes</a> </ul>
</li>
<li><a href="#headline-33">Footnotes</a>
</li> </li>
</ul> </ul>
</nav> </nav>
@ -550,10 +556,34 @@ index out of range in explicit line break parsing
<p>0\\ </p> <p>0\\ </p>
</div> </div>
</div> </div>
<div id="outline-container-headline-31" class="outline-4">
<h4 id="headline-31">
list items don&#39;t end on child headline
</h4>
<div id="outline-text-headline-31" class="outline-text-4">
<ul>
<li>
<p>a list item</p>
</li>
</ul>
<div id="outline-container-headline-32" class="outline-5">
<h5 id="headline-32">
followed by a child headline
</h5>
<div id="outline-text-headline-32" class="outline-text-5">
<ul>
<li>
<p>followed by another list item</p>
</li>
</ul>
</div> </div>
</div> </div>
<div id="outline-container-headline-31" class="outline-2"> </div>
<h2 id="headline-31"> </div>
</div>
</div>
<div id="outline-container-headline-33" class="outline-2">
<h2 id="headline-33">
Footnotes Footnotes
</h2> </h2>
</div> </div>

View file

@ -140,6 +140,10 @@ When inserting an image link like [[/home/amos/Pictures/Screenshots/img-2017-09-
*** index out of range in explicit line break parsing *** index out of range in explicit line break parsing
0\\ 0\\
*** list items don't end on child headline
- a list item
**** followed by a child headline
- followed by another list item
* Footnotes * Footnotes
[fn:1] a footnote /with/ *markup* [fn:1] a footnote /with/ *markup*

View file

@ -140,6 +140,10 @@ When inserting an image link like [[/home/amos/Pictures/Screenshots/img-2017-09-
*** index out of range in explicit line break parsing *** index out of range in explicit line break parsing
0\\ 0\\
*** list items don't end on child headline
- a list item
**** followed by a child headline
- followed by another list item
* Footnotes * Footnotes
[fn:1] a footnote /with/ *markup* [fn:1] a footnote /with/ *markup*