2015-05-06 10:37:29 +08:00
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
2015-05-09 06:45:31 +08:00
|
|
|
"bytes"
|
2015-05-06 10:37:29 +08:00
|
|
|
"encoding/json"
|
2015-05-09 06:45:31 +08:00
|
|
|
"fmt"
|
2015-05-06 10:37:29 +08:00
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
"gopkg.in/yaml.v2"
|
2015-07-28 12:21:09 +08:00
|
|
|
"time"
|
2015-05-06 10:37:29 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Metadata stores a page's metadata
|
|
|
|
type Metadata struct {
|
2015-05-08 23:20:07 +08:00
|
|
|
// Page title
|
|
|
|
Title string
|
|
|
|
|
|
|
|
// Page template
|
|
|
|
Template string
|
|
|
|
|
2015-07-28 12:21:09 +08:00
|
|
|
// Publish date
|
|
|
|
Date time.Time
|
|
|
|
|
2015-05-08 23:20:07 +08:00
|
|
|
// Variables to be used with Template
|
2015-07-27 00:32:34 +08:00
|
|
|
Variables map[string]string
|
2015-05-06 10:37:29 +08:00
|
|
|
}
|
|
|
|
|
2015-05-08 23:20:07 +08:00
|
|
|
// load loads parsed values in parsedMap into Metadata
|
2015-05-06 10:37:29 +08:00
|
|
|
func (m *Metadata) load(parsedMap map[string]interface{}) {
|
2015-07-28 12:21:09 +08:00
|
|
|
if title, ok := parsedMap["title"]; ok {
|
|
|
|
m.Title, _ = title.(string)
|
2015-05-07 07:19:02 +08:00
|
|
|
}
|
2015-05-06 10:37:29 +08:00
|
|
|
if template, ok := parsedMap["template"]; ok {
|
|
|
|
m.Template, _ = template.(string)
|
|
|
|
}
|
2015-07-28 12:21:09 +08:00
|
|
|
if date, ok := parsedMap["date"].(string); ok {
|
|
|
|
if t, err := time.Parse(timeLayout, date); err == nil {
|
|
|
|
m.Date = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// store everything as a variable
|
|
|
|
for key, val := range parsedMap {
|
|
|
|
if v, ok := val.(string); ok {
|
|
|
|
m.Variables[key] = v
|
|
|
|
}
|
2015-05-06 10:37:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-08 23:20:07 +08:00
|
|
|
// MetadataParser is a an interface that must be satisfied by each parser
|
2015-05-06 10:37:29 +08:00
|
|
|
type MetadataParser interface {
|
2015-05-08 23:20:07 +08:00
|
|
|
// Opening identifier
|
2015-05-06 10:37:29 +08:00
|
|
|
Opening() []byte
|
2015-05-08 23:20:07 +08:00
|
|
|
|
|
|
|
// Closing identifier
|
2015-05-06 10:37:29 +08:00
|
|
|
Closing() []byte
|
2015-05-07 07:19:02 +08:00
|
|
|
|
2015-05-09 18:49:54 +08:00
|
|
|
// Parse the metadata.
|
|
|
|
// Returns the remaining page contents (Markdown)
|
|
|
|
// after extracting metadata
|
|
|
|
Parse([]byte) ([]byte, error)
|
2015-05-08 23:20:07 +08:00
|
|
|
|
|
|
|
// Parsed metadata.
|
|
|
|
// Should be called after a call to Parse returns no error
|
2015-05-06 10:37:29 +08:00
|
|
|
Metadata() Metadata
|
|
|
|
}
|
|
|
|
|
2015-07-28 12:21:09 +08:00
|
|
|
// JSONMetadataParser is the MetadataParser for JSON
|
2015-05-06 10:37:29 +08:00
|
|
|
type JSONMetadataParser struct {
|
|
|
|
metadata Metadata
|
|
|
|
}
|
|
|
|
|
2015-05-08 23:20:07 +08:00
|
|
|
// Parse the metadata
|
2015-05-09 18:49:54 +08:00
|
|
|
func (j *JSONMetadataParser) Parse(b []byte) ([]byte, error) {
|
2015-11-22 10:50:31 +08:00
|
|
|
b, markdown, err := extractMetadata(j, b)
|
|
|
|
if err != nil {
|
|
|
|
return markdown, err
|
|
|
|
}
|
2015-05-06 10:37:29 +08:00
|
|
|
m := make(map[string]interface{})
|
2015-05-09 18:49:54 +08:00
|
|
|
|
|
|
|
// Read the preceding JSON object
|
|
|
|
decoder := json.NewDecoder(bytes.NewReader(b))
|
|
|
|
if err := decoder.Decode(&m); err != nil {
|
2015-11-22 10:50:31 +08:00
|
|
|
return markdown, err
|
2015-05-06 10:37:29 +08:00
|
|
|
}
|
|
|
|
j.metadata.load(m)
|
2015-05-09 18:49:54 +08:00
|
|
|
|
2015-11-22 10:50:31 +08:00
|
|
|
return markdown, nil
|
2015-05-06 10:37:29 +08:00
|
|
|
}
|
|
|
|
|
2015-05-25 10:52:34 +08:00
|
|
|
// Metadata returns parsed metadata. It should be called
|
|
|
|
// only after a call to Parse returns without error.
|
2015-05-06 10:37:29 +08:00
|
|
|
func (j *JSONMetadataParser) Metadata() Metadata {
|
|
|
|
return j.metadata
|
|
|
|
}
|
|
|
|
|
|
|
|
// Opening returns the opening identifier JSON metadata
|
|
|
|
func (j *JSONMetadataParser) Opening() []byte {
|
2015-05-09 18:49:54 +08:00
|
|
|
return []byte("{")
|
2015-05-06 10:37:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Closing returns the closing identifier JSON metadata
|
|
|
|
func (j *JSONMetadataParser) Closing() []byte {
|
2015-05-09 18:49:54 +08:00
|
|
|
return []byte("}")
|
2015-05-06 10:37:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TOMLMetadataParser is the MetadataParser for TOML
|
|
|
|
type TOMLMetadataParser struct {
|
|
|
|
metadata Metadata
|
|
|
|
}
|
|
|
|
|
2015-05-08 23:20:07 +08:00
|
|
|
// Parse the metadata
|
2015-05-09 18:49:54 +08:00
|
|
|
func (t *TOMLMetadataParser) Parse(b []byte) ([]byte, error) {
|
|
|
|
b, markdown, err := extractMetadata(t, b)
|
|
|
|
if err != nil {
|
|
|
|
return markdown, err
|
|
|
|
}
|
2015-05-06 10:37:29 +08:00
|
|
|
m := make(map[string]interface{})
|
|
|
|
if err := toml.Unmarshal(b, &m); err != nil {
|
2015-05-09 18:49:54 +08:00
|
|
|
return markdown, err
|
2015-05-06 10:37:29 +08:00
|
|
|
}
|
|
|
|
t.metadata.load(m)
|
2015-05-09 18:49:54 +08:00
|
|
|
return markdown, nil
|
2015-05-06 10:37:29 +08:00
|
|
|
}
|
|
|
|
|
2015-05-25 10:52:34 +08:00
|
|
|
// Metadata returns parsed metadata. It should be called
|
|
|
|
// only after a call to Parse returns without error.
|
2015-05-06 10:37:29 +08: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 23:20:07 +08:00
|
|
|
// YAMLMetadataParser is the MetadataParser for YAML
|
2015-05-06 10:37:29 +08:00
|
|
|
type YAMLMetadataParser struct {
|
|
|
|
metadata Metadata
|
|
|
|
}
|
|
|
|
|
2015-05-08 23:20:07 +08:00
|
|
|
// Parse the metadata
|
2015-05-09 18:49:54 +08:00
|
|
|
func (y *YAMLMetadataParser) Parse(b []byte) ([]byte, error) {
|
|
|
|
b, markdown, err := extractMetadata(y, b)
|
|
|
|
if err != nil {
|
|
|
|
return markdown, err
|
|
|
|
}
|
2015-07-19 02:57:16 +08:00
|
|
|
|
2015-05-06 10:37:29 +08:00
|
|
|
m := make(map[string]interface{})
|
|
|
|
if err := yaml.Unmarshal(b, &m); err != nil {
|
2015-05-09 18:49:54 +08:00
|
|
|
return markdown, err
|
2015-05-06 10:37:29 +08:00
|
|
|
}
|
|
|
|
y.metadata.load(m)
|
2015-05-09 18:49:54 +08:00
|
|
|
return markdown, nil
|
2015-05-06 10:37:29 +08:00
|
|
|
}
|
|
|
|
|
2015-05-25 10:52:34 +08:00
|
|
|
// Metadata returns parsed metadata. It should be called
|
|
|
|
// only after a call to Parse returns without error.
|
2015-05-06 10:37:29 +08:00
|
|
|
func (y *YAMLMetadataParser) Metadata() Metadata {
|
|
|
|
return y.metadata
|
|
|
|
}
|
|
|
|
|
2015-05-07 07:19:02 +08:00
|
|
|
// Opening returns the opening identifier YAML metadata
|
2015-05-06 10:37:29 +08:00
|
|
|
func (y *YAMLMetadataParser) Opening() []byte {
|
|
|
|
return []byte("---")
|
|
|
|
}
|
|
|
|
|
2015-05-07 07:19:02 +08:00
|
|
|
// Closing returns the closing identifier YAML metadata
|
2015-05-06 10:37:29 +08:00
|
|
|
func (y *YAMLMetadataParser) Closing() []byte {
|
|
|
|
return []byte("---")
|
|
|
|
}
|
2015-05-09 06:45:31 +08:00
|
|
|
|
2015-07-19 02:57:16 +08: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 18:49:54 +08:00
|
|
|
func extractMetadata(parser MetadataParser, b []byte) (metadata []byte, markdown []byte, err error) {
|
2015-05-09 06:45:31 +08:00
|
|
|
b = bytes.TrimSpace(b)
|
2015-11-22 10:50:31 +08:00
|
|
|
openingLine := append(parser.Opening(), '\n')
|
|
|
|
closingLine := append(parser.Closing(), '\n')
|
|
|
|
if !bytes.HasPrefix(b, openingLine) {
|
2015-07-19 02:57:16 +08:00
|
|
|
return nil, b, fmt.Errorf("first line missing expected metadata identifier")
|
2015-05-09 06:45:31 +08:00
|
|
|
}
|
2015-11-22 10:50:31 +08:00
|
|
|
metaStart := len(openingLine)
|
|
|
|
if _, ok := parser.(*JSONMetadataParser); ok {
|
|
|
|
metaStart = 0
|
2015-05-09 06:45:31 +08:00
|
|
|
}
|
2015-11-22 10:50:31 +08:00
|
|
|
metaEnd := bytes.Index(b[metaStart:], closingLine)
|
|
|
|
if metaEnd == -1 {
|
|
|
|
return nil, nil, fmt.Errorf("metadata not closed ('%s' not found)", parser.Closing())
|
|
|
|
}
|
|
|
|
metaEnd += metaStart
|
|
|
|
if _, ok := parser.(*JSONMetadataParser); ok {
|
|
|
|
metaEnd += len(closingLine)
|
|
|
|
}
|
|
|
|
metadata = b[metaStart:metaEnd]
|
|
|
|
markdown = b[metaEnd:]
|
|
|
|
if _, ok := parser.(*JSONMetadataParser); !ok {
|
|
|
|
markdown = b[metaEnd+len(closingLine):]
|
|
|
|
}
|
|
|
|
return metadata, markdown, nil
|
2015-05-09 06:45:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// findParser finds the parser using line that contains opening identifier
|
2015-05-09 18:49:54 +08: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-09 06:45:31 +08:00
|
|
|
line = bytes.TrimSpace(line)
|
2015-07-28 12:21:09 +08:00
|
|
|
for _, parser := range parsers() {
|
2015-05-09 06:45:31 +08:00
|
|
|
if bytes.Equal(parser.Opening(), line) {
|
|
|
|
return parser
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-07-28 12:21:09 +08:00
|
|
|
|
|
|
|
// parsers returns all available parsers
|
|
|
|
func parsers() []MetadataParser {
|
|
|
|
return []MetadataParser{
|
|
|
|
&JSONMetadataParser{metadata: Metadata{Variables: make(map[string]string)}},
|
|
|
|
&TOMLMetadataParser{metadata: Metadata{Variables: make(map[string]string)}},
|
|
|
|
&YAMLMetadataParser{metadata: Metadata{Variables: make(map[string]string)}},
|
|
|
|
}
|
|
|
|
}
|