-
Notifications
You must be signed in to change notification settings - Fork 375
/
footnote.go
260 lines (237 loc) · 5.29 KB
/
footnote.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package footnote
import (
"container/list"
"regexp"
"strconv"
"strings"
)
type seq interface {
seq()
}
type iindent struct {
v seq
}
type inewline struct{}
type istr struct {
v []rune
}
type inum struct {
v int
}
type iiappend struct {
v1 seq
v2 seq
}
type iempty struct{}
func (iindent) seq() {}
func (inewline) seq() {}
func (istr) seq() {}
func (inum) seq() {}
func (iiappend) seq() {}
func (iempty) seq() {}
func concat(seqs ...seq) seq {
l := len(seqs)
if l == 0 {
return iempty{}
}
if l == 1 {
return seqs[0]
}
r := iiappend{seqs[l-2], seqs[l-1]}
for i := l - 3; i >= 0; i-- {
r = iiappend{seqs[i], r}
}
return r
}
func interleave(join seq, seqs ...seq) seq {
l := len(seqs)
if l == 0 {
return iempty{}
}
if l == 1 {
return seqs[0]
}
r := concat(seqs[l-2], join, seqs[l-1])
for i := l - 3; i >= 0; i-- {
r = iiappend{seqs[i], iiappend{join, r}}
}
return r
}
type seqWithColumn struct {
s seq
col int
}
// sc: seqWithColumn list
func flatten(sc *list.List, col int) []rune {
if sc.Len() == 0 {
return []rune{}
}
e := sc.Front()
s := sc.Remove(e).(seqWithColumn)
switch v := s.s.(type) {
case iindent:
sc.PushFront(seqWithColumn{v.v, col})
return flatten(sc, col)
case inewline:
return append([]rune("\n"+spaces(s.col)), flatten(sc, s.col)...)
case istr:
return append(v.v, flatten(sc, col+len(v.v))...)
case inum:
n := []rune(strconv.Itoa(v.v))
return append(n, flatten(sc, col+len(n))...)
case iiappend:
s1 := v.v1
s2 := v.v2
sc.PushFront(seqWithColumn{s2, s.col})
sc.PushFront(seqWithColumn{s1, s.col})
return flatten(sc, col)
case iempty:
return flatten(sc, col)
}
// can't be here
return nil
}
func spaces(n int) string {
return mkString(n, rune(' '))
}
func mkString(n int, char rune) string {
r := []rune("")
for i := 0; i < n; i++ {
r = append(r, char)
}
return string(r)
}
func indent(s seq) seq {
return iindent{s}
}
func newline() seq {
return inewline{}
}
func str(s string) seq {
return istr{[]rune(s)}
}
func num(s int) seq {
return inum{s}
}
func iappend(s1, s2 seq) seq {
return iiappend{s1, s2}
}
func empty() seq {
return iempty{}
}
// =============================== above impl a wrapper for string building =================================
type FootNote struct {
content string
lines []string
notes map[int]string
}
func New(content string) *FootNote {
return &FootNote{
content: content,
notes: map[int]string{},
}
}
func (f *FootNote) NoteLine(line int, note string) *FootNote {
if f.lines == nil {
f.lines = strings.Split(f.content, "\n")
}
if line < len(f.lines) {
f.notes[line] = note
}
return f
}
func (f *FootNote) NotePoint(pos int, note string) *FootNote {
if f.lines == nil {
f.lines = strings.Split(f.content, "\n")
}
curr := 0
for l, line := range f.lines {
curr += len(line) + 1 // \n
if curr > pos {
return f.NoteLine(l, note)
}
}
return f
}
func (f *FootNote) NoteRegex(regex *regexp.Regexp, note string) *FootNote {
if f.lines == nil {
f.lines = strings.Split(f.content, "\n")
}
loc := regex.FindStringSubmatchIndex(f.content)
if len(loc) != 0 {
if len(loc) == 2 { // if no subcapture, use whole capture's position
return f.NotePoint(loc[0], note)
} else if len(loc) == 4 { // if exist sub capture, use the 1st subcapture's position
return f.NotePoint(loc[2], note)
}
}
return f
}
func (f *FootNote) Dump() string {
notelines := map[int]string{}
for l := range f.notes {
notelines[l] = f.lines[l]
}
alignLines(notelines)
for l := range f.notes {
f.lines[l] = notelines[l] + "(" + strconv.Itoa(l+1) + ")"
}
// assemble seqs
content := assemLines(f.lines)
partingLine := assemPartingLine()
notes := assemNotes(f.notes)
l := list.New()
l.PushFront(seqWithColumn{notes, 0})
l.PushFront(seqWithColumn{partingLine, 0})
l.PushFront(seqWithColumn{content, 0})
return string(flatten(l, 0))
}
// update in place, default to 75 column
func alignLines(ss map[int]string) {
maxcol := 72
for _, s := range ss {
if len([]rune(s)) > maxcol {
maxcol = len([]rune(s))
}
}
maxcol += 3 // add extra spaces
for i := range ss {
ss[i] = ss[i] + spaces(maxcol-len([]rune(ss[i])))
}
return
}
func assemLines(lines []string) seq {
seqs := []seq{}
for _, l := range lines {
seqs = append(seqs, concat(str(l), newline()))
}
return concat(seqs...)
}
func assemPartingLine() seq {
return concat(str(mkString(80, '-')), newline())
}
func assemNotes(notes map[int]string) seq {
r := empty()
for l, note := range notes {
r = concat(r, newline(), num(l+1), str(") "))
noteSplit := strings.Split(note, "\n")
noteseq := []seq{}
for _, n := range noteSplit {
noteseq = append(noteseq, str(n))
}
r = concat(r, indent(interleave(newline(), noteseq...)))
}
return r
}