From 2399fec2eb80fc2b6ce264fb3475d4f4d848ef60 Mon Sep 17 00:00:00 2001 From: Niklas Fasching Date: Mon, 3 Dec 2018 16:58:22 +0100 Subject: [PATCH] Refactor RegularLink image & video handling --- org/html.go | 17 ++++++++--------- org/inline.go | 17 ++++++++++++++++- org/testdata/example.html | 17 ++++++++++++++++- org/testdata/example.org | 6 +++++- 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/org/html.go b/org/html.go index ea7afce..0bb3879 100644 --- a/org/html.go +++ b/org/html.go @@ -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)) { - w.WriteString(fmt.Sprintf(`%s`, url, description, description)) - } else { - w.WriteString(fmt.Sprintf(`%s`, url, description)) - } + switch l.Kind() { + case "image": + w.WriteString(fmt.Sprintf(`%s`, url, description, description)) + case "video": + w.WriteString(fmt.Sprintf(``, url, description, description)) default: w.WriteString(fmt.Sprintf(`%s`, url, description)) } diff --git a/org/inline.go b/org/inline.go index 5b71d8a..e096ae0 100644 --- a/org/inline.go +++ b/org/inline.go @@ -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" +} diff --git a/org/testdata/example.html b/org/testdata/example.html index d44ada3..902af7c 100644 --- a/org/testdata/example.html +++ b/org/testdata/example.html @@ -162,7 +162,22 @@ regular link example.com link with description
  • -regular link to a file (image) file:my-img.png +regular link to a file (image) my-img.png +

    +
  • +
  • +

    +regular link to a file (video) +

    +
  • +
  • +

    +regular link to http (image) http://www.example.com/my-img.png +

    +
  • +
  • +

    +regular link to https (image) https://www.example.com/my-img.png

  • diff --git a/org/testdata/example.org b/org/testdata/example.org index abbe07f..2e77050 100644 --- a/org/testdata/example.org +++ b/org/testdata/example.org @@ -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