aboutsummaryrefslogtreecommitdiff
path: root/projects/readme.go
blob: 85fa71f4cb86778555840d3670ed21d2e75df440 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package projects

import (
	"bytes"
	"code.laria.me/code.laria.me/git"
	"html"
	"html/template"
	"io"
	"log"
	"os"
	"os/exec"
	"path"
	"strings"
)

type ReadmeFormat string

const (
	ReadmeMarkdown ReadmeFormat = "markdown"
	ReadmePlain    ReadmeFormat = "plain"
)

func (f ReadmeFormat) Format(raw []byte) template.HTML {
	switch f {
	case ReadmeMarkdown:
		return formatMarkdown(raw)
	default:
		return template.HTML("<pre><code>" + html.EscapeString(string(raw)) + "</code></pre>")
	}
}

func formatMarkdown(raw []byte) template.HTML {
	p := exec.Command("markdown")

	buf := new(bytes.Buffer)

	p.Stdin = bytes.NewReader(raw)
	p.Stdout = buf

	if err := p.Run(); err != nil {
		log.Printf("Failed formatting markdown readme: %s", err)
		return "<strong>Failed formatting markdown :(</strong>"
	}

	return template.HTML(buf.Bytes())
}

func gitReadme(gitpath string) (raw []byte, name string, err error) {
	ents, err := git.ReadTree(gitpath, "master")
	if err != nil {
		return raw, name, err
	}

	for _, ent := range ents {
		if ent.Type == "blob" && strings.HasPrefix(strings.ToLower(ent.Name), "readme") {
			name = ent.Name
			raw, err = git.ReadBlob(gitpath, ent.Object)
			return
		}
	}

	return
}

func (p *Project) formatReadme() {
	readme_name := ""
	var readme_raw []byte
	if p.ReadmePath != "" {
		fullpath := p.ReadmePath
		if !path.IsAbs(fullpath) {
			fullpath = path.Join(p.dir, fullpath)
		}
		fullpath = path.Clean(fullpath)

		f, err := os.Open(fullpath)
		if err != nil {
			log.Printf("Could not get readme for %s: failed opening %s: %s", p.Name, fullpath, err)
			return
		}
		defer f.Close()

		buf := new(bytes.Buffer)
		if _, err := io.Copy(buf, f); err != nil {
			log.Printf("Could not get readme for %s: failed reading %s: %s", p.Name, fullpath, err)
			return
		}

		readme_raw = buf.Bytes()
		readme_name = fullpath
	} else if len(p.Git) > 0 {
		var err error
		readme_raw, readme_name, err = gitReadme(p.Git)
		if err != nil {
			log.Printf("Could not get readme for %s from git: %s", p.Name, err)
			return
		}
		if readme_name == "" {
			return
		}
	} else {
		return
	}

	format := ReadmePlain
	switch strings.ToLower(path.Ext(readme_name)) {
	case ".md", ".mkd", ".markdown":
		format = ReadmeMarkdown
	}

	p.FormattedReadme = format.Format(readme_raw)
}