ciigo

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2021 License: BSD-3-Clause Imports: 16 Imported by: 2

README

Welcome to ciigo

PkgGoDev

ciigo is a library and a program to write static web server with embedded files using asciidoc markup format.

ciigo as CLI

ciigo as CLI can convert, generate, and/or serve a directory that contains markup files, as HTML files.

Usage
$ ciigo [-template <file>] convert <dir>

Scan the "dir" recursively to find markup files (.adoc) and convert them into HTML files. The template "file" is optional, default to embedded HTML template.

$ ciigo [-template <file>] [-out <file>] generate <dir>

Convert all markup files inside directory "dir" recursively and then embed them into ".go" source file. The output file is optional, default to "ciigo_static.go" in current directory.

$ ciigo [-template <file>] [-address <ip:port>] serve <dir>

Serve all files inside directory "dir" using HTTP server, watch changes on markup files and convert them to HTML files automatically. If the address is not set, its default to ":8080".

ciigo as library

This section describe step by step instructions on how to build and create pages to be viewed for local development using ciigo.

First, clone the ciigo repository. Let says that we have cloned the ciigo repository into $HOME/go/src/git.sr.ht/~shulhan/ciigo.

Create new Go repository for building a website. For example, in directory $HOME/go/src/remote.tld/user/mysite. Replace "remote.tld/user/mysite" with your private or public repository.

$ mkdir -p $HOME/go/src/remote.tld/user/mysite
$ cd $HOME/go/src/remote.tld/user/mysite

Initialize the Go module,

$ go mod init remote.tld/user/mysite

Create directories for storing our content and a package binary.

$ mkdir -p cmd/mysite
$ mkdir -p _contents

Copy the example of stylesheet and HTML template from ciigo repository,

$ cp $HOME/go/src/git.sr.ht/~shulhan/ciigo/_example/index.css ./_contents/
$ cp $HOME/go/src/git.sr.ht/~shulhan/ciigo/_example/html.tmpl ./_contents/

Create the main Go code inside cmd/mysite,

package main

import (
	"git.sr.ht/~shulhan/ciigo"
	"github.com/shuLhan/share/lib/memfs"
)

var mysiteFS *memfs.MemFS

func main() {
	ciigo.Serve(mysiteFS, "./_contents", ":8080", "_contents/html.tmpl")
}

Create a new markup file index.adoc inside the "_contents" directory. Each directory, or sub directory, should have index.adoc to be able to accessed by browser,

=  Test

Hello, world!

Now run the ./cmd/mysite with DEBUG environment variable set to non-zero,

$ DEBUG=1 go run ./cmd/mysite

Any non zero value on DEBUG environment signal the running program to watch changes in ".adoc" files inside "_contents" directory and serve the generated HTML directly.

Open the web browser at localhost:8080 to view the generated HTML. You should see "Hello, world!" as the main page.

Thats it!

Create or update any ".adoc" files inside "_contents" directory, the program will automatically generated the HTML file. Refresh the web browser to load the new generated file.

Deployment

First, we need to make sure that all markup files inside "_contents" are converted to HTML and embed it into the static Go code.

Create another Go source code, lets save it in internal/generate.go with the following content,

package main

import (
	"git.sr.ht/~shulhan/ciigo"
)

func main() {
	opts := &ciigo.GenerateOptions{
		Root:           "./_contents",
		HTMLTemplate:   "_contents/html.tmpl",
		GenPackageName: "main",
		GenVarName:     "mysiteFS",
		GenGoFileName:  "cmd/mysite/static.go",
	}
	ciigo.Generate(opts)
}

And then run,

$ go run ./internal

The above command will generate Go source code cmd/mysite/static.go that embed all files inside the "_contents" directory.

Second, build the web server that serve static contents in static.go,

$ go build cmd/mysite

Third, test the web server by running the program and opening localhost:8080 on web browser,

$ ./mysite

Finally, deploy the program to your server.

NOTE: By default, server will listen on address 0.0.0.0 at port 8080. If you need to use another port, you can change it at cmd/mysite/main.go.

That's it!

Limitations and Known Bugs

ciigo will not handle automatic certificate (e.g. using LetsEncrypt), its up to the user how the certificate are gathered, generated, or served.

Using symlink on ".adoc" file inside content directory is not supported yet.

Resources

The source code for this software can be viewed at https://git.sr.ht/~shulhan/ciigo under custom BSD license.

Documentation

Overview

Package ciigo is a program to write static web server with embedded files using asciidoc and markdown markup languages.

For more information see the README file at the page repository https://sr.ht/~shulhan/ciigo.

Index

Constants

View Source
const (
	// DefaultRoot define default Root value for GenerateOptions.
	DefaultRoot = "."
)

Variables

This section is empty.

Functions

func Convert

func Convert(dir, htmlTemplate string)

Convert all markup files inside directory "dir" recursively into HTML files using "htmlTemplate" file as template. If htmlTemplate is empty it will default to use embedded HTML template. See template_index_html.go for template format.

func Generate

func Generate(opts *GenerateOptions)

Generate a static Go file to be used for building binary.

It will convert all markup files inside directory "dir" into HTML files, recursively; and read all the HTML files and files in "content/assets" and convert them into Go file in "out".

If htmlTemplate is empty it will default to use embedded HTML template. See template_index_html.go for template format.

func Serve

func Serve(mfs *memfs.MemFS, dir, address, htmlTemplate string)

Serve the content at directory "dir" using HTTP server at specific "address".

Types

type GenerateOptions added in v0.4.1

type GenerateOptions struct {
	// Root directory where its content will be embedded into Go source
	// code.
	// Default to DefaultRoot if its empty.
	Root string

	// HTMLTemplate the HTML template to be used when converting asciidoc
	// file into HTML.
	// If empty it will default to use embedded HTML template.
	// See template_index_html.go for template format.
	HTMLTemplate string

	// GenPackageName the name of package in Go generated source code.
	// Default to memfs.DefaultGenPackageName if its empty.
	GenPackageName string

	// GenVarName the name of variable where all files in Root will be
	// stored.
	// Default to memfs.DefaultGenVarName if its empty.
	GenVarName string

	// GenGoFileName the file name of Go source code will be written.
	// Default to memfs.DefaultGenGoFileName if its empty.
	GenGoFileName string
}

GenerateOptions define the options for calling Generate function.

Directories

Path Synopsis
cmd
ciigo
ciigo is a CLI to convert, generate, and/or serve a directory that contains markup files, as HTML files.
ciigo is a CLI to convert, generate, and/or serve a directory that contains markup files, as HTML files.
ciigo-example
Program ciigo-example provide an example on how to build a binary that include the static, generated .go file.
Program ciigo-example provide an example on how to build a binary that include the static, generated .go file.
internal

Jump to

Keyboard shortcuts

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