page

package
v0.0.0-...-cf5c4a9 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package page defines a single page in a Kisipar web site.

Example
package main

import (
	"fmt"
	"github.com/biztos/kisipar/page"
)

func main() {

	// Most common use case: load a Markdown file using the default parser.
	p, err := page.Load("example.md")
	if err != nil {
		panic(err)
	}

	fmt.Println("Title:", p.Title())
	fmt.Println("Author:", p.Author())
	fmt.Println("Hamand:", p.MetaString("Hamand"))
	fmt.Println(p.Content)

}
Output:

Title: Example Kisipar Page
Author: Thelonius Mönch
Hamand: EGGS!
<h1>Example Kisipar Page</h1>

<h2>Welcome to the Example Page!</h2>

<p>A paragraph is all you need.</p>

Index

Examples

Constants

This section is empty.

Variables

View Source
var ExtParsers = []*ExtParser{
	{".md", &MdParser{}},
	{".MD", &MdParser{}},
	{".markdown", &MdParser{}},
	{".MARKDOWN", &MdParser{}},
	{".txt", &MdParser{}},
	{".TXT", &MdParser{}},
}

ExtParsers defines the parsers that will be used for various extensions in Load and LoadVirtual. Note that extensions are matched case-insensitively, but LoadAny is case-sensitive; thus if you specifically expecte to look up a particular cased extension (".Txt" for instance) then you need to set that up here in ExtParsers.

The standard values should be sufficient for any normal Kisipar site.

Functions

func LimitExtParsers

func LimitExtParsers(extlist []string)

LimitExtParsers removes any ExtParser entries from ExtParsers not matching the provided set of extensions, putting the resulting ExtParsers in the order of the extlist received. This is useful for keeping the behavior of LoadAny in sync with any site-wide preload operations.

Note that this is destructive: once limited, the original set of ExtParsers is gone.

Types

type ExtParser

type ExtParser struct {
	Ext    string
	Parser Parser
}

ExtParser pairs an extension to a Parser for use in the ordered ExtParsers.

type MdParser

type MdParser struct{}

MdParser is the standard Markdown parser, using the frostedmd parsing logic based on blackfriday.

func (*MdParser) Parse

func (p *MdParser) Parse(b []byte) (ParseResult, error)

Parse implements the Parser interface for the MdParser type.

type Page

type Page struct {

	// The data source:
	Path     string    // path as loaded; arbitrary unique for Virtual pages.
	Virtual  bool      // does the Page exist on disk?
	Unlisted bool      // should the Page be included in site listings?
	IsIndex  bool      // is the page an Index?
	ModTime  time.Time // file mod time; instantiation time for Virtual.
	Source   []byte    // raw, unparsed source data.

	// The standard rendered HTML content:
	Content template.HTML

	// The data extracted from the meta block:
	Meta map[string]interface{}
	// contains filtered or unexported fields
}

A Page represents a single page, to be rendered in a template. Pages should be created with New, Load or LoadVirtual.

func Load

func Load(path string) (*Page, error)

Load loads a page and parses it using the Parser associated with its extension in ExtParsers.

func LoadAny

func LoadAny(spath string) (*Page, error)

LoadAny loads the first page it finds for the source path with an extension listed in ExtParsers. Extensions are matched exactly, meaning that ExtParsers must have entries of every supported extension case.

func LoadVirtual

func LoadVirtual(path string, input []byte) (*Page, error)

LoadVirtual returns a page with a virtual path, i.e. not necessarily corresponding to any file on disk. The provided path should indicate the source type, e.g. "virtual/document.md" for Markdown.

func LoadVirtualString

func LoadVirtualString(path, input string) (*Page, error)

