gen

package
v0.0.0-...-588f2ff Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2016 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package gen is the modern day static site generator that focus on practical usage , performance , maintainability, and ability to extend.

Features

* Fast it has been designed for speed in mind. The implementation is well banchmarked.

* Robust plugin system. This allows you to write plugins in vanilla javascript. It avoids the pitfall of all Golang based static site generators by allowing uses to tap into the build process via javascript.

* Unparalled templating engine. Benefit with the power of go templates with the flexibility of writing template functions in javascript.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddRequire

func AddRequire(ctx *Context) error

AddRequire adds require function to the ctx VM. It also loads the modules that are shipped with the package( a.k.a builtin modules).

func Asset

func Asset(name string) ([]byte, error)

Asset loads and returns the asset for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetDir

func AssetDir(name string) ([]string, error)

AssetDir returns the file names below a certain directory embedded in the file by go-bindata. For example if you run go-bindata on data/... and data contains the following hierarchy:

data/
  foo.txt
  img/
    a.png
    b.png

then AssetDir("data") would return []string{"foo.txt", "img"} AssetDir("data/img") would return []string{"a.png", "b.png"} AssetDir("foo.txt") and AssetDir("notexist") would return an error AssetDir("") will return []string{"data"}.

func AssetInfo

func AssetInfo(name string) (os.FileInfo, error)

AssetInfo loads and returns the asset info for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetNames

func AssetNames() []string

AssetNames returns the names of the assets.

func Configure

func Configure(ctx *Context) error

Configure configures the ctx with default values. This make sure the ctx is in good shape to be passed around to any other stages.

It is safe to pass an eunintialized *Context, when you want to expect default behaviour.

passing unitialized *Context will make the current working directory as the context WOrkDir. This can have unintended effect so be warned and just make sure the context passed has WorkDir set if you inted to do something otherwise.

func EvaluateFile

func EvaluateFile(fs afero.Fs, vm *otto.Otto, path string) error

EvaluateFile opens the file in the specified path and evaluates it withing the context of the javascript runtine.

The evaluated javascript code can mutate the global state. Use execFile to execute the javascript without mutating the state of the generato'r javascript runtime.

func ExecPlan

func ExecPlan(ctx *Context, fl FileList, s *Plan) error

ExecPlan executes plan s for processing the FileList fi using the conetx ctx.

func Execute

func Execute(ctx *Context) error

Execute executes the plans that are outilend by the plans found in ctx.

Execution is done in two phases. First the files are evaluated using the matching plans. Plans are per file. Secondly the rendering of the site is done.

Evaluation of files is done concurrently, then the results are aggregated back together.

Rendering is done synchronously. As there is a lot of cross refences and stuffs to be considered, the rendering function is provided by the plan.

func Initilize

func Initilize(ctx *Context) error

Initilize initializes the build process. Any stages after this will have the generator already bootstraped.

