Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"output" option(-o) activated #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions Thorfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down