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
This commit is contained in:
Niklas Fasching 2018-12-11 20:16:16 +01:00
parent 08ff3ac22e
commit 0a905ca172
6 changed files with 28 additions and 12 deletions

View file

@ -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 - 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 - hopefully add reasonable org-mode support to hugo - sadly [[https://github.com/chaseadamsio/goorgeous][goorgeous]] is broken & abandoned
* next * 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 *** TODO [[https://github.com/chaseadamsio/goorgeous/issues/46][#46]]: Support for symbols like ndash and mdash
- see org-entities replacement: see org-entities-help - see org-entities replacement: see org-entities-help
*** TODO [[https://github.com/chaseadamsio/goorgeous/issues/10][#10]]: Support noexport *** TODO [[https://github.com/chaseadamsio/goorgeous/issues/10][#10]]: Support noexport

View file

@ -316,8 +316,7 @@ func withHTMLAttributes(input string, kvs ...string) string {
} }
out, node := strings.Builder{}, nodes[0] out, node := strings.Builder{}, nodes[0]
for i := 0; i < len(kvs)-1; i += 2 { for i := 0; i < len(kvs)-1; i += 2 {
k, v := strings.TrimPrefix(kvs[i], ":"), kvs[i+1] node.Attr = setHTMLAttribute(node.Attr, strings.TrimPrefix(kvs[i], ":"), kvs[i+1])
node.Attr = append(node.Attr, h.Attribute{Namespace: "", Key: k, Val: v})
} }
err = h.Render(&out, nodes[0]) err = h.Render(&out, nodes[0])
if err != nil { if err != nil {
@ -325,3 +324,18 @@ func withHTMLAttributes(input string, kvs ...string) string {
} }
return out.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})
}

View file

@ -182,14 +182,11 @@ func (w *OrgWriter) writeNodeWithMeta(n NodeWithMeta) {
for _, attributes := range n.Meta.HTMLAttributes { for _, attributes := range n.Meta.HTMLAttributes {
w.WriteString("#+ATTR_HTML: ") w.WriteString("#+ATTR_HTML: ")
for i := 0; i < len(attributes)-1; i += 2 { for i := 0; i < len(attributes)-1; i += 2 {
w.WriteString(attributes[i] + " ")
if strings.ContainsAny(attributes[i+1], "\t ") { if strings.ContainsAny(attributes[i+1], "\t ") {
w.WriteString(`"` + attributes[i+1] + `"`) attributes[i+1] = fmt.Sprintf(`"%s"`, attributes[i+1])
} else {
w.WriteString(attributes[i+1])
} }
} }
w.WriteString("\n") w.WriteString(strings.Join(attributes, " ") + "\n")
} }
w.writeNodes(n.Node) w.writeNodes(n.Node)
} }

View file

@ -1,5 +1,5 @@
<figure> <figure>
<div class="highlight" class="a b c" id="it"> <div class="highlight a b c" id="it">
<pre>echo &#34;a bash source block with custom html attributes&#34; <pre>echo &#34;a bash source block with custom html attributes&#34;
</pre> </pre>
</div> </div>

View file

@ -57,7 +57,8 @@ foo
<a href="https://www.example.com"><em>this</em> <strong>is</strong> <span style="text-decoration: underline;">markup</span>!</a> <a href="https://www.example.com"><em>this</em> <strong>is</strong> <span style="text-decoration: underline;">markup</span>!</a>
</p> </p>
</blockquote> </blockquote>
<h3><a href="https://github.com/chaseadamsio/goorgeous/issues/77">#77</a>: Recognize <code class="verbatim">code</code>--- as code plus dash</h3> <h3><a href="https://github.com/chaseadamsio/goorgeous/issues/72">#72</a>: Support for #+ATTR_HTML</h3>
<img src="https://golang.org/doc/gopher/pkg.png" alt="Go is fine though." title="https://golang.org/doc/gopher/pkg.png" width="300" style="border:2px solid black;"/>
<h3><a href="https://github.com/chaseadamsio/goorgeous/issues/75">#75</a>: Not parsing nested lists correctly</h3> <h3><a href="https://github.com/chaseadamsio/goorgeous/issues/75">#75</a>: Not parsing nested lists correctly</h3>
<ul> <ul>
<li> <li>
@ -73,6 +74,7 @@ sub bullet
</ul> </ul>
</li> </li>
</ul> </ul>
<h3><a href="https://github.com/chaseadamsio/goorgeous/issues/77">#77</a>: Recognize <code class="verbatim">code</code>--- as code plus dash</h3>
<h3><a href="https://github.com/chaseadamsio/goorgeous/issues/78">#78</a>: Emphasis at beginning of line</h3> <h3><a href="https://github.com/chaseadamsio/goorgeous/issues/78">#78</a>: Emphasis at beginning of line</h3>
<p> <p>
<em>italics</em> <em>italics</em>

View file

@ -30,11 +30,15 @@ or ~that~ foo.
#+BEGIN_QUOTE #+BEGIN_QUOTE
[[https://www.example.com][/this/ *is* _markup_!]] [[https://www.example.com][/this/ *is* _markup_!]]
#+END_QUOTE #+END_QUOTE
*** DONE [[https://github.com/chaseadamsio/goorgeous/issues/72][#72]]: Support for #+ATTR_HTML
*** DONE [[https://github.com/chaseadamsio/goorgeous/issues/77][#77]]: Recognize =code=--- as code plus dash #+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 *** DONE [[https://github.com/chaseadamsio/goorgeous/issues/75][#75]]: Not parsing nested lists correctly
- bullet 1 - bullet 1
- sub bullet - 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 *** DONE [[https://github.com/chaseadamsio/goorgeous/issues/78][#78]]: Emphasis at beginning of line
/italics/ /italics/