Class: Mkbook::MakeBook
- Inherits:
-
Object
- Object
- Mkbook::MakeBook
- Defined in:
- lib/mkbook/make_book.rb
Constant Summary collapse
- HERE =
Dir.getwd
- ROOT =
File.dirname(__FILE__)
- TEMPLATE =
File.join(ROOT, "..", "template")
- SETTING_FILE =
".mkbook.yml"
Class Method Summary collapse
Instance Method Summary collapse
- #add(template, name) ⇒ Object
- #build(params) ⇒ Object
- #check_environment ⇒ Object
- #create(params) ⇒ Object
- #generate_main_file ⇒ Object
- #get_name(template, name = nil) ⇒ Object
- #get_rank(prefix) ⇒ Object
- #initializes(params) ⇒ Object
- #latex2pdf ⇒ Object
- #list_template ⇒ Object
- #load ⇒ Object
- #load_config ⇒ Object
- #markdown2html(regex) ⇒ Object
- #markdown2index(regex) ⇒ Object
- #markdown2latex(regex) ⇒ Object
- #mkdir(*string) ⇒ Object
- #save ⇒ Object
- #save_config ⇒ Object
- #save_setting ⇒ Object
- #show ⇒ Object
- #touch(*string) ⇒ Object
Class Method Details
.find(params) ⇒ Object
22 23 24 25 26 |
# File 'lib/mkbook/make_book.rb', line 22 def self.find(params) book = new book.initializes(params) book end |
Instance Method Details
#add(template, name) ⇒ Object
146 147 148 149 |
# File 'lib/mkbook/make_book.rb', line 146 def add(template, name) name = get_name(template, name) touch(@lang, template, name) end |
#build(params) ⇒ Object
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 |
# File 'lib/mkbook/make_book.rb', line 38 def build(params) @project = params[:dir_source] @setting_file = File.join(@project, SETTING_FILE) @setting = YAML.load_file(@setting_file) @setting = {} unless @setting @setting.merge!(params) @setting[:name] ||= File.basename(@project) load check_environment load_config @setting[:build].each do |fmt| case fmt when 'pdf' then @preface = markdown2latex(@file_preface) @chapters = markdown2latex(@file_chapters) @appendix = markdown2latex(@file_appendix) generate_main_file latex2pdf when 'index' then index = {} index[:preface] = markdown2index(@file_preface) index[:chapters]= markdown2index(@file_chapters) index[:appendix]= markdown2index(@file_appendix) FileUtils.mkdir_p(@dir_html) unless Dir.exist?(@dir_html) IO.write(File.join(@dir_html, 'index.yml'), index.to_yaml) when 'html' then markdown2html(@file_preface) markdown2html(@file_chapters) markdown2html(@file_appendix) end end save_config save_setting end |
#check_environment ⇒ Object
156 157 158 159 160 161 162 163 |
# File 'lib/mkbook/make_book.rb', line 156 def check_environment missing = ['pandoc', 'xelatex'].reject { |command| Utils.command_exists?(command) } unless missing.empty? Utils.log_error "Missing dependencies: #{missing.join(', ')}." puts "\n\tInstall these and try again." exit end end |
#create(params) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/mkbook/make_book.rb', line 28 def create(params) @project = File.join(params[:workspace], params[:name]) @setting_file = File.join(TEMPLATE, SETTING_FILE) @setting = YAML.load_file(@setting_file) @setting.merge!(params) @setting[:name] ||= File.basename(@project) load save end |
#generate_main_file ⇒ Object
259 260 261 262 263 264 265 266 267 268 |
# File 'lib/mkbook/make_book.rb', line 259 def generate_main_file Dir.chdir(@project) FileUtils.mkdir_p(@dir_latex) unless Dir.exist?(@dir_latex) Utils.log_info("Generate main.tex file ...\n") target = File.join(@dir_latex, "main.tex") source = File.join(ROOT, "template_#{@template}.tex") template = ERB.new(File.read(source)) IO.write(target, template.result(binding)) end |
#get_name(template, name = nil) ⇒ Object
133 134 135 136 137 138 |
# File 'lib/mkbook/make_book.rb', line 133 def get_name(template, name=nil) prefix = template[0..3] rank = get_rank(prefix) name = template[9..-4] if name.nil? "#{prefix}#{rank}-#{name}.md" end |
#get_rank(prefix) ⇒ Object
140 141 142 143 144 |
# File 'lib/mkbook/make_book.rb', line 140 def get_rank(prefix) Dir.chdir(File.join(@project, "src")) num = Dir["#{prefix}*"].length + 10 100*num end |
#initializes(params) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/mkbook/make_book.rb', line 12 def initializes(params) @project = params[:project] @setting_file = File.join(@project, SETTING_FILE) @setting_file = File.join(TEMPLATE, SETTING_FILE) unless File.exist?(@setting_file) @setting = YAML.load_file(@setting_file) @setting.merge!(params) @setting[:name] ||= File.basename(@project) load end |
#latex2pdf ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/mkbook/make_book.rb', line 270 def latex2pdf Dir.chdir(@project) Dir.mkdir(@dir_output) unless Dir.exist?(@dir_output) Utils.log_info("Run xelatex main.tex ...\n") num = @final ? 2 : 1 num.times do |i| IO.popen("xelatex -output-directory='#{@dir_latex}' #{@dir_latex}/main.tex 2>&1") do |pipe| while line = pipe.gets STDERR.print line if @debug end end Utils.log_info("Run xelatex main.tex #{i+1} time(s).\n") end Utils.log_info("Generate PDF Complete!\n") source = File.join(@dir_latex, "main.pdf") target = File.join(@dir_output, "#{@name}.#{@lang}.pdf") Utils.log_info("Moving output to #{target}.\n") FileUtils.cp(source, target) end |
#list_template ⇒ Object
151 152 153 154 |
# File 'lib/mkbook/make_book.rb', line 151 def list_template Dir.chdir(File.join(TEMPLATE, @lang)) Dir["*.md"] end |
#load ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/mkbook/make_book.rb', line 73 def load @name = @setting[:name] @lang = @setting[:lang] @genre = @setting[:genre] @debug = @setting[:debug] @final = @setting[:final] @option = @setting[:option] @format = @setting[:format] || 'markdown' @template = @setting[:template] @dir_output = @setting[:dir_output] || File.join(@project, "out") @dir_latex = File.join(@dir_output, ".tex") @dir_html = File.join(@dir_output, "html") @file_preface = @setting[:file_preface] @file_chapters = @setting[:file_chapters] @file_appendix = @setting[:file_appendix] end |
#load_config ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/mkbook/make_book.rb', line 165 def load_config @config_file = File.join(ROOT, "config.yml") if File.exists? @config_file configs = YAML.load_file(@config_file) @config = configs["default"] @config.merge!(configs[@lang]) if configs[@lang] end @config_file = File.join(@project, "src", "config.yml") if File.exists? @config_file configs = YAML.load_file(@config_file) @config.merge!(configs) if configs end end |
#markdown2html(regex) ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/mkbook/make_book.rb', line 241 def markdown2html(regex) Dir.chdir(@project) FileUtils.mkdir_p(@dir_html) unless Dir.exist?(@dir_html) files = File.join("src", regex) Utils.log_info("Parsing markdown ... #{files}:\n") Dir["#{files}"].sort.map do |file| puts "\t\033[32mconvert\033[0m #{file}" markdown = IO.read(file) html = IO.popen("pandoc --no-wrap --chapters -f #{@format} -t html", 'w+') do |pipe| pipe.write(markdown) pipe.close_write pipe.read end IO.write(File.join(@dir_html, "#{File.basename(file, '.md')}.htm"), html) end end |
#markdown2index(regex) ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/mkbook/make_book.rb', line 218 def markdown2index(regex) Dir.chdir(@project) files = File.join("src", regex) Utils.log_info("Indexing markdown ... #{files}:\n") index_string = [] Dir["#{files}"].sort.map do |file| title_raw = IO.readlines(file)[0] title = if title_raw.nil? || title_raw.strip.empty? '未命名' else title_raw .chomp # 删除换行符 .gsub(/^#+\s*/, '') # 删除行首的 # 号 .gsub(/\s*\{.*?\}\s*$/, '') # 删除行尾的标识符 .gsub(/\s*#+\s*$/, '') # 删除行尾的 # 号 end name = file.gsub(/^src\//, '') index_string << {name: name, title: title} end index_string end |
#markdown2latex(regex) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/mkbook/make_book.rb', line 197 def markdown2latex(regex) Dir.chdir(@project) files = File.join("src", regex) Utils.log_info("Parsing markdown ... #{files}:\n") markdown = Dir["#{files}"].sort.map do |file| puts "\t\033[32minclude\033[0m #{file}" IO.read(file) end.join("\n\n") Utils.log_info("Convert markdown into latex ... ") latex = IO.popen("pandoc --no-wrap --chapters -f #{@format} -t latex", 'w+') do |pipe| pipe.write(Utils.pre_pandoc(markdown)) pipe.close_write Utils.post_pandoc(pipe.read) end puts "done" return latex end |
#mkdir(*string) ⇒ Object
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/mkbook/make_book.rb', line 102 def mkdir(*string) target = File.join(@project, string) file = File.join(@name, string) unless Dir.exist?(target) puts "\t\033[32mcreate\033[0m #{file}" FileUtils.mkdir_p(target) else puts "\t\033[31mexists\033[0m #{file}" end end |
#save ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/mkbook/make_book.rb', line 90 def save Utils.log_info("Generate project #{@name.upcase}\n") mkdir mkdir("src") mkdir("images") touch("images", ".keep") mkdir("resources") touch("resources", ".keep") touch(".gitignore") save_setting end |
#save_config ⇒ Object
180 181 182 |
# File 'lib/mkbook/make_book.rb', line 180 def save_config IO.write(@config_file, @config.to_yaml) end |
#save_setting ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/mkbook/make_book.rb', line 184 def save_setting @setting.delete(:command) @setting.delete(:dir_source) @setting.delete(:dir_output) @setting.delete(:dir_latex) @setting.delete(:workspace) @setting.delete(:project) @setting.delete(:final) @setting.delete(:debug) @setting_file = File.join(@project, SETTING_FILE) IO.write(@setting_file, @setting.to_yaml) end |
#show ⇒ Object
292 293 294 |
# File 'lib/mkbook/make_book.rb', line 292 def show puts @project end |
#touch(*string) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/mkbook/make_book.rb', line 113 def touch(*string) if string.length < 3 source = File.join(TEMPLATE, string) target = File.join(@project, string) else source = File.join(TEMPLATE, string[0..-2]) string.delete_at(-2) string[0] = "src" target = File.join(@project, string) end file = File.join(@name, string) unless File.exist?(target) puts "\t\033[32mcreate\033[0m #{file}" template = ERB.new(File.read(source)) IO.write(target, template.result(binding)) else puts "\t\033[31mexists\033[0m #{file}" end end |