From 18b91d8d09faa1c9b125d59c380c84258c7d3f3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=92=E1=85=A9=E1=86=BC=20=E1=84=89=E1=85=A5=E1=86=BC?= =?UTF-8?q?=E1=84=92=E1=85=AE=E1=86=AB?= Date: Fri, 27 Sep 2013 13:17:39 +0900 Subject: [PATCH] "output" option(-o) activated - default output path is "output". --- Thorfile | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/Thorfile b/Thorfile index f6353f2..3a9689e 100644 --- a/Thorfile +++ b/Thorfile @@ -11,6 +11,7 @@ class ::MD < Thor # :: is used to escape Thor::Sandbox DEFAULT_LAYOUT = File.join(DEFAULT_PATH, "layout.html") DEFAULT_HTML_CSS = File.join(DEFAULT_PATH, "css", "html.css") DEFAULT_PDF_CSS = File.join(DEFAULT_PATH, "css", "pdf.css") + DEFAULT_OUTPUT_PATH = File.expand_path("output") module Config MarkdownDefaults = { @@ -57,7 +58,11 @@ class ::MD < Thor # :: is used to escape Thor::Sandbox begin if FORMATS.include?(options[:format]) - self.send("generate_#{options[:format]}", md_file) + if FORMATS.include?(options[:output]) + self.send("generate_#{options[:format]}", md_file, DEFAULT_OUTPUT_PATH) + else + self.send("generate_#{options[:format]}", md_file, options[:output]) + end else raise "Unknown format" end @@ -94,30 +99,43 @@ class ::MD < Thor # :: is used to escape Thor::Sandbox raise "pdf.css not found." if !File.exists?(DEFAULT_PDF_CSS) && options[:format] == "pdf" end - def generate_html(md_file) + def generate_html(md_file, output) renderer = Redcarpet::Markdown.new(Config.renderer, Config.markdown_options) filename = File.basename(md_file, '.*') doc = File.read(md_file) template = Liquid::Template.parse(File.read(DEFAULT_LAYOUT)) - File.open("#{filename}.html", 'w') do |f| + output_path = File.expand_path(output); + + unless Dir.exists?(output_path) + FileUtils.mkdir_p output_path; + end + + File.open(output_path+"/"+"#{filename}.html", 'w') do |f| f.write(template.render('content' => renderer.render(doc), 'title' => filename, 'stylesheet' => DEFAULT_HTML_CSS)) end end - def generate_pdf(md_file) - generate_html(md_file) - + def generate_pdf(md_file, output) + generate_html(md_file, output) filename = File.basename(md_file, '.*') - html_path = "#{filename}.html" - pdf_path = "#{filename}.pdf" + output_path = File.expand_path(output); + html_path = output_path+"/"+"#{filename}.html" + + unless Dir.exists?(output_path) + FileUtils.mkdir_p output_path; + end + + pdf_path = output_path+"/"+"#{filename}.pdf" + command = pdf_command + " -o #{pdf_path} #{html_path}" + system(command) # Remove temp html file - FileUtils.rm("#{filename}.html") + FileUtils.rm(File.expand_path(html_path)); end def pdf_command