blog

package module
v0.0.0-...-f76e360 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2018 License: BSD-3-Clause Imports: 13 Imported by: 0

README

Blog

A simple Go based blogging system. This is the library behind bloggen.

  • Build Status
  • Maintainability
  • Test Coverage
  • Go Report Card

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyMacros

func ApplyMacros(content string, macros *Macros) string

ApplyMacros applies the passed macros to the passed string.

func OutputFilename

func OutputFilename(path string) string

OutputFilename returns the filename with `.html` instead of `.text`.

func ParseDate

func ParseDate(datestring string, location *time.Location) (time.Time, error)

ParseDate parses a blog post's date.

func PrettyDate

func PrettyDate(datetime time.Time) string

PrettyDate returns a nicely rendered timestamp for this page, based on its date.

func PublishDate

func PublishDate(datetime time.Time) string

PublishDate returns a RSS friendly timestamp.

func RelativePath

func RelativePath(base, path string) (string, error)

RelativePath updates the internal path, and removes the actual filesystem portion. For instance, if the path is `/srv/sites/teaincodeout` this function would be passed `/srv/sites/teaincodeout` so that `/srv/sites/teaincodeout/blog/index.text` has a path of `blog`.

func RenderArchives

func RenderArchives(context *SiteContext) error

RenderArchives renders the Blog Archives page, which is found at `blogpath/archives.html`. This renders a page with a reverse chronological listing of blog posts, under the month that they were released.

func RenderBlogIndex

func RenderBlogIndex(context *SiteContext) error

RenderBlogIndex renders the Blog Index page, which is found at `blogpath/index.html`. This renders a page with up to the user configured blog limit post content inline, with a link to the archives page.

func RenderContent

func RenderContent(page Page, macros *Macros) ([]byte, error)

RenderContent renders a Page's content with the passed macros to HTML.

func RenderFeed

func RenderFeed(context *SiteContext) error

RenderFeed renders the RSS Feed, which is found at `blogpath/rss.xml`. This performs a very similar operation to RenderBlogIndex, however there is no limit and the data is wrapped in XML templates, not HTML.

func RenderPage

func RenderPage(page Page, templates *Templates, macros *Macros) error

RenderPage renders the passed page, with the passed templates and the passed Macros. This renders the page to the filesystem and closes it.

func RenderTree

func RenderTree(context *SiteContext, clean bool) (bool, error)

RenderTree renders all active HTML data in the site tree. If clean is set to true, re-renders all content, not just content that has changed.

func UpdatePageMacros

func UpdatePageMacros(page Page, macros *Macros)

UpdatePageMacros updates the macros array to include some macros specific to the passed Page.

Types

type ByUnix

type ByUnix []Page

ByUnix is an interface for sorting Pages by unix timestamp.

func (ByUnix) Len

func (a ByUnix) Len() int

func (ByUnix) Less

func (a ByUnix) Less(i, j int) bool

func (ByUnix) Swap

func (a ByUnix) Swap(i, j int)

type Config

type Config struct {
	Macros    map[string]string `json:"macros"`     // A map of key-value pairs representing macros.
	BlogLimit int               `json:"blog_limit"` // The number of blog posts to list on the Index page.
	BlogPath  string            `json:"blog_path"`  // The directory that the blog posts are located under.
	Location  string            `json:"location"`   // The timezone that dates will be processed under.
}

Config is a type that matches what the user can modify directly. For instance, the macros, the number of blog posts to show in a listing, and the directory that the blog posts exist in.

type Macros

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

Macros are a simple, key-value pair used to do simple string replacements. The contents of the macro key are replaced with the macro value.

Macro keys always show in templates and pages as beginning with a `$` character.

BUG(caj): Unexpected behaviour may occur when two Macros have similar starting values. For example: $PAGE and $PAGEPATH.

func NewMacros

func NewMacros(capacity int) *Macros

NewMacros creates a new Macros list, starting with a capacity passed by the developer.

func (*Macros) Del

func (macros *Macros) Del(key string)

Del deletes a macro previously added to the Macros list.

func (*Macros) Get

func (macros *Macros) Get(key string) string

Get a macro value.

func (*Macros) Set

func (macros *Macros) Set(key string, value string)

Set sets a new macro to the Macros list. If a macro with that key exists, it is replaced with the new value.

