-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspell.go
134 lines (115 loc) · 3.63 KB
/
spell.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/behringer24/argumentative"
"github.com/writingtoole/epub"
)
const (
title = "spell"
description = "Smart Processing and Enhanced Lightweight Layout"
version = "v0.0.5"
)
var (
inFileName *string
outFileName *string
generateVer *string
generateCover *bool
showHelp *bool
showVer *bool
)
// Function for reading a file
func readFile(filename string) (string, error) {
data, err := os.ReadFile(filename)
if err != nil {
return "", err
}
return string(data), nil
}
// Replace all includes of md files using markdown syntax for images like
//  or
// 
// text is optional and ignored, you can use it as internal reference
func replaceAllIncludes(content string, baseDir string) string {
commandRegex := regexp.MustCompile(`\!\[include\]\(([^ \)]+)\s*(\"([^\"]*)\")?\)`)
return commandRegex.ReplaceAllStringFunc(content, func(match string) string {
// Extract includes and parameters
matches := commandRegex.FindStringSubmatch(match)
if len(matches) < 2 && strings.Compare(filepath.Ext(matches[2]), ".md") != 0 {
log.Printf("Error including %s with URI %s", matches[0], matches[1])
return match // Fallback: if the pattern is wrong or not an md file
}
includeContent, err := readFile(filepath.Join(baseDir, matches[1]))
if err != nil {
log.Printf("Error including %s with URI %s: %v", matches[0], matches[1], err)
return match
}
log.Printf("Including markdown file %s (%s)", matches[1], matches[3])
return includeContent
})
}
// Process Markdown file
func processMarkdownFile(book *epub.EPub, filePath string) error {
// Read markdown file
content, err := readFile(filePath)
if err != nil {
return err
}
// Replace all includes
baseDir := filepath.Dir(filePath)
content = replaceAllIncludes(content, baseDir)
// Parse markdown
err = parseMarkdown(book, content, baseDir)
if err != nil {
return err
}
return nil
}
// Parse command line parameters
func parseArgs() {
flags := &argumentative.Flags{}
showHelp = flags.Flags().AddBool("help", "h", "Show this help text")
showVer = flags.Flags().AddBool("version", "v", "Show version information")
generateCover = flags.Flags().AddBool("cover", "c", "Generate cover page. This is normally not recommended")
generateVer = flags.Flags().AddString("epub", "e", false, "3", "Generate epub version 2 or 3")
inFileName = flags.Flags().AddPositional("infile", true, "", "File to read from")
outFileName = flags.Flags().AddPositional("outfile", false, "./ebook.epub", "File to write to")
err := flags.Parse(os.Args)
if *showHelp {
flags.Usage(title, description, nil)
os.Exit(0)
} else if *showVer {
fmt.Print(strings.ToUpper(title), " version: ", version)
os.Exit(0)
} else if strings.Compare(*generateVer, "2") != 0 && strings.Compare(*generateVer, "3") != 0 {
fmt.Print("Error: epub version has to be 2 or 3")
os.Exit(1)
} else if err != nil {
flags.Usage(title, description, err)
os.Exit(1)
}
}
func main() {
// Use argumentative as command line parser
parseArgs()
// Create new empty epub book
book := epub.New()
// Process input file
err := processMarkdownFile(book, *inFileName)
if err != nil {
log.Fatalf("Fehler beim Verarbeiten der Datei '%s': %v", *inFileName, err)
}
// Save epub
epubVersion, _ := strconv.Atoi(*generateVer)
book.SetVersion(float64(epubVersion))
err = book.Write(*outFileName)
if err != nil {
log.Fatalf("Fehler beim Speichern der EPUB-Datei '%s': %v", *outFileName, err)
}
fmt.Printf("EPUB-Datei '%s' erfolgreich erstellt!\n", *outFileName)
}