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-07-18 13:57:16 -05:00
|
|
|
"io"
|
2015-05-05 21:37:29 -05:00
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
parsers = []MetadataParser{
|
2015-05-09 12:49:28 -05:00
|
|
|
&JSONMetadataParser{metadata: Metadata{Variables: make(map[string]interface{})}},
|
|
|
|
&TOMLMetadataParser{metadata: Metadata{Variables: make(map[string]interface{})}},
|
|
|
|
&YAMLMetadataParser{metadata: Metadata{Variables: make(map[string]interface{})}},
|
2015-05-05 21:37:29 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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-09 05:49:54 -05:00
|
|
|
// Parse the metadata.
|
|
|
|
// Returns the remaining page contents (Markdown)
|
|
|
|
// after extracting metadata
|
|
|
|
Parse([]byte) ([]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-09 05:49:54 -05:00
|
|
|
func (j *JSONMetadataParser) Parse(b []byte) ([]byte, error) {
|
2015-05-05 21:37:29 -05:00
|
|
|
m := make(map[string]interface{})
|
2015-05-09 05:49:54 -05:00
|
|
|
|
|
|
|
// Read the preceding JSON object
|
|
|
|
decoder := json.NewDecoder(bytes.NewReader(b))
|
|
|
|
if err := decoder.Decode(&m); err != nil {
|
|
|
|
return b, err
|
2015-05-05 21:37:29 -05:00
|
|
|
}
|
2015-05-09 05:49:54 -05:00
|
|
|
|
2015-05-05 21:37:29 -05:00
|
|
|
j.metadata.load(m)
|
2015-05-09 05:49:54 -05:00
|
|
|
|
|
|
|
// Retrieve remaining bytes after decoding
|
|
|
|
buf := make([]byte, len(b))
|
|
|
|
n, err := decoder.Buffered().Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf[:n], nil
|
2015-05-05 21:37:29 -05:00
|
|
|
}
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
// Metadata returns parsed metadata. It should be called
|
|
|
|
// only after a call to Parse returns without 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-09 05:49:54 -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-09 05:49:54 -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-09 05:49:54 -05:00
|
|
|
func (t *TOMLMetadataParser) Parse(b []byte) ([]byte, error) {
|
|
|
|
b, markdown, err := extractMetadata(t, b)
|
|
|
|
if err != nil {
|
|
|
|
return markdown, err
|
|
|
|
}
|
2015-05-05 21:37:29 -05:00
|
|
|
m := make(map[string]interface{})
|
|
|
|
if err := toml.Unmarshal(b, &m); err != nil {
|
2015-05-09 05:49:54 -05:00
|
|
|
return markdown, err
|
2015-05-05 21:37:29 -05:00
|
|
|
}
|
|
|
|
t.metadata.load(m)
|
2015-05-09 05:49:54 -05:00
|
|
|
return markdown, nil
|
2015-05-05 21:37:29 -05:00
|
|
|
}
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
// Metadata returns parsed metadata. It should be called
|
|
|
|
// only after a call to Parse returns without 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-09 05:49:54 -05:00
|
|
|
func (y *YAMLMetadataParser) Parse(b []byte) ([]byte, error) {
|
|
|
|
b, markdown, err := extractMetadata(y, b)
|
|
|
|
if err != nil {
|
|
|
|
return markdown, err
|
|
|
|
}
|
2015-07-18 13:57:16 -05:00
|
|
|
|
2015-05-05 21:37:29 -05:00
|
|
|
m := make(map[string]interface{})
|
|
|
|
if err := yaml.Unmarshal(b, &m); err != nil {
|
2015-05-09 05:49:54 -05:00
|
|
|
return markdown, err
|
2015-05-05 21:37:29 -05:00
|
|
|
}
|
2015-05-12 18:44:35 -05:00
|
|
|
|
|
|
|
// convert variables (if present) to map[string]interface{}
|
|
|
|
// to match expected type
|
|
|
|
if vars, ok := m["variables"].(map[interface{}]interface{}); ok {
|
|
|
|
vars1 := make(map[string]interface{})
|
|
|
|
for k, v := range vars {
|
|
|
|
if key, ok := k.(string); ok {
|
|
|
|
vars1[key] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m["variables"] = vars1
|
|
|
|
}
|
|
|
|
|
2015-05-05 21:37:29 -05:00
|
|
|
y.metadata.load(m)
|
2015-05-09 05:49:54 -05:00
|
|
|
return markdown, nil
|
2015-05-05 21:37:29 -05:00
|
|
|
}
|
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
// Metadata returns parsed metadata. It should be called
|
|
|
|
// only after a call to Parse returns without 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
|
|
|
|
2015-07-18 13:57:16 -05:00
|
|
|
// extractMetadata separates metadata content from from markdown content in b.
|
|
|
|
// It returns the metadata, the remaining bytes (markdown), and an error, if any.
|
2015-05-09 05:49:54 -05:00
|
|
|
func extractMetadata(parser MetadataParser, b []byte) (metadata []byte, markdown []byte, err error) {
|
2015-05-08 17:45:31 -05:00
|
|
|
b = bytes.TrimSpace(b)
|
2015-07-18 13:57:16 -05:00
|
|
|
reader := bufio.NewReader(bytes.NewBuffer(b))
|
2015-05-08 17:45:31 -05:00
|
|
|
|
2015-07-18 13:57:16 -05:00
|
|
|
// Read first line, which should indicate metadata or not
|
|
|
|
line, err := reader.ReadBytes('\n')
|
|
|
|
if err != nil || !bytes.Equal(bytes.TrimSpace(line), parser.Opening()) {
|
|
|
|
return nil, b, fmt.Errorf("first line missing expected metadata identifier")
|
2015-05-08 17:45:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// buffer for metadata contents
|
2015-07-18 13:57:16 -05:00
|
|
|
metaBuf := bytes.Buffer{}
|
2015-05-08 17:45:31 -05:00
|
|
|
|
|
|
|
// Read remaining lines until closing identifier is found
|
2015-07-18 13:57:16 -05:00
|
|
|
for {
|
|
|
|
line, err := reader.ReadBytes('\n')
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
// no closing metadata identifier found
|
|
|
|
return nil, nil, fmt.Errorf("metadata not closed ('%s' not found)", parser.Closing())
|
|
|
|
}
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2015-05-08 17:45:31 -05:00
|
|
|
|
2015-07-18 13:57:16 -05:00
|
|
|
// if closing identifier found, the remaining bytes must be markdown content
|
2015-05-08 17:45:31 -05:00
|
|
|
if bytes.Equal(bytes.TrimSpace(line), parser.Closing()) {
|
2015-07-18 13:57:16 -05:00
|
|
|
break
|
2015-05-08 17:45:31 -05:00
|
|
|
}
|
2015-07-18 13:57:16 -05:00
|
|
|
|
|
|
|
metaBuf.Write(line)
|
|
|
|
metaBuf.WriteString("\r\n")
|
2015-05-08 17:45:31 -05:00
|
|
|
}
|
|
|
|
|
2015-07-18 13:57:16 -05:00
|
|
|
// By now, the rest of the buffer contains markdown content
|
|
|
|
contentBuf := new(bytes.Buffer)
|
|
|
|
io.Copy(contentBuf, reader)
|
|
|
|
|
|
|
|
return metaBuf.Bytes(), contentBuf.Bytes(), nil
|
2015-05-08 17:45:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// findParser finds the parser using line that contains opening identifier
|
2015-05-09 05:49:54 -05:00
|
|
|
func findParser(b []byte) MetadataParser {
|
|
|
|
var line []byte
|
|
|
|
// Read first line
|
|
|
|
if _, err := fmt.Fscanln(bytes.NewReader(b), &line); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-05-08 17:45:31 -05:00
|
|
|
line = bytes.TrimSpace(line)
|
|
|
|
for _, parser := range parsers {
|
|
|
|
if bytes.Equal(parser.Opening(), line) {
|
|
|
|
return parser
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|