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
|
# File 'lib/asciibook/command.rb', line 6
def self.execute(argv)
p = Mercenary::Program.new(:asciibook)
p.version Asciibook::VERSION
p.description 'Asciibook is a ebook generator from Asciidoc to html/pdf/epub'
p.syntax 'asciibook <command> [options]'
p.command(:new) do |c|
c.description 'Create a new book scaffold in PATH'
c.syntax 'new PATH'
c.action do |args, options|
path = args[0]
if path
FileUtils.mkdir_p path
template_dir = File.expand_path('../../../book_template', __FILE__)
files = Dir.glob('*', base: template_dir).map { |file| File.join(template_dir, file) }
FileUtils.cp_r files, path
else
abort "Please specify PATH to create book"
end
end
end
p.command(:init) do |c|
c.description 'Init a asciibook config for a Asciidoc file'
c.syntax 'init FILE'
c.action do |args, options|
source = args[0]
if File.file?(source)
dir = File.dirname source
filename = File.basename source
File.open(File.join(dir, 'asciibook.yml'), 'w') do |file|
file.write " source: \#{filename}\n\n # formats:\n # - html\n # - pdf\n # - epub\n #\n # theme_dir:\n # template_dir:\n # page_level: 1\n #\n # plugins:\n # - asciidoctor-diagram\n EOF\n end\n else\n abort \"Please specify the Asciidoc document to build\"\n end\n end\n end\n\n p.command(:build) do |c|\n c.description 'Build book'\n c.syntax 'build [FILE|DIR]'\n c.option :formats, '--format FORMAT1[,FORMAT2[,FORMAT3...]]', Array, 'Formats you want to build, allow: html,pdf,epub, default is all.'\n c.option :theme_dir, '--theme-dir DIR', 'Theme dir.'\n c.option :template_dir, '--template-dir DIR', 'Template dir.'\n c.option :dest_dir, '--dest-dir DIR', 'Destination dir.'\n c.option :page_level, '--page-level NUM', Integer, 'Page split base on section level, default is 1.'\n c.option :plugins, '--plugin PLUGIN1[,PLUGIN2[,PLUGIN3...]]', Array, 'Require ruby gem or ruby script plugin.'\n c.action do |args, options|\n source = args[0] || '.'\n if File.directory?(source)\n config_options = YAML.safe_load(File.read(File.join(source, 'asciibook.yml'))).reduce({}) do |hash, (key, value)|\n hash[key.to_sym] = %w(source theme_dir template_dir dest_dir).include?(key) ? File.join(source, value) : value\n hash\n end\n options = config_options.merge(options)\n load_plugins(options[:plugins])\n Asciibook::Book.load_file(options.delete(:source), options).build\n elsif File.file?(source)\n load_plugins(options[:plugins])\n Asciibook::Book.load_file(source, options).build\n else\n abort \"Build target '\#{source}' neither a folder nor a file\"\n end\n end\n end\n\n p.action do |args, _|\n puts p\n end\n\n p.go(argv)\nend\n"
|