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:
parent
08ff3ac22e
commit
0a905ca172
6 changed files with 28 additions and 12 deletions
18
org/html.go
18
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})
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
2
org/testdata/keywords.html
vendored
2
org/testdata/keywords.html
vendored
|
@ -1,5 +1,5 @@
|
|||
<figure>
|
||||
<div class="highlight" class="a b c" id="it">
|
||||
<div class="highlight a b c" id="it">
|
||||
<pre>echo "a bash source block with custom html attributes"
|
||||
</pre>
|
||||
</div>
|
||||
|
|
4
org/testdata/misc.html
vendored
4
org/testdata/misc.html
vendored
|
@ -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>
|
||||
</p>
|
||||
</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>
|
||||
<ul>
|
||||
<li>
|
||||
|
@ -73,6 +74,7 @@ sub bullet
|
|||
</ul>
|
||||
</li>
|
||||
</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>
|
||||
<p>
|
||||
<em>italics</em>
|
||||
|
|
8
org/testdata/misc.org
vendored
8
org/testdata/misc.org
vendored
|
@ -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/
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue