wikifier

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2024 License: ISC Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// PageOptExternalTypeQuiki is the external type for quiki sites.
	PageOptExternalTypeQuiki PageOptExternalType = "quiki"

	// PageOptExternalTypeMediaWiki is the external type for MediaWiki sites.
	PageOptExternalTypeMediaWiki = "mediawiki"

	// PageOptExternalTypeNone is an external type that can be used for websites that
	// perform no normalization of page targets beyond normal URI escaping.
	PageOptExternalTypeNone = "none"
)

Variables

This section is empty.

Functions

func CategoryName

func CategoryName(name string) string

CategoryName returns a clean category name.

func CategoryNameNE

func CategoryNameNE(name string) string

CategoryNameNE returns a clean category with No Extension.

func InjectPageOpt

func InjectPageOpt(page *Page, opt *PageOpt) error

InjectPageOpt extracts page options found in the specified page and injects them into the provided PageOpt pointer.

func MakeDir

func MakeDir(dir, name string)

MakeDir creates directories recursively.

func ModelName

func ModelName(name string) string

ModelName returns a clean model name.

func PageName

func PageName(name string) string

PageName returns a clean page name.

func PageNameExt

func PageNameExt(name, ext string) string

PageNameExt returns a clean page name with the provided extension.

func PageNameLink(name string) string

PageNameLink returns a clean page name without the extension.

func PageNameNE

func PageNameNE(name string) string

PageNameNE returns a clean page name with No Extension.

func ScaleString

func ScaleString(name string, retina []int) string

ScaleString returns a string of scaled image names for use in srcset.

func UniqueFilesInDir

func UniqueFilesInDir(dir string, extensions []string, thisDirOnly bool) ([]string, error)

UniqueFilesInDir recursively scans a directory for files matching the requested extensions, resolves symlinks, and returns a list of unique files. That is, if more than one link resolves to the same thing (as is the case for quiki page redirects), there is only one occurrence in the output.

Types

type AttributedObject

type AttributedObject interface {

	// getters
	Get(key string) (any, error)
	GetBool(key string) (bool, error)
	GetStr(key string) (string, error)
	GetBlock(key string) (block, error)
	GetObj(key string) (AttributedObject, error)

	// setters
	Set(key string, value any) error
	// contains filtered or unexported methods
}

An AttributedObject is any object on which you can set and retrieve attributes.

For example, a Page is an attributed object since it contains variables. Likewise, a Map is an attributed object because it has named properties.

type FmtOpt added in v0.2.0

type FmtOpt struct {
	Pos        Position // position used for warnings (set internally)
	NoEntities bool     // disables html entity conversion
	NoWarnings bool     // silence warnings for undefined variables
	// contains filtered or unexported fields
}

FmtOpt describes options for page.FmtOpts.

type HTML

type HTML string

HTML encapsulates a string to indicate that it is preformatted HTML. It lets quiki's parsers know not to attempt to format it any further.

type List

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

List represents a list of items. It is a quiki data type as well as the base of many block types.

func NewList

func NewList(mb block) *List

NewList creates a new list, given the main block of the page it is to be associated with.

func (List) String

func (b List) String() string

type Map

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

Map represents a Key-value dictionary. It is a quiki data type as well as the base of many block types.

func NewMap

func NewMap(mb block) *Map

NewMap creates a new map, given the main block of the page it is to be associated with.

func (Map) Get

func (scope Map) Get(key string) (any, error)

Get fetches a a value regardless of type.

The key may be segmented to indicate properties of each object (e.g. person.name).

If attempting to read a property of an object that does not support properties, such as a string, Get returns an error.

If the key is valid but nothing exists at it, Get returns (nil, nil).

func (Map) GetBlock

func (scope Map) GetBlock(key string) (block, error)

GetBlock is like Get except it always returns a block.

func (Map) GetBool

func (scope Map) GetBool(key string) (bool, error)

