2020-08-05 15:08:59 +00:00
package main
import (
"fmt"
"net/http"
"strings"
2020-10-25 15:06:51 +00:00
"github.com/bouncepaw/mycorrhiza/util"
2020-08-05 15:08:59 +00:00
)
// isCanonicalName checks if the `name` is canonical.
func isCanonicalName ( name string ) bool {
return HyphaPattern . MatchString ( name )
}
// CanonicalName makes sure the `name` is canonical. A name is canonical if it is lowercase and all spaces are replaced with underscores.
func CanonicalName ( name string ) string {
return strings . ToLower ( strings . ReplaceAll ( name , " " , "_" ) )
}
// naviTitle turns `canonicalName` into html string with each hypha path parts higlighted as links.
2020-10-25 15:06:51 +00:00
// TODO: rework as a template
2020-08-05 15:08:59 +00:00
func naviTitle ( canonicalName string ) string {
var (
2020-10-25 15:06:51 +00:00
html = fmt . Sprintf ( ` < h1 class = "navi-title" id = "navi-title" >
2021-01-23 13:45:17 +00:00
< a href = "/page/%s" > % s < / a > < span aria - hidden = "true" class = "navi-title__colon" > : < / span > ` , util . HomePage , util . SiteNavIcon )
2020-08-05 15:08:59 +00:00
prevAcc = ` /page/ `
parts = strings . Split ( canonicalName , "/" )
2021-01-14 11:51:17 +00:00
rel = "up"
2020-08-05 15:08:59 +00:00
)
2021-01-10 11:58:02 +00:00
for i , part := range parts {
if i > 0 {
html += ` <span aria-hidden="true" class="navi-title__separator">/</span> `
}
2021-01-14 11:51:17 +00:00
if i == len ( parts ) - 1 {
rel = "bookmark"
}
2021-01-10 11:58:02 +00:00
html += fmt . Sprintf (
2021-01-14 11:51:17 +00:00
` <a href="%s" rel="%s">%s</a> ` ,
2020-08-05 15:08:59 +00:00
prevAcc + part ,
2021-01-14 11:51:17 +00:00
rel ,
2021-01-10 11:58:02 +00:00
util . BeautifulName ( part ) ,
)
2020-08-05 15:08:59 +00:00
prevAcc += part + "/"
}
return html + "</h1>"
}
// HyphaNameFromRq extracts hypha name from http request. You have to also pass the action which is embedded in the url. For url /page/hypha, the action would be "page".
func HyphaNameFromRq ( rq * http . Request , action string ) string {
return CanonicalName ( strings . TrimPrefix ( rq . URL . Path , "/" + action + "/" ) )
}