-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunk.go
executable file
·439 lines (396 loc) · 11.9 KB
/
chunk.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package main
import (
"bytes"
"errors"
"fmt"
"log"
"path/filepath"
"sort"
"strconv"
"strings"
)
type WithId interface {
GetId() string
}
type WithCaption interface {
GetCaption() string
SetCaption(string)
}
type WithIdCaption interface {
WithId
WithCaption
}
type withNumbering interface {
SetNumbering(c string)
GetNumbering() string
}
type WithIdCaptionNumbering interface {
WithIdCaption
withNumbering
}
// Chunk denotes a block of text, the block may be length 1 to n in bytes
type Chunk interface {
GetPosition() int
GetValue() string
SetPosition(pos int)
}
// MetaCharChunk denotes the trunk of length 1, and the content is meta char.
// Escapted meta char should locate in PlainTextChunk instead
type MetaCharChunk struct {
Position int
Value string
}
// GetPosition implements the Chunk interface
func (p *MetaCharChunk) GetPosition() int {
return p.Position
}
// SetPosition implements the Chunk interface
func (p *MetaCharChunk) SetPosition(pos int) {
p.Position = pos
}
// GetValue implements the Chunk interface
func (p *MetaCharChunk) GetValue() string {
return p.Value
}
// String implements the Stringer interface
func (p MetaCharChunk) String() string {
return fmt.Sprintf("MetaCharChunk{Position:%v,Value:'%v'}", p.Position, p.Value)
}
//ParseChunks is the top level function to Parse input string to Chunks
//there maybe be several passes to finish parsing
func ParseChunks(input string) ([]Chunk, error) {
var (
chunks []Chunk
err error
)
chunks, err = RawTextChunkHandle(input)
if err != nil {
log.Fatalln(err)
return chunks, err
}
chunks, err = MetaChunkHandle(chunks)
if err != nil {
log.Fatalln(err)
return chunks, err
}
chunks, err = KeywordChunkHandle(chunks)
if err != nil {
log.Fatalln(err)
return chunks, err
}
chunks, err = IncludeChunkHandle(chunks)
if err != nil {
log.Fatalln(err)
return chunks, err
}
chunks, err = CaptionChunkHandle(chunks)
if err != nil {
log.Fatalln(err)
return chunks, err
}
chunks, err = SectionChunkHandle(chunks)
if err != nil {
log.Fatalln(err)
return chunks, err
}
chunks, err = ChunkWithNumberingHandle(chunks)
if err != nil {
log.Fatalln(err)
return chunks, err
}
gInlineRenderMode = true
//first render inlineChunk, so that there is not extra <p> around inlineChunk
chunks, err = InlineChunkListRender(chunks)
if err != nil {
log.Fatalln(err)
return chunks, err
}
return chunks, nil
}
//SectionChunkHandle set numbering of SectionChunk
func SectionChunkHandle(inputChunks []Chunk) ([]Chunk, error) {
sectionChunkList := []*SectionChunk{}
var levels []int
var levelMap = make(map[int]bool)
var levelNumberingMap = make(map[int]int)
storeLevel := func(level int) {
if levelMap[level] {
return
}
levels = append(levels, level)
levelMap[level] = true
}
getLevelIndex := func(level int) int {
for i := 0; i < len(levels); i++ {
if levels[i] == level {
return i
}
}
log.Fatalln("should not reach here")
panic("should not reach here")
}
resetLevelLowerThan := func(level int) {
from := getLevelIndex(level)
for i := from + 1; i < len(levels); i++ {
levelNumberingMap[levels[i]] = 0
}
}
calcNumbering := func(level int) string {
var buf bytes.Buffer
idx := getLevelIndex(level)
for i := 0; i <= idx; i++ {
if i != 0 {
buf.WriteString(".")
}
buf.WriteString(strconv.Itoa(levelNumberingMap[levels[i]]))
}
return buf.String()
}
for _, c := range inputChunks {
if keywordChunk, ok := c.(*KeywordChunk); ok {
level, isSection := gSectionLevel[keywordChunk.Keyword]
if isSection {
sectionChunkList = append(sectionChunkList, keywordChunk.Children[0].(*SectionChunk))
storeLevel(level)
}
}
sort.Ints(levels)
}
var bufSectionIndex bytes.Buffer
for _, sectionChunk := range sectionChunkList {
level := sectionChunk.Level
resetLevelLowerThan(level)
levelNumberingMap[level]++
//generate numbering for this section
sectionChunk.Numbering = calcNumbering(level)
//generate index for the whole doc
err := gSectionIndexTemplate.Execute(&bufSectionIndex, sectionChunk)
if err != nil {
return nil, err
}
}
gDoc.SectionIndex = bufSectionIndex.String()
return inputChunks, nil
}
//Chunk with numbering handle
func ChunkWithNumberingHandle(inputChunks []Chunk) ([]Chunk, error) {
numberingMap := make(map[string]int) //to generate numbering
indexMap := make(map[string]*bytes.Buffer) //to generate index
for _, chunk := range inputChunks {
keywordChunk, ok := chunk.(*KeywordChunk)
if !ok {
continue
}
if gChunkWithCaptionMap[keywordChunk.Keyword] {
chunkWithIdCaptionNumbering := keywordChunk.Children[0].(WithIdCaptionNumbering)
//only chunks that the caption has been set, we set numbering for them
if len(chunkWithIdCaptionNumbering.GetCaption()) > 0 {
numberingMap[keywordChunk.Keyword]++
prefix := getKeywordName(keywordChunk.Keyword)
//set numbering
chunkWithIdCaptionNumbering.SetNumbering(prefix + " " + strconv.Itoa(numberingMap[keywordChunk.Keyword]) + ": ")
//generate index for this type
buf := indexMap[keywordChunk.Keyword]
if nil == buf {
buf = new(bytes.Buffer)
indexMap[keywordChunk.Keyword] = buf
}
err := gGlobalIndexTemplate.Execute(buf, chunkWithIdCaptionNumbering)
if err != nil {
return nil, err
}
}
}
}
//set indices to global doc obj
for _, keyword := range gChunkWithCaptionList {
buf := indexMap[keyword]
if buf != nil {
switch keyword {
case OrderList:
gDoc.OrderListIndex = buf.String()
case BulletList:
gDoc.BulletListIndex = buf.String()
case TableKeyword:
gDoc.TableIndex = buf.String()
case BlockCode:
gDoc.CodeIndex = buf.String()
case BlockTex:
gDoc.MathIndex = buf.String()
case ImageKeyword:
gDoc.ImageIndex = buf.String()
}
}
}
return inputChunks, nil
}
//CaptionChunkHandle filter the Caption chunk, and set caption to the chunk it refers to
func CaptionChunkHandle(inputChunks []Chunk) ([]Chunk, error) {
outputChunks := []Chunk{}
idToChunk := make(map[string]Chunk)
captionChunks := []Chunk{}
for _, chunk := range inputChunks {
keywordChunk, ok := chunk.(*KeywordChunk)
if !ok {
outputChunks = append(outputChunks, chunk)
continue
}
if gChunkWithCaptionMap[keywordChunk.Keyword] {
chunkWithIdCaption := keywordChunk.Children[0].(WithIdCaption)
idToChunk[chunkWithIdCaption.GetId()] = chunk
outputChunks = append(outputChunks, chunk)
continue
}
if keywordChunk.Keyword == CaptionKeyword {
captionChunks = append(captionChunks, chunk)
continue
}
outputChunks = append(outputChunks, chunk)
}
for _, captionChunk := range captionChunks {
id := captionChunk.(*KeywordChunk).Children[0]
caption := captionChunk.(*KeywordChunk).Children[1]
chunk, ok := idToChunk[id.GetValue()]
if !ok {
log.Println("caption ", caption, "has not found Id", id.GetValue())
return nil, errExpectChunkWithId
}
chunk.(*KeywordChunk).Children[0].(WithIdCaption).SetCaption(caption.GetValue())
}
return outputChunks, nil
}
//IncludeChunkHandle filter the Include chunk, and import contents of the file it refers to
func IncludeChunkHandle(inputChunks []Chunk) ([]Chunk, error) {
outputChunks := []Chunk{}
for _, chunk := range inputChunks {
keywordChunk, ok := chunk.(*KeywordChunk)
if !ok {
outputChunks = append(outputChunks, chunk)
continue
}
if keywordChunk.Keyword != IncludeKeyword {
outputChunks = append(outputChunks, chunk)
continue
}
//now it is include keyword, we first figure out the path of the included file.
//it is relative to the current file to be compiled or absolute path.
//Note: If the included file itself contains include keyword,
//it is still relative to the current file to be compiled(not the included file)
//Note: the implementation of include keyword has limitations.
//It is better the included content does not rely on chunks in other files. Otherwise, surprise may happens.
parentDir := filepath.Dir(gDoc.FilePath)
includedFileName := strings.Trim(keywordChunk.GetValue(), BlankChars)
absolutePath := false
if strings.HasPrefix(includedFileName, "/") || strings.HasPrefix(includedFileName, "\\") {
absolutePath = true
}
var parts []string
var includedFilePath string
if strings.Contains(includedFileName, "/") {
parts = strings.Split(includedFileName, "/")
if absolutePath {
includedFilePath = filepath.Join("/", filepath.Join(parts...))
} else {
includedFilePath = filepath.Join(parentDir, filepath.Join(parts...))
}
} else {
parts = strings.Split(includedFileName, "\\")
if absolutePath {
includedFilePath = filepath.Join("\\", filepath.Join(parts...))
} else {
includedFilePath = filepath.Join(parentDir, filepath.Join(parts...))
}
}
//included file to chunks, there are re-cursive calls inside
includedChunks, err := fileToChunks(includedFilePath)
if err != nil {
log.Println(err)
return nil, err
}
outputChunks = append(outputChunks, includedChunks...)
}
return outputChunks, nil
}
//MetaChunkHandle turns the chunk that is PlainTextChunk in inputChunks to MetaCharChunks if any
//It makes use of metaCharChunkHandle
func MetaChunkHandle(inputChunks []Chunk) ([]Chunk, error) {
var (
newChunks []Chunk
)
for _, chunk := range inputChunks {
if plainTextChunk, ok := chunk.(*PlainTextChunk); ok {
subChunks, err := metaCharChunkHandle(plainTextChunk.GetValue())
if err != nil {
return newChunks, err
}
for _, subChunk := range subChunks {
subChunk.SetPosition(subChunk.GetPosition() + chunk.GetPosition()) //child's position plus the parent's
}
newChunks = append(newChunks, subChunks...)
} else {
newChunks = append(newChunks, chunk)
}
}
return newChunks, nil
}
// metaCharChunkHandle converts string to a list of chunks that is used for latter phase handling
// the result list contains PlainTextChunk and MetaCharChunk
func metaCharChunkHandle(s string) ([]Chunk, error) {
var buf bytes.Buffer
var escaping = false
var startPos = 0
var chunks []Chunk
for i, arune := range s {
runeStr := string(arune)
_, isMetaChar := MetaCharMap[runeStr]
if escaping {
if isMetaChar {
//this meta char is a normal char
buf.WriteRune(arune)
} else {
//last meta char is escape char
//only store non-empty chunk before the meta char
if buf.Len() > 0 {
plainTextChunk := PlainTextChunk{Position: startPos, Value: buf.String()}
chunks = append(chunks, &plainTextChunk)
}
//store the meta char chunk
metaCharChunk := MetaCharChunk{Position: i - 1, Value: EscapeChar}
chunks = append(chunks, &metaCharChunk)
buf.Reset()
buf.WriteRune(arune) //store the current char to buf
startPos = i
}
escaping = false
continue
}
//from now on, it is not in escaping state
if isMetaChar {
if runeStr == EscapeChar {
escaping = true
continue // and don't write the EscapeChar to buf
}
//only store non-empty chunk before the meta char
if buf.Len() > 0 {
plainTextChunk := PlainTextChunk{Position: startPos, Value: buf.String()}
chunks = append(chunks, &plainTextChunk)
}
//store the meta char chunk
metaCharChunk := MetaCharChunk{Position: i, Value: runeStr}
chunks = append(chunks, &metaCharChunk)
buf.Reset()
startPos = i + 1
} else {
buf.WriteRune(arune)
}
}
// there are unhandled chunk
if buf.Len() > 0 {
if escaping {
return chunks, errors.New("come to the end, but it is still in escaping state")
}
plainTextChunk := PlainTextChunk{Position: startPos, Value: buf.String()}
chunks = append(chunks, &plainTextChunk)
}
return chunks, nil
}