Add Disqus support out of the box. Move template/bundle into hugolib.

This commit is contained in:
spf13 2014-04-23 02:52:01 -04:00
parent 41adafbc3e
commit 4a8de8ea46
7 changed files with 57 additions and 39 deletions

View file

@ -28,7 +28,6 @@ import (
"github.com/spf13/cast" "github.com/spf13/cast"
"github.com/spf13/hugo/helpers" "github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/parser" "github.com/spf13/hugo/parser"
"github.com/spf13/hugo/template/bundle"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/theplant/blackfriday" "github.com/theplant/blackfriday"
@ -49,7 +48,7 @@ type Page struct {
contentType string contentType string
Draft bool Draft bool
Aliases []string Aliases []string
Tmpl bundle.Template Tmpl Template
Markup string Markup string
renderable bool renderable bool
layout string layout string
@ -519,7 +518,7 @@ func (page *Page) parse(reader io.Reader) error {
return nil return nil
} }
func (p *Page) ProcessShortcodes(t bundle.Template) { func (p *Page) ProcessShortcodes(t Template) {
p.rawContent = []byte(ShortcodesHandle(string(p.rawContent), p, t)) p.rawContent = []byte(ShortcodesHandle(string(p.rawContent), p, t))
p.Summary = template.HTML(ShortcodesHandle(string(p.Summary), p, t)) p.Summary = template.HTML(ShortcodesHandle(string(p.Summary), p, t))
} }

View file

@ -1,4 +1,4 @@
package bundle package hugolib
import ( import (
"testing" "testing"

View file

@ -20,7 +20,6 @@ import (
"strings" "strings"
"unicode" "unicode"
"github.com/spf13/hugo/template/bundle"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
) )
@ -78,7 +77,7 @@ func (scp *ShortcodeWithPage) Get(key interface{}) interface{} {
type Shortcodes map[string]ShortcodeFunc type Shortcodes map[string]ShortcodeFunc
func ShortcodesHandle(stringToParse string, p *Page, t bundle.Template) string { func ShortcodesHandle(stringToParse string, p *Page, t Template) string {
leadStart := strings.Index(stringToParse, `{{%`) leadStart := strings.Index(stringToParse, `{{%`)
if leadStart >= 0 { if leadStart >= 0 {
leadEnd := strings.Index(stringToParse[leadStart:], `%}}`) + leadStart leadEnd := strings.Index(stringToParse[leadStart:], `%}}`) + leadStart
@ -147,7 +146,7 @@ func FindEnd(str string, name string) (int, int) {
return startPos, endPos return startPos, endPos
} }
func GetTemplate(name string, t bundle.Template) *template.Template { func GetTemplate(name string, t Template) *template.Template {
if x := t.Lookup("shortcodes/" + name + ".html"); x != nil { if x := t.Lookup("shortcodes/" + name + ".html"); x != nil {
return x return x
} }

View file

@ -27,7 +27,6 @@ import (
"github.com/spf13/hugo/helpers" "github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/source" "github.com/spf13/hugo/source"
"github.com/spf13/hugo/target" "github.com/spf13/hugo/target"
"github.com/spf13/hugo/template/bundle"
"github.com/spf13/hugo/transform" "github.com/spf13/hugo/transform"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/nitro" "github.com/spf13/nitro"
@ -57,7 +56,7 @@ var DefaultTimer *nitro.B
// 5. The entire collection of files is written to disk. // 5. The entire collection of files is written to disk.
type Site struct { type Site struct {
Pages Pages Pages Pages
Tmpl bundle.Template Tmpl Template
Taxonomies TaxonomyList Taxonomies TaxonomyList
Source source.Input Source source.Input
Sections Taxonomy Sections Taxonomy
@ -72,19 +71,20 @@ type Site struct {
} }
type SiteInfo struct { type SiteInfo struct {
BaseUrl template.URL BaseUrl template.URL
Taxonomies TaxonomyList Taxonomies TaxonomyList
Indexes *TaxonomyList // legacy, should be identical to Taxonomies Indexes *TaxonomyList // legacy, should be identical to Taxonomies
Recent *Pages Recent *Pages
Title string Title string
Author string Author map[string]string
AuthorEmail string LanguageCode string
LanguageCode string DisqusShortname string
Copyright string Copyright string
LastChange time.Time LastChange time.Time
ConfigGet func(key string) interface{} ConfigGet func(key string) interface{}
Permalinks PermalinkOverrides Permalinks PermalinkOverrides
Params map[string]interface{} Params map[string]interface{}
}
} }
type runmode struct { type runmode struct {
@ -130,7 +130,7 @@ func (s *Site) Analyze() {
} }
func (s *Site) prepTemplates() { func (s *Site) prepTemplates() {
s.Tmpl = bundle.NewTemplate() s.Tmpl = NewTemplate()
s.Tmpl.LoadTemplates(s.absLayoutDir()) s.Tmpl.LoadTemplates(s.absLayoutDir())
if s.hasTheme() { if s.hasTheme() {
s.Tmpl.LoadTemplatesWithPrefix(s.absThemeDir()+"/layouts", "theme") s.Tmpl.LoadTemplatesWithPrefix(s.absThemeDir()+"/layouts", "theme")
@ -234,16 +234,16 @@ func (s *Site) initializeSiteInfo() {
permalinks = make(PermalinkOverrides) permalinks = make(PermalinkOverrides)
} }
s.Info = SiteInfo{ s.Info = &SiteInfo{
BaseUrl: template.URL(helpers.SanitizeUrl(viper.GetString("BaseUrl"))), BaseUrl: template.URL(helpers.SanitizeUrl(viper.GetString("BaseUrl"))),
Title: viper.GetString("Title"), Title: viper.GetString("Title"),
Author: viper.GetString("author"), Author: viper.GetStringMapString("author"),
AuthorEmail: viper.GetString("authoremail"), LanguageCode: viper.GetString("languagecode"),
LanguageCode: viper.GetString("languagecode"), Copyright: viper.GetString("copyright"),
Copyright: viper.GetString("copyright"), DisqusShortname: viper.GetString("DisqusShortname"),
Recent: &s.Pages, Recent: &s.Pages,
Params: params, Params: params,
Permalinks: permalinks, Permalinks: permalinks,
} }
} }

View file

@ -1,4 +1,4 @@
package bundle package hugolib
import ( import (
"errors" "errors"
@ -192,7 +192,11 @@ func (t *GoHtmlTemplate) LoadEmbedded() {
} }
func (t *GoHtmlTemplate) AddInternalTemplate(prefix, name, tpl string) error { func (t *GoHtmlTemplate) AddInternalTemplate(prefix, name, tpl string) error {
return t.AddTemplate("_internal/"+prefix+"/"+name, tpl) if prefix != "" {
return t.AddTemplate("_internal/"+prefix+"/"+name, tpl)
} else {
return t.AddTemplate("_internal/"+name, tpl)
}
} }
func (t *GoHtmlTemplate) AddInternalShortcode(name, content string) error { func (t *GoHtmlTemplate) AddInternalShortcode(name, content string) error {

View file

@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package bundle package hugolib
type Tmpl struct { type Tmpl struct {
Name string Name string
@ -49,7 +49,7 @@ func (t *GoHtmlTemplate) EmbedTemplates() {
<generator uri="https://hugo.spf13.com">Hugo</generator> <generator uri="https://hugo.spf13.com">Hugo</generator>
<link>{{ .Permalink }}</link> <link>{{ .Permalink }}</link>
{{ with .Site.LanguageCode }}<language>{{.}}</language>{{end}} {{ with .Site.LanguageCode }}<language>{{.}}</language>{{end}}
{{ with .Site.Author }}<author>{{.}}</author>{{end}} {{ with .Site.Author.name }}<author>{{.}}</author>{{end}}
{{ with .Site.Copyright }}<copyright>{{.}}</copyright>{{end}} {{ with .Site.Copyright }}<copyright>{{.}}</copyright>{{end}}
<updated>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 MST" }}</updated> <updated>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 MST" }}</updated>
{{ range first 15 .Data.Pages }} {{ range first 15 .Data.Pages }}
@ -57,7 +57,7 @@ func (t *GoHtmlTemplate) EmbedTemplates() {
<title>{{ .Title }}</title> <title>{{ .Title }}</title>
<link>{{ .Permalink }}</link> <link>{{ .Permalink }}</link>
<pubDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 MST" }}</pubDate> <pubDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 MST" }}</pubDate>
{{with .Site.Author}}<author>{{.}}</author>{{end}} {{with .Site.Author.name}}<author>{{.}}</author>{{end}}
<guid>{{ .Permalink }}</guid> <guid>{{ .Permalink }}</guid>
<description>{{ .Content | html }}</description> <description>{{ .Content | html }}</description>
</item> </item>
@ -65,4 +65,20 @@ func (t *GoHtmlTemplate) EmbedTemplates() {
</channel> </channel>
</rss>`) </rss>`)
t.AddInternalTemplate("", "disqus.html", `{{ if .Site.DisqusShortname }}<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = '{{ .Site.DisqusShortname }}';
var disqus_identifier = '{{with .GetParam "disqus_identifier" }}{{ . }}{{ else }}{{ .Permalink }}{{end}}';
var disqus_title = '{{with .GetParam "disqus_title" }}{{ . }}{{ else }}{{ .Title }}{{end}}';
var disqus_url = '{{with .GetParam "disqus_url" }}{{ . | html }}{{ else }}{{ .Permalink }}{{end}}';
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>{{end}}`)
} }

View file

@ -1,4 +1,4 @@
package bundle package hugolib
import ( import (
"reflect" "reflect"