page: make receiver name on Page methods consistent

Apply some Golint rules on Page, esp. making the method receiver names consistent:

(page *Page) ==> (p *Page)
This commit is contained in:
bep 2015-03-07 12:25:38 +01:00
parent 8539f78143
commit e24a775962

View file

@ -252,25 +252,25 @@ func (p *Page) IsRenderable() bool {
return p.renderable
}
func (page *Page) Type() string {
if page.contentType != "" {
return page.contentType
func (p *Page) Type() string {
if p.contentType != "" {
return p.contentType
}
if x := page.Section(); x != "" {
if x := p.Section(); x != "" {
return x
}
return "page"
}
func (page *Page) Section() string {
return page.Source.Section()
func (p *Page) Section() string {
return p.Source.Section()
}
func (page *Page) Layout(l ...string) []string {
if page.layout != "" {
return layouts(page.Type(), page.layout)
func (p *Page) Layout(l ...string) []string {
if p.layout != "" {
return layouts(p.Type(), p.layout)
}
layout := ""
@ -280,7 +280,7 @@ func (page *Page) Layout(l ...string) []string {
layout = l[0]
}
return layouts(page.Type(), layout)
return layouts(p.Type(), layout)
}
func layouts(types string, layout string) (layouts []string) {
@ -303,7 +303,7 @@ func layouts(types string, layout string) (layouts []string) {
return
}
func NewPageFrom(buf io.Reader, name string) (page *Page, err error) {
func NewPageFrom(buf io.Reader, name string) (*Page, error) {
p, err := NewPage(name)
if err != nil {
return p, err
@ -313,7 +313,7 @@ func NewPageFrom(buf io.Reader, name string) (page *Page, err error) {
return p, err
}
func NewPage(name string) (page *Page, err error) {
func NewPage(name string) (*Page, error) {
if len(name) == 0 {
return nil, errors.New("Zero length page name")
}
@ -374,34 +374,32 @@ func (p *Page) permalink() (*url.URL, error) {
func (p *Page) Extension() string {
if p.extension != "" {
return p.extension
} else {
return viper.GetString("DefaultExtension")
}
return viper.GetString("DefaultExtension")
}
func (p *Page) LinkTitle() string {
if len(p.linkTitle) > 0 {
return p.linkTitle
} else {
return p.Title
}
return p.Title
}
func (page *Page) ShouldBuild() bool {
if viper.GetBool("BuildFuture") || page.PublishDate.IsZero() || page.PublishDate.Before(time.Now()) {
if viper.GetBool("BuildDrafts") || !page.Draft {
func (p *Page) ShouldBuild() bool {
if viper.GetBool("BuildFuture") || p.PublishDate.IsZero() || p.PublishDate.Before(time.Now()) {
if viper.GetBool("BuildDrafts") || !p.Draft {
return true
}
}
return false
}
func (page *Page) IsDraft() bool {
return page.Draft
func (p *Page) IsDraft() bool {
return p.Draft
}
func (page *Page) IsFuture() bool {
if page.PublishDate.Before(time.Now()) {
func (p *Page) IsFuture() bool {
if p.PublishDate.Before(time.Now()) {
return false
}
return true
@ -438,7 +436,7 @@ func (p *Page) RelPermalink() (string, error) {
return link.String(), nil
}
func (page *Page) update(f interface{}) error {
func (p *Page) update(f interface{}) error {
if f == nil {
return fmt.Errorf("no metadata found")
}
@ -448,66 +446,66 @@ func (page *Page) update(f interface{}) error {
loki := strings.ToLower(k)
switch loki {
case "title":
page.Title = cast.ToString(v)
p.Title = cast.ToString(v)
case "linktitle":
page.linkTitle = cast.ToString(v)
p.linkTitle = cast.ToString(v)
case "description":
page.Description = cast.ToString(v)
p.Description = cast.ToString(v)
case "slug":
page.Slug = helpers.Urlize(cast.ToString(v))
p.Slug = helpers.Urlize(cast.ToString(v))
case "url":
if url := cast.ToString(v); strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
return fmt.Errorf("Only relative urls are supported, %v provided", url)
}
page.Url = helpers.Urlize(cast.ToString(v))
p.Url = helpers.Urlize(cast.ToString(v))
case "type":
page.contentType = cast.ToString(v)
p.contentType = cast.ToString(v)
case "extension", "ext":
page.extension = cast.ToString(v)
p.extension = cast.ToString(v)
case "keywords":
page.Keywords = cast.ToStringSlice(v)
p.Keywords = cast.ToStringSlice(v)
case "date":
page.Date, err = cast.ToTimeE(v)
p.Date, err = cast.ToTimeE(v)
if err != nil {
jww.ERROR.Printf("Failed to parse date '%v' in page %s", v, page.File.Path())
jww.ERROR.Printf("Failed to parse date '%v' in page %s", v, p.File.Path())
}
case "publishdate", "pubdate":
page.PublishDate, err = cast.ToTimeE(v)
p.PublishDate, err = cast.ToTimeE(v)
if err != nil {
jww.ERROR.Printf("Failed to parse publishdate '%v' in page %s", v, page.File.Path())
jww.ERROR.Printf("Failed to parse publishdate '%v' in page %s", v, p.File.Path())
}
case "draft":
page.Draft = cast.ToBool(v)
p.Draft = cast.ToBool(v)
case "layout":
page.layout = cast.ToString(v)
p.layout = cast.ToString(v)
case "markup":
page.Markup = cast.ToString(v)
p.Markup = cast.ToString(v)
case "weight":
page.Weight = cast.ToInt(v)
p.Weight = cast.ToInt(v)
case "aliases":
page.Aliases = cast.ToStringSlice(v)
for _, alias := range page.Aliases {
p.Aliases = cast.ToStringSlice(v)
for _, alias := range p.Aliases {
if strings.HasPrefix(alias, "http://") || strings.HasPrefix(alias, "https://") {
return fmt.Errorf("Only relative aliases are supported, %v provided", alias)
}
}
case "status":
page.Status = cast.ToString(v)
p.Status = cast.ToString(v)
case "sitemap":
page.Sitemap = parseSitemap(cast.ToStringMap(v))
p.Sitemap = parseSitemap(cast.ToStringMap(v))
default:
// If not one of the explicit values, store in Params
switch vv := v.(type) {
case bool:
page.Params[loki] = vv
p.Params[loki] = vv
case string:
page.Params[loki] = vv
p.Params[loki] = vv
case int64, int32, int16, int8, int:
page.Params[loki] = vv
p.Params[loki] = vv
case float64, float32:
page.Params[loki] = vv
p.Params[loki] = vv
case time.Time:
page.Params[loki] = vv
p.Params[loki] = vv
default: // handle array of strings as well
switch vvv := vv.(type) {
case []interface{}:
@ -515,9 +513,9 @@ func (page *Page) update(f interface{}) error {
for i, u := range vvv {
a[i] = cast.ToString(u)
}
page.Params[loki] = a
p.Params[loki] = a
default:
page.Params[loki] = vv
p.Params[loki] = vv
}
}
}
@ -526,8 +524,8 @@ func (page *Page) update(f interface{}) error {
}
func (page *Page) GetParam(key string) interface{} {
v := page.Params[strings.ToLower(key)]
func (p *Page) GetParam(key string) interface{} {
v := p.Params[strings.ToLower(key)]
if v == nil {
return nil
@ -556,8 +554,8 @@ func (page *Page) GetParam(key string) interface{} {
return nil
}
func (page *Page) HasMenuCurrent(menu string, me *MenuEntry) bool {
menus := page.Menus()
func (p *Page) HasMenuCurrent(menu string, me *MenuEntry) bool {
menus := p.Menus()
if m, ok := menus[menu]; ok {
if me.HasChildren() {
@ -573,8 +571,8 @@ func (page *Page) HasMenuCurrent(menu string, me *MenuEntry) bool {
}
func (page *Page) IsMenuCurrent(menu string, inme *MenuEntry) bool {
menus := page.Menus()
func (p *Page) IsMenuCurrent(menu string, inme *MenuEntry) bool {
menus := p.Menus()
if me, ok := menus[menu]; ok {
return me.IsEqual(inme)
@ -583,21 +581,21 @@ func (page *Page) IsMenuCurrent(menu string, inme *MenuEntry) bool {
return false
}
func (page *Page) Menus() PageMenus {
page.pageMenusInit.Do(func() {
page.pageMenus = PageMenus{}
func (p *Page) Menus() PageMenus {
p.pageMenusInit.Do(func() {
p.pageMenus = PageMenus{}
if ms, ok := page.Params["menu"]; ok {
link, _ := page.RelPermalink()
if ms, ok := p.Params["menu"]; ok {
link, _ := p.RelPermalink()
me := MenuEntry{Name: page.LinkTitle(), Weight: page.Weight, Url: link}
me := MenuEntry{Name: p.LinkTitle(), Weight: p.Weight, Url: link}
// Could be the name of the menu to attach it to
mname, err := cast.ToStringE(ms)
if err == nil {
me.Menu = mname
page.pageMenus[mname] = &me
p.pageMenus[mname] = &me
return
}
@ -607,7 +605,7 @@ func (page *Page) Menus() PageMenus {
if err == nil {
for _, mname := range mnames {
me.Menu = mname
page.pageMenus[mname] = &me
p.pageMenus[mname] = &me
return
}
}
@ -616,25 +614,25 @@ func (page *Page) Menus() PageMenus {
menus, err := cast.ToStringMapE(ms)
if err != nil {
jww.ERROR.Printf("unable to process menus for %q\n", page.Title)
jww.ERROR.Printf("unable to process menus for %q\n", p.Title)
}
for name, menu := range menus {
menuEntry := MenuEntry{Name: page.LinkTitle(), Url: link, Weight: page.Weight, Menu: name}
jww.DEBUG.Printf("found menu: %q, in %q\n", name, page.Title)
menuEntry := MenuEntry{Name: p.LinkTitle(), Url: link, Weight: p.Weight, Menu: name}
jww.DEBUG.Printf("found menu: %q, in %q\n", name, p.Title)
ime, err := cast.ToStringMapE(menu)
if err != nil {
jww.ERROR.Printf("unable to process menus for %q\n", page.Title)
jww.ERROR.Printf("unable to process menus for %q\n", p.Title)
}
menuEntry.MarshallMap(ime)
page.pageMenus[name] = &menuEntry
p.pageMenus[name] = &menuEntry
}
}
})
return page.pageMenus
return p.pageMenus
}
func (p *Page) Render(layout ...string) template.HTML {
@ -647,89 +645,89 @@ func (p *Page) Render(layout ...string) template.HTML {
return tpl.ExecuteTemplateToHTML(p, p.Layout(curLayout)...)
}
func (page *Page) guessMarkupType() string {
func (p *Page) guessMarkupType() string {
// First try the explicitly set markup from the frontmatter
if page.Markup != "" {
format := helpers.GuessType(page.Markup)
if p.Markup != "" {
format := helpers.GuessType(p.Markup)
if format != "unknown" {
return format
}
}
return helpers.GuessType(page.Source.Ext())
return helpers.GuessType(p.Source.Ext())
}
func (page *Page) detectFrontMatter() (f *parser.FrontmatterType) {
return parser.DetectFrontMatter(rune(page.frontmatter[0]))
func (p *Page) detectFrontMatter() (f *parser.FrontmatterType) {
return parser.DetectFrontMatter(rune(p.frontmatter[0]))
}
func (page *Page) parse(reader io.Reader) error {
func (p *Page) parse(reader io.Reader) error {
psr, err := parser.ReadFrom(reader)
if err != nil {
return err
}
page.renderable = psr.IsRenderable()
page.frontmatter = psr.FrontMatter()
p.renderable = psr.IsRenderable()
p.frontmatter = psr.FrontMatter()
meta, err := psr.Metadata()
if meta != nil {
if err != nil {
jww.ERROR.Printf("Error parsing page meta data for %s", page.File.Path())
jww.ERROR.Printf("Error parsing page meta data for %s", p.File.Path())
jww.ERROR.Println(err)
return err
}
if err = page.update(meta); err != nil {
if err = p.update(meta); err != nil {
return err
}
}
page.rawContent = psr.Content()
p.rawContent = psr.Content()
return nil
}
func (page *Page) SetSourceContent(content []byte) {
page.Source.Content = content
func (p *Page) SetSourceContent(content []byte) {
p.Source.Content = content
}
func (page *Page) SetSourceMetaData(in interface{}, mark rune) (err error) {
func (p *Page) SetSourceMetaData(in interface{}, mark rune) (err error) {
by, err := parser.InterfaceToFrontMatter(in, mark)
if err != nil {
return err
}
by = append(by, '\n')
page.Source.Frontmatter = by
p.Source.Frontmatter = by
return nil
}
func (page *Page) SafeSaveSourceAs(path string) error {
return page.saveSourceAs(path, true)
func (p *Page) SafeSaveSourceAs(path string) error {
return p.saveSourceAs(path, true)
}
func (page *Page) SaveSourceAs(path string) error {
return page.saveSourceAs(path, false)
func (p *Page) SaveSourceAs(path string) error {
return p.saveSourceAs(path, false)
}
func (page *Page) saveSourceAs(path string, safe bool) error {
func (p *Page) saveSourceAs(path string, safe bool) error {
b := bp.GetBuffer()
defer bp.PutBuffer(b)
b.Write(page.Source.Frontmatter)
b.Write(page.Source.Content)
b.Write(p.Source.Frontmatter)
b.Write(p.Source.Content)
bc := make([]byte, b.Len(), b.Len())
copy(bc, b.Bytes())
err := page.saveSource(bc, path, safe)
err := p.saveSource(bc, path, safe)
if err != nil {
return err
}
return nil
}
func (page *Page) saveSource(by []byte, inpath string, safe bool) (err error) {
func (p *Page) saveSource(by []byte, inpath string, safe bool) (err error) {
if !filepath.IsAbs(inpath) {
inpath = helpers.AbsPathify(inpath)
}
@ -746,8 +744,8 @@ func (page *Page) saveSource(by []byte, inpath string, safe bool) (err error) {
return nil
}
func (page *Page) SaveSource() error {
return page.SaveSourceAs(page.FullFilePath())
func (p *Page) SaveSource() error {
return p.SaveSourceAs(p.FullFilePath())
}
func (p *Page) ProcessShortcodes(t tpl.Template) {
@ -762,21 +760,21 @@ func (p *Page) ProcessShortcodes(t tpl.Template) {
// TODO(spf13): Remove this entirely
// Here for backwards compatibility & testing. Only works in isolation
func (page *Page) Convert() error {
func (p *Page) Convert() error {
var h Handler
if page.Markup != "" {
h = FindHandler(page.Markup)
if p.Markup != "" {
h = FindHandler(p.Markup)
} else {
h = FindHandler(page.File.Extension())
h = FindHandler(p.File.Extension())
}
if h != nil {
h.PageConvert(page, tpl.T())
h.PageConvert(p, tpl.T())
}
//// now we know enough to create a summary of the page and count some words
page.setSummary()
p.setSummary()
//analyze for raw stats
page.analyzePage()
p.analyzePage()
return nil
}