From 0a905ca1724c00788a420a5c5713e9d87fa1ce91 Mon Sep 17 00:00:00 2001 From: Niklas Fasching Date: Tue, 11 Dec 2018 20:16:16 +0100 Subject: [PATCH] Fix HTML_ATTR rendering (naive) - was missing spaces between attributes when rendering to org - was duplicating attributes when rendering to html - now we join / replace attributes depending on the name - for now only class & style are appended --- README.org | 1 - org/html.go | 18 ++++++++++++++++-- org/org.go | 7 ++----- org/testdata/keywords.html | 2 +- org/testdata/misc.html | 4 +++- org/testdata/misc.org | 8 ++++++-- 6 files changed, 28 insertions(+), 12 deletions(-) diff --git a/README.org b/README.org index 630ea83..1736d2c 100644 --- a/README.org +++ b/README.org @@ -4,7 +4,6 @@ A basic org-mode parser in go - have a org-mode AST to play around with building an org-mode language server - hopefully add reasonable org-mode support to hugo - sadly [[https://github.com/chaseadamsio/goorgeous][goorgeous]] is broken & abandoned * next -*** TODO [[https://github.com/chaseadamsio/goorgeous/issues/72][#72:]] Support for #+ATTR_HTML *** TODO [[https://github.com/chaseadamsio/goorgeous/issues/46][#46]]: Support for symbols like ndash and mdash - see org-entities replacement: see org-entities-help *** TODO [[https://github.com/chaseadamsio/goorgeous/issues/10][#10]]: Support noexport diff --git a/org/html.go b/org/html.go index a7b11b4..86e043a 100644 --- a/org/html.go +++ b/org/html.go @@ -316,8 +316,7 @@ func withHTMLAttributes(input string, kvs ...string) string { } out, node := strings.Builder{}, nodes[0] for i := 0; i < len(kvs)-1; i += 2 { - k, v := strings.TrimPrefix(kvs[i], ":"), kvs[i+1] - node.Attr = append(node.Attr, h.Attribute{Namespace: "", Key: k, Val: v}) + node.Attr = setHTMLAttribute(node.Attr, strings.TrimPrefix(kvs[i], ":"), kvs[i+1]) } err = h.Render(&out, nodes[0]) if err != nil { @@ -325,3 +324,18 @@ func withHTMLAttributes(input string, kvs ...string) string { } return out.String() } + +func setHTMLAttribute(attributes []h.Attribute, k, v string) []h.Attribute { + for i, a := range attributes { + if strings.ToLower(a.Key) == strings.ToLower(k) { + switch strings.ToLower(k) { + case "class", "style": + attributes[i].Val += " " + v + default: + attributes[i].Val = v + } + return attributes + } + } + return append(attributes, h.Attribute{Namespace: "", Key: k, Val: v}) +} diff --git a/org/org.go b/org/org.go index 3408a34..a477bd5 100644 --- a/org/org.go +++ b/org/org.go @@ -182,14 +182,11 @@ func (w *OrgWriter) writeNodeWithMeta(n NodeWithMeta) { for _, attributes := range n.Meta.HTMLAttributes { w.WriteString("#+ATTR_HTML: ") for i := 0; i < len(attributes)-1; i += 2 { - w.WriteString(attributes[i] + " ") if strings.ContainsAny(attributes[i+1], "\t ") { - w.WriteString(`"` + attributes[i+1] + `"`) - } else { - w.WriteString(attributes[i+1]) + attributes[i+1] = fmt.Sprintf(`"%s"`, attributes[i+1]) } } - w.WriteString("\n") + w.WriteString(strings.Join(attributes, " ") + "\n") } w.writeNodes(n.Node) } diff --git a/org/testdata/keywords.html b/org/testdata/keywords.html index a108f03..5803476 100644 --- a/org/testdata/keywords.html +++ b/org/testdata/keywords.html @@ -1,5 +1,5 @@
-
+
echo "a bash source block with custom html attributes"
 
diff --git a/org/testdata/misc.html b/org/testdata/misc.html index 3e9ed0f..92b3379 100644 --- a/org/testdata/misc.html +++ b/org/testdata/misc.html @@ -57,7 +57,8 @@ foo this is markup!

-

#77: Recognize code--- as code plus dash

+

#72: Support for #+ATTR_HTML

+Go is fine though.

#75: Not parsing nested lists correctly

  • @@ -73,6 +74,7 @@ sub bullet
+

#77: Recognize code--- as code plus dash

#78: Emphasis at beginning of line

italics diff --git a/org/testdata/misc.org b/org/testdata/misc.org index 86ccfb6..8629bf2 100644 --- a/org/testdata/misc.org +++ b/org/testdata/misc.org @@ -30,11 +30,15 @@ or ~that~ foo. #+BEGIN_QUOTE [[https://www.example.com][/this/ *is* _markup_!]] #+END_QUOTE - -*** DONE [[https://github.com/chaseadamsio/goorgeous/issues/77][#77]]: Recognize =code=--- as code plus dash +*** DONE [[https://github.com/chaseadamsio/goorgeous/issues/72][#72]]: Support for #+ATTR_HTML +#+ATTR_HTML: :alt "Go is fine though." +#+ATTR_HTML: :width 300 :style "border:2px solid black;" +[[https://golang.org/doc/gopher/pkg.png]] *** DONE [[https://github.com/chaseadamsio/goorgeous/issues/75][#75]]: Not parsing nested lists correctly - bullet 1 - sub bullet + +*** DONE [[https://github.com/chaseadamsio/goorgeous/issues/77][#77]]: Recognize =code=--- as code plus dash *** DONE [[https://github.com/chaseadamsio/goorgeous/issues/78][#78]]: Emphasis at beginning of line /italics/