GetBool is like Get except it always returns a boolean.

func (Map) GetObj

func (scope Map) GetObj(key string) (AttributedObject, error)

GetObj is like Get except it always returns an AttributedObject.

func (Map) GetStr

func (scope Map) GetStr(key string) (string, error)

GetStr is like Get except it always returns a string.

If the value is HTML, it is converted to a string.

func (Map) GetStrList added in v0.2.0

func (scope Map) GetStrList(key string) ([]string, error)

GetStrList is like Get except it always returns a list of strings.

If the value is a `list{}` block, the list's values are returned, with non-strings quietly filtered out.

If the value is a string, it is treated as a comma-separated list, and each item is trimmed of prepending or suffixing whitespace.

func (*Map) Keys

func (m *Map) Keys() []string

Keys returns a string of actual underlying map keys.

func (*Map) Map

func (m *Map) Map() map[string]any

Map returns the actual underlying Go map.

func (*Map) OrderedKeys

func (m *Map) OrderedKeys() []string

OrderedKeys returns a string of map keys in the order provided in the source. Keys that were set internally (and not from quiki source code) are omitted.

func (Map) Set

func (scope Map) Set(key string, value any) error

Set sets a value at the given key.

The key may be segmented to indicate properties of each object (e.g. person.name).

If attempting to write to a property of an object that does not support properties, such as a string, Set returns an error.

func (Map) String

func (b Map) String() string

type ModelInfo added in v0.2.0

type ModelInfo struct {
	Title       string     `json:"title"`            // @model.title
	Author      string     `json:"author,omitempty"` // @model.author
	Description string     `json:"desc,omitempty"`   // @model.desc
	File        string     `json:"file"`             // filename
	FileNE      string     `json:"file_ne"`          // filename with no extension
	Path        string     `json:"path"`
	Created     *time.Time `json:"created,omitempty"`  // creation time
	Modified    *time.Time `json:"modified,omitempty"` // modify time
}

ModelInfo represents metadata associated with a model.

type Page

type Page struct {
	Source   string   // source content
	FilePath string   // Path to the .page file
	VarsOnly bool     // True if Parse() should only extract variables
	Opt      *PageOpt // page options

	Images    map[string][][]int   // references to images
	Models    map[string]ModelInfo // references to models
	PageLinks map[string][]int     // references to other pages

	Wiki     any  // only available during Parse() and HTML()
	Markdown bool // true if this is a markdown source

	Warnings []Warning // parser warnings
	Error    *Warning  // parser error, as an encodable Warning
	// contains filtered or unexported fields
}

Page represents a single page or article, generally associated with a .page file. It provides the most basic public interface to parsing with the wikifier engine.

func NewPage

func NewPage(filePath string) *Page

NewPage creates a page given its filepath.

func NewPagePath

func NewPagePath(filePath, name string) *Page

NewPagePath creates a page given its filepath and relative name.

func NewPageSource

func NewPageSource(source string) *Page

NewPageSource creates a page given some source code.

func (*Page) Author

func (p *Page) Author() string

Author returns the page author's name, if any.

func (*Page) CSS

func (p *Page) CSS() string

CSS generates and returns the CSS code for the page's inline styles.

func (*Page) CacheExists

func (p *Page) CacheExists() bool

CacheExists is true if the page cache file exists.

func (*Page) CacheModified

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

CacheModified returns the page cache file time.

func (*Page) CachePath

func (p *Page) CachePath() string

CachePath returns the absolute path to the page cache file.

func (*Page) Categories

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

Categories returns a list of categories the page belongs to.

func (*Page) Created

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

Created returns the page creation time.

func (*Page) Description added in v0.2.0

func (p *Page) Description() string

Description returns the page description.

func (*Page) Draft

func (p *Page) Draft() bool

Draft returns true if the page is marked as a draft.

func (*Page) Exists

func (p *Page) Exists() bool

