From f17923047b2d79be3f7d6cde7441f58f015ad613 Mon Sep 17 00:00:00 2001 From: Niklas Fasching Date: Mon, 3 Dec 2018 16:31:27 +0100 Subject: [PATCH] Refactor RegularLink: Do not fake description if none is given To more faithfully handle inline images we need to know whether the original link included a description - being more explicit about that will make it easier. see org.el/org-display-inline-images > An inline image is a link which follows either of these > conventions: > > 1. Its path is a file with an extension matching return value > from `image-file-name-regexp' and it has no contents. > > 2. Its description consists in a single link of the previous > type. --- org/html.go | 9 ++++++--- org/inline.go | 20 ++++++++------------ org/org.go | 12 ++++++------ 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/org/html.go b/org/html.go index 1db4ab4..ea7afce 100644 --- a/org/html.go +++ b/org/html.go @@ -188,9 +188,12 @@ func (w *HTMLWriter) writeFootnoteLink(l FootnoteLink) { func (w *HTMLWriter) writeRegularLink(l RegularLink) { url := html.EscapeString(l.URL) - descriptionWriter := w.emptyClone() - descriptionWriter.writeNodes(l.Description...) - description := descriptionWriter.String() + 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:"):] diff --git a/org/inline.go b/org/inline.go index df7bade..5b71d8a 100644 --- a/org/inline.go +++ b/org/inline.go @@ -144,8 +144,7 @@ func (d *Document) parseAutoLink(input string, start int) (int, int, Node) { if path == "://" { return 0, 0, nil } - link := RegularLink{protocol, []Node{Text{protocol + path}}, protocol + path, true} - return len(protocol), len(path + protocol), link + return len(protocol), len(path + protocol), RegularLink{protocol, nil, protocol + path, true} } func (d *Document) parseRegularLink(input string, start int) (int, Node) { @@ -157,18 +156,15 @@ func (d *Document) parseRegularLink(input string, start int) (int, Node) { if end == -1 { return 0, nil } - - rawLink := input[2:end] - link, description, parts := "", []Node{}, strings.Split(rawLink, "][") - if len(parts) == 2 { - link, description = parts[0], d.parseInline(parts[1]) - } else { - link, description = rawLink, []Node{Text{rawLink}} + rawLinkParts := strings.Split(input[2:end], "][") + description, link := ([]Node)(nil), rawLinkParts[0] + if len(rawLinkParts) == 2 { + link, description = rawLinkParts[0], d.parseInline(rawLinkParts[1]) } consumed := end + 2 - protocol, parts := "", strings.SplitN(link, ":", 2) - if len(parts) == 2 { - protocol = parts[0] + protocol, linkParts := "", strings.SplitN(link, ":", 2) + if len(linkParts) == 2 { + protocol = linkParts[0] } return consumed, RegularLink{protocol, description, link, false} } diff --git a/org/org.go b/org/org.go index b2aedea..4b77b43 100644 --- a/org/org.go +++ b/org/org.go @@ -247,14 +247,14 @@ func (w *OrgWriter) writeFootnoteLink(l FootnoteLink) { } func (w *OrgWriter) writeRegularLink(l RegularLink) { - descriptionWriter := w.emptyClone() - descriptionWriter.writeNodes(l.Description...) - description := descriptionWriter.String() if l.AutoLink { w.WriteString(l.URL) - } else if l.URL != description { - w.WriteString(fmt.Sprintf("[[%s][%s]]", l.URL, description)) - } else { + } else if l.Description == nil { w.WriteString(fmt.Sprintf("[[%s]]", l.URL)) + } else { + descriptionWriter := w.emptyClone() + descriptionWriter.writeNodes(l.Description...) + description := descriptionWriter.String() + w.WriteString(fmt.Sprintf("[[%s][%s]]", l.URL, description)) } }