LoadVirtualString is shorthand for LoadVirtual(path,[]byte(string).

func New

func New(path string) (*Page, error)

New returns a Page with source loaded from the provided path, but not yet parsed. The path must have an extension.

func (*Page) Author

func (p *Page) Author() string

Author returns the Author string from the Page's Meta block. Author is simply shorthand for MetaString("Author").

func (*Page) Created

func (p *Page) Created() *time.Time

Created returns the page's creation time as defined in the meta. Created is simply shorthand for MetaTime("Created").

func (*Page) Description

func (p *Page) Description() string

Description returns the Description string from the Page's Meta block. Description is simply shorthand for MetaString("Description").

func (*Page) Keywords

func (p *Page) Keywords() string

Keywords returns the Keywords string from the Page's Meta block. (Keywords are generally used in the HTML meta elements, which is why this is a string and not a []string. For an array, use Tags.) Keywords is simply shorthand for MetaString("Keywords").

func (*Page) Load

func (p *Page) Load() error

Load loads the source data from the Page's path, but does not parse it.

func (*Page) MetaBool

func (p *Page) MetaBool(key string) bool

MetaBool returns a boolean value from the Page's Meta block, with undefined and non-boolean values treated as false. If there is no match for the exact key, lookup is attempted on the lowercase and uppercase versions, in that order.

func (*Page) MetaString

func (p *Page) MetaString(key string) string

MetaString returns a string value from the Meta, with non-string values stringified. If there is no match for the exact key, lookup is attempted on the lowercase and uppercase versions, in that order. If there is no value for the key in any form, the empty string is returned. Thus MetaString should not be used to determine the presence of a key, only in cases where an empty string and nil/not-found have identical meaning.

func (*Page) MetaStringArray

func (p *Page) MetaStringArray(key string) []string

MetaStringArray returns an array of string values from the Meta, or an empty array if there is value for the key. If the value is already a []string, it is returned as-is. If the value is a string, it is split with a comma delimiter and the resulting items are trimmed of leading and trailing spaces. If the value is an array of integers or floats then its contents are stringified. For any type that implements the StringArrayer interface, the result of its StringArray function is returned. All other types return an empty array. Key lookup follows the logic of MetaString.

func (*Page) MetaTime

func (p *Page) MetaTime(key string) *time.Time

MetaTime returns a time value from the Meta, if the requested key holds a string value that can be parsed into a time. Nil is returned in all other cases.

func (*Page) Parse

func (p *Page) Parse() error

Parse parses the Page's Source data into Meta and Content using the first Parser in ExtParsers to match the Page's Path extension, or the DefaultParser if none matches. Extension matching is case-insensitive.

If the Meta defines a boolean "Unlisted" (or "unlisted" or "UNLISTED") with a value of true, the Page's Unlisted property is set to true.

func (*Page) Refresh

func (p *Page) Refresh() error

Refresh reloads the page if it is not Virtual and the modtime of the source file is different than the current ModTime. In case of file errors, including not-found, the error will be returned without the page being modified, and the caller must handle the error.

func (*Page) String

func (p *Page) String() string

String returns a hopefully-useful stringification of the page, for logging and debugging purposes.

func (*Page) Summary

func (p *Page) Summary() string

Summary returns the Summary string from the Page's Meta block. Summary is simply shorthand for MetaString("Summary").

func (*Page) Tags

func (p *Page) Tags() []string

Tags returns a list of tags from the Page's Meta block, as an array of strings. This is normally defined in the meta as either an array of strings directly, or a comma-delimited list in string form such as "foo, bar, baz" -- this function supports both forms within the Meta map but the implmentation is up to the Parser. Tags is simply shorthand for MetaStringArray("Tags")

func (*Page) Time

func (p *Page) Time() *time.Time

Time returns the newer of the page's Created and Updated meta times; if neither is set or neither is parseable, returns the ModTime.

func (*Page) Title

func (p *Page) Title() string

Title returns the Title string from the Page's Meta block, or the file name (without extension) of the Path if no Title is available in the Meta.

func (*Page) Updated

func (p *Page) Updated() *time.Time

Updated returns the page's update time as defined in the meta. Updated is simply shorthand for MetaTime("Updated").

type ParseResult

type ParseResult interface {
	Meta() map[string]interface{}
	Content() []byte
}

The ParseResult interface defines a struct representing a Parser's Parse output.

type Parser

type Parser interface {
	Parse([]byte) (ParseResult, error)
}

The Parser interface defines a struct capable of parsing source data into a meta map and HTML content.

var DefaultParser Parser = &VerbatimParser{}

DefaultParser defines the Parser to use if no more specific Parser is available.

type StringArrayer

type StringArrayer interface {
	StringArray() []string
}

The StringArrayer interface enables custom types, usually named array types, to generate string arrays via MetaStringArray.

type VerbatimParseResult

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

VerbatimParseResult is the result of a verbatim parse.

func (*VerbatimParseResult) Content

func (vpr *VerbatimParseResult) Content() []byte

Content implements the ParseResult interface for the VerbatimParseResult type.

func (*VerbatimParseResult) Meta

func (vpr *VerbatimParseResult) Meta() map[string]interface{}

Meta implements the ParseResult interface for the VerbatimParseResult type.

type VerbatimParser

type VerbatimParser struct{}

VerbatimParser is a parser that simply returns its input with an empty meta map.

func (*VerbatimParser) Parse

func (v *VerbatimParser) Parse(b []byte) (ParseResult, error)

Parse implements the Parser interface for the VerbatimParser type.

Jump to

Keyboard shortcuts

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