-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplitter.go
459 lines (412 loc) · 11.2 KB
/
splitter.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package main
import (
"asciidoc2md/ast"
"asciidoc2md/markdown"
"asciidoc2md/settings"
"asciidoc2md/utils"
"bufio"
"cdr.dev/slog"
"context"
"errors"
"fmt"
"gopkg.in/yaml.v3"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"strings"
)
type IdMapEntry struct {
FileName string
Caption string
}
type IdMap map[string]*IdMapEntry
type FileSplitter struct {
doc *ast.Document
conf *settings.Config
//headerMap map[string]string //header -> file name
idMaps map[string]IdMap //document name -> header or bookmark id -> output file name
log slog.Logger
slug string
path string //output path
level int //split at the specified level headers
firstHeader *ast.Header
fileIndex int
fileName string //current fileName
fileNames []string //all the filenames
file *os.File //current file
w *bufio.Writer //current writer
}
const (
SkipChapterMark = "<skip chapter>"
SkipHeaderMark = "<skip>"
)
func NewFileSplitter(doc *ast.Document, nameSlug string, conf *settings.Config, path string, splitLvl int, log slog.Logger) *FileSplitter {
return &FileSplitter{
doc: doc,
conf: conf,
idMaps: make(map[string]IdMap),
level: splitLvl,
log: log,
slug: nameSlug,
path: path}
}
func (fs *FileSplitter) GenerateIdMap() error {
return fs.init(true)
}
func (fs *FileSplitter) decreaseHeader(h *ast.Header) {
if fs.level != 1 {
h.Level--
}
}
func (fs *FileSplitter) RenderMarkdown(imagePath string) error {
err := fs.init(false)
if err != nil {
return err
}
err = fs.nextFile()
if err != nil {
return err
}
defer fs.Close()
conv := markdown.New(imagePath, nil, fs.log, func(header *ast.Header) io.Writer {
if header.Level < fs.level && fs.level != 1 {
header.Text = SkipHeaderMark
}
if header.Level == fs.level && header != fs.firstHeader {
if fs.skipChapter(header) {
}
err := fs.nextFile()
if err != nil {
fs.log.Error(context.Background(), err.Error())
return nil
}
fs.decreaseHeader(header)
return fs.w
}
fs.decreaseHeader(header)
return nil
})
conv.RenderMarkdown(fs.doc, fs.w)
return nil
}
func (fs *FileSplitter) getNextFileName(h *ast.Header) string {
fs.fileIndex++
if h == nil {
return fmt.Sprintf("%s_%v.md", fs.slug, fs.fileIndex)
}
name, ok := fs.conf.Headers[fs.doc.Name][h.Text]
if !ok {
//fs.log.Info(context.Background(), "no file name for h", slog.F("h", h.Text))
return fmt.Sprintf("%s_%v.md", fs.slug, fs.fileIndex)
}
return name
}
func (fs *FileSplitter) nextFile() error {
//close previous file
fs.Close()
var err error
fs.fileName = fs.fileNames[fs.fileIndex]
if fs.fileName == SkipChapterMark {
fs.w = bufio.NewWriter(ioutil.Discard)
fs.file = nil
fs.log.Warn(context.Background(), "discard writer created")
return nil
}
fullName := filepath.Join(fs.path, fs.fileName)
fs.file, err = os.Create(fullName)
if err != nil {
return err
}
fs.log.Debug(context.Background(), "output file created", slog.F("file", fullName))
fs.w = bufio.NewWriter(fs.file)
fs.fileIndex++
return nil
}
func (fs *FileSplitter) Close() {
//close previous files
if fs.w != nil {
fs.w.Flush()
}
if fs.file != nil {
fs.file.Close()
}
}
func (fs *FileSplitter) skipChapter(h *ast.Header) bool {
m, ko := fs.conf.Headers[fs.doc.Name][h.Text]
if ko && m == SkipChapterMark {
fs.log.Warn(context.Background(), "skipping chapter", slog.F("chapter", h.Text))
return true
}
return false
}
func (fs *FileSplitter) findFirstHeader() *ast.Header {
var hdr *ast.Header
fs.doc.Walk(func(b ast.Block, doc *ast.Document) bool {
h, ok := b.(*ast.Header)
if ok && h.Level == fs.level && !h.Float && !fs.skipChapter(h) {
hdr = h
return false
}
return true
}, fs.doc)
return hdr
}
func (fs *FileSplitter) applyRewriteRulesRE(url string) string {
for _, elem := range fs.conf.UrlRewrites {
for k, r := range elem {
if k[0] == '@' {
//fs.log.Warn(context.Background(), fmt.Sprintf("re: %q, replace: %q", k[1:], r))
re, err := regexp.Compile(k[1:])
if err != nil {
fs.log.Error(context.Background(), "invalid regexp", slog.F("re", k[1:]))
return err.Error()
}
url = re.ReplaceAllString(url, r)
}
}
}
return url
}
func (fs *FileSplitter) findRewriteDocRule(url string) string {
for _, elem := range fs.conf.UrlRewrites {
for k, r := range elem {
if strings.Contains(url, k) {
return r
}
}
}
return ""
}
func (fs *FileSplitter) linkRewrite(link *ast.Link, root *ast.Document) {
ctx := context.Background()
var idRef, adocRef string
var entry *IdMapEntry
link.Url = fs.applyRewriteRulesRE(link.Url)
adocRef = link.Url
idx := strings.Index(link.Url, "#")
if idx != -1 {
adocRef = link.Url[:idx]
idRef = link.Url[idx + 1:]
}
switch {
case idx == -1 && strings.HasSuffix(link.Url, ".adoc"):
//no #, link to the document ("file.adoc")
adocRef = link.Url
idRef = ""
case idx == -1 && link.Internal:
//no #, internal link ("apps-publish")
adocRef = fs.doc.Name
idRef = link.Url
case adocRef == "" && link.Internal:
//internal link with # ("#apps-publish")
adocRef = fs.doc.Name
case link.Internal:
// probably relative file name "../docs/admin.adoc"
// replace backslashes with slashes for compatibility
// path package works with slash-separated paths
_, adocRef = path.Split(strings.ReplaceAll(adocRef, `\`, `/`))
}
rule := fs.findRewriteDocRule(adocRef)
if rule != "" {
fs.log.Debug(ctx, "found rewrite rule", slog.F(adocRef, rule))
adocRef = rule
}
if !link.Internal && rule == "" {
// external link without rewrite rule
fs.log.Debug(ctx, "external link without rewrite rule", slog.F("link", link))
return
}
entry = fs.findIdMap(adocRef, idRef)
if entry == nil {
// let's try fallbacks if any
if fb := fs.conf.IdMapFallbacks[adocRef]; fb != "" {
if entry = fs.findIdMap(fb, idRef); entry != nil {
adocRef = fb
}
}
}
old := link.Url
if entry != nil {
link.Url = fmt.Sprintf("%v#%v", path.Join(fs.getDocPath(adocRef), entry.FileName), idRef)
if link.Text == "" {
link.Text = entry.Caption
}
fs.log.Debug(ctx, "successfully rewrote link", slog.F("new", link.Url), slog.F("old", old))
} else {
fs.log.Error(ctx, "cannot rewrite link: idmap is not found", slog.F("link", link), slog.F("doc", root.Name))
//link.Url = fmt.Sprintf("%v#%v", adocRef, idRef)
}
}
//should be called AFTER fillIdMap
func (fs *FileSplitter) fixUrls() {
fs.doc.Walk(
func(b ast.Block, root *ast.Document) bool {
ctx := context.Background()
if b == nil || utils.IsNil(b) {
fs.log.Error(ctx, "walker: nil block")
}
switch b.(type) {
case *ast.Link:
link := b.(*ast.Link)
fs.linkRewrite(link, root)
}
return true
},
nil)
}
func (fs *FileSplitter) writeIdMap(name string, idMap IdMap) error {
/* f, err := os.Create(name)
if err != nil {
return err
}
defer f.Close()
*/ out, err := yaml.Marshal(idMap)
// _, err = f.Write(out)
err = ioutil.WriteFile(name, out, 0666)
return err
}
func (fs *FileSplitter) getDocPath(doc string) string {
// we need to address outer document docs/doc2/file2.md from docs/doc1/file1.md
if doc == fs.doc.Name {
return ""
}
// first get to the root folder "docs" by writing <stepsUp> double dots: ../
stepsUp := utils.CountDirs(fs.conf.CrossLinks[fs.doc.Name])
base := strings.Repeat("../", stepsUp)
// now append relative path from the "docs" to "file2.md" to the path: ../doc2/file2.md
return path.Join(base, fs.conf.CrossLinks[doc])
}
func (fs *FileSplitter) fillIdMap(printYaml bool) {
ctx := context.Background()
// link can refer to the document in whole, without any id after '#'
// by this time fs.fileName SHOULD contain the first part's file name
fs.appendIdMap(fs.doc.Name, fs.doc.Name, fs.fileName, "")
if fs.firstHeader == nil {
//no headers in the document
fs.log.Warn(ctx, "no first header, skip filling idmap")
fs.fileIndex = 0
fs.fileName = ""
return
}
nav := []string{fmt.Sprintf("- %s: %s", fs.firstHeader.Text, fs.fileName)}
skipCurChapter := false
fs.doc.Walk(func(b ast.Block, root *ast.Document) bool {
//fs.log.Debug(ctx, "walker block", slog.F("block", b))
if b == nil || utils.IsNil(b) {
fs.log.Error(ctx, "walker: nil block")
}
switch b.(type) {
case *ast.Header:
hd := b.(*ast.Header)
//fs.log.Debug(ctx, "walking by header", slog.F("header", hd))
if hd.Level == fs.level && hd != fs.firstHeader && !hd.Float {
if fs.skipChapter(hd) {
skipCurChapter = true
fs.fileName = SkipChapterMark
} else {
skipCurChapter = false
fs.fileName = fs.getNextFileName(hd)
nav = append(nav, fmt.Sprintf("- %s: %s", hd.Text, fs.fileName))
}
fs.fileNames = append(fs.fileNames, fs.fileName)
}
if !skipCurChapter {
fs.appendIdMap(fs.doc.Name, hd.Id, fs.fileName, hd.Text)
}
case *ast.Bookmark:
//fs.log.Debug(ctx, "walking by bookmark", slog.F("header", b.(*ast.Bookmark)))
if !skipCurChapter {
fs.appendIdMap(fs.doc.Name, b.(*ast.Bookmark).Literal, fs.fileName, "")
}
}
return true
}, nil)
fs.fileIndex = 0
fs.fileName = ""
if printYaml {
fmt.Print(strings.Join(nav, "\n") + "\n")
}
if fs.conf.NavFile != "" {
err := fs.writeNavToFile(fs.conf.NavFile, fs.doc.Name, nav)
if err != nil {
panic(err)
}
}
}
func (fs *FileSplitter) writeNavToFile(navFile string, docFile string, nav []string) error {
data, err := ioutil.ReadFile(navFile)
if err != nil {
return err
}
out, err := writeNav(string(data), docFile, nav)
if err != nil {
return err
}
err = ioutil.WriteFile(navFile, []byte(out), 666)
return err
}
var permLinkRE = regexp.MustCompile(`[^a-яА-Яa-zA-Z0-9]+`)
func (fs *FileSplitter) appendIdMap(doc string, id string, file string, caption string) {
if fs.idMaps[fs.doc.Name] == nil {
fs.idMaps[fs.doc.Name] = make(IdMap)
}
if id != "" {
fs.idMaps[fs.doc.Name][id] = &IdMapEntry{file, caption}
}
if caption != "" {
perm := strings.ToLower(caption)
perm = permLinkRE.ReplaceAllLiteralString(perm, "-")
fs.idMaps[fs.doc.Name][perm] = &IdMapEntry{file, caption}
}
}
func (fs *FileSplitter) findIdMap(doc string, id string) *IdMapEntry {
ctx := context.Background()
if fs.idMaps[doc] == nil {
//try to read *.idmap file
data, err := ioutil.ReadFile(filepath.Join(fs.conf.ArtifactsDir, doc) + ".idmap")
if err != nil {
fs.log.Error(ctx, "cannot load idmap file", slog.F("err", err))
//no file, create emtpy map
fs.idMaps[doc] = make(IdMap)
return nil
}
var idm IdMap
err = yaml.Unmarshal(data, &idm)
if err != nil {
fs.log.Error(ctx, "cannot load idmap file", slog.F("err", err))
//no file, create emtpy map
fs.idMaps[doc] = make(IdMap)
return nil
}
fs.idMaps[doc] = idm
}
if id == "" {
//link to the document without id
return fs.idMaps[doc][doc]
}
return fs.idMaps[doc][id]
}
func (fs *FileSplitter) init(fillMapOnly bool) error {
fs.firstHeader = fs.findFirstHeader()
fs.fileName = fs.getNextFileName(fs.firstHeader)
fs.fileNames = append(fs.fileNames, fs.fileName)
fs.fillIdMap(false)
if fillMapOnly {
if len(fs.idMaps) != 1 {
return errors.New("several id maps found")
}
for _, m := range fs.idMaps {
err := fs.writeIdMap(filepath.Join(fs.conf.ArtifactsDir, fs.doc.Name + ".idmap"), m)
if err != nil {
return err
}
}
}
if !fillMapOnly {
fs.fixUrls()
}
return nil
}