diff --git a/link/link.go b/link/link.go
index 3f9b9c4..6c7e7b8 100644
--- a/link/link.go
+++ b/link/link.go
@@ -41,6 +41,13 @@ type Link struct {
RelativeTo string
}
+// DoubtExistence sets DestinationUnknown to true if the link is local hypha link.
+func (l *Link) DoubtExistence() {
+ if l.Kind == LinkLocalHypha {
+ l.DestinationUnknown = true
+ }
+}
+
// Classes returns CSS class string for given link.
func (l *Link) Classes() string {
if l.Kind == LinkExternal {
@@ -53,7 +60,7 @@ func (l *Link) Classes() string {
return classes
}
-// Href returns content for the href attrubite. You should always use it.
+// Href returns content for the href attrubite for hyperlink. You should always use it.
func (l *Link) Href() string {
switch l.Kind {
case LinkExternal, LinkLocalRoot:
@@ -63,6 +70,16 @@ func (l *Link) Href() string {
}
}
+// ImgSrc returns content for src attribute of img tag. Used with `img{}`.
+func (l *Link) ImgSrc() string {
+ switch l.Kind {
+ case LinkExternal, LinkLocalRoot:
+ return l.Address
+ default:
+ return "/binary/" + l.Address
+ }
+}
+
// From returns a Link object given these `address` and `display` on relative to given `hyphaName`.
func From(address, display, hyphaName string) *Link {
link := Link{
@@ -100,6 +117,7 @@ func From(address, display, hyphaName string) *Link {
link.Kind = LinkLocalHypha
link.Address = util.CanonicalName(path.Join(path.Dir(hyphaName), address[3:]))
default:
+ link.Kind = LinkLocalHypha
link.Address = util.CanonicalName(address)
}
diff --git a/markup/img.go b/markup/img.go
index 99c919b..b1a59c1 100644
--- a/markup/img.go
+++ b/markup/img.go
@@ -5,7 +5,7 @@ import (
"regexp"
"strings"
- "github.com/bouncepaw/mycorrhiza/util"
+ "github.com/bouncepaw/mycorrhiza/link"
)
var imgRe = regexp.MustCompile(`^img\s+{`)
@@ -14,44 +14,6 @@ func MatchesImg(line string) bool {
return imgRe.MatchString(line)
}
-type imgEntry struct {
- trimmedPath string
- path strings.Builder
- sizeW strings.Builder
- sizeH strings.Builder
- desc strings.Builder
-}
-
-func (entry *imgEntry) descriptionAsHtml(hyphaName string) (html string) {
- if entry.desc.Len() == 0 {
- return ""
- }
- lines := strings.Split(entry.desc.String(), "\n")
- for _, line := range lines {
- if line = strings.TrimSpace(line); line != "" {
- if html != "" {
- html += `
`
- }
- html += ParagraphToHtml(hyphaName, line)
- }
- }
- return `` + html + ``
-}
-
-func (entry *imgEntry) sizeWAsAttr() string {
- if entry.sizeW.Len() == 0 {
- return ""
- }
- return ` width="` + entry.sizeW.String() + `"`
-}
-
-func (entry *imgEntry) sizeHAsAttr() string {
- if entry.sizeH.Len() == 0 {
- return ""
- }
- return ` height="` + entry.sizeH.String() + `"`
-}
-
type imgState int
const (
@@ -71,6 +33,8 @@ type Img struct {
func (img *Img) pushEntry() {
if strings.TrimSpace(img.currEntry.path.String()) != "" {
+ img.currEntry.srclink = link.From(img.currEntry.path.String(), "", img.hyphaName)
+ img.currEntry.srclink.DoubtExistence()
img.entries = append(img.entries, img.currEntry)
img.currEntry = imgEntry{}
img.currEntry.path.Reset()
@@ -177,23 +141,6 @@ func ImgFromFirstLine(line, hyphaName string) (img *Img, shouldGoBackToNormal bo
return img, img.Process(line)
}
-func (img *Img) binaryPathFor(path string) string {
- path = strings.TrimSpace(path)
- if strings.IndexRune(path, ':') != -1 || strings.IndexRune(path, '/') == 0 {
- return path
- } else {
- return "/binary/" + xclCanonicalName(img.hyphaName, path)
- }
-}
-
-func (img *Img) ogBinaryPathFor(path string) string {
- path = img.binaryPathFor(path)
- if strings.HasPrefix(path, "/binary/") {
- return util.URL + path
- }
- return path
-}
-
func (img *Img) pagePathFor(path string) string {
path = strings.TrimSpace(path)
if strings.IndexRune(path, ':') != -1 || strings.IndexRune(path, '/') == 0 {
@@ -214,30 +161,18 @@ func parseDimensions(dimensions string) (sizeW, sizeH string) {
return
}
-func (img *Img) checkLinks() map[string]bool {
- m := make(map[string]bool)
- for i, entry := range img.entries {
- // Also trim them for later use
- entry.trimmedPath = strings.TrimSpace(entry.path.String())
- isAbsoluteUrl := strings.ContainsRune(entry.trimmedPath, ':')
- if !isAbsoluteUrl {
- entry.trimmedPath = canonicalName(entry.trimmedPath)
- }
- img.entries[i] = entry
- m[entry.trimmedPath] = isAbsoluteUrl
- }
- HyphaIterate(func(hyphaName string) {
+func (img *Img) markExistenceOfSrcLinks() {
+ HyphaIterate(func(hn string) {
for _, entry := range img.entries {
- if hyphaName == xclCanonicalName(img.hyphaName, entry.trimmedPath) {
- m[entry.trimmedPath] = true
+ if hn == entry.srclink.Address {
+ entry.srclink.DestinationUnknown = false
}
}
})
- return m
}
func (img *Img) ToHtml() (html string) {
- linkAvailabilityMap := img.checkLinks()
+ img.markExistenceOfSrcLinks()
isOneImageOnly := len(img.entries) == 1 && img.entries[0].desc.Len() == 0
if isOneImageOnly {
html += ``
@@ -247,15 +182,19 @@ func (img *Img) ToHtml() (html string) {
for _, entry := range img.entries {
html += ``
diff --git a/markup/img_entry.go b/markup/img_entry.go
new file mode 100644
index 0000000..acba2fd
--- /dev/null
+++ b/markup/img_entry.go
@@ -0,0 +1,45 @@
+package markup
+
+import (
+ "strings"
+
+ "github.com/bouncepaw/mycorrhiza/link"
+)
+
+type imgEntry struct {
+ srclink *link.Link
+ path strings.Builder
+ sizeW strings.Builder
+ sizeH strings.Builder
+ desc strings.Builder
+}
+
+func (entry *imgEntry) descriptionAsHtml(hyphaName string) (html string) {
+ if entry.desc.Len() == 0 {
+ return ""
+ }
+ lines := strings.Split(entry.desc.String(), "\n")
+ for _, line := range lines {
+ if line = strings.TrimSpace(line); line != "" {
+ if html != "" {
+ html += `
`
+ }
+ html += ParagraphToHtml(hyphaName, line)
+ }
+ }
+ return `` + html + ``
+}
+
+func (entry *imgEntry) sizeWAsAttr() string {
+ if entry.sizeW.Len() == 0 {
+ return ""
+ }
+ return ` width="` + entry.sizeW.String() + `"`
+}
+
+func (entry *imgEntry) sizeHAsAttr() string {
+ if entry.sizeH.Len() == 0 {
+ return ""
+ }
+ return ` height="` + entry.sizeH.String() + `"`
+}
diff --git a/markup/mycomarkup.go b/markup/mycomarkup.go
index 656ca28..f15c07b 100644
--- a/markup/mycomarkup.go
+++ b/markup/mycomarkup.go
@@ -7,6 +7,7 @@ import (
"regexp"
"strings"
+ "github.com/bouncepaw/mycorrhiza/link"
"github.com/bouncepaw/mycorrhiza/util"
)
@@ -57,15 +58,16 @@ func (md *MycoDoc) OpenGraphHTML() string {
ogTag("title", md.hyphaName),
ogTag("type", "article"),
ogTag("image", md.firstImageURL),
- ogTag("url", util.URL+"/page/"+md.hyphaName),
+ ogTag("url", util.URL+"/hypha/"+md.hyphaName),
ogTag("determiner", ""),
ogTag("description", htmlTagRe.ReplaceAllString(md.description, "")),
}, "\n")
}
func (md *MycoDoc) ogFillVars() *MycoDoc {
+ md.firstImageURL = util.URL + "/favicon.ico"
foundDesc := false
- md.firstImageURL = HyphaImageForOG(md.hyphaName)
+ foundImg := false
for _, line := range md.ast {
switch v := line.contents.(type) {
case string:
@@ -74,8 +76,12 @@ func (md *MycoDoc) ogFillVars() *MycoDoc {
foundDesc = true
}
case Img:
- if len(v.entries) > 0 {
- md.firstImageURL = v.entries[0].path.String()
+ if !foundImg && len(v.entries) > 0 {
+ md.firstImageURL = v.entries[0].srclink.ImgSrc()
+ if v.entries[0].srclink.Kind != link.LinkExternal {
+ md.firstImageURL = util.URL + md.firstImageURL
+ }
+ foundImg = true
}
}
}
@@ -153,6 +159,7 @@ func crawl(name, content string) []string {
preAcc += html.EscapeString(line)
}
}
+ break
}
return []string{}