mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-10-30 01:13:08 +00:00
Remove built-in geminiToHTML formatter
This commit is contained in:
parent
a7e8568b1e
commit
749fe58ccb
16
config.go
16
config.go
|
@ -1,7 +1,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"text/template"
|
||||
|
@ -25,15 +24,11 @@ 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)
|
||||
}
|
||||
|
||||
// DefaultConfig returns the default configuration.
|
||||
func DefaultConfig() *Config {
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
|
33
format.go
33
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
|
||||
})
|
||||
}
|
||||
|
|
82
html.go
82
html.go
|
@ -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, "<ul>\n")
|
||||
}
|
||||
} else if h.list {
|
||||
h.list = false
|
||||
fmt.Fprint(h.out, "</ul>\n")
|
||||
}
|
||||
switch line := line.(type) {
|
||||
case gemini.LineLink:
|
||||
url := html.EscapeString(line.URL)
|
||||
name := html.EscapeString(line.Name)
|
||||
if name == "" {
|
||||
name = url
|
||||
}
|
||||
fmt.Fprintf(h.out, "<p><a href='%s'>%s</a></p>\n", url, name)
|
||||
case gemini.LinePreformattingToggle:
|
||||
h.pre = !h.pre
|
||||
if h.pre {
|
||||
fmt.Fprint(h.out, "<pre>\n")
|
||||
} else {
|
||||
fmt.Fprint(h.out, "</pre>\n")
|
||||
}
|
||||
case gemini.LinePreformattedText:
|
||||
fmt.Fprintf(h.out, "%s\n", html.EscapeString(string(line)))
|
||||
case gemini.LineHeading1:
|
||||
fmt.Fprintf(h.out, "<h1>%s</h1>\n", html.EscapeString(string(line)))
|
||||
case gemini.LineHeading2:
|
||||
fmt.Fprintf(h.out, "<h2>%s</h2>\n", html.EscapeString(string(line)))
|
||||
case gemini.LineHeading3:
|
||||
fmt.Fprintf(h.out, "<h3>%s</h3>\n", html.EscapeString(string(line)))
|
||||
case gemini.LineListItem:
|
||||
fmt.Fprintf(h.out, "<li>%s</li>\n", html.EscapeString(string(line)))
|
||||
case gemini.LineQuote:
|
||||
fmt.Fprintf(h.out, "<blockquote>%s</blockquote>\n", html.EscapeString(string(line)))
|
||||
case gemini.LineText:
|
||||
if line == "" {
|
||||
fmt.Fprint(h.out, "<br>\n")
|
||||
} else {
|
||||
fmt.Fprintf(h.out, "<p>%s</p>\n", html.EscapeString(string(line)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *HTMLWriter) Finish() {
|
||||
if h.pre {
|
||||
fmt.Fprint(h.out, "</pre>\n")
|
||||
}
|
||||
if h.list {
|
||||
fmt.Fprint(h.out, "</ul>\n")
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue