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.
This commit is contained in:
Niklas Fasching 2018-12-03 16:31:27 +01:00
parent 4348505ada
commit f17923047b
3 changed files with 20 additions and 21 deletions

View file

@ -188,9 +188,12 @@ func (w *HTMLWriter) writeFootnoteLink(l FootnoteLink) {
func (w *HTMLWriter) writeRegularLink(l RegularLink) { func (w *HTMLWriter) writeRegularLink(l RegularLink) {
url := html.EscapeString(l.URL) url := html.EscapeString(l.URL)
descriptionWriter := w.emptyClone() description := url
descriptionWriter.writeNodes(l.Description...) if l.Description != nil {
description := descriptionWriter.String() descriptionWriter := w.emptyClone()
descriptionWriter.writeNodes(l.Description...)
description = descriptionWriter.String()
}
switch l.Protocol { switch l.Protocol {
case "file": case "file":
url = url[len("file:"):] url = url[len("file:"):]

View file

@ -144,8 +144,7 @@ func (d *Document) parseAutoLink(input string, start int) (int, int, Node) {
if path == "://" { if path == "://" {
return 0, 0, nil return 0, 0, nil
} }
link := RegularLink{protocol, []Node{Text{protocol + path}}, protocol + path, true} return len(protocol), len(path + protocol), RegularLink{protocol, nil, protocol + path, true}
return len(protocol), len(path + protocol), link
} }
func (d *Document) parseRegularLink(input string, start int) (int, Node) { 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 { if end == -1 {
return 0, nil return 0, nil
} }
rawLinkParts := strings.Split(input[2:end], "][")
rawLink := input[2:end] description, link := ([]Node)(nil), rawLinkParts[0]
link, description, parts := "", []Node{}, strings.Split(rawLink, "][") if len(rawLinkParts) == 2 {
if len(parts) == 2 { link, description = rawLinkParts[0], d.parseInline(rawLinkParts[1])
link, description = parts[0], d.parseInline(parts[1])
} else {
link, description = rawLink, []Node{Text{rawLink}}
} }
consumed := end + 2 consumed := end + 2
protocol, parts := "", strings.SplitN(link, ":", 2) protocol, linkParts := "", strings.SplitN(link, ":", 2)
if len(parts) == 2 { if len(linkParts) == 2 {
protocol = parts[0] protocol = linkParts[0]
} }
return consumed, RegularLink{protocol, description, link, false} return consumed, RegularLink{protocol, description, link, false}
} }

View file

@ -247,14 +247,14 @@ func (w *OrgWriter) writeFootnoteLink(l FootnoteLink) {
} }
func (w *OrgWriter) writeRegularLink(l RegularLink) { func (w *OrgWriter) writeRegularLink(l RegularLink) {
descriptionWriter := w.emptyClone()
descriptionWriter.writeNodes(l.Description...)
description := descriptionWriter.String()
if l.AutoLink { if l.AutoLink {
w.WriteString(l.URL) w.WriteString(l.URL)
} else if l.URL != description { } else if l.Description == nil {
w.WriteString(fmt.Sprintf("[[%s][%s]]", l.URL, description))
} else {
w.WriteString(fmt.Sprintf("[[%s]]", l.URL)) 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))
} }
} }