include a CSS class in htmlconv output

This commit is contained in:
tjpcc 2023-04-29 13:45:38 -06:00
parent 46ad450327
commit aa6bdb0649
2 changed files with 20 additions and 12 deletions

View File

@ -56,10 +56,17 @@ func Convert(wr io.Writer, doc gemtext.Document, overrides *template.Template) e
}
var baseTmpl = template.Must(template.New("htmlconv").Parse(`
{{ define "header" }}<html><body>{{ end }}
{{ define "textline" }}{{ if ne .String "\n" }}<p>{{ . }}</p>{{ end }}{{ end }}
{{ define "header" -}}
<!DOCTYPE html>
<html><body class="gemtext">{{ end }}
{{ define "textline" -}}
{{ if ne .String "\n" -}}
<p class="gemtext">{{ . }}</p>
{{- end }}
{{- end }}
{{ define "linkline" -}}
<p>=> <a href="{{ .ValidatedURL }}">{{ if eq .Label "" -}}
<p class="gemtext">=> <a class="gemtext" href="{{ .ValidatedURL }}">
{{- if eq .Label "" -}}
{{ .URL }}
{{- else -}}
{{ .Label }}
@ -67,22 +74,22 @@ var baseTmpl = template.Must(template.New("htmlconv").Parse(`
</a></p>
{{- end }}
{{ define "preformattedtextlines" -}}
<pre>
<pre class="gemtext">
{{- range . -}}
{{ . }}
{{- end -}}
</pre>
{{- end }}
{{ define "heading1line" }}<h1>{{ .Body }}</h1>{{ end }}
{{ define "heading2line" }}<h2>{{ .Body }}</h2>{{ end }}
{{ define "heading3line" }}<h3>{{ .Body }}</h3>{{ end }}
{{ define "heading1line" }}<h1 class="gemtext">{{ .Body }}</h1>{{ end }}
{{ define "heading2line" }}<h2 class="gemtext">{{ .Body }}</h2>{{ end }}
{{ define "heading3line" }}<h3 class="gemtext">{{ .Body }}</h3>{{ end }}
{{ define "listitemlines" -}}
<ul>
<ul class="gemtext">
{{- range . -}}
<li>{{ .Body }}</li>
<li class="gemtext">{{ .Body }}</li>
{{- end -}}
</ul>
{{- end }}
{{ define "quoteline" }}<blockquote>{{ .Body }}</blockquote>{{ end }}
{{ define "quoteline" }}<blockquote class="gemtext">{{ .Body }}</blockquote>{{ end }}
{{ define "footer" }}</body></html>{{ end }}
`))

View File

@ -32,8 +32,9 @@ This is some non-blank regular text.
func TestConvert(t *testing.T) {
htmlDoc := `
<html><body><h1>top-level header line</h1><h2>subtitle</h2><p>This is some non-blank regular text.
</p><ul><li>an</li><li>unordered</li><li>list</li></ul><p>=> <a href="gemini://google.com/">as if</a></p><p>=> <a href="https://google.com/">https://google.com/</a></p><blockquote> this is a quote</blockquote><blockquote> -tjp</blockquote><pre>doc := gemtext.Parse(req.Body)
<!DOCTYPE html>
<html><body class="gemtext"><h1 class="gemtext">top-level header line</h1><h2 class="gemtext">subtitle</h2><p class="gemtext">This is some non-blank regular text.
</p><ul class="gemtext"><li class="gemtext">an</li><li class="gemtext">unordered</li><li class="gemtext">list</li></ul><p class="gemtext">=> <a class="gemtext" href="gemini://google.com/">as if</a></p><p class="gemtext">=> <a class="gemtext" href="https://google.com/">https://google.com/</a></p><blockquote class="gemtext"> this is a quote</blockquote><blockquote class="gemtext"> -tjp</blockquote><pre class="gemtext">doc := gemtext.Parse(req.Body)
</pre></body></html>`[1:]
doc, err := gemtext.Parse(bytes.NewBufferString(gmiDoc))