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

@ -4,10 +4,6 @@ Take a look at [[https://niklasfasching.github.io/go-org/][github pages]] for so
* next * next
- test against [[https://raw.githubusercontent.com/kaushalmodi/ox-hugo/master/test/site/content-org/all-posts.org][ox-hugo all-posts.org]] - test against [[https://raw.githubusercontent.com/kaushalmodi/ox-hugo/master/test/site/content-org/all-posts.org][ox-hugo all-posts.org]]
- ATTR_HTML without printing child - i.e. above other, non-affiliated keyword - ATTR_HTML without printing child - i.e. above other, non-affiliated keyword
- blocks with indent < their parent
#+BEGIN_SRC
> this
#+END_SRC
- more keywords: https://orgmode.org/manual/In_002dbuffer-settings.html - more keywords: https://orgmode.org/manual/In_002dbuffer-settings.html
- headlines - headlines
- unique ids - unique ids

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]) name, parameters := t.content, strings.Fields(t.matches[3])
trim := trimIndentUpTo(d.tokens[i].lvl) trim := trimIndentUpTo(d.tokens[i].lvl)
stop := func(d *Document, i int) bool { 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) { if isRawTextBlock(name) {
rawText := "" rawText := ""
for ; !stop(d, i); i++ { for ; !stop(d, i); i++ {
rawText += trim(d.tokens[i].matches[0]) + "\n" rawText += trim(d.tokens[i].matches[0]) + "\n"
} }
consumed = i - start
block.Children = d.parseRawInline(rawText) block.Children = d.parseRawInline(rawText)
} else { } else {
consumed, block.Children = d.parseMany(i, stop) consumed, nodes := d.parseMany(i, stop)
consumed++ // line with BEGIN block.Children = nodes
i += consumed
} }
if parentStop(d, i) { if i < len(d.tokens) && d.tokens[i].kind == "endBlock" && d.tokens[i].content == name {
return 0, nil return i + 1 - start, block
} }
return consumed + 1, block return 0, nil
} }
func trimIndentUpTo(max int) func(string) string { 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) { func (w *OrgWriter) writeParagraph(p Paragraph) {
w.writeNodes(p.Children...) content := w.nodesAsString(p.Children...)
w.WriteString("\n") if len(content) > 0 && content[0] != '\n' {
w.WriteString(w.indent)
}
w.WriteString(content + "\n")
} }
func (w *OrgWriter) writeExample(e Example) { func (w *OrgWriter) writeExample(e Example) {

View file

@ -86,3 +86,41 @@ paragraphs
<script> <script>
console.log("Hello World!") console.log("Hello World!")
</script> </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!") console.log("Hello World!")
</script> </script>
#+END_EXPORT #+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!") console.log("Hello World!")
</script> </script>
#+END_EXPORT #+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