M ciigo_test.go => ciigo_test.go +59 -0
@@ 6,10 6,12 @@ package ciigo
import (
"html/template"
"os"
+ "path/filepath"
"regexp"
"sort"
"testing"
+ "git.sr.ht/~shulhan/pakakeh.go/lib/memfs"
"git.sr.ht/~shulhan/pakakeh.go/lib/test"
)
@@ 76,3 78,60 @@ func TestListFileMarkups(t *testing.T) {
test.Assert(t, `list`, c.exp, got)
}
}
+
+func TestGoEmbed(t *testing.T) {
+ type testCase struct {
+ tag string
+ opts EmbedOptions
+ }
+
+ var (
+ outDir = `testdata/goembed/out`
+
+ tdata *test.Data
+ err error
+ )
+
+ tdata, err = test.LoadData(`testdata/goembed/GoEmbed_test.txt`)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ var listCase = []testCase{{
+ opts: EmbedOptions{
+ ConvertOptions: ConvertOptions{
+ Root: `testdata/in/`,
+ },
+ EmbedOptions: memfs.EmbedOptions{
+ PackageName: `mypackage`,
+ VarName: `memfsIn`,
+ },
+ },
+ tag: `default`,
+ }}
+
+ var (
+ tcase testCase
+ fname string
+ fpath string
+ got []byte
+ )
+ for _, tcase = range listCase {
+ // Set the output file name based on tag.
+ fname = tcase.tag + `.go`
+ fpath = filepath.Join(outDir, fname)
+ tcase.opts.EmbedOptions.GoFileName = filepath.Join(outDir, fname)
+
+ err = GoEmbed(&tcase.opts)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ got, err = os.ReadFile(fpath)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ test.Assert(t, tcase.tag, string(tdata.Output[fname]), string(got))
+ }
+}
M cmd/ciigo/main.go => cmd/ciigo/main.go +17 -5
@@ 36,6 36,7 @@ import (
"strings"
"git.sr.ht/~shulhan/ciigo"
+ "git.sr.ht/~shulhan/pakakeh.go/lib/memfs"
)
const (
@@ 64,6 65,9 @@ func main() {
exclude = flag.String("exclude", "",
"a regex to exclude certain paths from being scanned during covert, embeded, watch, or serve")
+ var flagPackageName = flag.String(`package-name`, `main`, `package name for embed`)
+ var flagVarName = flag.String(`var-name`, `memFS`, `variable name for embed`)
+
flag.Parse()
var (
@@ 89,10 93,14 @@ func main() {
err = ciigo.Convert(&convertOpts)
case cmdEmbed:
- var embedOpts ciigo.EmbedOptions
-
- embedOpts.ConvertOptions = convertOpts
- embedOpts.EmbedOptions.GoFileName = *outputFile
+ var embedOpts = ciigo.EmbedOptions{
+ ConvertOptions: convertOpts,
+ EmbedOptions: memfs.EmbedOptions{
+ GoFileName: *outputFile,
+ PackageName: *flagPackageName,
+ VarName: *flagVarName,
+ },
+ }
err = ciigo.GoEmbed(&embedOpts)
@@ 139,12 147,16 @@ ciigo help
Print the usage (this output).
-ciigo [-template <file>] [-exclude <regex>] [-out <file>] embed <dir>
+ciigo [-template <file>] [-exclude <regex>] [-out <file>]
+ [-package-name <string>] [-var-name <string>]
+ embed <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.
+ The package name default to main.
+ The variable name default to memFS.
ciigo [-template <file>] [-exclude <regex>] [-address <ip:port>] serve <dir>
A testdata/goembed/GoEmbed_test.txt => testdata/goembed/GoEmbed_test.txt +136 -0
@@ 0,0 1,136 @@
+
+<<< default.go
+// Code generated by git.sr.ht/~shulhan/pakakeh.go/lib/memfs DO NOT EDIT.
+
+package mypackage
+
+import (
+ "git.sr.ht/~shulhan/pakakeh.go/lib/memfs"
+)
+
+func generate_testdata_in_() *memfs.Node {
+ var node = &memfs.Node{
+ SysPath: "testdata/in/",
+ Path: "/",
+ ContentType: "",
+ GenFuncName: "generate_testdata_in_",
+ }
+ node.SetMode(2147484141)
+ node.SetModTimeUnix(1712085827, 527471825)
+ node.SetName("/")
+ node.SetSize(0)
+ node.AddChild(_memfsIn_getNode(memfsIn, "/clu", generate_testdata_in_clu))
+ return node
+}
+
+func generate_testdata_in_clu() *memfs.Node {
+ var node = &memfs.Node{
+ SysPath: "testdata/in/clu",
+ Path: "/clu",
+ ContentType: "",
+ GenFuncName: "generate_testdata_in_clu",
+ }
+ node.SetMode(2147484141)
+ node.SetModTimeUnix(1712085827, 527471825)
+ node.SetName("clu")
+ node.SetSize(0)
+ node.AddChild(_memfsIn_getNode(memfsIn, "/clu/de", generate_testdata_in_clu_de))
+ return node
+}
+
+func generate_testdata_in_clu_de() *memfs.Node {
+ var node = &memfs.Node{
+ SysPath: "testdata/in/clu/de",
+ Path: "/clu/de",
+ ContentType: "",
+ GenFuncName: "generate_testdata_in_clu_de",
+ }
+ node.SetMode(2147484141)
+ node.SetModTimeUnix(1712769932, 568503131)
+ node.SetName("de")
+ node.SetSize(0)
+ node.AddChild(_memfsIn_getNode(memfsIn, "/clu/de/file.html", generate_testdata_in_clu_de_file_html))
+ node.AddChild(_memfsIn_getNode(memfsIn, "/clu/de/markdown.html", generate_testdata_in_clu_de_markdown_html))
+ return node
+}
+
+func generate_testdata_in_clu_de_file_html() *memfs.Node {
+ var node = &memfs.Node{
+ SysPath: "testdata/in/clu/de/file.html",
+ Path: "/clu/de/file.html",
+ ContentType: "text/html; charset=utf-8",
+ GenFuncName: "generate_testdata_in_clu_de_file_html",
+ Content: []byte("\x3C\x21\x44\x4F\x43\x54\x59\x50\x45\x20\x68\x74\x6D\x6C\x3E\x0A\x3C\x68\x74\x6D\x6C\x3E\x0A\x09\x3C\x68\x65\x61\x64\x3E\x0A\x09\x09\x3C\x6D\x65\x74\x61\x20\x68\x74\x74\x70\x2D\x65\x71\x75\x69\x76\x3D\x22\x43\x6F\x6E\x74\x65\x6E\x74\x2D\x54\x79\x70\x65\x22\x20\x63\x6F\x6E\x74\x65\x6E\x74\x3D\x22\x74\x65\x78\x74\x2F\x68\x74\x6D\x6C\x3B\x20\x63\x68\x61\x72\x73\x65\x74\x3D\x75\x74\x66\x2D\x38\x22\x3E\x0A\x09\x09\x3C\x6D\x65\x74\x61\x20\x6E\x61\x6D\x65\x3D\x22\x76\x69\x65\x77\x70\x6F\x72\x74\x22\x20\x63\x6F\x6E\x74\x65\x6E\x74\x3D\x22\x77\x69\x64\x74\x68\x3D\x64\x65\x76\x69\x63\x65\x2D\x77\x69\x64\x74\x68\x2C\x20\x69\x6E\x69\x74\x69\x61\x6C\x2D\x73\x63\x61\x6C\x65\x3D\x31\x22\x3E\x0A\x09\x09\x3C\x6D\x65\x74\x61\x20\x6E\x61\x6D\x65\x3D\x22\x74\x68\x65\x6D\x65\x2D\x63\x6F\x6C\x6F\x72\x22\x20\x63\x6F\x6E\x74\x65\x6E\x74\x3D\x22\x23\x33\x37\x35\x45\x41\x42\x22\x3E\x0A\x09\x09\x3C\x6D\x65\x74\x61\x20\x6E\x61\x6D\x65\x3D\x22\x67\x65\x6E\x65\x72\x61\x74\x6F\x72\x22\x20\x63\x6F\x6E\x74\x65\x6E\x74\x3D\x22\x61\x73\x63\x69\x69\x64\x6F\x63\x74\x6F\x72\x2D\x67\x6F\x20\x30\x2E\x35\x2E\x32\x22\x3E\x0A\x09\x09\x3C\x74\x69\x74\x6C\x65\x3E\x64\x75\x6D\x6D\x79\x3C\x2F\x74\x69\x74\x6C\x65\x3E\x0A\x09\x09\x3C\x73\x74\x79\x6C\x65\x3E\x0A\x09\x09\x62\x6F\x64\x79\x7B\x7D\x0A\x09\x09\x3C\x2F\x73\x74\x79\x6C\x65\x3E\x0A\x09\x3C\x2F\x68\x65\x61\x64\x3E\x0A\x09\x3C\x62\x6F\x64\x79\x3E\x0A\x09\x09\x3C\x64\x69\x76\x20\x63\x6C\x61\x73\x73\x3D\x22\x74\x6F\x70\x62\x61\x72\x22\x3E\x0A\x09\x09\x09\x3C\x64\x69\x76\x20\x63\x6C\x61\x73\x73\x3D\x22\x63\x6F\x6E\x74\x61\x69\x6E\x65\x72\x22\x3E\x0A\x09\x09\x09\x09\x3C\x64\x69\x76\x20\x63\x6C\x61\x73\x73\x3D\x22\x74\x6F\x70\x2D\x68\x65\x61\x64\x69\x6E\x67\x22\x3E\x0A\x09\x09\x09\x09\x09\x3C\x61\x20\x68\x72\x65\x66\x3D\x22\x2F\x22\x3E\x64\x75\x6D\x6D\x79\x3C\x2F\x61\x3E\x0A\x09\x09\x09\x09\x3C\x2F\x64\x69\x76\x3E\x0A\x09\x09\x09\x09\x3C\x64\x69\x76\x20\x63\x6C\x61\x73\x73\x3D\x22\x6D\x65\x6E\x75\x22\x3E\x0A\x09\x09\x09\x09\x09\x3C\x66\x6F\x72\x6D\x20\x63\x6C\x61\x73\x73\x3D\x22\x69\x74\x65\x6D\x22\x20\x61\x63\x74\x69\x6F\x6E\x3D\x22\x2F\x5F\x69\x6E\x74\x65\x72\x6E\x61\x6C\x2F\x73\x65\x61\x72\x63\x68\x22\x3E\x0A\x09\x09\x09\x09\x09\x09\x3C\x69\x6E\x70\x75\x74\x20\x74\x79\x70\x65\x3D\x22\x74\x65\x78\x74\x22\x20\x6E\x61\x6D\x65\x3D\x22\x71\x22\x20\x70\x6C\x61\x63\x65\x68\x6F\x6C\x64\x65\x72\x3D\x22\x53\x65\x61\x72\x63\x68\x22\x20\x2F\x3E\x0A\x09\x09\x09\x09\x09\x3C\x2F\x66\x6F\x72\x6D\x3E\x0A\x09\x09\x09\x09\x3C\x2F\x64\x69\x76\x3E\x0A\x09\x09\x09\x3C\x2F\x64\x69\x76\x3E\x0A\x09\x09\x3C\x2F\x64\x69\x76\x3E\x0A\x09\x09\x3C\x64\x69\x76\x20\x63\x6C\x61\x73\x73\x3D\x22\x70\x61\x67\x65\x22\x3E\x0A\x09\x09\x09\x3C\x64\x69\x76\x20\x63\x6C\x61\x73\x73\x3D\x22\x63\x6F\x6E\x74\x61\x69\x6E\x65\x72\x22\x3E\x0A\x3C\x64\x69\x76\x20\x69\x64\x3D\x22\x68\x65\x61\x64\x65\x72\x22\x3E\x0A\x3C\x68\x31\x3E\x64\x75\x6D\x6D\x79\x3C\x2F\x68\x31\x3E\x0A\x3C\x2F\x64\x69\x76\x3E\x0A\x3C\x64\x69\x76\x20\x69\x64\x3D\x22\x63\x6F\x6E\x74\x65\x6E\x74\x22\x3E\x0A\x3C\x2F\x64\x69\x76\x3E\x0A\x3C\x64\x69\x76\x20\x69\x64\x3D\x22\x66\x6F\x6F\x74\x65\x72\x22\x3E\x0A\x3C\x64\x69\x76\x20\x69\x64\x3D\x22\x66\x6F\x6F\x74\x65\x72\x2D\x74\x65\x78\x74\x22\x3E\x0A\x4C\x61\x73\x74\x20\x75\x70\x64\x61\x74\x65\x64\x20\x32\x30\x32\x34\x2D\x30\x34\x2D\x30\x33\x20\x30\x32\x3A\x32\x33\x3A\x34\x38\x20\x2B\x30\x37\x30\x30\x0A\x3C\x2F\x64\x69\x76\x3E\x0A\x3C\x2F\x64\x69\x76\x3E\x0A\x09\x09\x09\x3C\x2F\x64\x69\x76\x3E\x0A\x09\x09\x3C\x2F\x64\x69\x76\x3E\x0A\x09\x09\x3C\x64\x69\x76\x20\x63\x6C\x61\x73\x73\x3D\x22\x66\x6F\x6F\x74\x65\x72\x22\x3E\x0A\x09\x09\x09\x50\x6F\x77\x65\x72\x65\x64\x20\x62\x79\x20\x3C\x61\x0A\x09\x09\x09\x09\x68\x72\x65\x66\x3D\x22\x68\x74\x74\x70\x73\x3A\x2F\x2F\x67\x69\x74\x2E\x73\x72\x2E\x68\x74\x2F\x7E\x73\x68\x75\x6C\x68\x61\x6E\x2F\x63\x69\x69\x67\x6F\x22\x0A\x09\x09\x09\x3E\x0A\x09\x09\x09\x09\x63\x69\x69\x67\x6F\x0A\x09\x09\x09\x3C\x2F\x61\x3E\x0A\x09\x09\x3C\x2F\x64\x69\x76\x3E\x0A\x09\x3C\x2F\x62\x6F\x64\x79\x3E\x0A\x3C\x2F\x68\x74\x6D\x6C\x3E"),
+ }
+ node.SetMode(420)
+ node.SetModTimeUnix(1712770839, 81819250)
+ node.SetName("file.html")
+ node.SetSize(998)
+ return node
+}
+
+func generate_testdata_in_clu_de_markdown_html() *memfs.Node {
+ var node = &memfs.Node{
+ SysPath: "testdata/in/clu/de/markdown.html",
+ Path: "/clu/de/markdown.html",
+ ContentType: "text/html; charset=utf-8",
+ GenFuncName: "generate_testdata_in_clu_de_markdown_html",
+ Content: []byte("\x3C\x21\x44\x4F\x43\x54\x59\x50\x45\x20\x68\x74\x6D\x6C\x3E\x0A\x3C\x68\x74\x6D\x6C\x3E\x0A\x09\x3C\x68\x65\x61\x64\x3E\x0A\x09\x09\x3C\x6D\x65\x74\x61\x20\x68\x74\x74\x70\x2D\x65\x71\x75\x69\x76\x3D\x22\x43\x6F\x6E\x74\x65\x6E\x74\x2D\x54\x79\x70\x65\x22\x20\x63\x6F\x6E\x74\x65\x6E\x74\x3D\x22\x74\x65\x78\x74\x2F\x68\x74\x6D\x6C\x3B\x20\x63\x68\x61\x72\x73\x65\x74\x3D\x75\x74\x66\x2D\x38\x22\x3E\x0A\x09\x09\x3C\x6D\x65\x74\x61\x20\x6E\x61\x6D\x65\x3D\x22\x76\x69\x65\x77\x70\x6F\x72\x74\x22\x20\x63\x6F\x6E\x74\x65\x6E\x74\x3D\x22\x77\x69\x64\x74\x68\x3D\x64\x65\x76\x69\x63\x65\x2D\x77\x69\x64\x74\x68\x2C\x20\x69\x6E\x69\x74\x69\x61\x6C\x2D\x73\x63\x61\x6C\x65\x3D\x31\x22\x3E\x0A\x09\x09\x3C\x6D\x65\x74\x61\x20\x6E\x61\x6D\x65\x3D\x22\x74\x68\x65\x6D\x65\x2D\x63\x6F\x6C\x6F\x72\x22\x20\x63\x6F\x6E\x74\x65\x6E\x74\x3D\x22\x23\x33\x37\x35\x45\x41\x42\x22\x3E\x0A\x09\x09\x3C\x74\x69\x74\x6C\x65\x3E\x3C\x2F\x74\x69\x74\x6C\x65\x3E\x0A\x09\x09\x3C\x73\x74\x79\x6C\x65\x3E\x0A\x09\x09\x62\x6F\x64\x79\x7B\x7D\x0A\x09\x09\x3C\x2F\x73\x74\x79\x6C\x65\x3E\x0A\x09\x3C\x2F\x68\x65\x61\x64\x3E\x0A\x09\x3C\x62\x6F\x64\x79\x3E\x0A\x09\x09\x3C\x64\x69\x76\x20\x63\x6C\x61\x73\x73\x3D\x22\x74\x6F\x70\x62\x61\x72\x22\x3E\x0A\x09\x09\x09\x3C\x64\x69\x76\x20\x63\x6C\x61\x73\x73\x3D\x22\x63\x6F\x6E\x74\x61\x69\x6E\x65\x72\x22\x3E\x0A\x09\x09\x09\x09\x3C\x64\x69\x76\x20\x63\x6C\x61\x73\x73\x3D\x22\x74\x6F\x70\x2D\x68\x65\x61\x64\x69\x6E\x67\x22\x3E\x0A\x09\x09\x09\x09\x09\x3C\x61\x20\x68\x72\x65\x66\x3D\x22\x2F\x22\x3E\x3C\x2F\x61\x3E\x0A\x09\x09\x09\x09\x3C\x2F\x64\x69\x76\x3E\x0A\x09\x09\x09\x09\x3C\x64\x69\x76\x20\x63\x6C\x61\x73\x73\x3D\x22\x6D\x65\x6E\x75\x22\x3E\x0A\x09\x09\x09\x09\x09\x3C\x66\x6F\x72\x6D\x20\x63\x6C\x61\x73\x73\x3D\x22\x69\x74\x65\x6D\x22\x20\x61\x63\x74\x69\x6F\x6E\x3D\x22\x2F\x5F\x69\x6E\x74\x65\x72\x6E\x61\x6C\x2F\x73\x65\x61\x72\x63\x68\x22\x3E\x0A\x09\x09\x09\x09\x09\x09\x3C\x69\x6E\x70\x75\x74\x20\x74\x79\x70\x65\x3D\x22\x74\x65\x78\x74\x22\x20\x6E\x61\x6D\x65\x3D\x22\x71\x22\x20\x70\x6C\x61\x63\x65\x68\x6F\x6C\x64\x65\x72\x3D\x22\x53\x65\x61\x72\x63\x68\x22\x20\x2F\x3E\x0A\x09\x09\x09\x09\x09\x3C\x2F\x66\x6F\x72\x6D\x3E\x0A\x09\x09\x09\x09\x3C\x2F\x64\x69\x76\x3E\x0A\x09\x09\x09\x3C\x2F\x64\x69\x76\x3E\x0A\x09\x09\x3C\x2F\x64\x69\x76\x3E\x0A\x09\x09\x3C\x64\x69\x76\x20\x63\x6C\x61\x73\x73\x3D\x22\x70\x61\x67\x65\x22\x3E\x0A\x09\x09\x09\x3C\x64\x69\x76\x20\x63\x6C\x61\x73\x73\x3D\x22\x63\x6F\x6E\x74\x61\x69\x6E\x65\x72\x22\x3E\x0A\x3C\x68\x31\x3E\x6D\x61\x72\x6B\x64\x6F\x77\x6E\x3C\x2F\x68\x31\x3E\x0A\x0A\x09\x09\x09\x3C\x2F\x64\x69\x76\x3E\x0A\x09\x09\x3C\x2F\x64\x69\x76\x3E\x0A\x09\x09\x3C\x64\x69\x76\x20\x63\x6C\x61\x73\x73\x3D\x22\x66\x6F\x6F\x74\x65\x72\x22\x3E\x0A\x09\x09\x09\x50\x6F\x77\x65\x72\x65\x64\x20\x62\x79\x20\x3C\x61\x0A\x09\x09\x09\x09\x68\x72\x65\x66\x3D\x22\x68\x74\x74\x70\x73\x3A\x2F\x2F\x67\x69\x74\x2E\x73\x72\x2E\x68\x74\x2F\x7E\x73\x68\x75\x6C\x68\x61\x6E\x2F\x63\x69\x69\x67\x6F\x22\x0A\x09\x09\x09\x3E\x0A\x09\x09\x09\x09\x63\x69\x69\x67\x6F\x0A\x09\x09\x09\x3C\x2F\x61\x3E\x0A\x09\x09\x3C\x2F\x64\x69\x76\x3E\x0A\x09\x3C\x2F\x62\x6F\x64\x79\x3E\x0A\x3C\x2F\x68\x74\x6D\x6C\x3E"),
+ }
+ node.SetMode(420)
+ node.SetModTimeUnix(1712770839, 81819250)
+ node.SetName("markdown.html")
+ node.SetSize(790)
+ return node
+}
+
+// _memfsIn_getNode is internal function to minimize duplicate node
+// created on Node.AddChild() and on generatedPathNode.Set().
+func _memfsIn_getNode(mfs *memfs.MemFS, path string, fn func() *memfs.Node) (node *memfs.Node) {
+ node = mfs.PathNodes.Get(path)
+ if node != nil {
+ return node
+ }
+ return fn()
+}
+
+func init() {
+ memfsIn = &memfs.MemFS{
+ PathNodes: memfs.NewPathNode(),
+ Opts: &memfs.Options{
+ Root: "testdata/in/",
+ MaxFileSize: 5242880,
+ Includes: []string{
+ },
+ Excludes: []string{
+ `.*\.adoc$`,
+ `.*\.md$`,
+ `^\..*`,
+ },
+ Embed: memfs.EmbedOptions{
+ CommentHeader: ``,
+ PackageName: "mypackage",
+ VarName: "memfsIn",
+ GoFileName: "testdata/goembed/out/default.go",
+ WithoutModTime: false,
+ },
+ },
+ }
+ memfsIn.PathNodes.Set("/",
+ _memfsIn_getNode(memfsIn, "/", generate_testdata_in_))
+ memfsIn.PathNodes.Set("/clu",
+ _memfsIn_getNode(memfsIn, "/clu", generate_testdata_in_clu))
+ memfsIn.PathNodes.Set("/clu/de",
+ _memfsIn_getNode(memfsIn, "/clu/de", generate_testdata_in_clu_de))
+ memfsIn.PathNodes.Set("/clu/de/file.html",
+ _memfsIn_getNode(memfsIn, "/clu/de/file.html", generate_testdata_in_clu_de_file_html))
+ memfsIn.PathNodes.Set("/clu/de/markdown.html",
+ _memfsIn_getNode(memfsIn, "/clu/de/markdown.html", generate_testdata_in_clu_de_markdown_html))
+
+ memfsIn.Root = memfsIn.PathNodes.Get("/")
+
+ var err = memfsIn.Init()
+ if err != nil {
+ panic("memfsIn: " + err.Error())
+ }
+}
A testdata/goembed/out/.gitignore => testdata/goembed/out/.gitignore +2 -0
@@ 0,0 1,2 @@
+*
+!.gitignore