pagr

A 'static site generator', built using dati.
Log | Files | Refs | Atom

commit 694d6018c5f98e84e7a393fa5721b72a54228fcb
parent 8cf8567e14434f466421ff604798206d4b1e705e
Author: gearsix <gearsix@tuta.io>
Date:   Thu, 10 Jun 2021 19:09:01 +0100

started content_test.go; bugfixes, etc in content.go; removed
config_test.go#TestNewPage (not necessary).

Diffstat:
Mconfig_test.go | 14--------------
Mcontent.go | 61++++++++++++++++++++++++++++++++++++++++---------------------
Acontent_test.go | 122+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 162 insertions(+), 35 deletions(-)

diff --git a/config_test.go b/config_test.go @@ -6,20 +6,6 @@ import ( "testing" ) -func TestNewConfig(test *testing.T) { - test.Parallel() - cfg := NewConfig() - if cfg.Contents != "./content" { - test.Fatalf("invalid .Contents value: '%s'", cfg.Contents) - } - if cfg.Templates != "./templates" { - test.Fatalf("invalid .Templates value: '%s'", cfg.Templates) - } - if cfg.Output != "./out" { - test.Fatalf("invalid .Output value: %s", cfg) - } -} - func TestNewConfigFromFile(test *testing.T) { test.Parallel() tdir := test.TempDir() diff --git a/content.go b/content.go @@ -15,7 +15,7 @@ import ( goldmarkhtml "github.com/yuin/goldmark/renderer/html" ) -var SupportedContentFiletypes = []string{ +var ContentBodyExts = [5]string{ ".txt", // plain-text ".html", // HTML ".md", // commonmark with non-intrusive extensions: linkify, auto heading id, unsafe HTML @@ -34,12 +34,14 @@ func LoadContentDir(path string) (c Content, e error) { } if info.IsDir() { - p := NewPage(strings.TrimPrefix(fpath, path)) + path := strings.TrimPrefix(fpath, path) + p := NewPage(path) for _, dir := range strings.Split(fpath, "/") { if _, ok := defaults[dir]; ok { p.Meta.MergeMeta(defaults[dir], true) } } + pages[path] = p return nil } @@ -55,7 +57,7 @@ func LoadContentDir(path string) (c Content, e error) { } else if strings.Contains(fpath, ".defaults") { defaults[pdir] = m } - } else if ext := filepath.Ext(fpath); ext == ".txt" || ext == ".md" || ext == ".html" { + } else if isContentBodyExt(filepath.Ext(fpath)) > -1 { page.NewBodyFromFile(fpath) } else { page.Assets = append(page.Assets, strings.TrimPrefix(fpath, path)) @@ -72,6 +74,15 @@ func LoadContentDir(path string) (c Content, e error) { return c, e } +func isContentBodyExt(ext string) int { + for i, supported := range ContentBodyExts { + if ext == supported { + return i + } + } + return -1 +} + type Meta map[string]interface{} func (m Meta) MergeMeta(meta Meta, overwrite bool) { @@ -111,18 +122,22 @@ func (p *Page) NewBodyFromFile(fpath string) (err error) { } var body string - for _, lang := range SupportedContentFiletypes { + for _, lang := range ContentBodyExts { if filepath.Ext(fpath) == lang { switch (lang) { case ".txt": body = txt2html(bytes.NewReader(buf)) case ".md": - case ".gmd": + fallthrough + case ".gfm": + fallthrough case ".cm": markdown := getMarkdown(lang) var out bytes.Buffer if err = markdown.Convert(buf, &out); err == nil { body = out.String() + } else { + return err } case ".html": body = string(buf) @@ -133,7 +148,7 @@ func (p *Page) NewBodyFromFile(fpath string) (err error) { } if len(body) == 0 { - panic("passed invalid filetype to NewBodyFromFile") + panic("invalid filetype (" + filepath.Ext(fpath) + ") passed to NewBodyFromFile") } p.Body = append(p.Body, body) @@ -146,36 +161,39 @@ func (p *Page) NewBodyFromFile(fpath string) (err error) { // - If a text line is prefixed with a tab and no tag is open, it will open a <pre> tag // - Otherwise any line of text will open a <p> tag func txt2html(in io.Reader) (html string) { - var block int + var tag int const p = 1 - const pre = 2 + const pre = 2 fscan := bufio.NewScanner(in) for fscan.Scan() { line := fscan.Text() if len(strings.TrimSpace(line)) == 0 { - switch block { + switch tag { case p: html += "</p>\n" case pre: html += "</pre>\n" } - block = 0 - } else if block == 0 && line[0] == '\t' { - block = pre - html += "<pre>" + line + "\n" - } else if block == 0 { - block = p - html += "<p>" + line - } else if block == p { + tag = 0 + } else if tag == 0 && line[0] == '\t' { + tag = pre + html += "<pre>" + line[1:] + "\n" + } else if tag == 0 || (tag == pre && line[0] != '\t') { + if tag == pre { + html += "</pre>\n" + } + tag = p + html += "<p>" + line + } else if tag == p { html += " " + line - } else if block == pre { - html += line + "\n" + } else if tag == pre { + html += line[1:] + "\n" } } - if block == p { + if tag == p { html += "</p>" - } else if block == pre { + } else if tag == pre { html += "</pre>" } @@ -204,6 +222,7 @@ func getMarkdown(lang string) (markdown goldmark.Markdown) { case ".cm": markdown = goldmark.New() case ".md": + fallthrough default: markdown = goldmark.New( goldmark.WithExtensions( diff --git a/content_test.go b/content_test.go @@ -0,0 +1,122 @@ +package main + +import ( + "fmt" + "testing" + "os" + "strings" +) + +func TestLoadContentDir(t *testing.T) { + var err error + tdir := t.TempDir() + cdir := tdir + "/contents" + if err = createProjectContents(cdir); err != nil { + t.Errorf("failed to create project: %s", err) + } + + var c Content + if c, err = LoadContentDir(tdir); err != nil { + t.Fatalf("LoadContentDir failed: %s", err) + } + for _, p := range c { + fmt.Println("--------------") + fmt.Println("Path:", p.Path) + if p.Path != strings.TrimPrefix(p.Path, cdir) { + } + fmt.Println("Meta:", p.Meta) + for i, b := range p.Body { + fmt.Printf("Body%d: `%s`\n", i, b) + } + fmt.Println("Assets:", p.Assets) + } +} + +var contentBody = map[string]string{ + ".txt": `p1 +p2 + + pre1 + pre2 +p3 + +p4 +`, + ".html": `<p>p1<br> +p2</p> +<pre>pre1 +pre2 +</pre> +<p>p3</p> +<p>p4</p>`, + ".md": `p1 +p2 + + pre1 + pre2 + +p3 +`, + ".gfm": `p1 +p2 + + pre1 + pre2 + +p3`, + ".cm": `p1 +p2 + + pre1 + pre2 + +p3`, +} + +var contentAsset = []byte{ // 5x5 black png + 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, + 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, + 0x00, 0x05, 0x01, 0x03, 0x00, 0x00, 0x00, 0xb7, 0xa1, 0xb4, 0xa6, + 0x00, 0x00, 0x00, 0x06, 0x50, 0x4c, 0x54, 0x45, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0xa5, 0x67, 0xb9, 0xcf, 0x00, 0x00, 0x00, 0x02, + 0x74, 0x52, 0x4e, 0x53, 0xff, 0x00, 0xe5, 0xb7, 0x30, 0x4a, 0x00, + 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0e, 0xc4, + 0x00, 0x00, 0x0e, 0xc4, 0x01, 0x95, 0x2b, 0x0e, 0x1b, 0x00, 0x00, + 0x00, 0x10, 0x49, 0x44, 0x41, 0x54, 0x08, 0x99, 0x63, 0x60, 0x66, + 0x60, 0x66, 0x60, 0x00, 0x62, 0x76, 0x00, 0x00, 0x4a, 0x00, 0x11, + 0x3a, 0x34, 0x8c, 0xad, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, + 0x44, 0xae, 0x42, 0x60, 0x82, +} + +func createProjectContents(dir string) (err error) { + if err = os.Mkdir(dir, 0755); err != nil { + return + } + + var f *os.File + var path string + for l, lang := range ContentBodyExts { + if l > 0 { + path, err = os.MkdirTemp(dir, "page") + } else { + path = dir + } + f, err = os.Create(fmt.Sprintf("%s/body%d%s", path, l, lang)) + if err != nil { + return + } + f.WriteString(contentBody[lang]) + f.Close() + + if f, err = os.Create(fmt.Sprintf("%s/asset.png", path)); err != nil { + return + } + if _, err = f.Write(contentAsset); err != nil { + return + } + f.Close() + } + + return +} +