A lightweight static blog generator written in Go.
Why another one? I wrote a blog post that briefly describes why I created it.
LitePub is a static blog generator that tries to be as easy to use as possible.
It requires no software dependencies, needs no configuration files, uses no databases. All it needs is one binary, posts written in Markdown and a set of templates to render the posts to static HTML files.
Posts don't have to include any special metadata (aka front matter) like title or date in them - the title, date and optional tags are parsed from the natural flow of the posts.
To create a sample blog follow these steps:
-
Download a release and unpack it to a directory.
-
cdto the directory. -
Create a sample blog:
./litepub create
-
Build the blog:
./litepub build Generating: index.html Generating: tags/reference.html Generating: tags/tutorial.html Generating: tags/advanced.html Generating: tags/docs.html Generating: tags/basics.html Generating: overview.html Generating: quick-start.html Generating: installation.html Generating: creating-a-blog.html Generating: creating-posts.html Generating: generating-html-files-for-a-blog-aka-building-a-blog.html Generating: serving-a-blog.html Generating: templates.html Generating: getting-help.html
-
Run the built-in server:
./litepub serve Running on http://localhost:2703 Ctrl+C to quit
-
Open http://localhost:2703 in your browser.
go install github.com/mirovarga/litepub@latestDownload a release and unpack it to a directory.
You can optionally add the directory to the
PATHso you can runlitepubfrom any directory. All examples assume you havelitepubin yourPATH.
The following will create a sample blog in the current directory:
litepub createIf you don't need the sample templates and posts use the --skeleton option:
litepub create --skeletonBecause the template files are required they will be still created but with no content.
Each blog is stored in a directory with the following structure:
posts/ # the posts
draft/ # the draft posts (they are ignored when building the blog)
templates/ # the templates and accompanying files (html, css, js, png, etc.)
layout.tmpl
index.tmpl
post.tmpl
tag.tmpl
www/ # the generated HTML files (plus copied accompanying files)Usage:
litepub create [<dir>] [-s, --skeleton] [-q, --quiet]
Arguments:
<dir> The directory to create the blog in or look for; it will be created if
it doesn't exist (only when creating a blog) [default: .]
Options:
-s, --skeleton Don't create sample posts and templates
-q, --quiet Show only errors
To create a post just add a Markdown
file in the posts directory. The file name and extension aren't important,
only the content of the file.
All posts need to be stored directly in the
postsdirectory. In other words, subdirectories in thepostsdirectory are ignored when looking for posts.
Each post looks like this (it's the start of an actual post from my blog):
1 # How I Switched from Java to JavaScript
2
3 *Jan 25, 2015*
4
5 *Java, JavaScript*
6
7 I know that there are lots of posts about why JavaScript, or more specifically
8 Node.js, is better than Java but nevertheless I wanted to contribute, too.
9 ...- Line
1is the post's title. If it starts with one or more#s they are stripped. So in this case the title becomes How I Switched from Java to JavaScript. - Line
3is the post's date. It has to be in the*MMM d, YYYY*format. - Line
5are comma separated post tags. - Anything below line
6is the content of the post.
The post's title and date are required. Tags are optional.
Any post can be marked as draft by simply moving it to the draft subdirectory
of the posts directory. To unmark it just move it back to the posts
directory.
Deleting a post is analogous to drafting: just remove it from the
postsdirectory.
To generate the HTML files for a blog cd to the blog's directory and use the
build command:
litepub build
Generating: index.html
Generating: tags/reference.html
Generating: tags/tutorial.html
Generating: tags/advanced.html
Generating: tags/docs.html
Generating: tags/basics.html
Generating: overview.html
Generating: quick-start.html
Generating: installation.html
Generating: creating-a-blog.html
Generating: creating-posts.html
Generating: generating-html-files-for-a-blog-aka-building-a-blog.html
Generating: serving-a-blog.html
Generating: templates.html
Generating: getting-help.htmlThe draft posts and posts starting with a dot (
.) are ignored when building a blog.
LitePub takes the *.tmpl files from the templates directory, applies them to
posts stored in the posts directory and generates the HTML files to the www
directory. It also copies all accompanying files (and directories) from
the templates directory to the www directory.
The generated HTML file names are created by slugifying the post title (or the tag name when generating tag pages) and adding the
htmlextension. For example, a post with the How I Switched from Java to JavaScript title is generated to thehow-i-switched-from-java-to-javascript.htmlfile.
Usage:
litepub build [<dir>] [-q, --quiet]
Arguments:
<dir> The directory to create the blog in or look for; it will be created if
it doesn't exist (only when creating a blog) [default: .]
Options:
-q, --quiet Show only errors
LitePub has a built-in server so you can see how a generated blog looks like in
a browser. cd to the blog's directory and start the server:
litepub serve
Running on http://localhost:2703
Ctrl+C to quitNow open http://localhost:2703 in your browser to see the generated blog.
When starting the server you can specify a port on which to listen with the
--port option:
litepub serve --port 3000
Running on http://localhost:3000
Ctrl+C to quitWhen creating templates or even writing posts it's quite useful to be able to
immediately see the changes after refreshing the page. To tell LitePub that it
should watch for changes to posts and templates use the --watch option:
litepub serve --watch
Running on http://localhost:2703
Rebuilding when posts or templates change
Ctrl+C to quitNote that subdirectories in the
postsandtemplatesdirectories aren't watched.
Sometimes it can be useful to rebuild a blog before serving it, for example when
you don't remember if you made any changes to posts or templates. To rebuild a
blog before serving use the --rebuild option:
litepub serve --rebuild
Generating: index.html
Generating: tags/reference.html
Generating: tags/tutorial.html
Generating: tags/advanced.html
Generating: tags/docs.html
Generating: tags/basics.html
Generating: overview.html
Generating: quick-start.html
Generating: installation.html
Generating: creating-a-blog.html
Generating: creating-posts.html
Generating: generating-html-files-for-a-blog-aka-building-a-blog.html
Generating: serving-a-blog.html
Generating: templates.html
Generating: getting-help.html
Running on http://localhost:2703
Ctrl+C to quitUsage:
litepub serve [<dir>] [-R, --rebuild] [-p, --port <port>] [-w, --watch] [-q, --quiet]
Arguments:
<dir> The directory to create the blog in or look for; it will be created if
it doesn't exist (only when creating a blog) [default: .]
Options:
-R, --rebuild Rebuild the blog before serving
-p, --port <port> The port to listen on [default: 2703]
-w, --watch Rebuild the blog when posts or templates change
-q, --quiet Show only errors
The create command adds sample templates to the templates directory. Of
course, you can change them or create your own from scratch.
LitePub uses the Go html/template package to define the templates.
Design changes require no knowledge of Go templates. However changes that affect what data is displayed will require it less or more (depending on the change).
There are four required files in the templates directory:
templates/ # the templates and accompanying files (html, css, js, png, etc.)
layout.tmpl
index.tmpl
post.tmpl
tag.tmpllayout.tmpldefines the common layout for the home page (index.tmpl), post pages (post.tmpl) and tag pages (tag.tmpl)index.tmplis used when generating the home page (index.html)post.tmplis used when generating post pages- and
tag.tmplis used when generating tag pages
Besides the four files there can be any number of html, css, js, png,
etc. files that are used by the .tmpl files.
If you're not familiar with Go templates, some things in the next sections can be unclear.
Templates have access to data they are meant to display. There are two types of
data: Posts and Tags.
A Post has the following properties:
Title- the post titleContent- the content of the post as Markdown textWritten- the post's dateTags- an array of tags the post is tagged with (can be empty)Draft-trueif the post is a draft
To get a post's page URL in a template use the
slugfunction (described below) like this:<a href="/{{.Title | slug}}.html">A Post</a>.
A Tag has the following properties:
Name- the tag namePosts- an array ofPosts that are tagged with the tag sorted byWrittenin descending order
To get a tag's page URL in a template use the
slugfunction (described below) like this:<a href="/tags/{{.Name | slug}}.html">A Tag</a>.
The index.tmpl template has access to an array of Posts sorted by Written
in descending order. The post.tmpl template has access to the Post it
displays. The tag.tmpl template has access to the Tag it displays.
The index.tmpl, post.tmpl and tag.tmpl templates have access to the
following functions:
Converts a Markdown string to a raw HTML, for example {{.Content | html}}.
Extracts the first paragraph of a Markdown string that isn't a header (doesn't
start with a #), for example {{.Content | summary | html}}.
Returns true if an integer is even, for example
{{if even $i}}<divclass="row">{{end}}.
Increments an integer by one, for example
{{if or (eq (inc $i) $l) (not (even $i))}}</div>{{end}}.
Slugifies a string, for example <a href="/{{.Title | slug}}.html">A Post</a>.
The available functions represent my needs when converting my handmade blog to a generated one.
To see all available commands and their options use the --help option:
litepub --help
LitePub 0.5.7 [github.com/mirovarga/litepub]
Copyright (c) 2024 Miro Varga [mirovarga.com]
Usage:
litepub create [<dir>] [-s, --skeleton] [-q, --quiet]
litepub build [<dir>] [-q, --quiet]
litepub serve [<dir>] [-R, --rebuild] [-p, --port <port>] [-w, --watch] [-q, --quiet]
Arguments:
<dir> The directory to create the blog in or look for; it will be created if
it doesn't exist (only when creating a blog) [default: .]
Options:
-s, --skeleton Don't create sample posts and templates
-R, --rebuild Rebuild the blog before serving
-p, --port <port> The port to listen on [default: 2703]
-w, --watch Rebuild the blog when posts or templates change
-q, --quiet Show only errors
-h, --help Show this screen
-v, --version Show version