mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-31 22:41:53 +02:00
Reorganization of helpers. Centralized Url/Path logic. Fixed #175.
This commit is contained in:
@@ -17,6 +17,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/spf13/hugo/helpers"
|
||||
"io/ioutil"
|
||||
"launchpad.net/goyaml"
|
||||
"os"
|
||||
@@ -67,7 +68,7 @@ func SetupConfig(cfgfile *string, path *string) *Config {
|
||||
c.readInConfig()
|
||||
|
||||
// set index defaults if none provided
|
||||
if len(c.Indexes) == 0 {
|
||||
if c.Indexes == nil {
|
||||
c.Indexes = make(map[string]string)
|
||||
c.Indexes["tag"] = "tags"
|
||||
c.Indexes["category"] = "categories"
|
||||
@@ -169,15 +170,15 @@ func (c *Config) GetAbsPath(name string) string {
|
||||
func (c *Config) findConfigFile(configFileName string) (string, error) {
|
||||
|
||||
if configFileName == "" { // config not specified, let's search
|
||||
if b, _ := exists(c.GetAbsPath("config.json")); b {
|
||||
if b, _ := helpers.Exists(c.GetAbsPath("config.json")); b {
|
||||
return c.GetAbsPath("config.json"), nil
|
||||
}
|
||||
|
||||
if b, _ := exists(c.GetAbsPath("config.toml")); b {
|
||||
if b, _ := helpers.Exists(c.GetAbsPath("config.toml")); b {
|
||||
return c.GetAbsPath("config.toml"), nil
|
||||
}
|
||||
|
||||
if b, _ := exists(c.GetAbsPath("config.yaml")); b {
|
||||
if b, _ := helpers.Exists(c.GetAbsPath("config.yaml")); b {
|
||||
return c.GetAbsPath("config.yaml"), nil
|
||||
}
|
||||
|
||||
@@ -191,7 +192,7 @@ func (c *Config) findConfigFile(configFileName string) (string, error) {
|
||||
|
||||
// Else check the local directory
|
||||
t := c.GetAbsPath(configFileName)
|
||||
if b, _ := exists(t); b {
|
||||
if b, _ := helpers.Exists(t); b {
|
||||
return t, nil
|
||||
} else {
|
||||
return "", fmt.Errorf("config file not found at: %s", t)
|
||||
|
@@ -76,7 +76,7 @@ type Pages []*Page
|
||||
|
||||
func (p *Page) Plain() string {
|
||||
if len(p.plain) == 0 {
|
||||
p.plain = StripHTML(StripShortcodes(string(p.renderBytes(p.rawContent))))
|
||||
p.plain = helpers.StripHTML(StripShortcodes(string(p.renderBytes(p.rawContent))))
|
||||
}
|
||||
return p.plain
|
||||
}
|
||||
@@ -147,38 +147,6 @@ func newPage(filename string) *Page {
|
||||
return &page
|
||||
}
|
||||
|
||||
func StripHTML(s string) string {
|
||||
output := ""
|
||||
|
||||
// Shortcut strings with no tags in them
|
||||
if !strings.ContainsAny(s, "<>") {
|
||||
output = s
|
||||
} else {
|
||||
s = strings.Replace(s, "\n", " ", -1)
|
||||
s = strings.Replace(s, "</p>", " \n", -1)
|
||||
s = strings.Replace(s, "<br>", " \n", -1)
|
||||
s = strings.Replace(s, "</br>", " \n", -1)
|
||||
|
||||
// Walk through the string removing all tags
|
||||
b := new(bytes.Buffer)
|
||||
inTag := false
|
||||
for _, r := range s {
|
||||
switch r {
|
||||
case '<':
|
||||
inTag = true
|
||||
case '>':
|
||||
inTag = false
|
||||
default:
|
||||
if !inTag {
|
||||
b.WriteRune(r)
|
||||
}
|
||||
}
|
||||
}
|
||||
output = b.String()
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
func (p *Page) IsRenderable() bool {
|
||||
return p.renderable
|
||||
}
|
||||
@@ -274,40 +242,17 @@ func (p *Page) permalink() (*url.URL, error) {
|
||||
}
|
||||
//fmt.Printf("have an override for %q in section %s → %s\n", p.Title, p.Section, permalink)
|
||||
} else {
|
||||
|
||||
if len(pSlug) > 0 {
|
||||
if p.Site.Config != nil && p.Site.Config.UglyUrls {
|
||||
filename := fmt.Sprintf("%s.%s", p.Slug, p.Extension)
|
||||
permalink = path.Join(dir, filename)
|
||||
} else {
|
||||
permalink = path.Join(dir, p.Slug) + "/"
|
||||
}
|
||||
permalink = helpers.UrlPrep(p.Site.Config.UglyUrls, path.Join(dir, p.Slug+"."+p.Extension))
|
||||
} else if len(pUrl) > 2 {
|
||||
permalink = pUrl
|
||||
} else {
|
||||
_, t := path.Split(p.FileName)
|
||||
if p.Site.Config != nil && p.Site.Config.UglyUrls {
|
||||
x := replaceExtension(strings.TrimSpace(t), p.Extension)
|
||||
permalink = path.Join(dir, x)
|
||||
} else {
|
||||
file, _ := fileExt(strings.TrimSpace(t))
|
||||
permalink = path.Join(dir, file)
|
||||
}
|
||||
permalink = helpers.UrlPrep(p.Site.Config.UglyUrls, path.Join(dir, helpers.ReplaceExtension(strings.TrimSpace(t), p.Extension)))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
base, err := url.Parse(baseUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
path, err := url.Parse(permalink)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return MakePermalink(base, path), nil
|
||||
return helpers.MakePermalink(baseUrl, permalink), nil
|
||||
}
|
||||
|
||||
func (p *Page) LinkTitle() string {
|
||||
@@ -696,7 +641,7 @@ func (p *Page) TargetPath() (outfile string) {
|
||||
} else {
|
||||
// Fall back to filename
|
||||
_, t := path.Split(p.FileName)
|
||||
outfile = replaceExtension(strings.TrimSpace(t), p.Extension)
|
||||
outfile = helpers.ReplaceExtension(strings.TrimSpace(t), p.Extension)
|
||||
}
|
||||
|
||||
return path.Join(p.Dir, strings.TrimSpace(outfile))
|
||||
|
@@ -17,9 +17,9 @@ func TestPermalink(t *testing.T) {
|
||||
}{
|
||||
{"x/y/z/boofar.md", "x/y/z", "", "", false, "/x/y/z/boofar", "/x/y/z/boofar"},
|
||||
{"x/y/z/boofar.md", "x/y/z/", "", "", false, "/x/y/z/boofar", "/x/y/z/boofar"},
|
||||
{"x/y/z/boofar.md", "x/y/z/", "", "boofar", false, "/x/y/z/boofar/", "/x/y/z/boofar/"},
|
||||
{"x/y/z/boofar.md", "x/y/z/", "", "boofar", false, "/x/y/z/boofar", "/x/y/z/boofar"},
|
||||
{"x/y/z/boofar.md", "x/y/z", "http://barnew/", "", false, "http://barnew/x/y/z/boofar", "/x/y/z/boofar"},
|
||||
{"x/y/z/boofar.md", "x/y/z/", "http://barnew/", "boofar", false, "http://barnew/x/y/z/boofar/", "/x/y/z/boofar/"},
|
||||
{"x/y/z/boofar.md", "x/y/z/", "http://barnew/", "boofar", false, "http://barnew/x/y/z/boofar", "/x/y/z/boofar"},
|
||||
{"x/y/z/boofar.md", "x/y/z", "", "", true, "/x/y/z/boofar.html", "/x/y/z/boofar.html"},
|
||||
{"x/y/z/boofar.md", "x/y/z/", "", "", true, "/x/y/z/boofar.html", "/x/y/z/boofar.html"},
|
||||
{"x/y/z/boofar.md", "x/y/z/", "", "boofar", true, "/x/y/z/boofar.html", "/x/y/z/boofar.html"},
|
||||
|
@@ -1,34 +0,0 @@
|
||||
package hugolib
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func fileExt(path string) (file, ext string) {
|
||||
if strings.Contains(path, ".") {
|
||||
i := len(path) - 1
|
||||
for path[i] != '.' {
|
||||
i--
|
||||
}
|
||||
return path[:i], path[i+1:]
|
||||
}
|
||||
return path, ""
|
||||
}
|
||||
|
||||
func replaceExtension(path string, newExt string) string {
|
||||
f, _ := fileExt(path)
|
||||
return f + "." + newExt
|
||||
}
|
||||
|
||||
// Check if Exists && is Directory
|
||||
func dirExists(path string) (bool, error) {
|
||||
fi, err := os.Stat(path)
|
||||
if err == nil && fi.IsDir() {
|
||||
return true, nil
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
@@ -25,7 +25,6 @@ import (
|
||||
"github.com/spf13/nitro"
|
||||
"html/template"
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -35,10 +34,6 @@ var _ = transform.AbsURL
|
||||
|
||||
var DefaultTimer *nitro.B
|
||||
|
||||
func MakePermalink(base *url.URL, path *url.URL) *url.URL {
|
||||
return base.ResolveReference(path)
|
||||
}
|
||||
|
||||
// Site contains all the information relevant for constructing a static
|
||||
// site. The basic flow of information is as follows:
|
||||
//
|
||||
@@ -228,18 +223,6 @@ func (s *Site) initializeSiteInfo() {
|
||||
}
|
||||
}
|
||||
|
||||
// Check if File / Directory Exists
|
||||
func exists(path string) (bool, error) {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
func (s *Site) absLayoutDir() string {
|
||||
return s.Config.GetAbsPath(s.Config.LayoutDir)
|
||||
}
|
||||
@@ -253,12 +236,7 @@ func (s *Site) absPublishDir() string {
|
||||
}
|
||||
|
||||
func (s *Site) checkDirectories() (err error) {
|
||||
/*
|
||||
if b, _ := dirExists(s.absLayoutDir()); !b {
|
||||
return fmt.Errorf("No layout directory found, expecting to find it at " + s.absLayoutDir())
|
||||
}
|
||||
*/
|
||||
if b, _ := dirExists(s.absContentDir()); !b {
|
||||
if b, _ := helpers.DirExists(s.absContentDir()); !b {
|
||||
return fmt.Errorf("No source directory found, expecting to find it at " + s.absContentDir())
|
||||
}
|
||||
return
|
||||
@@ -418,18 +396,12 @@ func (s *Site) RenderIndexes() error {
|
||||
for k, o := range s.Indexes[plural] {
|
||||
n := s.NewNode()
|
||||
n.Title = strings.Title(k)
|
||||
url := helpers.Urlize(plural + "/" + k)
|
||||
n.Url = url + ".html"
|
||||
plink := n.Url
|
||||
n.Permalink = permalink(s, plink)
|
||||
n.RSSLink = permalink(s, url+".xml")
|
||||
base := plural + "/" + k
|
||||
s.setUrls(n, base)
|
||||
n.Date = o[0].Page.Date
|
||||
n.Data[singular] = o
|
||||
n.Data["Pages"] = o.Pages()
|
||||
layout := "indexes/" + singular + ".html"
|
||||
|
||||
var base string
|
||||
base = plural + "/" + k
|
||||
err := s.render(n, base+".html", layout)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -437,8 +409,7 @@ func (s *Site) RenderIndexes() error {
|
||||
|
||||
if a := s.Tmpl.Lookup("rss.xml"); a != nil {
|
||||
// XML Feed
|
||||
n.Url = helpers.Urlize(plural + "/" + k + ".xml")
|
||||
n.Permalink = permalink(s, n.Url)
|
||||
s.setUrls(n, base+".xml")
|
||||
err := s.render(n, base+".xml", "rss.xml")
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -455,9 +426,7 @@ func (s *Site) RenderIndexesIndexes() (err error) {
|
||||
for singular, plural := range s.Config.Indexes {
|
||||
n := s.NewNode()
|
||||
n.Title = strings.Title(plural)
|
||||
url := helpers.Urlize(plural)
|
||||
n.Url = url + "/index.html"
|
||||
n.Permalink = permalink(s, n.Url)
|
||||
s.setUrls(n, plural)
|
||||
n.Data["Singular"] = singular
|
||||
n.Data["Plural"] = plural
|
||||
n.Data["Index"] = s.Indexes[plural]
|
||||
@@ -477,9 +446,7 @@ func (s *Site) RenderLists() error {
|
||||
for section, data := range s.Sections {
|
||||
n := s.NewNode()
|
||||
n.Title = strings.Title(inflect.Pluralize(section))
|
||||
n.Url = helpers.Urlize(section + "/" + "index.html")
|
||||
n.Permalink = permalink(s, n.Url)
|
||||
n.RSSLink = permalink(s, section+".xml")
|
||||
s.setUrls(n, section)
|
||||
n.Date = data[0].Page.Date
|
||||
n.Data["Pages"] = data.Pages()
|
||||
layout := "indexes/" + section + ".html"
|
||||
@@ -491,8 +458,7 @@ func (s *Site) RenderLists() error {
|
||||
|
||||
if a := s.Tmpl.Lookup("rss.xml"); a != nil {
|
||||
// XML Feed
|
||||
n.Url = helpers.Urlize(section + ".xml")
|
||||
n.Permalink = template.HTML(string(n.Site.BaseUrl) + n.Url)
|
||||
s.setUrls(n, section+".xml")
|
||||
err = s.render(n, section+".xml", "rss.xml")
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -505,9 +471,7 @@ func (s *Site) RenderLists() error {
|
||||
func (s *Site) RenderHomePage() error {
|
||||
n := s.NewNode()
|
||||
n.Title = n.Site.Title
|
||||
n.Url = helpers.Urlize(string(n.Site.BaseUrl))
|
||||
n.RSSLink = permalink(s, "index.xml")
|
||||
n.Permalink = permalink(s, "")
|
||||
s.setUrls(n, "/")
|
||||
n.Data["Pages"] = s.Pages
|
||||
err := s.render(n, "/", "index.html")
|
||||
if err != nil {
|
||||
@@ -518,7 +482,7 @@ func (s *Site) RenderHomePage() error {
|
||||
// XML Feed
|
||||
n.Url = helpers.Urlize("index.xml")
|
||||
n.Title = "Recent Content"
|
||||
n.Permalink = permalink(s, "index.xml")
|
||||
n.Permalink = s.permalink("index.xml")
|
||||
high := 50
|
||||
if len(s.Pages) < high {
|
||||
high = len(s.Pages)
|
||||
@@ -536,7 +500,7 @@ func (s *Site) RenderHomePage() error {
|
||||
if a := s.Tmpl.Lookup("404.html"); a != nil {
|
||||
n.Url = helpers.Urlize("404.html")
|
||||
n.Title = "404 Page not found"
|
||||
n.Permalink = permalink(s, "404.html")
|
||||
n.Permalink = s.permalink("404.html")
|
||||
return s.render(n, "404.html", "404.html")
|
||||
}
|
||||
|
||||
@@ -550,18 +514,26 @@ func (s *Site) Stats() {
|
||||
}
|
||||
}
|
||||
|
||||
func permalink(s *Site, plink string) template.HTML {
|
||||
base, err := url.Parse(string(s.Config.BaseUrl))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
func (s *Site) setUrls(n *Node, in string) {
|
||||
n.Url = s.prepUrl(in)
|
||||
n.Permalink = s.permalink(n.Url)
|
||||
n.RSSLink = s.permalink(in + ".xml")
|
||||
}
|
||||
|
||||
path, err := url.Parse(plink)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
func (s *Site) permalink(plink string) template.HTML {
|
||||
return template.HTML(helpers.MakePermalink(string(s.Config.BaseUrl), s.prepUrl(plink)).String())
|
||||
}
|
||||
|
||||
return template.HTML(MakePermalink(base, path).String())
|
||||
func (s *Site) prepUrl(in string) string {
|
||||
return helpers.Urlize(s.PrettifyUrl(in))
|
||||
}
|
||||
|
||||
func (s *Site) PrettifyUrl(in string) string {
|
||||
return helpers.UrlPrep(s.Config.UglyUrls, in)
|
||||
}
|
||||
|
||||
func (s *Site) PrettifyPath(in string) string {
|
||||
return helpers.PathPrep(s.Config.UglyUrls, in)
|
||||
}
|
||||
|
||||
func (s *Site) NewNode() *Node {
|
||||
|
Reference in New Issue
Block a user