html: Improve link abbreviation (#+LINK) resolution
Handle `%s`/`%h` interpolation and add support for shorthand of prefix only links / links with empty tag (i.e. `[example]` instead of [example:]). See [1] and [2]. As so often we don't match org mode output completely but our output looks sane enough to me so let's move along for now. [1] https://orgmode.org/manual/Link-Abbreviations.html [2] https://github.com/bzg/org-mode/blob/main/lisp/ol.el#L1011
This commit is contained in:
parent
34dfe32024
commit
9995b3cdad
5 changed files with 37 additions and 5 deletions
|
@ -10,6 +10,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
|
u "net/url"
|
||||||
|
|
||||||
h "golang.org/x/net/html"
|
h "golang.org/x/net/html"
|
||||||
"golang.org/x/net/html/atom"
|
"golang.org/x/net/html/atom"
|
||||||
)
|
)
|
||||||
|
@ -361,7 +363,13 @@ func (w *HTMLWriter) WriteRegularLink(l RegularLink) {
|
||||||
url = strings.TrimSuffix(url, ".org") + ".html"
|
url = strings.TrimSuffix(url, ".org") + ".html"
|
||||||
}
|
}
|
||||||
if prefix := w.document.Links[l.Protocol]; prefix != "" {
|
if prefix := w.document.Links[l.Protocol]; prefix != "" {
|
||||||
url = html.EscapeString(prefix) + strings.TrimPrefix(url, l.Protocol+":")
|
if tag := strings.TrimPrefix(l.URL, l.Protocol+":"); strings.Contains(prefix, "%s") || strings.Contains(prefix, "%h") {
|
||||||
|
url = html.EscapeString(strings.ReplaceAll(strings.ReplaceAll(prefix, "%s", tag), "%h", u.QueryEscape(tag)))
|
||||||
|
} else {
|
||||||
|
url = html.EscapeString(prefix) + tag
|
||||||
|
}
|
||||||
|
} else if prefix := w.document.Links[l.URL]; prefix != "" {
|
||||||
|
url = html.EscapeString(strings.ReplaceAll(strings.ReplaceAll(prefix, "%s", ""), "%h", ""))
|
||||||
}
|
}
|
||||||
switch l.Kind() {
|
switch l.Kind() {
|
||||||
case "image":
|
case "image":
|
||||||
|
|
|
@ -63,7 +63,7 @@ func (d *Document) parseKeyword(i int, stop stopFn) (int, Node) {
|
||||||
case "INCLUDE":
|
case "INCLUDE":
|
||||||
return d.parseInclude(k)
|
return d.parseInclude(k)
|
||||||
case "LINK":
|
case "LINK":
|
||||||
if parts := strings.Split(k.Value, " "); len(parts) >= 2 {
|
if parts := strings.SplitN(k.Value, " ", 2); len(parts) == 2 {
|
||||||
d.Links[parts[0]] = parts[1]
|
d.Links[parts[0]] = parts[1]
|
||||||
}
|
}
|
||||||
return 1, k
|
return 1, k
|
||||||
|
|
10
org/testdata/inline.html
vendored
10
org/testdata/inline.html
vendored
|
@ -55,7 +55,15 @@ not emphasized/</li>
|
||||||
</ul>
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<p><code class="verbatim">#+LINK</code> based links: <a href="https://www.example.com/foobar">https://www.example.com/foobar</a></p>
|
<p><code class="verbatim">#+LINK</code> based links:</p>
|
||||||
|
<ul>
|
||||||
|
<li><a href="https://www.example.com/foobar">https://www.example.com/foobar</a></li>
|
||||||
|
<li><a href="https://www.example.com/">https://www.example.com/</a></li>
|
||||||
|
<li><a href="https://www.example.com/">https://www.example.com/</a></li>
|
||||||
|
<li><a href="https://www.example.com/">description</a></li>
|
||||||
|
<li><a href="https://www.example.com?raw_tag=tag value with specical chars % : &">https://www.example.com?raw_tag=tag value with specical chars % : &</a> (w/o tag <a href="https://www.example.com?raw_tag=">https://www.example.com?raw_tag=</a>)</li>
|
||||||
|
<li><a href="https://www.example.com?encoded_tag=tag+value+with+specical+chars+%25+%3A+%26">https://www.example.com?encoded_tag=tag+value+with+specical+chars+%25+%3A+%26</a> (w/o tag <a href="https://www.example.com?encoded_tag=">https://www.example.com?encoded_tag=</a>)</li>
|
||||||
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<p><code class="verbatim">#+MACROs</code>: <p><h1>yolo</h1></p>
|
<p><code class="verbatim">#+MACROs</code>: <p><h1>yolo</h1></p>
|
||||||
|
|
10
org/testdata/inline.org
vendored
10
org/testdata/inline.org
vendored
|
@ -39,7 +39,15 @@
|
||||||
- <2019-01-06 Sun 18:00 +1w>
|
- <2019-01-06 Sun 18:00 +1w>
|
||||||
- <2019-01-06 18:00>
|
- <2019-01-06 18:00>
|
||||||
- <2019-01-06 18:00 +1w>
|
- <2019-01-06 18:00 +1w>
|
||||||
- =#+LINK= based links: [[example:foobar]]
|
- =#+LINK= based links:
|
||||||
#+LINK: example https://www.example.com/
|
#+LINK: example https://www.example.com/
|
||||||
|
#+LINK: example_interpolate_s https://www.example.com?raw_tag=%s
|
||||||
|
#+LINK: example_interpolate_h https://www.example.com?encoded_tag=%h
|
||||||
|
- [[example:foobar]]
|
||||||
|
- [[example:]]
|
||||||
|
- [[example]]
|
||||||
|
- [[example][description]]
|
||||||
|
- [[example_interpolate_s:tag value with specical chars % : &]] (w/o tag [[example_interpolate_s]])
|
||||||
|
- [[example_interpolate_h:tag value with specical chars % : &]] (w/o tag [[example_interpolate_h]])
|
||||||
- =#+MACROs=: {{{headline(yolo)}}}
|
- =#+MACROs=: {{{headline(yolo)}}}
|
||||||
#+MACRO: headline @@html:<h1>$1</h1>@@
|
#+MACRO: headline @@html:<h1>$1</h1>@@
|
||||||
|
|
10
org/testdata/inline.pretty_org
vendored
10
org/testdata/inline.pretty_org
vendored
|
@ -39,7 +39,15 @@
|
||||||
- <2019-01-06 Sun 18:00 +1w>
|
- <2019-01-06 Sun 18:00 +1w>
|
||||||
- <2019-01-06 Sun 18:00>
|
- <2019-01-06 Sun 18:00>
|
||||||
- <2019-01-06 Sun 18:00 +1w>
|
- <2019-01-06 Sun 18:00 +1w>
|
||||||
- =#+LINK= based links: [[example:foobar]]
|
- =#+LINK= based links:
|
||||||
#+LINK: example https://www.example.com/
|
#+LINK: example https://www.example.com/
|
||||||
|
#+LINK: example_interpolate_s https://www.example.com?raw_tag=%s
|
||||||
|
#+LINK: example_interpolate_h https://www.example.com?encoded_tag=%h
|
||||||
|
- [[example:foobar]]
|
||||||
|
- [[example:]]
|
||||||
|
- [[example]]
|
||||||
|
- [[example][description]]
|
||||||
|
- [[example_interpolate_s:tag value with specical chars % : &]] (w/o tag [[example_interpolate_s]])
|
||||||
|
- [[example_interpolate_h:tag value with specical chars % : &]] (w/o tag [[example_interpolate_h]])
|
||||||
- =#+MACROs=: {{{headline(yolo)}}}
|
- =#+MACROs=: {{{headline(yolo)}}}
|
||||||
#+MACRO: headline @@html:<h1>$1</h1>@@
|
#+MACRO: headline @@html:<h1>$1</h1>@@
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue