-
Notifications
You must be signed in to change notification settings - Fork 0
/
proccess.go
135 lines (111 loc) · 2.44 KB
/
proccess.go
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
var head = `#cover
h1.book-title Knuth Interview 2006
h2.author by Donald E. Knuth
`
var foot = `
template#page-footer
style(type='text/css').
.pdf-footer {
font-family: Helvetica;
font-size: 10px;
width: 100%;
text-align: center;
}
.pdf-footer #[span.pageNumber]
style
include:scss knuth.scss
`
var temp = `
.chapter
include:markdown-it(html=true) sections/%d.md
`
func main() {
sharding()
generatePug()
}
func generatePug() {
var (
name = "knuth.pug"
chapterNums = 97
)
var content = head
for i := 0; i <= chapterNums; i++ {
content += fmt.Sprintf(temp, i)
}
content += foot
f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
fmt.Println("Failed to open the file", err.Error())
os.Exit(2)
}
defer f.Close()
if _, err := f.WriteString(content); err != nil {
fmt.Println("Error writing to thr file with os.OpenFile and *File.WriteString method.", err)
}
f.Sync()
}
func sharding() {
f, err := os.Open("./knuth-interview-2006/README.md")
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
defer f.Close()
var (
sectionNum int
content string
oldContent string
chapter string
isEnd bool
)
var (
br = bufio.NewReader(f)
summary = "## Summary"
)
for {
a, _, err := br.ReadLine()
if err == io.EOF {
isEnd = true
content = strings.Replace(oldContent, "Code used to originally pull the above", "End of The Book", -1)
}
var name string
if strings.Contains(string(a), "](http") || isEnd {
name = fmt.Sprintf("./sections/%d.md", sectionNum)
if chapter == "" {
chapter = summary
content = strings.Replace(content, "Donald Knuth Interview 2006\n===========================", "", -1)
}
content = chapter + content
f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
fmt.Println("Failed to open the file", err.Error())
os.Exit(2)
}
defer f.Close()
if _, err := f.WriteString(content); err != nil {
fmt.Println("Error writing to thr file with os.OpenFile and *File.WriteString method.", err)
os.Exit(2)
}
f.Sync()
sectionNum++
chapter = "## " + string(a) + "\n"
}
if strings.Contains(string(a), "------------------------------") {
oldContent = content
content = ""
} else {
content += string(a) + "\n"
}
if isEnd {
break
}
}
}