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