2013-07-04 11:32:55 -04:00
|
|
|
// Copyright © 2013 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// Licensed under the Simple Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://opensource.org/licenses/Simple-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
2013-08-05 10:53:58 -04:00
|
|
|
"bufio"
|
2013-07-04 11:32:55 -04:00
|
|
|
"bytes"
|
2013-07-06 23:52:14 -04:00
|
|
|
"encoding/json"
|
2013-08-05 10:53:58 -04:00
|
|
|
"errors"
|
2013-07-04 11:32:55 -04:00
|
|
|
"fmt"
|
2013-07-08 22:23:54 -04:00
|
|
|
"github.com/BurntSushi/toml"
|
2013-07-04 11:32:55 -04:00
|
|
|
"github.com/theplant/blackfriday"
|
|
|
|
"html/template"
|
2013-08-05 10:53:58 -04:00
|
|
|
"io"
|
2013-07-04 11:32:55 -04:00
|
|
|
"io/ioutil"
|
2013-07-08 17:57:01 -04:00
|
|
|
"launchpad.net/goyaml"
|
2013-07-04 11:32:55 -04:00
|
|
|
"os"
|
2013-07-06 22:31:43 -04:00
|
|
|
"os/exec"
|
2013-07-04 11:32:55 -04:00
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2013-08-05 10:53:58 -04:00
|
|
|
"unicode"
|
2013-07-04 11:32:55 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = filepath.Base("")
|
|
|
|
|
|
|
|
type Page struct {
|
2013-07-08 17:57:01 -04:00
|
|
|
Status string
|
|
|
|
Images []string
|
|
|
|
Content template.HTML
|
|
|
|
Summary template.HTML
|
|
|
|
RawMarkdown string // TODO should be []byte
|
|
|
|
Params map[string]interface{}
|
2013-07-04 11:32:55 -04:00
|
|
|
RenderedContent *bytes.Buffer
|
2013-07-08 17:57:01 -04:00
|
|
|
contentType string
|
|
|
|
Draft bool
|
2013-08-10 09:08:38 -04:00
|
|
|
Aliases []string
|
2013-07-08 17:57:01 -04:00
|
|
|
Tmpl *template.Template
|
|
|
|
Markup string
|
2013-07-04 11:32:55 -04:00
|
|
|
PageMeta
|
|
|
|
File
|
|
|
|
Position
|
|
|
|
Node
|
|
|
|
}
|
|
|
|
|
|
|
|
const summaryLength = 70
|
|
|
|
|
|
|
|
type File struct {
|
|
|
|
FileName, OutFile, Extension string
|
|
|
|
}
|
|
|
|
|
|
|
|
type PageMeta struct {
|
|
|
|
WordCount int
|
|
|
|
FuzzyWordCount int
|
|
|
|
}
|
|
|
|
|
|
|
|
type Position struct {
|
|
|
|
Prev *Page
|
|
|
|
Next *Page
|
|
|
|
}
|
|
|
|
|
|
|
|
type Pages []*Page
|
|
|
|
|
2013-07-08 17:57:01 -04:00
|
|
|
func (p Pages) Len() int { return len(p) }
|
2013-07-04 11:32:55 -04:00
|
|
|
func (p Pages) Less(i, j int) bool { return p[i].Date.Unix() > p[j].Date.Unix() }
|
2013-07-08 17:57:01 -04:00
|
|
|
func (p Pages) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
2013-07-04 11:32:55 -04:00
|
|
|
|
|
|
|
// TODO eliminate unnecessary things
|
2013-07-08 17:57:01 -04:00
|
|
|
func (p Pages) Sort() { sort.Sort(p) }
|
2013-07-04 11:32:55 -04:00
|
|
|
func (p Pages) Limit(n int) Pages { return p[0:n] }
|
|
|
|
|
|
|
|
func initializePage(filename string) (page Page) {
|
2013-08-03 13:52:40 -04:00
|
|
|
page = Page{contentType: "",
|
2013-08-04 22:02:15 -04:00
|
|
|
File: File{FileName: filename, Extension: "html"},
|
|
|
|
Node: Node{Keywords: make([]string, 10, 30)},
|
2013-08-03 13:52:40 -04:00
|
|
|
Params: make(map[string]interface{}),
|
|
|
|
Markup: "md"}
|
2013-07-04 11:32:55 -04:00
|
|
|
page.Date, _ = time.Parse("20060102", "20080101")
|
|
|
|
page.setSection()
|
|
|
|
|
|
|
|
return page
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) setSection() {
|
2013-08-01 16:27:07 -04:00
|
|
|
x := strings.Split(p.FileName, string(os.PathSeparator))
|
2013-08-03 13:51:21 -04:00
|
|
|
if len(x) <= 1 {
|
|
|
|
return
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
|
|
|
|
if section := x[len(x)-2]; section != "content" {
|
|
|
|
p.Section = section
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (page *Page) Type() string {
|
|
|
|
if page.contentType != "" {
|
|
|
|
return page.contentType
|
|
|
|
}
|
2013-08-09 16:35:23 -04:00
|
|
|
page.setSection()
|
2013-07-04 11:32:55 -04:00
|
|
|
if x := page.GetSection(); x != "" {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
return "page"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (page *Page) Layout(l ...string) string {
|
|
|
|
layout := ""
|
|
|
|
if len(l) == 0 {
|
|
|
|
layout = "single"
|
|
|
|
} else {
|
|
|
|
layout = l[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if x := page.layout; x != "" {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.ToLower(page.Type()) + "/" + layout + ".html"
|
|
|
|
}
|
|
|
|
|
2013-08-05 10:53:58 -04:00
|
|
|
func ReadFrom(buf io.Reader, name string) (page *Page, err error) {
|
|
|
|
if len(name) == 0 {
|
|
|
|
return nil, errors.New("Zero length page name")
|
|
|
|
}
|
|
|
|
|
|
|
|
p := initializePage(name)
|
|
|
|
|
|
|
|
if err = p.parse(buf); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.analyzePage()
|
|
|
|
|
|
|
|
return &p, nil
|
|
|
|
}
|
|
|
|
|
2013-07-04 11:32:55 -04:00
|
|
|
// TODO should return errors as well
|
|
|
|
// TODO new page should return just a page
|
|
|
|
// TODO initalize separately... load from reader (file, or []byte)
|
|
|
|
func NewPage(filename string) *Page {
|
|
|
|
p := initializePage(filename)
|
2013-08-09 16:35:23 -04:00
|
|
|
|
2013-07-04 11:32:55 -04:00
|
|
|
if err := p.buildPageFromFile(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.analyzePage()
|
|
|
|
|
|
|
|
return &p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) analyzePage() {
|
|
|
|
p.WordCount = TotalWords(p.RawMarkdown)
|
|
|
|
p.FuzzyWordCount = int((p.WordCount+100)/100) * 100
|
|
|
|
}
|
|
|
|
|
2013-07-07 10:01:53 -04:00
|
|
|
func splitPageContent(data []byte, start string, end string) ([]string, []string) {
|
2013-07-06 23:52:14 -04:00
|
|
|
lines := strings.Split(string(data), "\n")
|
|
|
|
datum := lines[0:]
|
|
|
|
|
|
|
|
var found = 0
|
2013-07-08 17:57:01 -04:00
|
|
|
if start != end {
|
|
|
|
for i, line := range lines {
|
|
|
|
|
|
|
|
if strings.HasPrefix(line, start) {
|
|
|
|
found += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(line, end) {
|
|
|
|
found -= 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if found == 0 {
|
2013-07-09 19:04:22 -04:00
|
|
|
datum = lines[0 : i+1]
|
2013-07-08 17:57:01 -04:00
|
|
|
lines = lines[i+1:]
|
|
|
|
break
|
|
|
|
}
|
2013-07-06 23:52:14 -04:00
|
|
|
}
|
|
|
|
}
|
2013-07-07 10:01:53 -04:00
|
|
|
return datum, lines
|
2013-07-06 23:52:14 -04:00
|
|
|
}
|
|
|
|
|
2013-07-04 11:32:55 -04:00
|
|
|
func (p *Page) Permalink() template.HTML {
|
|
|
|
if len(strings.TrimSpace(p.Slug)) > 0 {
|
2013-07-26 09:51:07 -04:00
|
|
|
if p.Site.Config.UglyUrls {
|
|
|
|
return template.HTML(MakePermalink(string(p.Site.BaseUrl), strings.TrimSpace(p.Section)+"/"+p.Slug+"."+p.Extension))
|
|
|
|
} else {
|
|
|
|
return template.HTML(MakePermalink(string(p.Site.BaseUrl), strings.TrimSpace(p.Section)+"/"+p.Slug))
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
} else if len(strings.TrimSpace(p.Url)) > 2 {
|
|
|
|
return template.HTML(MakePermalink(string(p.Site.BaseUrl), strings.TrimSpace(p.Url)))
|
|
|
|
} else {
|
|
|
|
_, t := filepath.Split(p.FileName)
|
2013-07-26 09:51:07 -04:00
|
|
|
if p.Site.Config.UglyUrls {
|
|
|
|
x := replaceExtension(strings.TrimSpace(t), p.Extension)
|
|
|
|
return template.HTML(MakePermalink(string(p.Site.BaseUrl), strings.TrimSpace(p.Section)+"/"+x))
|
|
|
|
} else {
|
|
|
|
file, _ := fileExt(strings.TrimSpace(t))
|
|
|
|
return template.HTML(MakePermalink(string(p.Site.BaseUrl), strings.TrimSpace(p.Section)+"/"+file))
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-09 18:53:08 -04:00
|
|
|
func (page *Page) handleTomlMetaData(datum []byte) (interface{}, error) {
|
2013-07-08 22:23:54 -04:00
|
|
|
m := map[string]interface{}{}
|
|
|
|
if _, err := toml.Decode(string(datum), &m); err != nil {
|
2013-07-09 18:53:08 -04:00
|
|
|
return m, fmt.Errorf("Invalid TOML in %s \nError parsing page meta data: %s", page.FileName, err)
|
2013-07-08 22:23:54 -04:00
|
|
|
}
|
2013-07-09 18:53:08 -04:00
|
|
|
return m, nil
|
2013-07-08 22:23:54 -04:00
|
|
|
}
|
|
|
|
|
2013-07-09 18:53:08 -04:00
|
|
|
func (page *Page) handleYamlMetaData(datum []byte) (interface{}, error) {
|
2013-07-06 22:58:08 -04:00
|
|
|
m := map[string]interface{}{}
|
|
|
|
if err := goyaml.Unmarshal(datum, &m); err != nil {
|
2013-07-09 18:53:08 -04:00
|
|
|
return m, fmt.Errorf("Invalid YAML in %s \nError parsing page meta data: %s", page.FileName, err)
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
2013-07-09 18:53:08 -04:00
|
|
|
return m, nil
|
2013-07-07 00:49:57 -04:00
|
|
|
}
|
|
|
|
|
2013-07-19 03:10:42 -04:00
|
|
|
func (page *Page) handleJsonMetaData(datum []byte) (interface{}, error) {
|
2013-07-07 00:49:57 -04:00
|
|
|
var f interface{}
|
|
|
|
if err := json.Unmarshal(datum, &f); err != nil {
|
2013-07-09 18:53:08 -04:00
|
|
|
return f, fmt.Errorf("Invalid JSON in %v \nError parsing page meta data: %s", page.FileName, err)
|
2013-07-07 00:49:57 -04:00
|
|
|
}
|
2013-07-09 18:53:08 -04:00
|
|
|
return f, nil
|
2013-07-07 00:49:57 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 10:53:58 -04:00
|
|
|
func (page *Page) update(f interface{}) error {
|
2013-07-07 00:49:57 -04:00
|
|
|
m := f.(map[string]interface{})
|
2013-07-04 11:32:55 -04:00
|
|
|
|
|
|
|
for k, v := range m {
|
|
|
|
switch strings.ToLower(k) {
|
|
|
|
case "title":
|
|
|
|
page.Title = interfaceToString(v)
|
|
|
|
case "description":
|
|
|
|
page.Description = interfaceToString(v)
|
|
|
|
case "slug":
|
|
|
|
page.Slug = Urlize(interfaceToString(v))
|
|
|
|
case "url":
|
|
|
|
if url := interfaceToString(v); strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
|
|
|
|
return fmt.Errorf("Only relative urls are supported, %v provided", url)
|
|
|
|
}
|
|
|
|
page.Url = Urlize(interfaceToString(v))
|
|
|
|
case "type":
|
|
|
|
page.contentType = interfaceToString(v)
|
|
|
|
case "keywords":
|
|
|
|
page.Keywords = interfaceArrayToStringArray(v)
|
|
|
|
case "date", "pubdate":
|
|
|
|
page.Date = interfaceToStringToDate(v)
|
|
|
|
case "draft":
|
|
|
|
page.Draft = interfaceToBool(v)
|
|
|
|
case "layout":
|
|
|
|
page.layout = interfaceToString(v)
|
2013-07-06 22:31:43 -04:00
|
|
|
case "markup":
|
|
|
|
page.Markup = interfaceToString(v)
|
2013-08-10 09:08:38 -04:00
|
|
|
case "aliases":
|
|
|
|
page.Aliases = interfaceArrayToStringArray(v)
|
|
|
|
for _, alias := range page.Aliases {
|
|
|
|
if strings.HasPrefix(alias, "http://") || strings.HasPrefix(alias, "https://") {
|
|
|
|
return fmt.Errorf("Only relative aliases are supported, %v provided", alias)
|
|
|
|
}
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
case "status":
|
|
|
|
page.Status = interfaceToString(v)
|
|
|
|
default:
|
|
|
|
// If not one of the explicit values, store in Params
|
|
|
|
switch vv := v.(type) {
|
|
|
|
case string: // handle string values
|
|
|
|
page.Params[strings.ToLower(k)] = vv
|
|
|
|
default: // handle array of strings as well
|
|
|
|
switch vvv := vv.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
var a = make([]string, len(vvv))
|
|
|
|
for i, u := range vvv {
|
|
|
|
a[i] = interfaceToString(u)
|
|
|
|
}
|
|
|
|
page.Params[strings.ToLower(k)] = a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2013-07-07 00:49:57 -04:00
|
|
|
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (page *Page) GetParam(key string) interface{} {
|
|
|
|
v := page.Params[strings.ToLower(key)]
|
|
|
|
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v.(type) {
|
|
|
|
case string:
|
|
|
|
return interfaceToString(v)
|
|
|
|
case []string:
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-07 02:04:49 -04:00
|
|
|
var ErrDetectingFrontMatter = errors.New("unable to detect front matter")
|
|
|
|
var ErrMatchingStartingFrontMatterDelimiter = errors.New("unable to match beginning front matter delimiter")
|
|
|
|
var ErrMatchingEndingFrontMatterDelimiter = errors.New("unable to match ending front matter delimiter")
|
|
|
|
|
2013-08-05 10:53:58 -04:00
|
|
|
func (page *Page) parseFrontMatter(data *bufio.Reader) (err error) {
|
|
|
|
|
|
|
|
if err = checkEmpty(data); err != nil {
|
2013-08-07 02:04:49 -04:00
|
|
|
return
|
2013-08-05 10:53:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var mark rune
|
|
|
|
if mark, err = chompWhitespace(data); err != nil {
|
2013-08-07 02:04:49 -04:00
|
|
|
return
|
2013-08-05 10:53:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
f := page.detectFrontMatter(mark)
|
|
|
|
if f == nil {
|
2013-08-07 02:04:49 -04:00
|
|
|
return ErrDetectingFrontMatter
|
2013-08-05 10:53:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if found, err := beginFrontMatter(data, f); err != nil || !found {
|
2013-08-07 02:04:49 -04:00
|
|
|
return ErrMatchingStartingFrontMatterDelimiter
|
2013-08-05 10:53:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var frontmatter = new(bytes.Buffer)
|
|
|
|
for {
|
|
|
|
line, _, err := data.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
2013-08-07 02:04:49 -04:00
|
|
|
return ErrMatchingEndingFrontMatterDelimiter
|
2013-08-05 10:53:58 -04:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2013-08-07 02:04:49 -04:00
|
|
|
|
2013-08-05 10:53:58 -04:00
|
|
|
if bytes.Equal(line, f.markend) {
|
2013-08-07 02:04:49 -04:00
|
|
|
if f.includeMark {
|
|
|
|
frontmatter.Write(line)
|
|
|
|
}
|
2013-08-05 10:53:58 -04:00
|
|
|
break
|
|
|
|
}
|
2013-08-07 02:04:49 -04:00
|
|
|
|
2013-08-05 10:53:58 -04:00
|
|
|
frontmatter.Write(line)
|
|
|
|
frontmatter.Write([]byte{'\n'})
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata, err := f.parse(frontmatter.Bytes())
|
|
|
|
if err != nil {
|
2013-08-07 02:04:49 -04:00
|
|
|
return
|
2013-08-05 10:53:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = page.update(metadata); err != nil {
|
2013-08-07 02:04:49 -04:00
|
|
|
return
|
2013-08-05 10:53:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 10:53:58 -04:00
|
|
|
func checkEmpty(data *bufio.Reader) (err error) {
|
|
|
|
if _, _, err = data.ReadRune(); err != nil {
|
|
|
|
return errors.New("unable to locate front matter")
|
|
|
|
}
|
|
|
|
if err = data.UnreadRune(); err != nil {
|
|
|
|
return errors.New("unable to unread first charactor in page buffer.")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type frontmatterType struct {
|
|
|
|
markstart, markend []byte
|
|
|
|
parse func([]byte) (interface{}, error)
|
2013-08-07 02:04:49 -04:00
|
|
|
includeMark bool
|
2013-08-05 10:53:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (page *Page) detectFrontMatter(mark rune) (f *frontmatterType) {
|
|
|
|
switch mark {
|
|
|
|
case '-':
|
2013-08-07 02:04:49 -04:00
|
|
|
return &frontmatterType{[]byte{'-', '-', '-'}, []byte{'-', '-', '-'}, page.handleYamlMetaData, false}
|
2013-08-05 10:53:58 -04:00
|
|
|
case '+':
|
2013-08-07 02:04:49 -04:00
|
|
|
return &frontmatterType{[]byte{'+', '+', '+'}, []byte{'+', '+', '+'}, page.handleTomlMetaData, false}
|
2013-08-05 10:53:58 -04:00
|
|
|
case '{':
|
2013-08-07 02:04:49 -04:00
|
|
|
return &frontmatterType{[]byte{'{'}, []byte{'}'}, page.handleJsonMetaData, true}
|
2013-08-05 10:53:58 -04:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func beginFrontMatter(data *bufio.Reader, f *frontmatterType) (bool, error) {
|
2013-08-07 02:04:49 -04:00
|
|
|
var err error
|
|
|
|
var peek []byte
|
|
|
|
if f.includeMark {
|
|
|
|
peek, err = data.Peek(len(f.markstart))
|
|
|
|
} else {
|
|
|
|
peek = make([]byte, len(f.markstart))
|
|
|
|
_, err = data.Read(peek)
|
|
|
|
}
|
2013-08-05 10:53:58 -04:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return bytes.Equal(peek, f.markstart), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func chompWhitespace(data *bufio.Reader) (r rune, err error) {
|
|
|
|
for {
|
|
|
|
r, _, err = data.ReadRune()
|
|
|
|
if err != nil {
|
|
|
|
return
|
2013-07-06 23:52:14 -04:00
|
|
|
}
|
2013-08-05 10:53:58 -04:00
|
|
|
if unicode.IsSpace(r) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := data.UnreadRune(); err != nil {
|
|
|
|
return r, errors.New("unable to unread first charactor in front matter.")
|
|
|
|
}
|
|
|
|
return r, nil
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) Render(layout ...string) template.HTML {
|
|
|
|
curLayout := ""
|
|
|
|
|
|
|
|
if len(layout) > 0 {
|
|
|
|
curLayout = layout[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return template.HTML(string(p.ExecuteTemplate(curLayout).Bytes()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) ExecuteTemplate(layout string) *bytes.Buffer {
|
|
|
|
l := p.Layout(layout)
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
p.Tmpl.ExecuteTemplate(buffer, l, p)
|
|
|
|
return buffer
|
|
|
|
}
|
|
|
|
|
2013-08-05 10:53:58 -04:00
|
|
|
func (page *Page) readFile() (data []byte, err error) {
|
|
|
|
data, err = ioutil.ReadFile(page.FileName)
|
2013-07-04 11:32:55 -04:00
|
|
|
if err != nil {
|
2013-08-05 10:53:58 -04:00
|
|
|
return nil, err
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
2013-08-05 10:53:58 -04:00
|
|
|
return data, nil
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (page *Page) buildPageFromFile() error {
|
2013-08-05 10:53:58 -04:00
|
|
|
f, err := os.Open(page.FileName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return page.parse(bufio.NewReader(f))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (page *Page) parse(reader io.Reader) error {
|
|
|
|
data := bufio.NewReader(reader)
|
2013-07-04 11:32:55 -04:00
|
|
|
|
2013-08-05 10:53:58 -04:00
|
|
|
err := page.parseFrontMatter(data)
|
2013-07-04 11:32:55 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-07-06 22:31:43 -04:00
|
|
|
switch page.Markup {
|
|
|
|
case "md":
|
2013-08-05 10:53:58 -04:00
|
|
|
page.convertMarkdown(data)
|
2013-07-06 22:31:43 -04:00
|
|
|
case "rst":
|
2013-08-05 10:53:58 -04:00
|
|
|
page.convertRestructuredText(data)
|
2013-07-06 22:31:43 -04:00
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-05 10:53:58 -04:00
|
|
|
func (page *Page) convertMarkdown(lines io.Reader) {
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
b.ReadFrom(lines)
|
|
|
|
content := string(blackfriday.MarkdownCommon(b.Bytes()))
|
2013-07-04 11:32:55 -04:00
|
|
|
page.Content = template.HTML(content)
|
|
|
|
page.Summary = template.HTML(TruncateWordsToWholeSentence(StripHTML(StripShortcodes(content)), summaryLength))
|
|
|
|
}
|
2013-07-06 22:31:43 -04:00
|
|
|
|
2013-08-05 10:53:58 -04:00
|
|
|
func (page *Page) convertRestructuredText(lines io.Reader) {
|
2013-07-09 08:16:29 -04:00
|
|
|
cmd := exec.Command("rst2html.py")
|
2013-08-05 10:53:58 -04:00
|
|
|
cmd.Stdin = lines
|
2013-07-06 22:31:43 -04:00
|
|
|
var out bytes.Buffer
|
|
|
|
cmd.Stdout = &out
|
|
|
|
if err := cmd.Run(); err != nil {
|
2013-07-07 00:49:57 -04:00
|
|
|
fmt.Println(err)
|
2013-07-06 22:31:43 -04:00
|
|
|
}
|
|
|
|
|
2013-07-09 08:16:29 -04:00
|
|
|
rstLines := strings.Split(out.String(), "\n")
|
|
|
|
for i, line := range rstLines {
|
|
|
|
if strings.HasPrefix(line, "<body>") {
|
2013-07-19 03:10:42 -04:00
|
|
|
rstLines = (rstLines[i+1 : len(rstLines)-3])
|
2013-07-09 08:16:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
content := strings.Join(rstLines, "\n")
|
2013-07-06 22:31:43 -04:00
|
|
|
page.Content = template.HTML(content)
|
|
|
|
page.Summary = template.HTML(TruncateWordsToWholeSentence(StripHTML(StripShortcodes(content)), summaryLength))
|
|
|
|
}
|