-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
513 lines (443 loc) · 19.7 KB
/
main.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package main
import (
"fmt"
"github.com/alecthomas/kong"
"github.com/hauke96/sigolo/v2"
"github.com/pkg/errors"
"os"
"path"
"path/filepath"
"runtime/pprof"
"runtime/trace"
"strings"
"time"
"wiki2book/api"
"wiki2book/config"
"wiki2book/generator"
"wiki2book/generator/epub"
"wiki2book/generator/html"
"wiki2book/parser"
"wiki2book/project"
"wiki2book/util"
)
const VERSION = "v0.2.0"
const RFC1123Millis = "Mon, 02 Jan 2006 15:04:05.999 MST"
var cli struct {
Logging string `help:"Logging verbosity. Possible values: \"info\" (default), \"debug\", \"trace\"." short:"l" default:"info"`
DiagnosticsProfiling bool `help:"Enable profiling and write results to ./profiling.prof."`
DiagnosticsTrace bool `help:"Enable tracing to analyse memory usage and write results to ./trace.out."`
ForceRegenerateHtml bool `help:"Forces wiki2book to recreate HTML files even if they exists from a previous run." short:"r"`
SvgSizeToViewbox bool `help:"Sets the 'width' and 'height' property of an SimpleSvgAttributes image to its viewbox width and height. This might fix wrong SVG sizes on some eBook-readers."`
Config string `help:"The path to the overall application config. If not specified, default values are used." type:"existingfile" short:"c" placeholder:"<file>"`
Version VersionFlag `help:"Print version information and quit" name:"version" short:"v"`
Standalone struct {
File string `help:"A mediawiki file tha should be rendered to an eBook." arg:""`
OutputFile string `help:"The path to the output file." short:"o" default:"ebook.epub" placeholder:"<file>"`
OutputType string `help:"The output file type. Possible values are: \"epub2\", \"epub3\"." short:"t" default:"epub2" placeholder:"<type>"`
OutputDriver string `help:"The method to generate the output file. Available driver: \"pandoc\" (default), \"internal\" (experimental!)" short:"d" placeholder:"<driver>" default:"pandoc"`
CacheDir string `help:"The directory where all cached files will be written to." default:".wiki2book" placeholder:"<dir>"`
StyleFile string `help:"The CSS file that should be used." short:"s" placeholder:"<file>"`
CoverImage string `help:"A cover image for the front cover of the eBook." short:"i" placeholder:"<file>"`
PandocDataDir string `help:"The data directory for pandoc. This enables you to override pandocs defaults for HTML and therefore EPUB generation." short:"p" placeholder:"<dir>"`
FontFiles []string `help:"A list of font files that should be used. They are references in your style file." short:"f" placeholder:"<file> ..."`
ImagesToGrayscale bool `help:"Set to true in order to convert raster images to grayscale." short:"g" default:"false"`
} `cmd:"" help:"Renders a single mediawiki file into an eBook."`
Project struct {
ProjectFile string `help:"A project JSON-file tha should be used to create an eBook." type:"existingfile:" arg:"" placeholder:"<file>"`
OutputFile string `help:"The path to the output file." short:"o" default:"ebook.epub" placeholder:"<file>"`
OutputType string `help:"The output file type. Possible values are: \"epub2\", \"epub3\"." short:"t" default:"epub2" placeholder:"<type>"`
OutputDriver string `help:"The method to generate the output file. Available driver: \"pandoc\" (default), \"internal\" (experimental!)" short:"d" placeholder:"<driver>" default:"pandoc"`
CacheDir string `help:"The directory where all cached files will be written to." placeholder:"<dir>"`
StyleFile string `help:"The CSS file that should be used." short:"s" placeholder:"<file>"`
CoverImage string `help:"A cover image for the front cover of the eBook." short:"i" placeholder:"<file>"`
PandocDataDir string `help:"The data directory for pandoc. This enables you to override pandocs defaults for HTML and therefore EPUB generation." short:"p" placeholder:"<dir>"`
FontFiles []string `help:"A list of font files that should be used. They are references in your style file." short:"f" placeholder:"<file> ..."`
ImagesToGrayscale bool `help:"Set to true in order to convert raster images to grayscale." short:"g"`
} `cmd:"" help:"Uses a project file to create the eBook."`
Article struct {
ArticleName string `help:"The name of the article to render." arg:""`
OutputFile string `help:"The path to the output file." short:"o" default:"ebook.epub" placeholder:"<file>"`
OutputType string `help:"The output file type. Possible values are: \"epub2\", \"epub3\"." short:"t" default:"epub2" placeholder:"<type>"`
OutputDriver string `help:"The method to generate the output file. Available driver: \"pandoc\" (default), \"internal\" (experimental!)" short:"d" placeholder:"<driver>" default:"pandoc"`
CacheDir string `help:"The directory where all cached files will be written to." default:".wiki2book" placeholder:"<dir>"`
StyleFile string `help:"The CSS file that should be used." short:"s" placeholder:"<file>"`
CoverImage string `help:"A cover image for the front cover of the eBook." short:"i" placeholder:"<file>"`
PandocDataDir string `help:"The data directory for pandoc. This enables you to override pandocs defaults for HTML and therefore EPUB generation." short:"p" placeholder:"<dir>"`
FontFiles []string `help:"A list of font files that should be used. They are references in your style file." short:"f" placeholder:"<file> ..."`
ImagesToGrayscale bool `help:"Set to true in order to convert raster images to grayscale." short:"g" default:"false"`
} `cmd:"" help:"Renders a single article into an eBook."`
}
type VersionFlag string
func (v VersionFlag) Decode(ctx *kong.DecodeContext) error { return nil }
func (v VersionFlag) IsBool() bool { return true }
func (v VersionFlag) BeforeApply(app *kong.Kong, vars kong.Vars) error {
fmt.Println(vars["version"])
app.Exit(0)
return nil
}
func main() {
ctx := kong.Parse(
&cli,
kong.Name("wiki2book"),
kong.Description("A CLI tool to turn one or multiple Wikipedia articles into a good-looking eBook."),
kong.Vars{
"version": VERSION,
},
)
if strings.ToLower(cli.Logging) == "debug" {
sigolo.SetDefaultLogLevel(sigolo.LOG_DEBUG)
} else if strings.ToLower(cli.Logging) == "trace" {
sigolo.SetDefaultLogLevel(sigolo.LOG_TRACE)
} else if strings.ToLower(cli.Logging) == "info" {
sigolo.SetDefaultLogLevel(sigolo.LOG_INFO)
sigolo.SetDefaultFormatFunctionAll(sigolo.LogPlain)
} else {
sigolo.SetDefaultFormatFunctionAll(sigolo.LogPlain)
sigolo.Fatalf("Unknown logging level '%s'", cli.Logging)
}
sigolo.Tracef("CLI config:\n%+v", cli)
if cli.Config != "" {
err := config.LoadConfig(cli.Config)
sigolo.FatalCheck(err)
}
if cli.DiagnosticsProfiling {
f, err := os.Create("profiling.prof")
sigolo.FatalCheck(err)
err = pprof.StartCPUProfile(f)
sigolo.FatalCheck(err)
defer pprof.StopCPUProfile()
}
if cli.DiagnosticsTrace {
f, err := os.Create("trace.out")
sigolo.FatalCheck(err)
err = trace.Start(f)
sigolo.FatalCheck(err)
defer trace.Stop()
}
start := time.Now()
switch ctx.Command() {
case "standalone <file>":
generateStandaloneEbook(
cli.Standalone.File,
cli.Standalone.OutputFile,
cli.Standalone.OutputType,
cli.Standalone.OutputDriver,
cli.Standalone.CacheDir,
cli.Standalone.StyleFile,
cli.Standalone.CoverImage,
cli.Standalone.PandocDataDir,
cli.Standalone.FontFiles,
cli.Standalone.ImagesToGrayscale,
cli.ForceRegenerateHtml,
cli.SvgSizeToViewbox,
)
case "project <project-file>":
generateProjectEbook(
cli.Project.ProjectFile,
cli.Project.OutputFile,
cli.Project.OutputType,
cli.Project.OutputDriver,
cli.Project.CacheDir,
cli.Project.StyleFile,
cli.Project.CoverImage,
cli.Project.PandocDataDir,
cli.Project.FontFiles,
cli.Project.ImagesToGrayscale,
cli.ForceRegenerateHtml,
cli.SvgSizeToViewbox,
)
case "article <article-name>":
generateArticleEbook(
cli.Article.ArticleName,
cli.Article.OutputFile,
cli.Article.OutputType,
cli.Article.OutputDriver,
cli.Article.CacheDir,
cli.Article.StyleFile,
cli.Article.CoverImage,
cli.Article.PandocDataDir,
cli.Article.FontFiles,
cli.Article.ImagesToGrayscale,
cli.ForceRegenerateHtml,
cli.SvgSizeToViewbox,
)
default:
if sigolo.GetCurrentLogLevel() > sigolo.LOG_DEBUG {
sigolo.Tracef("CLI config:\n%+v", cli)
}
sigolo.Fatalf("Unknown command: %v", ctx.Command())
}
end := time.Now()
sigolo.Debugf("Start : %s", start.Format(RFC1123Millis))
sigolo.Debugf("End : %s", end.Format(RFC1123Millis))
sigolo.Debugf("Duration: %f seconds", end.Sub(start).Seconds())
}
func generateProjectEbook(projectFile string, outputFile string, outputType string, outputDriver string, cacheDir string, styleFile string, coverImageFile string, pandocDataDir string, fontFiles []string, imagesToGrayscale bool, forceHtmlRecreate bool, svgSizeToViewbox bool) {
var err error
sigolo.Infof("Use project file: %s", projectFile)
sigolo.Debug("Turn paths from CLI arguments into absolute paths before going into the project file directory")
if outputFile != "" {
outputFile, err = util.ToAbsolutePath(outputFile)
sigolo.FatalCheck(err)
}
if cacheDir != "" {
cacheDir, err = util.ToAbsolutePath(cacheDir)
sigolo.FatalCheck(err)
}
if styleFile != "" {
styleFile, err = util.ToAbsolutePath(styleFile)
sigolo.FatalCheck(err)
}
if coverImageFile != "" {
coverImageFile, err = util.ToAbsolutePath(coverImageFile)
sigolo.FatalCheck(err)
}
if pandocDataDir != "" {
pandocDataDir, err = util.ToAbsolutePath(pandocDataDir)
sigolo.FatalCheck(err)
}
if fontFiles != nil && len(fontFiles) > 0 {
fontFiles, err = util.ToAbsolutePaths(fontFiles...)
sigolo.FatalCheck(err)
}
directory, projectFile := filepath.Split(projectFile)
if directory != "" {
sigolo.Debugf("Go into folder %s", directory)
err = os.Chdir(directory)
sigolo.FatalCheck(err)
}
proj, err := project.LoadProject(projectFile)
sigolo.FatalCheck(err)
if outputFile != "" {
sigolo.Tracef("Override outputFile from project file with %s", outputFile)
proj.OutputFile = outputFile
}
if outputType != "" {
sigolo.Tracef("Override outputType from project file with %s", outputType)
proj.OutputType = outputType
}
if outputDriver != "" {
sigolo.Tracef("Override outputDriver from project file with %s", outputDriver)
proj.OutputDriver = outputDriver
}
if cacheDir != "" {
sigolo.Tracef("Override cacheDir from project file with %s", cacheDir)
proj.CacheDir = cacheDir
}
if styleFile != "" {
sigolo.Tracef("Override styleFile from project file with %s", styleFile)
proj.StyleFile = styleFile
}
if coverImageFile != "" {
sigolo.Tracef("Override coverImageFile from project file with %s", coverImageFile)
proj.CoverImage = coverImageFile
}
if pandocDataDir != "" {
sigolo.Tracef("Override pandocDataDir from project file with %s", pandocDataDir)
proj.PandocDataDir = pandocDataDir
}
if fontFiles != nil && len(fontFiles) > 0 {
sigolo.Tracef("Override fontFiles from project file with %v", fontFiles)
proj.FontFiles = fontFiles
}
if imagesToGrayscale {
sigolo.Tracef("Override imagesToGrayscale from project file with %v", imagesToGrayscale)
proj.ImagesToGrayscale = imagesToGrayscale
}
generateBookFromArticles(proj, forceHtmlRecreate, svgSizeToViewbox)
}
func generateStandaloneEbook(inputFile string, outputFile string, outputType string, outputDriver string, cacheDir string, styleFile string, coverImageFile string, pandocDataDir string, fontFiles []string, imagesToGrayscale bool, forceHtmlRecreate bool, svgSizeToViewbox bool) {
var err error
imageCache := "images"
mathCache := "math"
templateCache := "templates"
articleCache := "articles"
htmlOutputFolder := "html"
util.AssertFileExists(styleFile)
util.AssertFileExists(coverImageFile)
err = generator.VerifyOutputAndDriver(outputType, outputDriver)
sigolo.FatalCheck(err)
_, inputFileName := path.Split(inputFile)
title := strings.Split(inputFileName, ".")[0]
fileContent, err := os.ReadFile(inputFile)
sigolo.FatalCheck(err)
file, err := os.Open(outputFile)
sigolo.FatalCheck(err)
// Assign default output file name if given path is a directory
fileInfo, err := file.Stat()
sigolo.FatalCheck(err)
if fileInfo.IsDir() {
// TODO Adjust this when additional non-epub output types are supported.
outputFile = path.Join(outputFile, "standalone.epub")
}
// Make all relevant paths absolute
paths, err := util.ToAbsolutePaths(styleFile, outputFile, coverImageFile, pandocDataDir)
sigolo.FatalCheck(err)
styleFile = paths[0]
outputFile = paths[1]
coverImageFile = paths[2]
pandocDataDir = paths[3]
// Create cache dir and go into it
err = os.MkdirAll(cacheDir, os.ModePerm)
sigolo.FatalCheck(err)
err = os.Chdir(cacheDir)
sigolo.FatalCheck(err)
// Make all relevant paths relative again. This ensures that the locations within the HTML files are independent
// of the systems' directory structure.
paths, err = util.ToRelativePaths(styleFile, outputFile, coverImageFile)
sigolo.FatalCheck(err)
styleFile = paths[0]
outputFile = paths[1]
coverImageFile = paths[2]
tokenizer := parser.NewTokenizer(imageCache, templateCache)
article, err := tokenizer.Tokenize(string(fileContent), title)
sigolo.FatalCheck(err)
err = api.DownloadImages(article.Images, imageCache, articleCache, svgSizeToViewbox, imagesToGrayscale)
sigolo.FatalCheck(err)
// TODO Adjust this when additional non-epub output types are supported.
htmlFilePath := path.Join(htmlOutputFolder, article.Title+".html")
if shouldRecreateHtml(htmlFilePath, forceHtmlRecreate) {
htmlGenerator := &html.HtmlGenerator{
ImageCacheFolder: imageCache,
MathCacheFolder: mathCache,
ArticleCacheFolder: articleCache,
TokenMap: article.TokenMap,
}
htmlFilePath, err = htmlGenerator.Generate(article, htmlOutputFolder, styleFile)
sigolo.FatalCheck(err)
}
sigolo.Infof("Start generating %s file", outputType)
metadata := project.Metadata{
Title: title,
}
err = Generate(outputDriver, []string{htmlFilePath}, outputFile, outputType, styleFile, coverImageFile, pandocDataDir, fontFiles, metadata)
sigolo.FatalCheck(err)
absoluteOutputFile, err := util.ToAbsolutePath(outputFile)
sigolo.FatalCheck(err)
sigolo.Infof("Successfully created %s file %s", outputType, absoluteOutputFile)
}
func generateArticleEbook(articleName string, outputFile string, outputType string, outputDriver string, cacheDir string, styleFile string, coverImageFile string, pandocDataDir string, fontFiles []string, imagesToGrayscale bool, forceHtmlRecreate bool, svgSizeToViewbox bool) {
var articles []string
articles = append(articles, articleName)
proj := project.NewWithDefaults()
proj.Metadata = project.Metadata{}
proj.OutputFile = outputFile
proj.OutputType = outputType
proj.OutputDriver = outputDriver
proj.CacheDir = cacheDir
proj.CoverImage = coverImageFile
proj.StyleFile = styleFile
proj.PandocDataDir = pandocDataDir
proj.Articles = articles
proj.FontFiles = fontFiles
proj.ImagesToGrayscale = imagesToGrayscale
generateBookFromArticles(
proj,
forceHtmlRecreate,
svgSizeToViewbox,
)
}
func generateBookFromArticles(project *project.Project, forceHtmlRecreate bool, svgSizeToViewbox bool) {
var articleFiles []string
var err error
articles := project.Articles
cacheDir := project.CacheDir
styleFile := project.StyleFile
coverImageFile := project.CoverImage
metadata := project.Metadata
outputFile := project.OutputFile
outputType := project.OutputType
outputDriver := project.OutputDriver
pandocDataDir := project.PandocDataDir
fontFiles := project.FontFiles
imagesToGrayscale := project.ImagesToGrayscale
imageCache := "images"
mathCache := "math"
templateCache := "templates"
articleCache := "articles"
htmlOutputFolder := "html"
util.AssertFileExists(styleFile)
util.AssertFileExists(coverImageFile)
err = generator.VerifyOutputAndDriver(outputType, outputDriver)
sigolo.FatalCheck(err)
// Make all relevant paths absolute
paths, err := util.ToAbsolutePaths(styleFile, outputFile, coverImageFile, pandocDataDir)
sigolo.FatalCheck(err)
styleFile = paths[0]
outputFile = paths[1]
coverImageFile = paths[2]
pandocDataDir = paths[3]
// Create cache dir and go into it
sigolo.Debugf("Ensure cache folder '%s'", cacheDir)
err = os.MkdirAll(cacheDir, os.ModePerm)
sigolo.FatalCheck(err)
err = os.Chdir(cacheDir)
sigolo.FatalCheck(err)
// Make all relevant paths relative again. This ensures that the locations within the HTML files are independent
// of the systems' directory structure.
paths, err = util.ToRelativePaths(styleFile, outputFile, coverImageFile)
sigolo.FatalCheck(err)
styleFile = paths[0]
outputFile = paths[1]
coverImageFile = paths[2]
var images []string
for _, articleName := range articles {
sigolo.Infof("Article '%s': Start processing", articleName)
htmlFilePath := filepath.Join(htmlOutputFolder, articleName+".html")
if !shouldRecreateHtml(htmlFilePath, forceHtmlRecreate) {
sigolo.Infof("Article '%s': HTML for article does already exist. Skip parsing and HTML generation.", articleName)
} else {
sigolo.Infof("Article '%s': Download article", articleName)
wikiArticleDto, err := api.DownloadArticle(config.Current.WikipediaInstance, config.Current.WikipediaHost, articleName, articleCache)
sigolo.FatalCheck(err)
sigolo.Infof("Article '%s': Tokenize content", articleName)
tokenizer := parser.NewTokenizer(imageCache, templateCache)
article, err := tokenizer.Tokenize(wikiArticleDto.Parse.Wikitext.Content, wikiArticleDto.Parse.OriginalTitle)
sigolo.FatalCheck(err)
images = append(images, article.Images...)
sigolo.Infof("Article '%s': Download images", articleName)
err = api.DownloadImages(article.Images, imageCache, articleCache, svgSizeToViewbox, imagesToGrayscale)
sigolo.FatalCheck(err)
// TODO Adjust this when additional non-epub output types are supported.
sigolo.Infof("Article '%s': Generate HTML", articleName)
htmlGenerator := &html.HtmlGenerator{
ImageCacheFolder: imageCache,
MathCacheFolder: mathCache,
ArticleCacheFolder: articleCache,
TokenMap: article.TokenMap,
}
htmlFilePath, err = htmlGenerator.Generate(article, htmlOutputFolder, "../css/style.css")
sigolo.FatalCheck(err)
}
sigolo.Infof("Article '%s': Finished processing", articleName)
articleFiles = append(articleFiles, htmlFilePath)
}
images = util.RemoveDuplicates(images)
sigolo.Infof("Start generating %s file", outputType)
err = Generate(outputDriver, articleFiles, outputFile, outputType, styleFile, coverImageFile, pandocDataDir, fontFiles, metadata)
sigolo.FatalCheck(err)
absoluteOutputFile, err := util.ToAbsolutePath(outputFile)
sigolo.FatalCheck(err)
sigolo.Infof("Successfully created %s file %s", outputType, absoluteOutputFile)
}
func Generate(outputDriver string, articleFiles []string, outputFile string, outputType string, styleFile string, coverImageFile string, pandocDataDir string, fontFiles []string, metadata project.Metadata) error {
var err error
switch outputDriver {
case generator.OutputDriverPandoc:
err = epub.Generate(articleFiles, outputFile, outputType, styleFile, coverImageFile, pandocDataDir, fontFiles, metadata)
case generator.OutputDriverInternal:
err = epub.GenerateWithGoLibrary(articleFiles, outputFile, coverImageFile, styleFile, fontFiles, metadata)
default:
err = errors.Errorf("No implementation found for output driver %s", outputDriver)
}
return err
}
func shouldRecreateHtml(htmlFilePath string, forceHtmlRecreate bool) bool {
if forceHtmlRecreate {
return true
}
// Check if HTML file already exists. If so, no recreate is wanted.
_, err := os.Stat(htmlFilePath)
htmlFileExists := err == nil
return !htmlFileExists
}