mdgo

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2022 License: BSD-3-Clause Imports: 19 Imported by: 0

README

mdgo

PkgGoDev

mdgo is a library and a program to write static web server with embedded files using markdown markup language.

For working with asciidoc markup language with Go see ciigo.

mdgo as command

mdgo provide a command line interface (CLI) can convert, generate, and/or serve a directory that contains markup files, as HTML files.

Usage
$ mdgo [-template <file>] [-exclude <regex>] convert <dir>

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

$ mdgo [-template <file>] [-out <file>] [-exclude <regex>] 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 "mdgo_static.go" in current directory.

$ mdgo [-template <file>] [-address <ip:port>] [-exclude <regex>] 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".

mdgo as library

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

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

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 mdgo repository,

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

Create the main Go code inside cmd/mysite,

package main

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

var mysiteFS *memfs.MemFS

func main() {
	opts := &mdgo.ServeOptions{
		Mfs:          mysiteFS,
		Address:      ":8080",
		HtmlTemplate: "_contents/html.tmpl",
		Root:         "_contents",
	}
	err := mdgo.Serve(opts)
	if err != nil {
		log.Fatal(err)
	}
}

Create a new markup file index.md inside the "_contents" directory. Each directory, or sub directory, should have index.md 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 ".md" 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 ".md" 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/mdgo"
)

func main() {
	opts := &mdgo.GenerateOptions{
		Root:           "./_contents",
		HTMLTemplate:   "_contents/html.tmpl",
		GenPackageName: "main",
		GenVarName:     "mysiteFS",
		GenGoFileName:  "cmd/mysite/static.go",
	}
	mdgo.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

mdgo 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 ".md" file inside content directory is not supported yet.

Resources

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

Credits

This software use the following third party libraries,

  • github.com/yuin/goldmark. MIT License. Copyright (c) 2019 Yusuke Inuzuka
  • github.com/yuin/goldmark-meta. MIT License. Copyright (c) 2019 Yusuke Inuzuka

Documentation

Overview

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

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

Index

Constants

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

Variables

This section is empty.

Functions

func Convert

func Convert(opts *ConvertOptions) (err error)

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

func GoEmbed

func GoEmbed(opts *EmbedOptions) (err error)

GoEmbed generate a static Go file that embed all files inside Root except the one that being excluded explicitly by ConvertOptions Exclude.

It convert all markup files inside directory "dir" into HTML files, recursively, and then embed them into Go file defined by EmbedOptions.GoFileName.

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

func Serve

func Serve(opts *ServeOptions) (err error)

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

func Watch

func Watch(opts *ConvertOptions) (err error)

Watch any changes on markup files on directory Root recursively and changes on the HTML template file. If there is new or modified markup files it will convert them into HTML files using HTML template automatically.

If the HTML template file modified, it will re-convert all markup files. If the HTML template file deleted, it will replace them with internal, default HTML template.

Types

type ConvertOptions

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

	// Exclude define regular expresion to exclude certain paths from
	// being scanned.
	Exclude string

	// HtmlTemplate the HTML template to be used when converting markup
	// file into HTML.
	// If empty it will default to use embedded HTML template.
	// See template_index_html.go for template format.
	HtmlTemplate string
	// contains filtered or unexported fields
}

ConvertOptions define the options to use on Convert function.

type EmbedOptions

type EmbedOptions struct {
	ConvertOptions

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

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

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

EmbedOptions define the options for calling GoEmbed function.

type ServeOptions

type ServeOptions struct {
	ConvertOptions

	// Mfs contains pointer to variable generated from Generate.
	// This option is used to use embedded files for serving on HTTP.
	Mfs *memfs.MemFS

	// Address to listen and serve for HTTP request.
	Address string
}

ServeOptions contains the options to use on Serve function.

Directories

Path Synopsis
cmd
mdgo
mdgo is a CLI to convert, embed, and/or serve a directory that contains markup files, as HTML files.
mdgo is a CLI to convert, embed, and/or serve a directory that contains markup files, as HTML files.
mdgo-example
Program mdgo-example provide an example on how to build a binary that include the static, generated .go file.
Program mdgo-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