diff --git a/config.go b/config.go index 82372d5..50ce170 100644 --- a/config.go +++ b/config.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "os" "path" "text/template" @@ -25,14 +24,10 @@ type Task struct { Template string `toml:"template"` // template file extension PostProcess string `toml:"postprocess"` // postprocess directive Destination string `toml:"destination"` // destination directory - postProcess Format } func (t Task) Format(p *Page) (string, []byte) { - if t.postProcess == nil { - return path.Join(p.Path, "index"+t.Output), []byte(p.Content) - } - return t.postProcess.Format(p) + return path.Join(p.Path, "index"+t.Output), []byte(p.Content) } // DefaultConfig returns the default configuration. @@ -62,17 +57,6 @@ func LoadConfig(path string) (*Config, error) { return nil, err } - for _, task := range c.Tasks { - switch task.PostProcess { - case "": - continue - case "geminiToHTML": - task.postProcess = GeminiToHTML(c) - default: - return nil, fmt.Errorf("unrecognized postprocess directive %q", task.PostProcess) - } - } - return c, nil } diff --git a/format.go b/format.go index 44afc4a..f2dab49 100644 --- a/format.go +++ b/format.go @@ -1,11 +1,5 @@ package main -import ( - "bytes" - pathpkg "path" - "strings" -) - // Format represents an output format. type Format interface { Format(*Page) (path string, content []byte) @@ -16,30 +10,3 @@ type FormatFunc func(*Page) (string, []byte) func (f FormatFunc) Format(p *Page) (string, []byte) { return f(p) } - -// GeminiToHTML returns an output format that converts Gemini text to HTML. -func GeminiToHTML(cfg *Config) Format { - return FormatFunc(func(p *Page) (path string, content []byte) { - path = pathpkg.Join(p.Path, "index.html") - - r := strings.NewReader(p.Content) - content = textToHTML(r) - - // html template context - type htmlCtx struct { - Title string // page title - Content string // page HTML contents - } - - var b bytes.Buffer - // clean path to remove trailing slash - dir := pathpkg.Dir(pathpkg.Clean(p.Path)) - tmpl := cfg.Templates.FindTemplate(dir, "output.html") - tmpl.Execute(&b, &htmlCtx{ - Title: p.Title, - Content: string(content), - }) - content = b.Bytes() - return - }) -} diff --git a/html.go b/html.go deleted file mode 100644 index 72cc1e6..0000000 --- a/html.go +++ /dev/null @@ -1,82 +0,0 @@ -package main - -import ( - "bytes" - "fmt" - "html" - "io" - - "git.sr.ht/~adnano/go-gemini" -) - -// textToHTML returns the Gemini text response as HTML. -func textToHTML(r io.Reader) []byte { - buf := &bytes.Buffer{} - hw := &HTMLWriter{ - out: buf, - } - defer hw.Finish() - gemini.ParseLines(r, hw.Handle) - return buf.Bytes() -} - -type HTMLWriter struct { - out io.Writer - pre bool - list bool -} - -func (h *HTMLWriter) Handle(line gemini.Line) { - if _, ok := line.(gemini.LineListItem); ok { - if !h.list { - h.list = true - fmt.Fprint(h.out, "
\n") - } else { - fmt.Fprint(h.out, "\n") - } - case gemini.LinePreformattedText: - fmt.Fprintf(h.out, "%s\n", html.EscapeString(string(line))) - case gemini.LineHeading1: - fmt.Fprintf(h.out, "
%s\n", html.EscapeString(string(line))) - case gemini.LineText: - if line == "" { - fmt.Fprint(h.out, "
%s
\n", html.EscapeString(string(line))) - } - } -} - -func (h *HTMLWriter) Finish() { - if h.pre { - fmt.Fprint(h.out, "\n") - } - if h.list { - fmt.Fprint(h.out, "\n") - } -}