autosite

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

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

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

README

autosite

Autosite defines Google App Engine sites automatically based on file structure.

See docs at http://hkjn.me/autosite.

Documentation

Overview

Package autosite hosts websites automatically based on file structure.

See https://github.com/hkjn/hkjnweb for a setup (that implements http://www.hkjn.me / http://blog.hkjn.me) using this package.

Example usage:

mysite := New(
  "Some title",   // for HTML <head>
  "pages/*.tmpl", // pattern for pages on disk
  "domain.com",   // live domain
  []string{       // shared templates
    "base.tmpl",
    "other.tmpl",
  },
  func(r *http.Request) Logger {
    return appengine.NewContext(r)
  },
  !appengine.IsDevAppServer(),
  template.FuncMap{
    "live": func() bool { !appengine.IsDevAppServer() }
  },
)
mysite.Register()

This will host pages like domain.com/Foo and /Bar if there's files pages/Foo.tmpl and pages/Bar.tmpl relative to the calling package, also using "base.tmpl" and "other.tmpl" to compile the templates for rendering those pages.

The template functions specified are are available within templates, in addition to the built-in ones. In the example above, {{live}} could be used.

The following data is available within each template:

{{.Title}}: The <title> of the page.
{{.Date.Year}}, {{.Date.Month}}: Year and month that the page was
   published, if file pattern includes it.
{{.URI}}: URI to the page.

Support for blogs in autosite

Example usage:

myblog := NewBlog(
  "Some title",       // for HTML <head>
  "blog/*/*/*.tmpl",  // pattern for posts on disk
  "blogdomain.com",   // live domain
  []string{           // shared templates
    "base.tmpl",
    "blog.tmpl",
  },
  []string{           // templates for special handlers
    "base.tmpl",
    "listing.tmpl",
  },
)
myblog.Register()

This will host pages like blogdomain.com/2014/02/Foo and 2016/01/Bar if there's files blog/2014/02/Foo.tmpl and blog/2016/01/Bar.tmpl relative to the calling package, also using "base.tmpl" and "blog.tmpl" for all blog posts.

Special handlers for listing posts will be registered, using "base.tmpl" and "listing.tmpl":

  • the index (/) will show all posts from all years.
  • for each year YYYY that has at least one blog post, /YYYY will be registered, showing all posts from that year.
  • for each year + month YYYY/MM that have at least one blog post, /YYYY/MM will be registered, showing all posts from that month in that year.

Within the templates for these "blog listing" handlers, the following data is available in addition to the usual:

  • {{.Data.Posts}}: a slice of the posts for that unit of time (all time, one year, or one month, respectively)
  • {{.Data.TimeUnit}}: the unit of time itself ("April, 2014", "2014" or "all time", respectively)

Logging helpers

Index

Constants

This section is empty.

Variables

View Source
var BaseTemplate = "base" // name of top-level template to invoke for each page

Functions

This section is empty.

Types

type Glogger

type Glogger struct{}

Glogger implements Logger using package glog.

Note that Glogger should not be used on appengine, since attempting to write to disk causes a panic.

func (Glogger) Criticalf

func (Glogger) Criticalf(format string, args ...interface{})

Criticalf is like Debugf, but at Critical level.

func (Glogger) Debugf

func (Glogger) Debugf(format string, args ...interface{})

Debugf formats its arguments according to the format, analogous to fmt.Printf, and records the text as a log message at Debug level.

func (Glogger) Errorf

func (Glogger) Errorf(format string, args ...interface{})

Errorf is like Debugf, but at Error level.

func (Glogger) Infof

func (Glogger) Infof(format string, args ...interface{})

Infof is like Debugf, but at Info level.

func (Glogger) Warningf

func (Glogger) Warningf(format string, args ...interface{})

Warningf is like Debugf, but at Warning level.

type Logger

type Logger interface {
	// Debugf formats its arguments according to the format, analogous to fmt.Printf,
	// and records the text as a log message at Debug level.
	Debugf(format string, args ...interface{})

	// Infof is like Debugf, but at Info level.
	Infof(format string, args ...interface{})

	// Warningf is like Debugf, but at Warning level.
	Warningf(format string, args ...interface{})

	// Errorf is like Debugf, but at Error level.
	Errorf(format string, args ...interface{})

	// Criticalf is like Debugf, but at Critical level.
	Criticalf(format string, args ...interface{})
}

Logger specifies logging functions.

The methods are chosen to match the logging methods from appengine.Context, without needing to depend on appengine.

type LoggerFunc

type LoggerFunc func(*http.Request) Logger

LoggerFunc returns a logger from a http request.

type Site

type Site interface {
	Register()                           // Registers the HTTP handlers for the site.
	ChangeURI(oldURI, newURI string)     // Changes an URI.
	AddRedirect(uri, redirectURI string) // Adds a redirect URI.
}

Site represents a website.

func New

func New(title, glob, liveDomain string, templates []string, logger LoggerFunc, isLive bool, tmplFuncs template.FuncMap) Site

New creates a new autosite.

New panics on errors reading templates.

func NewBlog

func NewBlog(title, glob, live string, articleTmpls []string, listingTmpls []string, logger LoggerFunc, isLive bool, tmplFuncs template.FuncMap) Site

NewBlog creates a new blog.

NewBlog panics on errors reading templates. TODO(hkjn): Convert to ...Options() funcs.

Jump to

Keyboard shortcuts

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