-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
138 lines (120 loc) · 3.22 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
package main
import (
"fmt"
"log"
"os"
"strings"
"time"
)
const version = "1.1.0"
func showUsage() {
usage := `Create/Batch Create/Pack/Extract EPUB file(s). Merge HTML/Text files.
It can also work as a web server to convert an uploaded zip file to an EPUB.
Please refer to manual for detailed usage.
COMMAND LINE
Create : makeepub <VirtualFolder> [OutputFolder] [-epub2] [-noduokan]
Batch Create : makeepub -b <InputFolder> [OutputFolder] [-epub2] [-noduokan]
makeepub -b <BatchFile> [OutputFolder] [-epub2] [-noduokan]
Pack : makeepub -p <VirtualFolder> <OutputFile>
Extract : makeepub -e <EpubFile> <OutputFolder>
Merge HTML : makeepub -mh <VirtualFolder> <OutputFile>
Merge Text : makeepub -mt <VirtualFolder> <OutputFile>
Web Server : makeepub -s [Port]
ARGUMENT
VirtualFolder: An OS folder or a zip file which contains the input files.
OutputFolder : An OS folder to store the output file(s).
-epub2 : Generate books using EPUB2 format, otherwise EPUB3.
-noduokan : Disable DuoKan externsion.
InputFolder : An OS folder which contains the input folder(s)/file(s).
BatchFile : A text which lists the path of 'VirtualFolders' to be
processed, one line for one 'VirtualFolder'
OutputFile : The path of the output file.
EpubFile : The path of an EPUB file.
Port : The TCP port to listen to, default value is 80.
`
fmt.Print(usage)
os.Exit(0)
}
func onCommandLineError() {
logger.Fatalln("invalid command line. see 'makeepub -?'")
}
func getArg(index int, dflt string) string {
count := 0
for _, arg := range os.Args[1:] {
if !isFlag(arg) {
if count == index {
return arg
}
count++
}
}
return dflt
}
func getFlag(index int) string {
count := 0
for _, arg := range os.Args[1:] {
if isFlag(arg) {
if count == index {
return arg[1:]
}
count++
}
}
return ""
}
func isFlag(arg string) bool {
if os.PathSeparator == '/' {
return arg[0] == '-'
}
return arg[0] == '-' || arg[0] == '/'
}
func getFlagBool(flag string) bool {
flag = strings.ToLower(flag)
for _, arg := range os.Args[1:] {
if isFlag(arg) && strings.ToLower(arg[1:]) == flag {
return true
}
}
return false
}
type CommandHandler struct {
command string
handler func()
}
var (
logger = log.New(os.Stderr, "makeepub: ", 0)
handlers = make([]CommandHandler, 0, 8)
)
func AddCommandHandler(cmd string, handler func()) {
for _, h := range handlers {
if h.command == cmd {
logger.Fatalf("handler for command '%s' already exists.\n", cmd)
}
}
handlers = append(handlers, CommandHandler{command: cmd, handler: handler})
}
func findCommandHandler(cmd string) func() {
if !isFlag(cmd) {
return RunMake
}
cmd = strings.ToLower(cmd[1:])
for _, h := range handlers {
if cmd == h.command {
return h.handler
}
}
return onCommandLineError
}
func main() {
fmt.Println("makeepub v" + version + ", home page: https://github.com/localvar/makeepub")
if len(os.Args) < 2 {
onCommandLineError()
}
AddCommandHandler("?", showUsage)
AddCommandHandler("h", showUsage)
handler := findCommandHandler(os.Args[1])
start := time.Now()
handler()
logger.Println("done, time used:", time.Now().Sub(start).String())
os.Exit(0)
}