-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathrepo.go
More file actions
79 lines (66 loc) · 1.6 KB
/
repo.go
File metadata and controls
79 lines (66 loc) · 1.6 KB
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
package main
import (
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"text/template"
gfm "github.com/shurcooL/github_flavored_markdown"
)
const (
readmePath = "./README.md"
tplPath = "tmpl/tmpl.html"
idxPath = "tmpl/index.html"
)
type content struct {
Body string
}
func updateRepo() {
branchCmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
out, err := branchCmd.Output()
if err != nil {
log.Printf("Skipping git pull (unable to detect branch): %v", err)
return
}
currentBranch := strings.TrimSpace(string(out))
if currentBranch == "HEAD" || currentBranch == "" {
log.Println("Skipping git pull: detached HEAD detected")
return
}
pullCmd := exec.Command("git", "pull", "--ff-only")
pullCmd.Stdout = os.Stdout
pullCmd.Stderr = os.Stderr
if err := pullCmd.Run(); err != nil {
log.Printf("Continuing without git pull (failed to update repository): %v", err)
}
}
func readMarkdownFile() []byte {
input, err := ioutil.ReadFile(readmePath)
if err != nil {
log.Fatalf("Error reading README.md: %v", err)
}
return input
}
func generateHTML(input []byte) {
body := string(gfm.Markdown(input))
c := &content{Body: body}
t, err := template.ParseFiles(tplPath)
if err != nil {
log.Fatalf("Error parsing template: %v", err)
}
f, err := os.Create(idxPath)
if err != nil {
log.Fatalf("Error creating index.html: %v", err)
}
defer f.Close()
if err := t.Execute(f, c); err != nil {
log.Fatalf("Error executing template: %v", err)
}
}
func main() {
updateRepo()
markdown := readMarkdownFile()
generateHTML(markdown)
log.Println("Successfully generated index.html")
}