internal

package
v0.1.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 12, 2021 License: GPL-3.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Name is the application name shown for "dumblog version"
	Name string = "dumblog"
	// Version is the current version shown for "dumblog version"
	Version string = "0.1.7"
)

Variables

View Source
var TemplateFuncs = text.FuncMap{
	"atomdate": func(t time.Time) string {
		return t.UTC().Format(time.RFC3339)
	},
	"shortdate": func(t time.Time) string {
		return t.UTC().Format("2006-01-02")
	},
	"prettydate": func(t time.Time) string {
		return t.UTC().Format("Monday, 02 January 2006")
	},
	"prettyduration": func(d time.Duration) string {
		s, m, h := int(d.Seconds()), int(d.Minutes()), int(d.Hours())
		if s < 1 {
			return "instant"
		} else if s == 1 {
			return "1 second"
		} else if s < 60 {
			return fmt.Sprintf("%d seconds", s)
		} else if m == 1 {
			return "1 minute"
		} else if m < 60 {
			return fmt.Sprintf("%d minutes", m)
		} else if h == 1 {
			return "1 hour"
		}
		return fmt.Sprintf("%d hours", h)
	},
	"safehtml": func(s string) html.HTML {
		return html.HTML(s)
	},
	"postslimit": func(max int, posts []Post) []Post {
		l := len(posts)
		if l > max {
			l = max
		}
		return posts[:l]
	},
	"postsbydir": func(dir string, posts []Post) []Post {
		var list []Post
		for _, p := range posts {
			if firstDir(p.rel) == dir {
				list = append(list, p)
			}
		}
		return list
	},
	"slugify": func(s string) string {
		return url.PathEscape(strings.ReplaceAll(strings.ToLower(s), " ", "_"))
	},
	"isset": func(field string, v interface{}) bool {

		rv := reflect.ValueOf(v)
		if rv.Kind() == reflect.Ptr {
			rv = rv.Elem()
		}
		if rv.Kind() != reflect.Struct {
			return false
		}
		return rv.FieldByName(field).IsValid()
	},
}

TemplateFuncs contains helper functions for the templates

Functions

func CreateTemplate

func CreateTemplate(dst, src string, content embed.FS) error

CreateTemplate creates an example dir with some template files you can use.

Types

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator is loads & parses templates and then execs & writes them to a directory.

func New

func New() *Generator

New returns a new *Generator instance.

func (*Generator) ExecuteTemplate

func (g *Generator) ExecuteTemplate(dir string) error

ExecuteTemplate executes the templates and write the resulting files to dir. It also copy over any other plain files. ReadTemplate must have been called before.

func (*Generator) ReadTemplate

func (g *Generator) ReadTemplate(dir string) error

ReadTemplate loads and parses the template files from `dir`. Optionally tries to load a `.meta.yaml` file, used for providing global meta data to the templates.

type Meta

type Meta map[string]string

Meta is a map of strings that allows you to insert custom data into your templates, like links and titles. See example/.meta.yaml and the example html templates for usage.

type Params added in v0.1.7

type Params struct {
	// Time is the current date and time
	Time time.Time
	// Meta contains user defined meta data, see Meta
	Meta Meta
	// Posts is a list of parsed Post
	Posts []Post
	// Tags is a list of parsed Tag
	Tags []Tag
	// Pages is a list of all html pages that will be written
	Pages []string
}

Params is a struct holding all available meta data you can use in a template.

type Post

type Post struct {
	Meta struct {
		// Title is the title of the post
		Title string
		// Published is the publishing date
		Published time.Time
		// Short is a short description for the post
		Short string
		// Tags is a list of optional string tags
		Tags []string
	}
	// contains filtered or unexported fields
}

Post contains the meta data header from a `post.md`.

func (Post) Body

func (p Post) Body() (string, error)

Body parses the post's body and parses it as commonmark.

func (p Post) Link() string

Link returns a relative http link to the post.

type PostParams added in v0.1.7

type PostParams struct {
	Params

	// Current is the active post being written, otherwise it's nil
	Current Post
}

PostParams is struct similar to Params, but it also holds the currently active post.

type Tag

type Tag struct {
	Title string
	Posts []Post
}

Tag contains a list of posts it was tagged in.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL