mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2025-03-30 15:27:04 +00:00
Make img galleries use the new link abstraction
This commit is contained in:
parent
dd98ae492b
commit
b4e866446e
20
link/link.go
20
link/link.go
@ -41,6 +41,13 @@ type Link struct {
|
|||||||
RelativeTo string
|
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.
|
// Classes returns CSS class string for given link.
|
||||||
func (l *Link) Classes() string {
|
func (l *Link) Classes() string {
|
||||||
if l.Kind == LinkExternal {
|
if l.Kind == LinkExternal {
|
||||||
@ -53,7 +60,7 @@ func (l *Link) Classes() string {
|
|||||||
return classes
|
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 {
|
func (l *Link) Href() string {
|
||||||
switch l.Kind {
|
switch l.Kind {
|
||||||
case LinkExternal, LinkLocalRoot:
|
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`.
|
// From returns a Link object given these `address` and `display` on relative to given `hyphaName`.
|
||||||
func From(address, display, hyphaName string) *Link {
|
func From(address, display, hyphaName string) *Link {
|
||||||
link := Link{
|
link := Link{
|
||||||
@ -100,6 +117,7 @@ func From(address, display, hyphaName string) *Link {
|
|||||||
link.Kind = LinkLocalHypha
|
link.Kind = LinkLocalHypha
|
||||||
link.Address = util.CanonicalName(path.Join(path.Dir(hyphaName), address[3:]))
|
link.Address = util.CanonicalName(path.Join(path.Dir(hyphaName), address[3:]))
|
||||||
default:
|
default:
|
||||||
|
link.Kind = LinkLocalHypha
|
||||||
link.Address = util.CanonicalName(address)
|
link.Address = util.CanonicalName(address)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/bouncepaw/mycorrhiza/util"
|
"github.com/bouncepaw/mycorrhiza/link"
|
||||||
)
|
)
|
||||||
|
|
||||||
var imgRe = regexp.MustCompile(`^img\s+{`)
|
var imgRe = regexp.MustCompile(`^img\s+{`)
|
||||||
@ -14,44 +14,6 @@ func MatchesImg(line string) bool {
|
|||||||
return imgRe.MatchString(line)
|
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
|
type imgState int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -71,6 +33,8 @@ type Img struct {
|
|||||||
|
|
||||||
func (img *Img) pushEntry() {
|
func (img *Img) pushEntry() {
|
||||||
if strings.TrimSpace(img.currEntry.path.String()) != "" {
|
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.entries = append(img.entries, img.currEntry)
|
||||||
img.currEntry = imgEntry{}
|
img.currEntry = imgEntry{}
|
||||||
img.currEntry.path.Reset()
|
img.currEntry.path.Reset()
|
||||||
@ -177,23 +141,6 @@ func ImgFromFirstLine(line, hyphaName string) (img *Img, shouldGoBackToNormal bo
|
|||||||
return img, img.Process(line)
|
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 {
|
func (img *Img) pagePathFor(path string) string {
|
||||||
path = strings.TrimSpace(path)
|
path = strings.TrimSpace(path)
|
||||||
if strings.IndexRune(path, ':') != -1 || strings.IndexRune(path, '/') == 0 {
|
if strings.IndexRune(path, ':') != -1 || strings.IndexRune(path, '/') == 0 {
|
||||||
@ -214,30 +161,18 @@ func parseDimensions(dimensions string) (sizeW, sizeH string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (img *Img) checkLinks() map[string]bool {
|
func (img *Img) markExistenceOfSrcLinks() {
|
||||||
m := make(map[string]bool)
|
HyphaIterate(func(hn string) {
|
||||||
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) {
|
|
||||||
for _, entry := range img.entries {
|
for _, entry := range img.entries {
|
||||||
if hyphaName == xclCanonicalName(img.hyphaName, entry.trimmedPath) {
|
if hn == entry.srclink.Address {
|
||||||
m[entry.trimmedPath] = true
|
entry.srclink.DestinationUnknown = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return m
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (img *Img) ToHtml() (html string) {
|
func (img *Img) ToHtml() (html string) {
|
||||||
linkAvailabilityMap := img.checkLinks()
|
img.markExistenceOfSrcLinks()
|
||||||
isOneImageOnly := len(img.entries) == 1 && img.entries[0].desc.Len() == 0
|
isOneImageOnly := len(img.entries) == 1 && img.entries[0].desc.Len() == 0
|
||||||
if isOneImageOnly {
|
if isOneImageOnly {
|
||||||
html += `<section class="img-gallery img-gallery_one-image">`
|
html += `<section class="img-gallery img-gallery_one-image">`
|
||||||
@ -247,15 +182,19 @@ func (img *Img) ToHtml() (html string) {
|
|||||||
|
|
||||||
for _, entry := range img.entries {
|
for _, entry := range img.entries {
|
||||||
html += `<figure>`
|
html += `<figure>`
|
||||||
// If is existing hypha or an external path
|
if entry.srclink.DestinationUnknown {
|
||||||
if linkAvailabilityMap[entry.trimmedPath] {
|
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(
|
html += fmt.Sprintf(
|
||||||
`<a href="%s"><img src="%s" %s %s></a>`,
|
`<a href="%s"><img src="%s" %s %s></a>`,
|
||||||
img.pagePathFor(entry.trimmedPath),
|
entry.srclink.Href(),
|
||||||
img.binaryPathFor(entry.trimmedPath),
|
entry.srclink.ImgSrc(),
|
||||||
entry.sizeWAsAttr(), entry.sizeHAsAttr())
|
entry.sizeWAsAttr(),
|
||||||
} else { // If is a non-existent hypha
|
entry.sizeHAsAttr())
|
||||||
html += fmt.Sprintf(`<a class="wikilink_new" href="%s">Hypha <em>%s</em> does not exist</a>`, img.pagePathFor(entry.trimmedPath), entry.trimmedPath)
|
|
||||||
}
|
}
|
||||||
html += entry.descriptionAsHtml(img.hyphaName)
|
html += entry.descriptionAsHtml(img.hyphaName)
|
||||||
html += `</figure>`
|
html += `</figure>`
|
||||||
|
45
markup/img_entry.go
Normal file
45
markup/img_entry.go
Normal 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() + `"`
|
||||||
|
}
|
@ -7,6 +7,7 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/bouncepaw/mycorrhiza/link"
|
||||||
"github.com/bouncepaw/mycorrhiza/util"
|
"github.com/bouncepaw/mycorrhiza/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -57,15 +58,16 @@ func (md *MycoDoc) OpenGraphHTML() string {
|
|||||||
ogTag("title", md.hyphaName),
|
ogTag("title", md.hyphaName),
|
||||||
ogTag("type", "article"),
|
ogTag("type", "article"),
|
||||||
ogTag("image", md.firstImageURL),
|
ogTag("image", md.firstImageURL),
|
||||||
ogTag("url", util.URL+"/page/"+md.hyphaName),
|
ogTag("url", util.URL+"/hypha/"+md.hyphaName),
|
||||||
ogTag("determiner", ""),
|
ogTag("determiner", ""),
|
||||||
ogTag("description", htmlTagRe.ReplaceAllString(md.description, "")),
|
ogTag("description", htmlTagRe.ReplaceAllString(md.description, "")),
|
||||||
}, "\n")
|
}, "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (md *MycoDoc) ogFillVars() *MycoDoc {
|
func (md *MycoDoc) ogFillVars() *MycoDoc {
|
||||||
|
md.firstImageURL = util.URL + "/favicon.ico"
|
||||||
foundDesc := false
|
foundDesc := false
|
||||||
md.firstImageURL = HyphaImageForOG(md.hyphaName)
|
foundImg := false
|
||||||
for _, line := range md.ast {
|
for _, line := range md.ast {
|
||||||
switch v := line.contents.(type) {
|
switch v := line.contents.(type) {
|
||||||
case string:
|
case string:
|
||||||
@ -74,8 +76,12 @@ func (md *MycoDoc) ogFillVars() *MycoDoc {
|
|||||||
foundDesc = true
|
foundDesc = true
|
||||||
}
|
}
|
||||||
case Img:
|
case Img:
|
||||||
if len(v.entries) > 0 {
|
if !foundImg && len(v.entries) > 0 {
|
||||||
md.firstImageURL = v.entries[0].path.String()
|
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)
|
preAcc += html.EscapeString(line)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
return []string{}
|
return []string{}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user