Documentation
¶
Overview ¶
Package zas implements the Zas static site generator: it loads configuration and translations, walks a site's source tree, and renders Markdown and HTML pages through a shared layout.
Index ¶
- Constants
- Variables
- func NewI18n(mainlang string) (strs i18n.Strings, err error)
- type ConfigSection
- func (cs ConfigSection) GetBool(key string) (value bool)
- func (cs ConfigSection) GetBoolOK(key string) (value, ok bool)
- func (cs ConfigSection) GetSection(key string) (value ConfigSection)
- func (cs ConfigSection) GetSectionOK(key string) (value ConfigSection, ok bool)
- func (cs ConfigSection) GetString(key string) (value string)
- func (cs ConfigSection) GetStringOK(key string) (value string, ok bool)
- func (cs ConfigSection) GetStringSlice(key string) (value []string)
- func (cs ConfigSection) GetStringSliceOK(key string) (value []string, ok bool)
- func (cs ConfigSection) GetZString(key string) string
- func (cs ConfigSection) GetZStringSlice(key string) []string
- type Generator
- type Init
- type Subcommand
- type ZasData
- func (zd *ZasData) E(s string, a ...interface{}) (t string, err error)
- func (zd *ZasData) Extra(keypath string) (value string, err error)
- func (zd *ZasData) H(s string, a ...interface{}) (h thtml.HTML, err error)
- func (zd *ZasData) IsHome() (bool, error)
- func (zd *ZasData) Language() (string, error)
- func (zd *ZasData) Resolve(id string) (string, error)
- func (zd *ZasData) Title() (title string)
- func (zd *ZasData) URL() string
- type ZasSiteData
Constants ¶
const ( DefaultDirPerm os.FileMode = 0o755 DefaultFilePerm os.FileMode = 0o644 )
DefaultDirPerm and DefaultFilePerm are the permissions zas uses when creating directories and files of its own: the .zas directory, deploy output, generated pages, and the scaffolded config.yml.
const Dir = "." + Name
Dir is the name of the per-site zas directory (".zas") holding a site's config, i18n strings, layout template and deploy output.
const DirConfigFile = "." + Name + ".yml"
DirConfigFile is the name of the optional per-directory override file (".zas.yml") zas looks for while walking a site's directory tree.
const DisplayName = "Zas"
DisplayName is the Title-cased, human-readable form of Name, as printed in the CLI's help and version output. It is a hardcoded literal rather than computed from Name via cases.Title at init time: Name never changes, so title-casing it can never produce anything other than "Zas", and hardcoding it drops this file's runtime dependency on golang.org/x/text/cases and golang.org/x/text/language.
const Name = "zas"
Name is the application/binary name.
const PluginPrefix = "zs"
PluginPrefix is prepended to a mimetype's Title-cased name to build the external plugin command dispatched for it (see generate.go), e.g. the "markdown" mimetype's "Markdown" title becomes the "zsMarkdown" command.
Variables ¶
var ( ConfigFile = filepath.Join(Dir, "config.yml") I18nFile = filepath.Join(Dir, "i18n.yml") LayoutFile = filepath.Join(Dir, "layout.html") )
ConfigFile, I18nFile and LayoutFile are a site's config, i18n and default layout file paths, relative to its root. They can't be Go constants because filepath.Join isn't a constant expression - but building them with filepath.Join, rather than hand-concatenating with "/", is what keeps them correct on Windows, where the OS path separator is "\\".
Functions ¶
Types ¶
type ConfigSection ¶
type ConfigSection map[string]interface{}
ConfigSection is a section of zas configuration: a string-keyed map, as produced by decoding a YAML mapping and by every ConfigSection{...} literal in this package. go.yaml.in/yaml/v3 special-cases a named map type whose keys are strings and whose values are interface{}: once it decodes the outermost mapping into a ConfigSection, it keeps using that same named type for every nested mapping found inside an interface{} value, at any depth (see stringMapType propagation in its decoder) - so a config.yml with several levels of nested sections decodes into nested ConfigSection values all the way down, with no custom UnmarshalYAML method required. (That propagation requires every key in the mapping to be a string; GetSection's map[string]interface{} fallback below covers a section that arrives some other way.)
func DefaultConfig ¶ added in v0.0.7
func DefaultConfig() ConfigSection
DefaultConfig returns a deep copy of the built-in default configuration, safe for callers to read or even mutate without affecting subsequent calls or any other code in the process. It reuses the same mergo-based technique NewConfig uses in init.go rather than a hand-rolled deep-copy helper: pre-seeding a fresh, empty map for every nested section gives mergo.Merge's own recursive merge somewhere to copy each value into that nothing else references.
func NewConfig ¶
func NewConfig() (ConfigSection, error)
NewConfig loads ConfigFile (as defined in constants.go). It must be a YAML file.
func (ConfigSection) GetBool ¶ added in v0.0.8
func (cs ConfigSection) GetBool(key string) (value bool)
GetBool returns a bool value from current section, or false if key is missing or not a bool. Callers that need to tell "absent"/"wrong type" apart from a legitimately false value should use GetBoolOK instead.
func (ConfigSection) GetBoolOK ¶ added in v0.0.8
func (cs ConfigSection) GetBoolOK(key string) (value, ok bool)
GetBoolOK returns a bool value from current section, and whether key was present and held a bool value. ok is false both when key is absent and when it holds a non-bool value.
func (ConfigSection) GetSection ¶
func (cs ConfigSection) GetSection(key string) (value ConfigSection)
GetSection returns a subsection from current section, or nil if key is missing or not a section.
func (ConfigSection) GetSectionOK ¶ added in v0.0.7
func (cs ConfigSection) GetSectionOK(key string) (value ConfigSection, ok bool)
GetSectionOK returns a subsection from current section, and whether key resolved to a real section.
func (ConfigSection) GetString ¶
func (cs ConfigSection) GetString(key string) (value string)
GetString returns a string value from current section, or "" if key is missing or not a string. Callers that need to tell "absent"/"wrong type" apart from a legitimately empty string should use GetStringOK instead.
func (ConfigSection) GetStringOK ¶ added in v0.0.7
func (cs ConfigSection) GetStringOK(key string) (value string, ok bool)
GetStringOK returns a string value from current section, and whether key was present and held a string value. ok is false both when key is absent and when it holds a non-string value; callers that need to tell those two cases apart should check key's presence themselves beforehand.
func (ConfigSection) GetStringSlice ¶ added in v0.0.7
func (cs ConfigSection) GetStringSlice(key string) (value []string)
GetStringSlice returns a string-slice value from current section, or nil if key is missing, not a sequence, or contains any non-string element.
func (ConfigSection) GetStringSliceOK ¶ added in v0.0.7
func (cs ConfigSection) GetStringSliceOK(key string) (value []string, ok bool)
GetStringSliceOK returns a string-slice value from current section, and whether key was present and held a sequence of strings. ok is false when key is absent, holds a non-sequence value, or holds a sequence with any non-string element - go.yaml.in/yaml/v3 decodes a YAML sequence into []interface{}, the same way it decodes a mapping into ConfigSection (see that type's doc comment above), so each element still needs its own type assertion.
func (ConfigSection) GetZString ¶
func (cs ConfigSection) GetZString(key string) string
GetZString returns a string value from default Zas section.
func (ConfigSection) GetZStringSlice ¶ added in v0.0.7
func (cs ConfigSection) GetZStringSlice(key string) []string
GetZStringSlice returns a string-slice value from the default Zas section.
type Generator ¶
type Generator struct {
// Verbose output.
Verbose bool
// Full generation (non-incremental mode).
Full bool
// NoPlugins disables every content-triggered plugin execution: an
// <embed> resolving to an external MIME-type plugin
// (handleMIMETypePlugin) and a <script type="application/zas+name">
// tag that would exec zs<name> (handleScriptPlugin) each fail with a
// clear per-page error instead of exec'ing anything. It has no effect
// on zas's own internal embed handlers (Markdown, Plain, Html), which
// never spawn a process, or on the argv dispatch in cmd/zas, which is
// chosen by a name the invoking user typed. Note the zs<name> binaries
// themselves are no longer argv-only reachable - script-tag plugins let
// page content name one directly, which is exactly what this flag
// shuts off. Set this when generating from content you don't fully
// control (see README's "Plugins" section for the full trust model
// this guards).
NoPlugins bool
// Config holds the site configuration. Run always overwrites it with
// the freshly loaded contents of ConfigFile, so setting it before
// calling Run has no effect on Run itself; it's only meaningful when
// calling Generator's other methods directly without going through
// Run.
Config ConfigSection
// Default layout from Config[Name]["layout"].
Layout *thtml.Template
// i18n helper.
I18n *i18n.Build
// contains filtered or unexported fields
}
Generator groups relevant rendering info. Like sync.WaitGroup, which it embeds, a Generator must not be copied after it has been constructed - use NewGenerator, or a Generator{} literal, and refer to it thereafter only by pointer.
func NewGenerator ¶ added in v0.0.7
NewGenerator returns a Generator ready to Run with the given verbosity, full-generation, and plugin-execution settings.
func (*Generator) BuildDeployPath ¶
BuildDeployPath builds deployment path for specific file pointed by path.
func (*Generator) Generate ¶
Generate renders and writes the file at data.Path using the given template context.
func (*Generator) GetDeployPath ¶
GetDeployPath returns deployment base path in config.
type Init ¶
type Init struct {
// Force, when true, makes Run overwrite an existing ConfigFile or
// LayoutFile with its scaffolded default. When false (the default),
// Run leaves either file alone if it already exists, so re-running
// "zas init" on a site never silently discards hand-edited content.
Force bool
}
Init implements the "init" subcommand, which scaffolds a new Zas repository in the current directory.
type Subcommand ¶
type Subcommand struct {
// Run runs the subcommand. It takes no arguments - flags are package
// globals wired up in init() - and returns an error instead of
// panicking or exiting directly.
Run func() error
// UsageLine is the one-line usage message.
// The first word in the line is taken to be the subcommand name.
UsageLine string
// Name is the name of the subcommand.
Name string
// Flag is a set of flags specific to this command.
Flag flag.FlagSet
}
Subcommand is a Zas internal subcommand, inspired by the go command.
func NewSubcommand ¶
func NewSubcommand(usageLine string, run func() error) *Subcommand
NewSubcommand builds a Subcommand from a usage line and its run function.
type ZasData ¶
type ZasData struct {
// Template used as body from current file.
//
// Body is only ever populated after this page's own template has
// finished executing - render derives it from that very execution's
// HTML5-parsed output (see render in generate.go) - so {{.Body}}
// always evaluates empty when used from inside the page's own body
// content, unlike in the layout template, which only runs later, once
// Body is already populated (see Generate in generate.go). This is an
// inherent limitation, not a bug: giving a page's own body a preview
// of its own final rendered self would need a multi-pass rendering
// model. Contrast with Page and FirstTitle below, which render does
// give the page's own body a best-effort preview of, extracted from
// the page's raw source ahead of its own template execution (see
// earlyPageConfig and leadingH1Text in generate.go) precisely because
// neither is circularly defined in terms of that same execution.
Body thtml.HTML
// Current path (usable in URLs).
Path string
// Title from first level header (H1).
//
// Inside the page's own body content (as opposed to the layout),
// FirstTitle holds a best-effort preview extracted from the page's raw
// source before its own template executes - see leadingH1Text in
// generate.go - rather than the canonical value render later derives
// from the fully rendered, HTML5-parsed document. The two normally
// agree; the preview is left unset only when no leading <h1> is found,
// or when one is found but its content still contains unexecuted
// template syntax ("{{"), to avoid exposing placeholder text as if it
// were a real title.
FirstTitle string
// Site configuration, as found in ConfigFile.
Site ZasSiteData
// In-page configuration, from first HTML comment (expected as YAML map).
Page map[interface{}]interface{}
// Current directory configuration, from DirConfigFile.
Directory ConfigSection
// contains filtered or unexported fields
}
ZasData is the context data store used in templates.
func NewZasData ¶
NewZasData builds a ZasData for the page at srcPath.
func (*ZasData) E ¶
E translates s for the page's resolved language, falling back to "**s**" when no translation is found.
func (*ZasData) Extra ¶
Extra is a helper template method to get any value from ZasData.config using paths. It errors both when a path segment isn't itself a section and when the final key is missing or isn't a string - the two ways a keypath can fail to resolve to a real value.
func (*ZasData) Resolve ¶
Resolve resolves id from Page, then Directory, then site-wide Extra config, in that order. It returns an error only when id is present in Page or Directory but isn't a string (e.g. a page-config typo like "language:" with no value, or a numeric value) — a genuinely absent key still falls back to Extra's own lenient "" default.
func (*ZasData) Title ¶
Title returns the current title, from page's config and first level header (H1), in this order.
type ZasSiteData ¶
ZasSiteData is the site configuration.
They are required fields in order to complete social/semantic meta tags.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
zas
command
* Copyright (c) 2013 Dario Castañé.
|
* Copyright (c) 2013 Dario Castañé. |
|
internal
|
|
|
i18n
Package i18n is a vendored, trimmed copy of github.com/melvinmt/gt v1.0.1 (unmaintained upstream since 2013).
|
Package i18n is a vendored, trimmed copy of github.com/melvinmt/gt v1.0.1 (unmaintained upstream since 2013). |