Exists is true if the page exists.

func (*Page) External added in v0.2.0

func (p *Page) External() bool

External returns true if the page is outside the page directory as defined by the configuration, with symlinks considered.

If `dir.wiki` isn't set, External is always true (since the page is not associated with a wiki at all).

func (*Page) Fmt added in v0.2.0

func (p *Page) Fmt(text string, pos Position) HTML

Fmt generates HTML from a quiki-encoded formatted string.

func (*Page) FmtOpts added in v0.2.0

func (p *Page) FmtOpts(text string, pos Position, o FmtOpt) HTML

FmtOpts is like Fmt except you can specify additional options with the FmtOpt argument.

func (*Page) FmtTitle

func (p *Page) FmtTitle() HTML

FmtTitle returns the page title, preserving any possible text formatting.

func (*Page) Generated

func (p *Page) Generated() bool

Generated returns true if the page was auto-generated from some other source content.

func (Page) Get

func (scope Page) Get(key string) (any, error)

Get fetches a a value regardless of type.

The key may be segmented to indicate properties of each object (e.g. person.name).

If attempting to read a property of an object that does not support properties, such as a string, Get returns an error.

If the key is valid but nothing exists at it, Get returns (nil, nil).

func (Page) GetBlock

func (scope Page) GetBlock(key string) (block, error)

GetBlock is like Get except it always returns a block.

func (Page) GetBool

func (scope Page) GetBool(key string) (bool, error)

GetBool is like Get except it always returns a boolean.

func (Page) GetObj

func (scope Page) GetObj(key string) (AttributedObject, error)

GetObj is like Get except it always returns an AttributedObject.

func (Page) GetStr

func (scope Page) GetStr(key string) (string, error)

GetStr is like Get except it always returns a string.

If the value is HTML, it is converted to a string.

func (Page) GetStrList added in v0.2.0

func (scope Page) GetStrList(key string) ([]string, error)

GetStrList is like Get except it always returns a list of strings.

If the value is a `list{}` block, the list's values are returned, with non-strings quietly filtered out.

If the value is a string, it is treated as a comma-separated list, and each item is trimmed of prepending or suffixing whitespace.

func (*Page) HTML

func (p *Page) HTML() HTML

HTML generates and returns the HTML code for the page. The page must be parsed with Parse before attempting this method.

func (*Page) Info

func (p *Page) Info() PageInfo

Info returns the PageInfo for the page.

func (p *Page) IsSymlink() bool

IsSymlink returns true if the page is a symbolic link to another file within the page directory. If it is symlinked to somewhere outside the page directory, it is treated as a normal page rather than a redirect.

func (*Page) Keywords added in v0.2.0

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

Keywords returns the list of page keywords.

func (*Page) Modified

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

Modified returns the page modification time.

func (*Page) Name

func (p *Page) Name() string

Name returns the resolved page name with extension.

This DOES take symbolic links into account. and DOES include the page prefix if applicable. Any prefix will have forward slashes regardless of OS.

func (*Page) NameNE

func (p *Page) NameNE() string

NameNE returns the resolved page name with No Extension.

func (*Page) OSName

func (p *Page) OSName() string

OSName is like Name, except it uses the native path separator. It should be used for file operations only.

func (*Page) OSNameNE

func (p *Page) OSNameNE() string

OSNameNE is like NameNE, except it uses the native path separator. It should be used for file operations only.

func (*Page) Parse

func (p *Page) Parse() error

Parse opens the page file and attempts to parse it, returning any errors encountered.

func (*Page) Path

func (p *Page) Path() string

Path returns the absolute path to the page as resolved. If the path does not resolve, returns an empty string.

func (*Page) Prefix

func (p *Page) Prefix() string

Prefix returns the page prefix.

For example, for a page named a/b.page, this is a. For a page named a.page, this is an empty string.

func (*Page) Preview added in v0.2.0

func (p *Page) Preview() string

