2015-05-05 21:37:29 -05:00
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
2015-05-08 17:45:31 -05:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2015-05-05 21:37:29 -05:00
|
|
|
"encoding/json"
|
2015-05-08 17:45:31 -05:00
|
|
|
"fmt"
|
2015-05-05 21:37:29 -05:00
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
parsers = []MetadataParser{
|
|
|
|
&JSONMetadataParser{},
|
|
|
|
&TOMLMetadataParser{},
|
|
|
|
&YAMLMetadataParser{},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Metadata stores a page's metadata
|
|
|
|
type Metadata struct {
|
2015-05-08 10:20:07 -05:00
|
|
|
// Page title
|
|
|
|
Title string
|
|
|
|
|
|
|
|
// Page template
|
|
|
|
Template string
|
|
|
|
|
|
|
|
// Variables to be used with Template
|
2015-05-05 21:37:29 -05:00
|
|
|
Variables map[string]interface{}
|
|
|
|
}
|
|
|
|
|
2015-05-08 10:20:07 -05:00
|
|
|
// load loads parsed values in parsedMap into Metadata
|
2015-05-05 21:37:29 -05:00
|
|
|
func (m *Metadata) load(parsedMap map[string]interface{}) {
|
2015-05-06 18:19:02 -05:00
|
|
|
if template, ok := parsedMap["title"]; ok {
|
|
|
|
m.Title, _ = template.(string)
|
|
|
|
}
|
2015-05-05 21:37:29 -05:00
|
|
|
if template, ok := parsedMap["template"]; ok {
|
|
|
|
m.Template, _ = template.(string)
|
|
|
|
}
|
|
|
|
if variables, ok := parsedMap["variables"]; ok {
|
|
|
|
m.Variables, _ = variables.(map[string]interface{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-08 10:20:07 -05:00
|
|
|
// MetadataParser is a an interface that must be satisfied by each parser
|
2015-05-05 21:37:29 -05:00
|
|
|
type MetadataParser interface {
|
2015-05-08 10:20:07 -05:00
|
|
|
// Opening identifier
|
2015-05-05 21:37:29 -05:00
|
|
|
Opening() []byte
|
2015-05-08 10:20:07 -05:00
|
|
|
|
|
|
|
// Closing identifier
|
2015-05-05 21:37:29 -05:00
|
|
|
Closing() []byte
|
2015-05-06 18:19:02 -05:00
|
|
|
|
2015-05-08 10:20:07 -05:00
|
|
|
// Parse the metadata
|
2015-05-05 21:37:29 -05:00
|
|
|
Parse([]byte) error
|
2015-05-08 10:20:07 -05:00
|
|
|
|
|
|
|
// Parsed metadata.
|
|
|
|
// Should be called after a call to Parse returns no error
|
2015-05-05 21:37:29 -05:00
|
|
|
Metadata() Metadata
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSONMetadataParser is the MetdataParser for JSON
|
|
|
|
type JSONMetadataParser struct {
|
|
|
|
metadata Metadata
|
|
|
|
}
|
|
|
|
|
2015-05-08 10:20:07 -05:00
|
|
|
// Parse the metadata
|
2015-05-05 21:37:29 -05:00
|
|
|
func (j *JSONMetadataParser) Parse(b []byte) error {
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal(b, &m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
j.metadata.load(m)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-08 10:20:07 -05:00
|
|
|
// Parsed metadata.
|
|
|
|
// Should be called after a call to Parse returns no error
|
2015-05-05 21:37:29 -05:00
|
|
|
func (j *JSONMetadataParser) Metadata() Metadata {
|
|
|
|
return j.metadata
|
|
|
|
}
|
|
|
|
|
|
|
|
// Opening returns the opening identifier JSON metadata
|
|
|
|
func (j *JSONMetadataParser) Opening() []byte {
|
2015-05-08 17:45:31 -05:00
|
|
|
return []byte(":::")
|
2015-05-05 21:37:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Closing returns the closing identifier JSON metadata
|
|
|
|
func (j *JSONMetadataParser) Closing() []byte {
|
2015-05-08 17:45:31 -05:00
|
|
|
return []byte(":::")
|
2015-05-05 21:37:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TOMLMetadataParser is the MetadataParser for TOML
|
|
|
|
type TOMLMetadataParser struct {
|
|
|
|
metadata Metadata
|
|
|
|
}
|
|
|
|
|
2015-05-08 10:20:07 -05:00
|
|
|
// Parse the metadata
|
2015-05-05 21:37:29 -05:00
|
|
|
func (t *TOMLMetadataParser) Parse(b []byte) error {
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
if err := toml.Unmarshal(b, &m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.metadata.load(m)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-08 10:20:07 -05:00
|
|
|
// Parsed metadata.
|
|
|
|
// Should be called after a call to Parse returns no error
|
2015-05-05 21:37:29 -05:00
|
|
|
func (t *TOMLMetadataParser) Metadata() Metadata {
|
|
|
|
return t.metadata
|
|
|
|
}
|
|
|
|
|
|
|
|
// Opening returns the opening identifier TOML metadata
|
|
|
|
func (t *TOMLMetadataParser) Opening() []byte {
|
|
|
|
return []byte("+++")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Closing returns the closing identifier TOML metadata
|
|
|
|
func (t *TOMLMetadataParser) Closing() []byte {
|
|
|
|
return []byte("+++")
|
|
|
|
}
|
|
|
|
|
2015-05-08 10:20:07 -05:00
|
|
|
// YAMLMetadataParser is the MetadataParser for YAML
|
2015-05-05 21:37:29 -05:00
|
|
|
type YAMLMetadataParser struct {
|
|
|
|
metadata Metadata
|
|
|
|
}
|
|
|
|
|
2015-05-08 10:20:07 -05:00
|
|
|
// Parse the metadata
|
2015-05-05 21:37:29 -05:00
|
|
|
func (y *YAMLMetadataParser) Parse(b []byte) error {
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
if err := yaml.Unmarshal(b, &m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
y.metadata.load(m)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-08 10:20:07 -05:00
|
|
|
// Parsed metadata.
|
|
|
|
// Should be called after a call to Parse returns no error
|
2015-05-05 21:37:29 -05:00
|
|
|
func (y *YAMLMetadataParser) Metadata() Metadata {
|
|
|
|
return y.metadata
|
|
|
|
}
|
|
|
|
|
2015-05-06 18:19:02 -05:00
|
|
|
// Opening returns the opening identifier YAML metadata
|
2015-05-05 21:37:29 -05:00
|
|
|
func (y *YAMLMetadataParser) Opening() []byte {
|
|
|
|
return []byte("---")
|
|
|
|
}
|
|
|
|
|
2015-05-06 18:19:02 -05:00
|
|
|
// Closing returns the closing identifier YAML metadata
|
2015-05-05 21:37:29 -05:00
|
|
|
func (y *YAMLMetadataParser) Closing() []byte {
|
|
|
|
return []byte("---")
|
|
|
|
}
|
2015-05-08 17:45:31 -05:00
|
|
|
|
|
|
|
// extractMetadata extracts metadata content from a page.
|
|
|
|
// it returns the metadata, the remaining bytes (markdown),
|
|
|
|
// and an error if any
|
|
|
|
func extractMetadata(b []byte) (metadata Metadata, markdown []byte, err error) {
|
|
|
|
b = bytes.TrimSpace(b)
|
|
|
|
reader := bytes.NewBuffer(b)
|
|
|
|
scanner := bufio.NewScanner(reader)
|
|
|
|
var parser MetadataParser
|
|
|
|
|
|
|
|
// Read first line
|
2015-05-08 18:54:39 -05:00
|
|
|
if !scanner.Scan() {
|
|
|
|
// if no line is read,
|
2015-05-08 17:45:31 -05:00
|
|
|
// assume metadata not present
|
2015-05-08 18:54:39 -05:00
|
|
|
return metadata, b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
line := scanner.Bytes()
|
|
|
|
parser = findParser(line)
|
|
|
|
// if no parser found,
|
|
|
|
// assume metadata not present
|
|
|
|
if parser == nil {
|
|
|
|
return metadata, b, nil
|
2015-05-08 17:45:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// buffer for metadata contents
|
|
|
|
buf := bytes.Buffer{}
|
|
|
|
|
|
|
|
// Read remaining lines until closing identifier is found
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Bytes()
|
|
|
|
|
|
|
|
// if closing identifier found
|
|
|
|
if bytes.Equal(bytes.TrimSpace(line), parser.Closing()) {
|
|
|
|
// parse the metadata
|
|
|
|
err := parser.Parse(buf.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return metadata, nil, err
|
|
|
|
}
|
|
|
|
// get the scanner to return remaining bytes
|
|
|
|
scanner.Split(func(data []byte, atEOF bool) (int, []byte, error) {
|
|
|
|
return len(data), data, nil
|
|
|
|
})
|
|
|
|
// scan the remaining bytes
|
|
|
|
scanner.Scan()
|
|
|
|
|
|
|
|
return parser.Metadata(), scanner.Bytes(), nil
|
|
|
|
}
|
|
|
|
buf.Write(line)
|
|
|
|
buf.WriteString("\r\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
// closing identifier not found
|
|
|
|
return metadata, nil, fmt.Errorf("Metadata not closed. '%v' not found", string(parser.Closing()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// findParser finds the parser using line that contains opening identifier
|
|
|
|
func findParser(line []byte) MetadataParser {
|
|
|
|
line = bytes.TrimSpace(line)
|
|
|
|
for _, parser := range parsers {
|
|
|
|
if bytes.Equal(parser.Opening(), line) {
|
|
|
|
return parser
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|