func (*Macros) Values

func (macros *Macros) Values() map[string]string

Values returns the macro values as key, value pairs.

type Page

type Page struct {
	Title    string    `json:"title"`    // This Page's title.
	Template string    `json:"template"` // The template to use when rendering the page.
	Date     time.Time `json:"date"`     // The date line for page page.
	Path     string    // The path to the directory that the page will be saved in.
	// contains filtered or unexported fields
}

Page represents the pre-rendered pages in the system. These are files whose filenames end in `.text`, which contain metadata, followed by a Markdown body.

A Page can be rendered to HTML by calling `RenderPage()` and passing the page in question.

An example page would be:

Title: Page Title
Date: 2017-03-25 22:01
Template: post
---
# Header

Page content follows.

This page would be rendered to an HTML page with the title "Page Title", with the date based on the Date field, using the template "post".

func LoadPage

func LoadPage(filename string, location *time.Location) (*Page, error)

LoadPage loads a page from the filesystem using the pageParser interface.

func NewPage

func NewPage(filename, title string) *Page

NewPage creates a new page with the passed path. The path should end in `.text` if you would like the page to be picked up by the auto-renderer.

func PostListing

func PostListing(path string, location *time.Location) ([]Page, error)

PostListing returns a reverse-chronologically ordered listing of blog pages, based off of the passed path.

func SiteListing

func SiteListing(path string, location *time.Location) ([]Page, error)

SiteListing creates a listing of all pages in the system, regardless of if they're blog posts or not.

func (*Page) Content

func (page *Page) Content() (string, error)

Content returns the (unrendered) content from the Page.

func (*Page) HasChanged

func (page *Page) HasChanged() bool

HasChanged returns true if the original Page is newer than the rendered version

type SiteContext

type SiteContext struct {
	Location  *time.Location // The timezone to treat all parsed time in.
	Config    *Config        // The options for this site.
	Templates *Templates     // The templates store to use when rendering this site.
	// contains filtered or unexported fields
}

SiteContext contains the information relating to this site/blog. The loaded templates and configuration is tracked in this structure.

func NewContext

func NewContext(path, templatePath, configPath string) (*SiteContext, error)

NewContext creates a new SiteContext and loads the appropriate data from the filesystem.

func (*SiteContext) LoadConfig

func (context *SiteContext) LoadConfig() error

LoadConfig loads a configuration file and returns a Config struct containing both macros and site configuration.

func (*SiteContext) LoadTemplates

func (context *SiteContext) LoadTemplates() error

LoadTemplates reloads the templates from the passed templates directory.

func (*SiteContext) Macros

func (context *SiteContext) Macros() *Macros

Macros returns the macros defined in the user configuration.

func (*SiteContext) Path

func (context *SiteContext) Path() string

Path returns the absolute site root.

type Templates

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

Templates is a struct which contains all of the templates found in the templates directory.

Rather than doing filesystem lookups, the Templates stores all of the possible templates accessible by the system, and those templates can be queried at any point.

Templates has a few functions for rendering, which are used to render Pages into their final, HTML version.

func LoadTemplates

func LoadTemplates(path string) (*Templates, error)

LoadTemplates loads all of the templates in the template directory specified in the path parameter.

func NewTemplates

func NewTemplates(capacity int) *Templates

NewTemplates creates a new Templates list, initialized with a certain capacity.

func (*Templates) HasTemplate

func (templates *Templates) HasTemplate(key string) bool

HasTemplate verifies if a template exists

func (*Templates) Load

func (templates *Templates) Load(filename string) error

Load a template into the templates object

func (*Templates) Render

func (templates *Templates) Render(key string, macros *Macros) []byte

Render a template

func (*Templates) RenderFooter

func (templates *Templates) RenderFooter(key string, macros *Macros) []byte

RenderFooter renders the footer version of the template referenced by key, and falls back to the default "footer.html" template, if needed.

func (*Templates) RenderHeader

func (templates *Templates) RenderHeader(key string, macros *Macros) []byte

RenderHeader renders the header version of the template referenced by key, and falls back to the default "header.html" template, if needed.

Notes

Bugs

  • Unexpected behaviour may occur when two Macros have similar starting values. For example: $PAGE and $PAGEPATH.

Jump to

Keyboard shortcuts

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