Preview returns a preview of the text on the page, up to 25 words or 150 characters. If the page has a Description, that is used instead of generating a preview.

func (*Page) Redirect

func (p *Page) Redirect() string

Redirect returns the location to which the page redirects, if any. This may be a relative or absolute URL, suitable for use in a Location header.

func (*Page) RelName

func (p *Page) RelName() string

RelName returns the unresolved page filename, with or without extension. This does NOT take symbolic links into account. It is not guaranteed to exist.

func (*Page) RelNameNE

func (p *Page) RelNameNE() string

RelNameNE returns the unresolved page name with No Extension, relative to the page directory option. This does NOT take symbolic links into account. It is not guaranteed to exist.

func (*Page) RelPath

func (p *Page) RelPath() string

RelPath returns the unresolved file path to the page. It may be a relative or absolute path. It is not guaranteed to exist.

func (*Page) SearchPath

func (p *Page) SearchPath() string

SearchPath returns the absolute path to the page search text file.

func (Page) Set

func (scope Page) Set(key string, value any) error

Set sets a value at the given key.

The key may be segmented to indicate properties of each object (e.g. person.name).

If attempting to write to a property of an object that does not support properties, such as a string, Set returns an error.

func (*Page) Text added in v0.2.0

func (p *Page) Text() string

Text generates and returns the rendered plain text for the page. The page must be parsed with Parse before attempting this method.

func (*Page) Title

func (p *Page) Title() string

Title returns the page title with HTML text formatting tags stripped.

func (*Page) TitleOrName

func (p *Page) TitleOrName() string

TitleOrName returns the result of Title if available, otherwise that of Name.

type PageInfo

type PageInfo struct {
	Path        string     `json:"-"`                   // absolute filepath
	File        string     `json:"file,omitempty"`      // name with extension, always with forward slashes
	FileNE      string     `json:"file_ne,omitempty"`   // name without extension, always with forward slashes
	Created     *time.Time `json:"created,omitempty"`   // creation time
	Modified    *time.Time `json:"modified,omitempty"`  // modify time
	Draft       bool       `json:"draft,omitempty"`     // true if page is marked as draft
	Generated   bool       `json:"generated,omitempty"` // true if page was generated from another source
	External    bool       `json:"external,omitempty"`  // true if page is outside the page directory
	Redirect    string     `json:"redirect,omitempty"`  // path page is to redirect to
	FmtTitle    HTML       `json:"fmt_title,omitempty"` // title with formatting tags
	Title       string     `json:"title,omitempty"`     // title without tags
	Author      string     `json:"author,omitempty"`    // author's name
	Description string     `json:"desc,omitempty"`      // description
	Keywords    []string   `json:"keywords,omitempty"`  // keywords
	Preview     string     `json:"preview,omitempty"`   // first 25 words or 150 chars. empty w/ description
	Warnings    []Warning  `json:"warnings,omitempty"`  // parser warnings
	Error       *Warning   `json:"error,omitempty"`     // parser error, as an encodable warning
}

PageInfo represents metadata associated with a page.

type PageOpt

type PageOpt struct {
	Name         string // wiki name
	MainPage     string // name of main page
	ErrorPage    string // name of error page
	Template     string // name of template
	MainRedirect bool   // redirect on main page rather than serve root
	Page         PageOptPage
	Host         PageOptHost
	Dir          PageOptDir
	Root         PageOptRoot
	Image        PageOptImage
	Category     PageOptCategory
	Search       PageOptSearch
	Link         PageOptLink
	External     map[string]PageOptExternal
	Navigation   []PageOptNavigation
}

PageOpt describes wiki/website options to a Page.

type PageOptCategory

type PageOptCategory struct {
	PerPage int
}

PageOptCategory describes wiki category options.

type PageOptCode added in v0.2.0

type PageOptCode struct {
	Lang  string
	Style string
}

