|
##!/usr/bin/env ruby |
|
|
|
quiet = false |
|
print = false |
|
header_footer = true |
|
while ARGV[0].to_s.start_with?('-') |
|
case ARGV[0] |
|
when '-o' |
|
print = true |
|
ARGV.shift |
|
when '-s' |
|
header_footer = false |
|
ARGV.shift |
|
when '-q' |
|
quiet = true |
|
ARGV.shift |
|
end |
|
end |
|
|
|
#require 'rubygems' |
|
script_start = start = Time.now |
|
puts start unless quiet |
|
require_relative 'asciidoctor/lib/asciidoctor' |
|
elapsed = Time.now - start |
|
puts "Time to load Asciidoctor: #{elapsed}s" unless quiet |
|
|
|
enginelibs = {'haml' => 'haml', 'slim' => 'slim'} |
|
|
|
input = ARGV[0] |
|
backend = ARGV[1] |
|
engine = ARGV[2] |
|
if !input |
|
raise "Please specify an input file" |
|
end |
|
if !File.exist? input |
|
raise "Input file #{input} does not exist" |
|
end |
|
|
|
outputpdf = false |
|
|
|
if !backend |
|
backend = 'html5' |
|
elsif backend == 'pdf' |
|
backend = 'docbook45' |
|
outputpdf = true |
|
end |
|
|
|
basebackend = backend.sub(/\d+/, '') |
|
|
|
=begin |
|
if basebackend == 'html' |
|
start = Time.now |
|
require 'coderay' |
|
elapsed = Time.now - start |
|
puts "Time to load CodeRay: #{elapsed}s" |
|
end |
|
=end |
|
|
|
outfilesuffix = backend =~ /^html/ ? '.html' : '.xml' |
|
output_filetype = outfilesuffix[1..-1] |
|
|
|
template_dir = nil |
|
if engine |
|
template_dir = "asciidoctor-backends/#{backend}-dist/#{engine}" |
|
end |
|
input_dir = File.dirname(input) |
|
input_mtime = File.mtime(input) |
|
|
|
if engine |
|
output_filename = "test-asciidoctor-#{backend}-dist#{outfilesuffix}" |
|
start = Time.now |
|
require enginelibs[engine] |
|
elapsed = Time.now - start |
|
puts "Time to load #{engine}: #{elapsed}s" |
|
else |
|
output_filename = "test-asciidoctor-builtin#{outfilesuffix}" |
|
end |
|
|
|
start = Time.now |
|
lines = File.readlines(input) |
|
doc = Asciidoctor::Document.new(lines, :template_dir => template_dir, :header_footer => header_footer, :attributes => { |
|
'docfile' => File.expand_path(input), |
|
'docdir' => File.expand_path(input_dir), |
|
'docname' => File.basename(input, File.extname(input)), |
|
'docdate' => input_mtime.strftime('%Y-%m-%d'), |
|
'doctime' => input_mtime.strftime('%H:%m:%S %Z'), |
|
'filetype' => output_filetype, |
|
'outfile' => File.join(File.expand_path(input_dir), output_filename), |
|
'outdir' => File.expand_path(input_dir), |
|
'backend' => backend, |
|
'basebackend' => basebackend, |
|
# overrides |
|
#'doctype' => 'book', |
|
#'sectids' => nil, |
|
#'imagesdir' => 'images', |
|
#'safepaths' => false, |
|
#'include-depth' => 0, |
|
'data-uri' => nil, |
|
'icons' => 1 |
|
} ) {|inc| |
|
File.readlines(File.join(input_dir, inc)) rescue [] |
|
} |
|
|
|
=begin |
|
puts doc.attributes.keys |
|
|
|
puts doc.blocks.first.blocks |
|
puts doc.sections.first.sections.first.blocks.first.level |
|
|
|
print the outline |
|
puts doc.attributes |
|
puts doc.doctitle unless doc.doctitle.nil? |
|
doc.blocks.each {|b1| |
|
if b1.context == :section |
|
puts b1.sectnum + ' ' + b1.title |
|
b1.blocks.each {|b2| |
|
if b2.context == :section |
|
puts ' ' + b2.sectnum + ' ' + b2.title |
|
b2.blocks.each {|b3| |
|
if b3.context == :section |
|
puts ' ' + b3.sectnum + ' ' + b3.title |
|
end |
|
} |
|
end |
|
} |
|
end |
|
} |
|
=end |
|
|
|
elapsed = Time.now - start |
|
elapsed_total = elapsed |
|
puts "Time to load and parse document #{input}: #{elapsed}s\n" unless quiet |
|
start = Time.now |
|
output = doc.render |
|
puts output if print && !output.empty? |
|
elapsed = Time.now - start |
|
elapsed_total += elapsed |
|
puts "Time to render document #{input}: #{elapsed}s\n" unless quiet |
|
puts "Time to load, parse and render document #{input}: #{elapsed_total}s\n" unless quiet |
|
puts "Time to run script: #{Time.now - script_start}s\n\n" unless quiet |
|
|
|
# format |
|
# xmllint --format --html --xmlout - | xmllint --format - |
|
|
|
#start = Time.now |
|
File.open(File.join(input_dir, output_filename), 'w+') do |file| |
|
file.write output |
|
end |
|
#elapsed = Time.now - start |
|
#puts "Time to write output: #{elapsed}s" |
|
|
|
if outputpdf |
|
# TODO use nokogiri to run xslt transformation |
|
# write PDF using a2x |
|
# a2x -k -fpdf -dbook --fop docbook-output.xml |
|
# a2x -k -fpdf -darticle --fop docbook-output.xml |
|
%x{a2x -k -fpdf -d#{doc.attr('doctype')} --fop #{File.join(input_dir, output_filename)}} |
|
end |