From 4e2fb02b57ac1bde97c15de399e8159f1255a38f Mon Sep 17 00:00:00 2001 From: Niklas Fasching Date: Sun, 2 Dec 2018 18:54:53 +0100 Subject: [PATCH] html: Improve block rendering --- README.org | 2 -- org/html.go | 38 +++++++++++++++++++++++++++----------- org/testdata/example.html | 19 ++++++++++++++++++- org/testdata/example.org | 19 +++++++++++++++++++ 4 files changed, 64 insertions(+), 14 deletions(-) diff --git a/README.org b/README.org index af67a39..49faa3f 100644 --- a/README.org +++ b/README.org @@ -5,8 +5,6 @@ A basic org-mode parser in go * next - hugo frontmatter - see https://gohugo.io/content-management/front-matter/ - captions: images, tables & blocks -- blocks: highlighted src code, blockquote -- basic tables * later - affiliated keywords see org-element.el - org-element-affiliated-keywords diff --git a/org/html.go b/org/html.go index 41af658..277af26 100644 --- a/org/html.go +++ b/org/html.go @@ -33,7 +33,9 @@ var listTags = map[string][]string{ func NewHTMLWriter() *HTMLWriter { return &HTMLWriter{ - HighlightCodeBlock: func(source, lang string) string { return html.EscapeString(source) }, + HighlightCodeBlock: func(source, lang string) string { + return fmt.Sprintf("
%s
", html.EscapeString(source))
+		},
 	}
 }
 
@@ -110,17 +112,31 @@ func (w *HTMLWriter) writeLines(lines []Node) {
 }
 
 func (w *HTMLWriter) writeBlock(b Block) {
-	w.WriteString("")
-	lang := ""
-	if len(b.Parameters) >= 1 {
-		lang = b.Parameters[0]
+	switch b.Name {
+	case "SRC":
+		lines, lang := []string{}, "text"
+		if len(b.Parameters) >= 1 {
+			lang = b.Parameters[0]
+		}
+		for _, n := range b.Children {
+			lines = append(lines, n.(Line).Children[0].(Text).Content)
+		}
+		w.WriteString(fmt.Sprintf(``, lang) + "\n")
+		w.WriteString(w.HighlightCodeBlock(strings.Join(lines, "\n"), lang))
+		w.WriteString("\n\n")
+	case "EXAMPLE":
+		w.WriteString(`
` + "\n")
+		w.writeNodes(b.Children...)
+		w.WriteString("\n
\n") + case "QUOTE": + w.WriteString("
\n") + w.writeNodes(b.Children...) + w.WriteString("\n
\n") + case "CENTER": + w.WriteString(`
` + "\n") + w.writeNodes(b.Children...) + w.WriteString("\n
\n") } - lines := []string{} - for _, n := range b.Children { - lines = append(lines, n.(Line).Children[0].(Text).Content) - } - w.WriteString(w.HighlightCodeBlock(strings.Join(lines, "\n"), lang)) - w.WriteString("
\n") } func (w *HTMLWriter) writeFootnoteDefinition(f FootnoteDefinition) { diff --git a/org/testdata/example.html b/org/testdata/example.html index 4d6af74..96d9397 100644 --- a/org/testdata/example.html +++ b/org/testdata/example.html @@ -23,7 +23,9 @@
  • unordered list item 3 - and a link and some lines of text

    1. and another subitem

      -echo with a block + +
      echo with a block
      +
       
    2. and another one with a table

      @@ -62,6 +64,21 @@ +

      blocks

      + +
      echo a bash source block
      +
      +
      +
      a source block without a language
      +and a second line
      +and a third one
      +
      +
      +an example blockwith multiple lines
      +
      +
      +Mongodb is very webscale +

      Footnotes

      • normal footnote reference 1
      • diff --git a/org/testdata/example.org b/org/testdata/example.org index 258964e..ad29d1b 100644 --- a/org/testdata/example.org +++ b/org/testdata/example.org @@ -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 3. regular link to a file (image) [[file:my-img.png]] 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 - normal footnote reference [fn:1] - further references to the same footnote should not [fn:1] render duplicates in the footnote list