Support blocks with unindented content

list items only contain content that is indented to their respective
level. Except when that content is inside a block. To allow for this we have to
ignore the parentStop when parsing a block and just include everything until
the end of that block.
Can't think of any problems with this right now. Let's see if this comes
back to bite me.
This commit is contained in:
Niklas Fasching 2018-12-18 23:36:51 +01:00
parent ade2a1c875
commit 42dc70e7ad
6 changed files with 87 additions and 14 deletions

View file

@ -31,24 +31,24 @@ func (d *Document) parseBlock(i int, parentStop stopFn) (int, Node) {
name, parameters := t.content, strings.Fields(t.matches[3])
trim := trimIndentUpTo(d.tokens[i].lvl)
stop := func(d *Document, i int) bool {
return parentStop(d, i) || (d.tokens[i].kind == "endBlock" && d.tokens[i].content == name)
return i >= len(d.tokens) || (d.tokens[i].kind == "endBlock" && d.tokens[i].content == name)
}
block, consumed, i := Block{name, parameters, nil}, 0, i+1
block, i := Block{name, parameters, nil}, i+1
if isRawTextBlock(name) {
rawText := ""
for ; !stop(d, i); i++ {
rawText += trim(d.tokens[i].matches[0]) + "\n"
}
consumed = i - start
block.Children = d.parseRawInline(rawText)
} else {
consumed, block.Children = d.parseMany(i, stop)
consumed++ // line with BEGIN
consumed, nodes := d.parseMany(i, stop)
block.Children = nodes
i += consumed
}
if parentStop(d, i) {
return 0, nil
if i < len(d.tokens) && d.tokens[i].kind == "endBlock" && d.tokens[i].content == name {
return i + 1 - start, block
}
return consumed + 1, block
return 0, nil
}
func trimIndentUpTo(max int) func(string) string {

View file

@ -162,8 +162,11 @@ func (w *OrgWriter) writeFootnoteDefinition(f FootnoteDefinition) {
}
func (w *OrgWriter) writeParagraph(p Paragraph) {
w.writeNodes(p.Children...)
w.WriteString("\n")
content := w.nodesAsString(p.Children...)
if len(content) > 0 && content[0] != '\n' {
w.WriteString(w.indent)
}
w.WriteString(content + "\n")
}
func (w *OrgWriter) writeExample(e Example) {

View file

@ -86,3 +86,41 @@ paragraphs
<script>
console.log("Hello World!")
</script>
<ul>
<li>
<p>
list item 1
blocks can contain unindented lines that would normally end a list item
</p>
<pre class="example">
this line is not indented - if it was outside of a block the list item would end
</pre>
<p>
now we&#39;re outside the block again and the following unindented line will be outside of the list item
</p>
</li>
</ul>
<p>
this unindented line is outside of the list item
</p>
<ul>
<li>
<p>
list item 2
</p>
<div class="highlight">
<pre>
#+BEGIN_EXAMPLE
</pre>
</div>
<p>
#+END_EXAMPLE
</p>
<p>
#+BEGIN_QUOTE
</p>
<pre class="example">
#+END_QUOTE
</pre>
</li>
</ul>

View file

@ -48,3 +48,21 @@ blocks can contain other elements like
console.log("Hello World!")
</script>
#+END_EXPORT
- list item 1
blocks can contain unindented lines that would normally end a list item
#+BEGIN_EXAMPLE
this line is not indented - if it was outside of a block the list item would end
#+END_EXAMPLE
now we're outside the block again and the following unindented line will be outside of the list item
this unindented line is outside of the list item
- list item 2
#+BEGIN_SRC
#+BEGIN_EXAMPLE
#+END_SRC
#+END_EXAMPLE
#+BEGIN_QUOTE
#+BEGIN_EXAMPLE
#+END_QUOTE
#+END_EXAMPLE

View file

@ -48,3 +48,21 @@ blocks can contain other elements like
console.log("Hello World!")
</script>
#+END_EXPORT
- list item 1
blocks can contain unindented lines that would normally end a list item
#+BEGIN_EXAMPLE
this line is not indented - if it was outside of a block the list item would end
#+END_EXAMPLE
now we're outside the block again and the following unindented line will be outside of the list item
this unindented line is outside of the list item
- list item 2
#+BEGIN_SRC
#+BEGIN_EXAMPLE
#+END_SRC
#+END_EXAMPLE
#+BEGIN_QUOTE
#+BEGIN_EXAMPLE
#+END_QUOTE
#+END_EXAMPLE