Categorygithub.com/ancientlore/whisper
modulepackage
0.4.12
Repository: https://github.com/ancientlore/whisper.git
Documentation: pkg.go.dev

# README

whisper

Go Reference

A web server mainly oriented at media serving for small websites.

whisper

Conventions

In general, whisper serves static content from the location it's found - making it easy to structure your site how you want. There is special handling for certain content like Markdown files.

See the example folder for a sample site layout. In general, whisper uses conventions instead of configuration files. Conventions used by this server include:

  • The template folder holds HTML templates, using Go's html/template package. These templates are used for rendering content but never served directly.
  • A sitemap.txt can be created as a template. See the example for details.
  • The default page for a folder is a Markdown file called index.md.
  • An optional whisper.cfg file holds settings should you want to preserve them.
  • Files 404.md and 500.md can be provided for custom errors.

Markdown

Web pages are generally written in Markdown and use HTML templates to render into the site. The default template to use is called default; you must have a default template and an image template. Templates are stored in the template folder.

NOTE: If no template folder is found, then default templates are loaded named default and image. You probably don't want these because they are extremely basic, but it's okay for just messing around and viewing Markdown locally.

Markdown may contain front matter which is in TOML format. The front matter is delimited by +++ at the start and end. For example:

+++
# This is my front matter
title = "My glorious page"
+++
# This is my Heading
This is my [Markdown](https://en.wikipedia.org/wiki/Markdown).

Front matter may include:

NameTypeDescription
titlestringTitle of page
datetimePublish date
tagsarray of stringsTags for the articles (not used yet)
templatestringOverride the template to render this file
redirectdurationProvide redirect info (not automated)
originalfilestringName of the base Markdown or image file

Front matter is used for sorting and constructing lists of articles.

Templates

whisper uses standard Go templates from the html/template package. Templates are passed the following data:

// FrontMatter holds data scraped from a Markdown page.
type FrontMatter struct {
    Title        string    `toml:"title"`        // Title of this page
    Date         time.Time `toml:"date"`         // Date the article appears
    Template     string    `toml:"template"`     // The name of the template to use
    Tags         []string  `toml:"tags"`         // Tags to assign to this article
    Redirect     string    `toml:"redirect"`     // Issue a redirect to another location
    OriginalFile string    `toml:"originalfile"` // The original file (markdown or image)
}

// PageInfo has information about the current page.
type PageInfo struct {
    Path     string // path from URL
    Filename string // end portion (file) from URL
}

// data is what is passed to markdown templates.
type data struct {
    FrontMatter FrontMatter   // front matter from Markdown file or defaults
    Page        PageInfo      // information aboout current page
    Content     template.HTML // rendered Markdown
}

Page is information about the current page, and FrontMatter is the front-matter from the current Markdown file. Content contains the HTML version of the Markdown file.

Functions are added to the template for your convenience.

FunctionDescription
dir(path string) []FileReturn the contents of the given folder, excluding special files and subfolders.
sortbyname([]File) []FileSort by name (reverse)
sortbytime([]File) []FileSort by time (reverse)
match(string, ...string) boolMatch string against file patterns
filter([]File, ...string) []FileFilter list against file patterns
join(parts ...string) stringThe same as path.Join
ext(path string) stringThe same as path.Ext
prev([]File, string) *FileFind the previous file based on Filename
next([]File, string) *FileFind the next file based on Filename
reverse([]File) []FileReverse the list
trimsuffix(string, string) stringThe same as strings.TrimSuffix
trimprefix(string, string) stringThe same as strings.TrimPrefix
trimspace(string) string The same as strings.TrimSpace
markdown(string) template.HTMLRender Markdown file into HTML
frontmatter(string) *FrontMatterRead front matter from file
now() time.TimeCurrent time

File is defined as:

// File holds data about a page endpoint.
type File struct {
    FrontMatter FrontMatter
    Filename    string
}

If File is not a Markdown file, then FrontMatter.Title is set to the file name and FrontMatter.Date is set to the modification time. The array is sorted by reverse date (most recent items first).

Note that FrontMatter.OriginalFile is very useful because, for image templates, it will hold the name of the image file. You probably want to use it in the template.

Image Templates

Folders named photos, images, pictures, cartoons, toons, sketches, artwork, or drawings use a special handler that can serve images using an HTML template called image.

Non-Goals

  • It's not a goal to make templates reusable. I expect templates need editing for new sites.
  • It's not a goal to automate creation of the menu.
  • It's not a goal to be a fully-featured server. I run Caddy in front of it.

# Packages

Package cachefs implements a read-only cache around a fs.FS, using groupcache.
Package virtual implements a "virtual" view over a fs.FS that makes it suitable for serving Markdown and other files in a web format.
Package web implements handlers and helpers for an application building a web server that uses the virtual package.