Refactor RegularLink image & video handling

This commit is contained in:
Niklas Fasching 2018-12-03 16:58:22 +01:00
parent f17923047b
commit 2399fec2eb
4 changed files with 45 additions and 12 deletions

View file

@ -3,7 +3,6 @@ package org
import (
"fmt"
"html"
"path"
"strings"
)
@ -188,20 +187,20 @@ func (w *HTMLWriter) writeFootnoteLink(l FootnoteLink) {
func (w *HTMLWriter) writeRegularLink(l RegularLink) {
url := html.EscapeString(l.URL)
if l.Protocol == "file" {
url = url[len("file:"):]
}
description := url
if l.Description != nil {
descriptionWriter := w.emptyClone()
descriptionWriter.writeNodes(l.Description...)
description = descriptionWriter.String()
}
switch l.Protocol {
case "file":
url = url[len("file:"):]
if strings.Contains(".png.jpg.jpeg.gif", path.Ext(l.URL)) {
switch l.Kind() {
case "image":
w.WriteString(fmt.Sprintf(`<img src="%s" alt="%s" title="%s" />`, url, description, description))
} else {
w.WriteString(fmt.Sprintf(`<a href="%s">%s</a>`, url, description))
}
case "video":
w.WriteString(fmt.Sprintf(`<video src="%s" title="%s">%s</video>`, url, description, description))
default:
w.WriteString(fmt.Sprintf(`<a href="%s">%s</a>`, url, description))
}

View file

@ -1,6 +1,7 @@
package org
import (
"path"
"regexp"
"strings"
"unicode"
@ -29,8 +30,9 @@ type RegularLink struct {
var validURLCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;="
var autolinkProtocols = regexp.MustCompile(`(https?|ftp|file)`)
var imageExtensionRegexp = regexp.MustCompile(`^[.](png|gif|jpe?g|svg|tiff?)$`)
var videoExtensionRegexp = regexp.MustCompile(`^[.](webm|mp4)$`)
var redundantSpaces = regexp.MustCompile("[ \t]+")
var subScriptSuperScriptRegexp = regexp.MustCompile(`([_^])\{(.*?)\}`)
var footnoteRegexp = regexp.MustCompile(`\[fn:([\w-]+?)(:(.*?))?\]`)
@ -205,3 +207,16 @@ func isValidPostChar(r rune) bool {
}
func isValidBorderChar(r rune) bool { return !unicode.IsSpace(r) }
func (l RegularLink) Kind() string {
if p := l.Protocol; l.Description != nil || (p != "" && p != "file" && p != "http" && p != "https") {
return "regular"
}
if imageExtensionRegexp.MatchString(path.Ext(l.URL)) {
return "image"
}
if videoExtensionRegexp.MatchString(path.Ext(l.URL)) {
return "video"
}
return "regular"
}

View file

@ -162,7 +162,22 @@ regular link <a href="https://example.com">example.com</a> link with description
</li>
<li>
<p>
regular link to a file (image) <img src="my-img.png" alt="file:my-img.png" title="file:my-img.png" />
regular link to a file (image) <img src="my-img.png" alt="my-img.png" title="my-img.png" />
</p>
</li>
<li>
<p>
regular link to a file (video) <video src="my-video.mp4" title="my-video.mp4">my-video.mp4</video>
</p>
</li>
<li>
<p>
regular link to http (image) <img src="http://www.example.com/my-img.png" alt="http://www.example.com/my-img.png" title="http://www.example.com/my-img.png" />
</p>
</li>
<li>
<p>
regular link to https (image) <img src="https://www.example.com/my-img.png" alt="https://www.example.com/my-img.png" title="https://www.example.com/my-img.png" />
</p>
</li>
<li>

View file

@ -58,7 +58,11 @@ this one is cheating a little as tags are ALWAYS printed right aligned to a give
1. regular link [[https://example.com]] link without description
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
4. regular link to a file (video) [[my-video.mp4]]
5. regular link to http (image) [[http://www.example.com/my-img.png]]
6. regular link to https (image) [[https://www.example.com/my-img.png]]
7. auto link, i.e. not inside =\[[square brackets]\]= https://www.example.com
** blocks
#+BEGIN_SRC bash
echo a bash source block