Giter VIP home page Giter VIP logo

frontmatter's People

Contributors

adrg avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

frontmatter's Issues

Issues with custom unmarshal function

Hey, thank you for this great package, I'm currently using it in my static site generator. I'm having issues defining a custom unmarshal function. I'm trying to parse frontmatter to grab an ISO8601 date, convert it into a time.Time, and marshal it into my structure.

Here's what I've got so far:

package main

import (
	"bytes"
	"fmt"
	"github.com/adrg/frontmatter"
	"gopkg.in/yaml.v2"
	"time"
)

type Matter struct {
	Template string    `yaml:"template"`
	URL      string    `yaml:"url"`
	Title    string    `yaml:"title"`
	Subtitle string    `yaml:"subtitle"`
	Date     time.Time `yaml:"date"`
	Body     string
}

func (m *Matter) UnmarshalYAML(unmarshal func(interface{}) error) error {
	var temp struct {
		Template string `yaml:"template"`
		URL      string `yaml:"url"`
		Title    string `yaml:"title"`
		Subtitle string `yaml:"subtitle"`
		Date     string `yaml:"date"`
		Body     string
	}

	if err := unmarshal(&temp); err != nil {
		return err
	}

	layout := "2006-01-02"
	var date8601 time.Time
	var err error
	if temp.Date != "" {
		date8601, err = time.Parse(layout, temp.Date)
		if err != nil {
			return err
		}
	}

	m = &Matter{
		Template: temp.Template,
		URL:      temp.URL,
		Title:    temp.Title,
		Subtitle: temp.Subtitle,
		Date:     date8601,
		Body:     temp.Body,
	}
        fmt.Println(m)  // The full object gets printed!
	return nil
}

var format = frontmatter.NewFormat("---", "---", yaml.Unmarshal)

// Parses frontmatter, populates the `matter` struct and
// returns the rest
func parseFrontmatter(inputBytes []byte) ([]byte, Matter) {
	m := Matter{}
	input := bytes.NewReader(inputBytes)
	rest, err := frontmatter.Parse(input, &m, format)
	fmt.Println(m)    // Empty object

	if err != nil {
		printErr(err)
	}
	return rest, m
}

Ideally, this should populate the m object, and it seems to do so when I print it from within UnmarshalYAML(), but when I print it in my parseFrontmatter function, it's empty. Any ideas?

Excuse the bad quality of code—I'm only a few days into the language. :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.