It is possible to bootstrap the generator from the project( User's side) by providing an entry javascript file in the default path of scripts/init/index.js which will be executed and you can overide the default entry script which is evaluated internally

Initialzation is offloaded to the javascript runtine of the generator..Any error returned is a build error.

func MustAsset

func MustAsset(name string) []byte

MustAsset is like Asset but panics when Asset would return an error. It simplifies safe initialization of global variables.

func PlanExecution

func PlanExecution(ctx *Context) error

PlanExecution is the planning phase of the generator. First a project specific plan file is evaluated, the file is loacetd in /scripts_dir/plan_fir/index.js for example /scripts/plan/index.js. Second the existing system plan is merged with default plan.

TODO: Use user defined plan only when it is set, and use default plan only when there is no user defined plan.

This is executed to prepare the Plan object, which is the blueprint on how the whole execution is going to take place.

func RegisterBuiltins

func RegisterBuiltins(ctx *Context, r *Require) error

RegisterBuiltins adds important modules like underscore and fs. This relies on the assumption that the reuire function that is vailable in the ctx.VM instance is the one passed as r.

It caches the pre evaluated values for the modules to the reauire object.

func RestoreAsset

func RestoreAsset(dir, name string) error

RestoreAsset restores an asset under the given directory

func RestoreAssets

func RestoreAssets(dir, name string) error

RestoreAssets restores an asset under the given directory recursively

func ToValue

func ToValue(o interface{}) otto.Value

ToValue helper which convers o into otto.Value, ignoring any kind of error arising from the process.

Types

type Author

type Author struct {
	Name     string `json:"name"`
	Github   string `json:"github"`
	Twitter  string `json:"twitter"`
	Linkedin string `json:"linkedin"`
	Email    string `json:"email"`
	Website  string `json:"website"`
}

Author description of the author of the project being built.

type Boot

type Boot struct {
	ConfigiFile string            `json:"config_file"`
	PlanFile    string            `json:"plan_file"`
	ENV         map[string]string `json:"env"`
}

Boot necessary info to bootstrap the Generator.

type Config

type Config struct {
	Name         string   `json:"name" yaml:"name"`
	Source       string   `json:"source" yaml:"source"`
	Destination  string   `json:"destination" yaml:"destination"`
	StaticDir    string   `json:"statc_dir" yaml:"statc_dir"`
	TemplatesDir string   `json:"templates_dir" yaml:"templates_dir"`
	ThemeDir     string   `json:"theme_dir" yaml:"theme_dir"`
	DefaultTheme string   `json:"default_theme" yaml:"default_theme"`
	PluginDir    string   `json:"plugin_dir" yaml:"plugin_dir"`
	Safe         bool     `json:"safe" yaml:"safe"`
	Excluede     []string `json:"exclude" yaml:"exclude"`
	Include      []string `json:"include" yaml:"include"`
}

Config is the settings needed by the static generatot. It is inspired by the jekyll configuration options.

The format can either be json, yaml or toml TODO: add yaml and toml support.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig retruns *Config with default settings

type Context

type Context struct {
	FS      afero.Fs
	VM      *otto.Otto
	Sys     *System
	WorkDir string
	Verbose bool
	Job     *health.Job
	Out     io.Writer
}

Context is the context of a static website project.

type Export

type Export map[string]interface{}

Export helper for building javascript objects from Go objects.

func Markdown

func Markdown() Export

Markdown module for the otto runtime. This exposes one method exec, which accepts a File object as an argument. It renders the Contents of the file to markdown.

A HTML method is provided just in case you want to render a piece of text as markdown.

func (Export) Set

func (e Export) Set(key string, value interface{})

Set add a new key key, to the export object with value value.

func (Export) ToValue

func (e Export) ToValue(vm *otto.Otto) otto.Value

ToValue converts the export object to otto.Value

type File

type File struct {
	Name     string                 `json:"name"`
	Meta     map[string]interface{} `json:"meta"`
	Contents string                 `json:"contents"`
}

File is a representation of a file unit as it is passed arouund for processing. File content is passed as a string so as to allow easy trasition between Go and javascript boundary.

func ExecStrategy

func ExecStrategy(ctx *Context, filename string, s *Strategy) (*File, error)

ExecStrategy executes the stratedy using the given ctx. The endgame is a *File as we are only interested in processing the file named filename.

The strategy outlines how filename is processed.

type FileList

type FileList []*File

FileList is a list of files, which can be ordered based on various criterias

type Plan

type Plan struct {
	Title string `json:"title"`

	// Modules that are supposed to be loaded before the execution starts. The
	// execution process wont start if one of the dependencies is missing.
	Dependency []string `json:"dependencies"`

	TemplateEngine string      `json:"template_engine"`
	Strategies     []*Strategy `json:"strategies"`
	After          []string    `json:"after"`
}

Plan is the execution planner object. It states the steps and stages on which the execution process should take.

func (*Plan) FindStrategy

func (p *Plan) FindStrategy(filePath string) (*Strategy, error)

FindStrategy searches for a strategy matching the given filePath that is defined in the Plan.

TODO: Support multiple return strategies, since more than one strategy can be defined for a given file path.

type Require

type Require struct {
	Cache *cache2go.CacheTable
	Paths []string
	Fs    afero.Fs
}

Require implements a simple node.js like require mechanism. It loads jsavascript files from the source and exposes them as modules in the runtime.

This follows node.js convention by using the exports object to attach the functions or objects exposed by the module.

NOTE: cyclic dependencies are not taken care yet, so this will break in case of cyclic dependency.

func NewRequire

func NewRequire(fs afero.Fs, paths ...string) *Require

NewRequire returns an initialized Require object

func (*Require) Load

func (r *Require) Load(call otto.FunctionCall) otto.Value

Load loads module into the otto runtime.

type Stage

type Stage interface {
	Name() string
	Exec(*Context) error
}

Stage is an interface representing a generation step

func NewStage

func NewStage(name string, e func(*Context) error) Stage

NewStage returns struct that satisfies the Stage interface. The deails are hidden deilibarately as a way to quickly define stages based on functions of struct methods.

It is recommended to implement Stage interface explicity. Use this only when it is necessary.

type Strategy

type Strategy struct {
	Title     string   `json:"title"`
	Patterns  []string `json:"patterns"`
	FullMatch bool     `json:"fullMatch"`
	Before    []string `json:"before"`
	Exec      []string `json:"exec"`
	After     []string `json:"after"`
}

Strategy defines what should be done when a file matches a certain pattern. the pattern is glob like and matches only on the filenames

The FullMatch field specifies the pattern to be matchig the full file path. if set to false is means the pattern only matches the base of the file path( ignoring the directories).

type System

type System struct {
	Boot   *Boot   `json:"boot"`
	Config *Config `json:"config"`
	Plan   *Plan   `json:"plan"`
}

System configuration for the whole static generator system. The information consost of things necessary to bootup, configure and plan the execution of the static project..

type Template

type Template struct {
	*otto.Otto

	*template.Template
	// contains filtered or unexported fields
}

Template extends the *template.Template struct by supporting template functions defined in the javascript programming language. This depends heavily on the otto VM , and the functions are extracted from the otto Virtual Machine.

func (*Template) New

func (t *Template) New() *Template

New created a new *Template. The returned *Template supports template fuctions defined in javascript.

type Theme

type Theme struct {
	Name   string   `json:"name"`
	Author []Author `json:"author"`
}

Theme discreption of a theme.

Jump to

Keyboard shortcuts

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