Get rid of some viper.Get* calls

Enforce usage of PathSpec

Fixes #3060
Updates #2728
This commit is contained in:
bogem 2017-02-20 03:53:48 +05:00 committed by Bjørn Erik Pedersen
parent 3d4e99ed50
commit 7e0fa13faa
3 changed files with 25 additions and 40 deletions

View file

@ -95,7 +95,7 @@ func applyDepsIfNeeded(cfg deps.DepsCfg, sites ...*Site) error {
d = deps.New(cfg)
s.Deps = d
if err := d.LoadResources(); err != nil {
if err = d.LoadResources(); err != nil {
return err
}
@ -123,9 +123,9 @@ func NewHugoSites(cfg deps.DepsCfg) (*HugoSites, error) {
func (s *Site) withSiteTemplates(withTemplates ...func(templ tpl.Template) error) func(templ tpl.Template) error {
return func(templ tpl.Template) error {
templ.LoadTemplates(s.absLayoutDir())
if s.hasTheme() {
templ.LoadTemplatesWithPrefix(s.absThemeDir()+"/layouts", "theme")
templ.LoadTemplates(s.PathSpec.GetLayoutDirPath())
if s.PathSpec.ThemeSet() {
templ.LoadTemplatesWithPrefix(s.PathSpec.GetThemeDir()+"/layouts", "theme")
}
for _, wt := range withTemplates {

View file

@ -929,7 +929,7 @@ func (p *Page) URL() string {
func (p *Page) RelPermalink() string {
link := p.getPermalink()
if p.s.Cfg.GetBool("canonifyURLs") {
if p.s.Info.canonifyURLs {
// replacements for relpermalink with baseURL on the form http://myhost.com/sub/ will fail later on
// have to return the URL relative from baseURL
relpath, err := helpers.GetRelativePath(link.String(), string(p.Site.BaseURL))

View file

@ -148,12 +148,11 @@ func newSite(cfg deps.DepsCfg) (*Site, error) {
// Note: This is mainly used in single site tests.
func NewSite(cfg deps.DepsCfg) (*Site, error) {
s, err := newSite(cfg)
if err != nil {
return nil, err
}
if err := applyDepsIfNeeded(cfg, s); err != nil {
if err = applyDepsIfNeeded(cfg, s); err != nil {
return nil, err
}
@ -302,7 +301,7 @@ func newSiteInfo(cfg siteBuilderCfg) SiteInfo {
// linkedin
type SiteSocial map[string]string
// Param is a convenience method to do lookups in Site's Params map.
// Param is a convenience method to do lookups in SiteInfo's Params map.
//
// This method is also implemented on Page and Node.
func (s *SiteInfo) Param(key interface{}) (interface{}, error) {
@ -903,7 +902,7 @@ func (s *Site) initialize() (err error) {
return err
}
staticDir := s.PathSpec.AbsPathify(s.Cfg.GetString("staticDir") + "/")
staticDir := s.PathSpec.GetStaticDirPath() + "/"
sp := source.NewSourceSpec(s.Cfg, s.Fs)
s.Source = sp.NewFilesystem(s.absContentDir(), staticDir)
@ -992,13 +991,10 @@ func (s *Site) initializeSiteInfo() {
s.Info.RSSLink = s.Info.permalinkStr(lang.GetString("rssURI"))
}
func (s *Site) hasTheme() bool {
return s.Cfg.GetString("theme") != ""
}
func (s *Site) dataDir() string {
return s.Cfg.GetString("dataDir")
}
func (s *Site) absDataDir() string {
return s.PathSpec.AbsPathify(s.dataDir())
}
@ -1023,10 +1019,10 @@ func (s *Site) getI18nDir(path string) string {
}
func (s *Site) getThemeI18nDir(path string) string {
if !s.hasTheme() {
if !s.PathSpec.ThemeSet() {
return ""
}
return s.getRealDir(s.PathSpec.AbsPathify(filepath.Join(s.themeDir(), s.i18nDir())), path)
return s.getRealDir(filepath.Join(s.PathSpec.GetThemeDir(), s.i18nDir()), path)
}
func (s *Site) isDataDirEvent(e fsnotify.Event) bool {
@ -1041,28 +1037,16 @@ func (s *Site) getDataDir(path string) string {
}
func (s *Site) getThemeDataDir(path string) string {
if !s.hasTheme() {
if !s.PathSpec.ThemeSet() {
return ""
}
return s.getRealDir(s.PathSpec.AbsPathify(filepath.Join(s.themeDir(), s.dataDir())), path)
}
func (s *Site) themeDir() string {
return s.Cfg.GetString("themesDir") + "/" + s.Cfg.GetString("theme")
}
func (s *Site) absThemeDir() string {
return s.PathSpec.AbsPathify(s.themeDir())
return s.getRealDir(filepath.Join(s.PathSpec.GetThemeDir(), s.dataDir()), path)
}
func (s *Site) layoutDir() string {
return s.Cfg.GetString("layoutDir")
}
func (s *Site) absLayoutDir() string {
return s.PathSpec.AbsPathify(s.layoutDir())
}
func (s *Site) isLayoutDirEvent(e fsnotify.Event) bool {
if s.getLayoutDir(e.Name) != "" {
return true
@ -1071,14 +1055,14 @@ func (s *Site) isLayoutDirEvent(e fsnotify.Event) bool {
}
func (s *Site) getLayoutDir(path string) string {
return s.getRealDir(s.absLayoutDir(), path)
return s.getRealDir(s.PathSpec.GetLayoutDirPath(), path)
}
func (s *Site) getThemeLayoutDir(path string) string {
if !s.hasTheme() {
if !s.PathSpec.ThemeSet() {
return ""
}
return s.getRealDir(s.PathSpec.AbsPathify(filepath.Join(s.themeDir(), s.layoutDir())), path)
return s.getRealDir(filepath.Join(s.PathSpec.GetThemeDir(), s.layoutDir()), path)
}
func (s *Site) absContentDir() string {
@ -1671,7 +1655,7 @@ func errorCollator(results <-chan error, errs chan<- error) {
}
func (s *Site) appendThemeTemplates(in []string) []string {
if !s.hasTheme() {
if !s.PathSpec.ThemeSet() {
return in
}
@ -1798,8 +1782,9 @@ func (s *Site) renderAndWritePage(name string, dest string, d interface{}, layou
}
transformLinks := transform.NewEmptyTransforms()
relativeURLs := s.Cfg.GetBool("relativeURLs")
if s.Cfg.GetBool("relativeURLs") || s.Cfg.GetBool("canonifyURLs") {
if relativeURLs || s.Info.canonifyURLs {
transformLinks = append(transformLinks, transform.AbsURL)
}
@ -1816,18 +1801,18 @@ func (s *Site) renderAndWritePage(name string, dest string, d interface{}, layou
var path []byte
if s.Cfg.GetBool("relativeURLs") {
if relativeURLs {
translated, err := pageTarget.(target.OptionalTranslator).TranslateRelative(dest)
if err != nil {
return err
}
path = []byte(helpers.GetDottedRelativePath(translated))
} else if s.Cfg.GetBool("canonifyURLs") {
s := s.Cfg.GetString("baseURL")
if !strings.HasSuffix(s, "/") {
s += "/"
} else if s.Info.canonifyURLs {
url := s.Cfg.GetString("baseURL")
if !strings.HasSuffix(url, "/") {
url += "/"
}
path = []byte(s)
path = []byte(url)
}
transformer := transform.NewChain(transformLinks...)