1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-14 14:20:25 +00:00
mycorrhiza/markup/link.go

59 lines
1.6 KiB
Go
Raw Normal View History

2020-11-04 17:42:02 +00:00
package markup
import (
2020-12-15 18:59:36 +00:00
"fmt"
2020-11-04 17:42:02 +00:00
"path"
"strings"
)
// LinkParts determines what href, text and class should resulting <a> have based on mycomarkup's addr, display and hypha name.
//
// => addr display
// [[addr|display]]
func LinkParts(addr, display, hyphaName string) (href, text, class string) {
2020-11-04 17:42:02 +00:00
if display == "" {
text = addr
} else {
text = strings.TrimSpace(display)
}
class = "wikilink wikilink_internal"
2020-11-04 17:42:02 +00:00
switch {
case strings.ContainsRune(addr, ':'):
2020-12-15 18:59:36 +00:00
pos := strings.IndexRune(addr, ':')
destination := addr[:pos]
2020-12-17 14:58:11 +00:00
if display == "" {
text = addr[pos+1:]
if strings.HasPrefix(text, "//") && len(text) > 2 {
text = text[2:]
}
2020-12-15 18:59:36 +00:00
}
return addr, text, fmt.Sprintf("wikilink wikilink_external wikilink_%s", destination)
2020-11-04 17:42:02 +00:00
case strings.HasPrefix(addr, "/"):
return addr, text, class
2020-11-04 17:42:02 +00:00
case strings.HasPrefix(addr, "./"):
hyphaName = canonicalName(path.Join(hyphaName, addr[2:]))
case strings.HasPrefix(addr, "../"):
hyphaName = canonicalName(path.Join(path.Dir(hyphaName), addr[3:]))
default:
hyphaName = canonicalName(addr)
}
if !HyphaExists(hyphaName) {
class += " wikilink_new"
}
return "/page/" + hyphaName, text, class
2020-11-04 17:42:02 +00:00
}
// Parse markup line starting with "=>" according to wikilink rules.
// See http://localhost:1737/page/wikilink
func Rocketlink(src, hyphaName string) (href, text, class string) {
2020-11-04 17:42:02 +00:00
src = strings.TrimSpace(src[2:]) // Drop =>
if src == "" {
return
}
// Href is text after => till first whitespace
addr := strings.Fields(src)[0]
display := strings.TrimPrefix(src, addr)
return LinkParts(addr, display, hyphaName)
}