depp

No frills static page generator for Git repositories

git clone https://git.8pit.net/depp.git

 1package main
 2
 3import (
 4	"bytes"
 5	"html/template"
 6	"strings"
 7
 8	"github.com/nmeum/depp/gitweb"
 9)
10
11func summarize(msg string) string {
12	newline := strings.IndexByte(msg, '\n')
13	if newline != -1 {
14		msg = msg[0:newline]
15	}
16	return msg
17}
18
19func getRelPath(n int) string {
20	var ret string
21	for i := 0; i < n; i++ {
22		ret += "../"
23	}
24
25	if ret == "" {
26		return "./"
27	} else {
28		return ret
29	}
30}
31
32func getLines(data string) []string {
33	if len(data) == 0 {
34		return []string{""} // empty file
35	}
36
37	// Remove terminating newline (if any)
38	if data[len(data)-1] == '\n' {
39		data = data[0 : len(data)-1]
40	}
41
42	lines := strings.Split(data, "\n")
43	for i, line := range lines {
44		// Deal with files containing DOS line endings.
45		lines[i] = strings.TrimRight(line, "\r")
46	}
47
48	return lines
49}
50
51func padNumber(maxnum int, curnum int) template.HTML {
52	const nonBreakingSpace string = "&nbsp;"
53	digitsReq := func(n int) int {
54		r := 1
55		for n/10 > 0 {
56			n /= 10
57			r++
58		}
59		return r
60	}
61
62	max := digitsReq(maxnum)
63	cur := digitsReq(curnum)
64
65	diff := max - cur
66	if diff == 0 {
67		return ""
68	}
69
70	buf := new(bytes.Buffer)
71	buf.Grow(diff)
72
73	for i := 0; i < diff; i++ {
74		buf.WriteString(nonBreakingSpace)
75	}
76
77	return template.HTML(buf.String())
78}
79
80func relIndex(file *gitweb.RepoFile) string {
81	elems := file.PathElements()
82	return getRelPath(len(elems) - 1)
83}
84
85func isIndexPage(page *gitweb.RepoPage) bool {
86	return page.CurrentFile.Path == ""
87}
88
89func increment(n int) int {
90	return n + 1
91}
92
93func decrement(n int) int {
94	return n - 1
95}