1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
package main
// The two filters: in process for the forge's pages, as programs for cgit.
import (
"bytes"
"fmt"
"html/template"
"io"
"log"
"os"
"os/exec"
"path"
"github.com/yuin/goldmark"
)
// Raw HTML is left unrendered, goldmark's default, which is what makes user
// text safe to hand it.
var markdown = goldmark.New()
func renderMarkdown(source string) template.HTML {
var buf bytes.Buffer
if err := markdown.Convert([]byte(source), &buf); err != nil {
log.Printf("markdown: %v", err)
return template.HTML(template.HTMLEscapeString(source))
}
return template.HTML(buf.String())
}
// runMarkdown is the about-filter: markdown on stdin, HTML on stdout.
func runMarkdown() int {
source, err := io.ReadAll(os.Stdin)
if err != nil {
fmt.Fprintln(os.Stderr, "md:", err)
return 1
}
if err := markdown.Convert(source, os.Stdout); err != nil {
// A readable page beats an empty one.
fmt.Fprintln(os.Stderr, "md:", err)
template.HTMLEscape(os.Stdout, source)
return 1
}
return 0
}
// runHighlight is the source-filter: cgit's bundled one issues a command line
// the installed highlight refuses, silently.
func runHighlight(args []string) int {
source, err := io.ReadAll(os.Stdin)
if err != nil {
fmt.Fprintln(os.Stderr, "highlight:", err)
return 1
}
name := "source"
if len(args) > 0 && args[0] != "" {
name = path.Base(args[0])
}
// --force: an unknown syntax comes back as escaped text, not as nothing.
cmd := exec.Command("highlight", "--fragment", "--force", "--out-format=html", "--syntax-by-name="+name)
cmd.Stdin = bytes.NewReader(source)
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil || len(bytes.TrimSpace(out)) == 0 {
if err != nil {
fmt.Fprintln(os.Stderr, "highlight:", err)
}
template.HTMLEscape(os.Stdout, source)
return 0
}
os.Stdout.Write(out)
return 0
}
|