html: Improve block rendering

This commit is contained in:
Niklas Fasching 2018-12-02 18:54:53 +01:00
parent d2d9dc0fc8
commit 4e2fb02b57
4 changed files with 64 additions and 14 deletions

View file

@ -5,8 +5,6 @@ A basic org-mode parser in go
* next * next
- hugo frontmatter - see https://gohugo.io/content-management/front-matter/ - hugo frontmatter - see https://gohugo.io/content-management/front-matter/
- captions: images, tables & blocks - captions: images, tables & blocks
- blocks: highlighted src code, blockquote
- basic tables
* later * later
- affiliated keywords - affiliated keywords
see org-element.el - org-element-affiliated-keywords see org-element.el - org-element-affiliated-keywords

View file

@ -33,7 +33,9 @@ var listTags = map[string][]string{
func NewHTMLWriter() *HTMLWriter { func NewHTMLWriter() *HTMLWriter {
return &HTMLWriter{ return &HTMLWriter{
HighlightCodeBlock: func(source, lang string) string { return html.EscapeString(source) }, HighlightCodeBlock: func(source, lang string) string {
return fmt.Sprintf("<pre>%s<pre>", html.EscapeString(source))
},
} }
} }
@ -110,17 +112,31 @@ func (w *HTMLWriter) writeLines(lines []Node) {
} }
func (w *HTMLWriter) writeBlock(b Block) { func (w *HTMLWriter) writeBlock(b Block) {
w.WriteString("<code>") switch b.Name {
lang := "" case "SRC":
lines, lang := []string{}, "text"
if len(b.Parameters) >= 1 { if len(b.Parameters) >= 1 {
lang = b.Parameters[0] lang = b.Parameters[0]
} }
lines := []string{}
for _, n := range b.Children { for _, n := range b.Children {
lines = append(lines, n.(Line).Children[0].(Text).Content) lines = append(lines, n.(Line).Children[0].(Text).Content)
} }
w.WriteString(fmt.Sprintf(`<code class="src src-%s">`, lang) + "\n")
w.WriteString(w.HighlightCodeBlock(strings.Join(lines, "\n"), lang)) w.WriteString(w.HighlightCodeBlock(strings.Join(lines, "\n"), lang))
w.WriteString("</code>\n") w.WriteString("\n</code>\n")
case "EXAMPLE":
w.WriteString(`<pre class="example">` + "\n")
w.writeNodes(b.Children...)
w.WriteString("\n</pre>\n")
case "QUOTE":
w.WriteString("<blockquote>\n")
w.writeNodes(b.Children...)
w.WriteString("\n</blockquote>\n")
case "CENTER":
w.WriteString(`<div style="text-align: center; margin-left: auto; margin-right: auto;">` + "\n")
w.writeNodes(b.Children...)
w.WriteString("\n</div>\n")
}
} }
func (w *HTMLWriter) writeFootnoteDefinition(f FootnoteDefinition) { func (w *HTMLWriter) writeFootnoteDefinition(f FootnoteDefinition) {

View file

@ -23,7 +23,9 @@
<li><p>unordered list item 3 - and a <a href="https://example.com">link</a> and some lines of text</p> <li><p>unordered list item 3 - and a <a href="https://example.com">link</a> and some lines of text</p>
<ol> <ol>
<li><p>and another subitem</p> <li><p>and another subitem</p>
<code>echo with a block</code> <code class="src src-sh">
<pre>echo with a block<pre>
</code>
</li> </li>
<li><p>and another one with a table</p> <li><p>and another one with a table</p>
<table> <table>
@ -62,6 +64,21 @@
</ol> </ol>
</li> </li>
</ul> </ul>
<h2>blocks</h2>
<code class="src src-bash">
<pre>echo a bash source block<pre>
</code>
<code class="src src-text">
<pre>a source block without a language
and a second line
and a third one<pre>
</code>
<pre class="example">
an example blockwith multiple lines
</pre>
<blockquote>
Mongodb is very webscale
</blockquote>
<h2>Footnotes</h2> <h2>Footnotes</h2>
<ul> <ul>
<li>normal footnote reference <sup class="footnote-reference"><a href="#footnote-1">1</a></sup></li> <li>normal footnote reference <sup class="footnote-reference"><a href="#footnote-1">1</a></sup></li>

View file

@ -51,6 +51,25 @@ this one is cheating a little as tags are ALWAYS printed right aligned to a give
2. regular link [[https://example.com][example.com]] link with description 2. regular link [[https://example.com][example.com]] link with description
3. regular link to a file (image) [[file:my-img.png]] 3. regular link to a file (image) [[file:my-img.png]]
4. auto link, i.e. not inside =\[[square brackets]\]= https://www.example.com 4. auto link, i.e. not inside =\[[square brackets]\]= https://www.example.com
** blocks
#+BEGIN_SRC bash
echo a bash source block
#+END_SRC
#+BEGIN_SRC
a source block without a language
and a second line
and a third one
#+END_SRC
#+BEGIN_EXAMPLE foo bar baz
an example block
with multiple lines
#+END_EXAMPLE
#+BEGIN_QUOTE
Mongodb is very webscale
#+END_QUOTE
** Footnotes ** Footnotes
- normal footnote reference [fn:1] - normal footnote reference [fn:1]
- further references to the same footnote should not [fn:1] render duplicates in the footnote list - further references to the same footnote should not [fn:1] render duplicates in the footnote list