maliq 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/maliq +1 -1
- data/bin/maliq_gepub +19 -110
- data/lib/maliq.rb +3 -3
- data/lib/maliq/epub.rb +109 -0
- data/lib/maliq/version.rb +1 -1
- metadata +3 -2
data/bin/maliq
CHANGED
@@ -36,7 +36,7 @@ class OptionParser
|
|
36
36
|
EOS
|
37
37
|
|
38
38
|
opt :liquid, "Liquid plugin path", :type => :string
|
39
|
-
opt :seq, "Build consecutive filenames", :default =>
|
39
|
+
opt :seq, "Build consecutive filenames", :default => true
|
40
40
|
opt :nav, "Create nav.xhtml(TOC)", :default => true
|
41
41
|
opt :toc, "Set title for TOC", :default => ''
|
42
42
|
end
|
data/bin/maliq_gepub
CHANGED
@@ -2,127 +2,36 @@
|
|
2
2
|
# encoding: UTF-8
|
3
3
|
require "maliq"
|
4
4
|
require "trollop"
|
5
|
-
require "gepub"
|
6
5
|
|
7
6
|
class OptionParser
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
def self.parse!
|
8
|
+
Trollop::options do
|
9
|
+
version "Maliq #{Maliq::VERSION} (c) 2012 kyoendo"
|
10
|
+
banner ~<<-EOS
|
11
|
+
Maliq is a markdown, liquid converter for EPUB's xhtml.
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
metadata = read_metadata(markdowns.first)
|
15
|
-
generate(metadata, files, opts)
|
16
|
-
end
|
17
|
-
|
18
|
-
def build_opts
|
19
|
-
Trollop::options do
|
20
|
-
version "Maliq #{Maliq::VERSION} (c) 2012 kyoendo"
|
21
|
-
banner ~<<-EOS
|
22
|
-
Maliq is a markdown, liquid converter for EPUB's xhtml.
|
23
|
-
|
24
|
-
'maliq_gepub' command is a tool for generating a EPUB package
|
25
|
-
using Gepub gem.
|
26
|
-
|
27
|
-
Usage:
|
28
|
-
|
29
|
-
1. Install gepub gem(gem install gepub)
|
30
|
-
|
31
|
-
2. Set meta data required for Epub generation in Yaml Front
|
32
|
-
Matter(YFM) of your markdown file.
|
33
|
-
|
34
|
-
3. Create xhtml files with 'maliq' command(see maliq -h).
|
13
|
+
'maliq_gepub' command is a tool for generating a EPUB package
|
14
|
+
using Gepub gem.
|
35
15
|
|
36
|
-
|
16
|
+
Usage:
|
37
17
|
|
38
|
-
|
39
|
-
EOS
|
40
|
-
|
41
|
-
opt :output, "Output Epub filename", :default => 'out.epub'
|
42
|
-
opt :toc, "Add Table of Contents page", :default => true
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
# Returns a Hash, in which filenames are grouped by extname.
|
47
|
-
def read_files
|
48
|
-
Dir['*', '*/*'].group_by { |f| f.ext || :_dir }.to_symkey
|
49
|
-
end
|
50
|
-
|
51
|
-
# Returns a Hash of metadata.
|
52
|
-
def read_metadata(path)
|
53
|
-
unless path
|
54
|
-
abort "Must exist a markdown filename which have Yaml Front Matter."
|
55
|
-
end
|
56
|
-
yml, _ = Maliq::FileUtils.retrieveYFM(File.read path)
|
57
|
-
return nil if yml.empty?
|
58
|
-
YAML.load(yml).to_symkey
|
59
|
-
end
|
18
|
+
1. Install gepub gem(gem install gepub)
|
60
19
|
|
61
|
-
|
62
|
-
|
63
|
-
metadata.select! { |k, _| meta_nodes.include? k.to_s }
|
64
|
-
csses = files.values_atx(:css)
|
65
|
-
images = files.values_atx(:png, :jpg, :jpeg, :gif, :bmp, :tiff)
|
66
|
-
cover_images, images = images.partition { |f| File.basename(f, '.*').match /^cover/ }
|
67
|
-
xhtmls = files.values_atx(:xhtml, :html)
|
68
|
-
cover_xhtml = xhtmls.delete('cover.xhtml')
|
20
|
+
2. Set meta data required for Epub generation in Yaml Front
|
21
|
+
Matter(YFM) of your markdown file.
|
69
22
|
|
70
|
-
|
71
|
-
if (cover = cover_images.first) && !cover_xhtml
|
72
|
-
cover_xhtml = create_cover_page(cover)
|
73
|
-
end
|
23
|
+
3. Create xhtml files with 'maliq' command(see maliq -h).
|
74
24
|
|
75
|
-
|
25
|
+
4. fire up 'maliq_gepub' on the direcroty.
|
76
26
|
|
77
|
-
|
78
|
-
|
79
|
-
xhtmls.map! { |f| xhtml_with_heading f }
|
80
|
-
|
81
|
-
GEPUB::Builder.new do
|
82
|
-
metadata.each { |k, v| send k, *Array(v) }
|
83
|
-
|
84
|
-
resources(:workdir => '.') {
|
85
|
-
csses.each { |f| file f }
|
86
|
-
cover_image cover if cover
|
87
|
-
images.each { |f| file(f) }
|
88
|
-
ordered {
|
89
|
-
if cover_xhtml
|
90
|
-
file 'cover.xhtml' => cover_xhtml
|
91
|
-
heading 'Cover'
|
92
|
-
end
|
93
|
-
|
94
|
-
nav navfile if navfile
|
95
|
-
|
96
|
-
xhtmls.each do |fname, head|
|
97
|
-
file fname
|
98
|
-
heading head if !navfile || heading_files.include?(fname)
|
99
|
-
end
|
100
|
-
}
|
101
|
-
}
|
102
|
-
end.generate_epub(opts[:output])
|
103
|
-
end
|
104
|
-
|
105
|
-
def create_cover_page(cover)
|
106
|
-
out = Maliq::Converter.new("![cover](#{cover})", title:'Cover').run
|
107
|
-
StringIO.new(out)
|
108
|
-
end
|
109
|
-
|
110
|
-
def pick_nav_file(xhtmls)
|
111
|
-
navs, xhtmls = xhtmls.partition { |fname| fname.match /^(nav|toc)\.x*html$/ }
|
112
|
-
nav = navs.empty? ? nil : navs.first
|
113
|
-
return nav, xhtmls
|
114
|
-
end
|
115
|
-
|
116
|
-
def get_heading_files(navfile)
|
117
|
-
File.read(navfile).scan(/\w+\.xhtml(?=.*?<\/li>)/) if navfile
|
118
|
-
end
|
27
|
+
where [options] are:
|
28
|
+
EOS
|
119
29
|
|
120
|
-
|
121
|
-
|
122
|
-
File.read(xhtml).match(/<h(?:1|2|3)>(.*?)<\/h(?:1|2|3)>/) { heading = $1 }
|
123
|
-
[xhtml, heading]
|
30
|
+
opt :output, "Output Epub filename", :default => 'out.epub'
|
31
|
+
opt :toc, "Add Table of Contents page", :default => true
|
124
32
|
end
|
125
33
|
end
|
126
34
|
end
|
127
35
|
|
128
|
-
OptionParser.parse!
|
36
|
+
opts = OptionParser.parse!
|
37
|
+
Maliq::Epub.new(opts).create!
|
data/lib/maliq.rb
CHANGED
data/lib/maliq/epub.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require "gepub"
|
3
|
+
|
4
|
+
class Maliq::Epub
|
5
|
+
def initialize(opts)
|
6
|
+
@opts = opts
|
7
|
+
@output = @opts.delete(:output) || 'out.epub'
|
8
|
+
@path = opts[:path] || '.'
|
9
|
+
set_files_by_type(@path)
|
10
|
+
@metadata = read_metadata
|
11
|
+
end
|
12
|
+
|
13
|
+
def create!
|
14
|
+
# Instance variables need to be locals, because a block of,
|
15
|
+
# GEPUB::Builder.new changes its context with instance_eval.
|
16
|
+
metadata = @metadata
|
17
|
+
csses = @csses
|
18
|
+
cover_img = @cover_image
|
19
|
+
images = @images
|
20
|
+
cover_xhtml = @cover_xhtml
|
21
|
+
navfile = @navfile
|
22
|
+
heading_files = @heading_files
|
23
|
+
xhtmls = @xhtmls
|
24
|
+
path = @path
|
25
|
+
|
26
|
+
GEPUB::Builder.new do
|
27
|
+
metadata.each { |k, *v| send k, *v }
|
28
|
+
|
29
|
+
resources(:workdir => path) {
|
30
|
+
csses.each { |f| file f }
|
31
|
+
cover_image cover_img if cover_img
|
32
|
+
images.each { |f| file(f) }
|
33
|
+
ordered {
|
34
|
+
if cover_xhtml
|
35
|
+
file 'cover.xhtml' => cover_xhtml
|
36
|
+
heading 'Cover'
|
37
|
+
end
|
38
|
+
|
39
|
+
nav navfile if navfile
|
40
|
+
|
41
|
+
xhtmls.each do |fname, head|
|
42
|
+
file fname
|
43
|
+
heading head if !navfile || heading_files.include?(fname)
|
44
|
+
end
|
45
|
+
}
|
46
|
+
}
|
47
|
+
end.generate_epub(@output)
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
# Returns a Hash, in which filenames are grouped by extname.
|
52
|
+
def set_files_by_type(path)
|
53
|
+
files = Dir.chdir(path) { Dir["*", "*/*"].group_by { |f| f.ext || :_dir }.to_symkey }
|
54
|
+
@mds = files.values_atx(:md, :markdown)
|
55
|
+
xhtmls = files.values_atx(:xhtml, :html)
|
56
|
+
@csses = files.values_atx(:css)
|
57
|
+
images = files.values_atx(:png, :jpg, :jpeg, :gif, :bmp, :tiff)
|
58
|
+
(@cover_image, *_), @images =
|
59
|
+
images.partition { |f| File.basename(f, '.*').match /^cover/ }
|
60
|
+
|
61
|
+
@cover_xhtml =
|
62
|
+
if cover = xhtmls.delete('cover.xhtml')
|
63
|
+
cover
|
64
|
+
elsif @cover_image
|
65
|
+
# Create cover page if page not provided for a cover image.
|
66
|
+
create_cover_page(@cover_image)
|
67
|
+
end
|
68
|
+
|
69
|
+
@navfile, xhtmls = pick_nav_file(xhtmls)
|
70
|
+
|
71
|
+
@heading_files = get_heading_files(@navfile)
|
72
|
+
|
73
|
+
@xhtmls = xhtmls.map { |f| xhtml_with_heading f }
|
74
|
+
end
|
75
|
+
|
76
|
+
# Returns a Hash of metadata.
|
77
|
+
def read_metadata
|
78
|
+
path = @mds.first
|
79
|
+
unless path
|
80
|
+
abort "Must exist a markdown filename which have Yaml Front Matter."
|
81
|
+
end
|
82
|
+
yml, _ = Maliq::FileUtils.retrieveYFM(File.read path)
|
83
|
+
return nil if yml.empty?
|
84
|
+
metadata = YAML.load(yml).to_symkey
|
85
|
+
|
86
|
+
meta_nodes = GEPUB::Metadata::CONTENT_NODE_LIST + ["unique_identifier"]
|
87
|
+
metadata.select { |k, _| meta_nodes.include? k.to_s }
|
88
|
+
end
|
89
|
+
|
90
|
+
def create_cover_page(cover)
|
91
|
+
out = Maliq::Converter.new("![cover](#{cover})", title:'Cover').run
|
92
|
+
StringIO.new(out)
|
93
|
+
end
|
94
|
+
|
95
|
+
def pick_nav_file(xhtmls)
|
96
|
+
(nav, *_), xhtmls = xhtmls.partition { |fname| fname.match /^(nav|toc)\.x*html$/ }
|
97
|
+
return nav, xhtmls
|
98
|
+
end
|
99
|
+
|
100
|
+
def get_heading_files(navfile)
|
101
|
+
File.read(navfile).scan(/\w+\.xhtml(?=.*?<\/li>)/) if navfile
|
102
|
+
end
|
103
|
+
|
104
|
+
def xhtml_with_heading(xhtml)
|
105
|
+
heading = File.basename(xhtml, '.*').capitalize
|
106
|
+
File.read(xhtml).match(/<h(?:1|2|3)>(.*?)<\/h(?:1|2|3)>/) { heading = $1 }
|
107
|
+
[xhtml, heading]
|
108
|
+
end
|
109
|
+
end
|
data/lib/maliq/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: maliq
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: trollop
|
@@ -110,6 +110,7 @@ files:
|
|
110
110
|
- lib/maliq.rb
|
111
111
|
- lib/maliq/converter.rb
|
112
112
|
- lib/maliq/create.rb
|
113
|
+
- lib/maliq/epub.rb
|
113
114
|
- lib/maliq/file_utils.rb
|
114
115
|
- lib/maliq/system_extensions.rb
|
115
116
|
- lib/maliq/version.rb
|