PageOptCode describes options for `code{}` blocks.

type PageOptDir

type PageOptDir struct {
	Wiki     string // path to wiki root directory
	Image    string // Deprecated: path to image directory
	Category string // Deprecated: path to category directory
	Page     string // Deprecated: path to page directory
	Model    string // Deprecated: path to model directory
	Markdown string // Deprecated: path to markdown directory
	Cache    string // Deprecated: path to cache directory
}

PageOptDir describes actual filepaths to wiki resources.

type PageOptExternal

type PageOptExternal struct {
	Name string              // long name (e.g. Wikipedia)
	Root string              // wiki page root (no trailing slash)
	Type PageOptExternalType // wiki type
}

PageOptExternal describes an external wiki that we can use for link targets.

type PageOptExternalType

type PageOptExternalType string

PageOptExternalType describes

type PageOptHost

type PageOptHost struct {
	Wiki string // HTTP host for the wiki
}

PageOptHost describes HTTP hosts for a wiki.

type PageOptImage

type PageOptImage struct {
	Retina     []int
	SizeMethod string
	Calc       func(file string, width, height int, page *Page) (w, h int, fullSize bool)
	Sizer      func(file string, width, height int, page *Page) (path string)
}

PageOptImage describes wiki imaging options.

type PageOptLink struct {
	ParseInternal PageOptLinkFunction // internal page links
	ParseExternal PageOptLinkFunction // external wiki page links
	ParseCategory PageOptLinkFunction // category links
}

PageOptLink describes functions to assist with link targets.

type PageOptLinkFunction

type PageOptLinkFunction func(page *Page, opts *PageOptLinkOpts)

A PageOptLinkFunction sanitizes a link target.

type PageOptLinkOpts added in v0.2.0

type PageOptLinkOpts struct {
	Ok             *bool   // func sets to true if the link is valid
	Target         *string // func sets to overwrite the link target
	Tooltip        *string // func sets tooltip to display
	DisplayDefault *string // func sets default text to display (if no pipe)
	*FmtOpt                // formatter options available to func
}

PageOptLinkOpts contains options passed to a PageOptLinkFunction.

type PageOptNavigation

type PageOptNavigation struct {
	Link    string // link
	Display string // text to display
}

PageOptNavigation represents an ordered navigation item.

type PageOptPage

type PageOptPage struct {
	EnableTitle bool        // enable page title headings
	EnableCache bool        // enable page caching
	Code        PageOptCode // `code{}` block options
}

PageOptPage describes option relating to a page.

type PageOptRoot

type PageOptRoot struct {
	Wiki     string // wiki root path
	Image    string // image root path
	Category string // category root path
	Page     string // page root path
	File     string // file index path
}

PageOptRoot describes HTTP paths to wiki resources.

type PageOptSearch

type PageOptSearch struct {
	Enable bool
}

PageOptSearch describes wiki search options.

type ParserError added in v0.2.0

type ParserError struct {
	Pos Position
	Err error
}

ParserError represents an error in parsing with positional info.

func (*ParserError) Error added in v0.2.0

func (e *ParserError) Error() string

func (*ParserError) Unwrap added in v0.2.0

func (e *ParserError) Unwrap() error

type Position added in v0.2.0

type Position struct {
	Line, Column int
}

Position represents a line and column position within a quiki source file.

func (*Position) MarshalJSON added in v0.2.0

func (pos *Position) MarshalJSON() ([]byte, error)

MarshalJSON encodes the position to `[line, column]`.

func (Position) String added in v0.2.0

func (pos Position) String() string

String returns the position as `{line column}`.

func (*Position) UnmarshalJSON added in v0.2.0

func (pos *Position) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes the position from `[line, column]`.

type Warning added in v0.2.0

type Warning struct {
	Message string   `json:"message"`
	Pos     Position `json:"position"`
}

Warning represents a warning on a page.

Jump to

Keyboard shortcuts

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