Skip to content

Instantly share code, notes, and snippets.

@mojavelinux
Last active January 15, 2018 17:42
Show Gist options
  • Save mojavelinux/4402262 to your computer and use it in GitHub Desktop.
Save mojavelinux/4402262 to your computer and use it in GitHub Desktop.
A Ruby script to load and render an AsciiDoc file using AsciiDoctor.
##!/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

Place this script in the parent directory of the Asciidoctor project folder.

Example using built-in templates to output HTML5:

ruby test-asciidoctor.rb samples/test.ad

Example using built-in templates to output DocBook:

ruby test-asciidoctor.rb samples/test.ad docbook45

If you want to use the asciidoctor-backends, checkout that project in the same folder as the script.

Example using Haml templates under the asciidoctor-backends folder.

ruby test-asciidoctor.rb samples/test.ad html5 haml

Example using the Slim templates under the asciidoctor-backends folder.

ruby test-asciidoctor.rb samples/test.ad html5 slim

NOTE: Many of the attributes that are assigned in this script will eventually be initialized by Asciidoctor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment