1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-02-03 20:59:19 +00:00

Make img galleries use the new link abstraction

This commit is contained in:
bouncepaw 2021-01-31 21:39:08 +05:00
parent dd98ae492b
commit b4e866446e
4 changed files with 94 additions and 85 deletions

View File

@ -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)
}

View File

@ -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 += `<br>`
}
html += ParagraphToHtml(hyphaName, line)
}
}
return `<figcaption>` + html + `</figcaption>`
}
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 += `<section class="img-gallery img-gallery_one-image">`
@ -247,15 +182,19 @@ func (img *Img) ToHtml() (html string) {
for _, entry := range img.entries {
html += `<figure>`
// If is existing hypha or an external path
if linkAvailabilityMap[entry.trimmedPath] {
if entry.srclink.DestinationUnknown {
html += fmt.Sprintf(
`<a class="%s" href="%s">Hypha <i>%s</i> does not exist</a>`,
entry.srclink.Classes(),
entry.srclink.Href(),
entry.srclink.Address)
} else {
html += fmt.Sprintf(
`<a href="%s"><img src="%s" %s %s></a>`,
img.pagePathFor(entry.trimmedPath),
img.binaryPathFor(entry.trimmedPath),
entry.sizeWAsAttr(), entry.sizeHAsAttr())
} else { // If is a non-existent hypha
html += fmt.Sprintf(`<a class="wikilink_new" href="%s">Hypha <em>%s</em> does not exist</a>`, img.pagePathFor(entry.trimmedPath), entry.trimmedPath)
entry.srclink.Href(),
entry.srclink.ImgSrc(),
entry.sizeWAsAttr(),
entry.sizeHAsAttr())
}
html += entry.descriptionAsHtml(img.hyphaName)
html += `</figure>`

45
markup/img_entry.go Normal file
View File

@ -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 += `<br>`
}
html += ParagraphToHtml(hyphaName, line)
}
}
return `<figcaption>` + html + `</figcaption>`
}
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() + `"`
}

View File

@ -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{}