diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rwxr-xr-x | bin/asciidoctor-pdf | 22 | ||||
| -rw-r--r-- | lib/asciidoctor-pdf/implicit_header_processor.rb | 63 |
3 files changed, 79 insertions, 7 deletions
@@ -1,3 +1,4 @@ +/_*/ /Gemfile.lock /pkg/ /.ruby-gemset diff --git a/bin/asciidoctor-pdf b/bin/asciidoctor-pdf index b5b5927a..46d1553f 100755 --- a/bin/asciidoctor-pdf +++ b/bin/asciidoctor-pdf @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -attributes = %w(idprefix=@ listing-caption=Listing@) +attributes = %w(idprefix=@ listing-caption=Listing@ buildfor-editor) while ARGV[0] == '-a' ARGV.shift attributes << ARGV.shift @@ -23,6 +23,7 @@ require 'ostruct' require 'asciidoctor' require 'asciidoctor/pdf_renderer' require 'asciidoctor/theme_loader' +require 'asciidoctor-pdf/implicit_header_processor' require 'tilt' require 'slim' @@ -30,11 +31,11 @@ DataDir = File.join File.dirname(__FILE__), '..', 'data' ThemesDir = File.join DataDir, 'themes' TemplatesDir = File.join DataDir, 'templates' -#theme_data = ::Asciidoctor::ThemeLoader.load_file File.join ThemesDir, 'default-theme.yml' -#theme_data = ::Asciidoctor::ThemeLoader.load_file File.join ThemesDir, 'asciidoctor-theme.yml' -theme_data = ::Asciidoctor::ThemeLoader.load_file(theme_file || (File.join ThemesDir, 'default-theme.yml')) +#theme_data = Asciidoctor::ThemeLoader.load_file File.join ThemesDir, 'default-theme.yml' +#theme_data = Asciidoctor::ThemeLoader.load_file File.join ThemesDir, 'asciidoctor-theme.yml' +theme_data = Asciidoctor::ThemeLoader.load_file(theme_file || (File.join ThemesDir, 'default-theme.yml')) -file_rootname = ::Asciidoctor::Helpers.rootname File.basename asciidoc_file +file_rootname = Asciidoctor::Helpers.rootname File.basename asciidoc_file pdf_file = %(#{file_rootname}.pdf) options = { safe: :safe, @@ -42,5 +43,12 @@ options = { attributes: attributes } -doc = ::Asciidoctor.load_file asciidoc_file, options -::Asciidoctor::PdfRenderer.render doc, pdf_file, theme_data +options[:extensions_registry] = Asciidoctor::Extensions.build_registry :pdf do + include_processor Asciidoctor::Pdf::ImplicitHeaderProcessor.new @document +end + +workdir, asciidoc_file = File.split asciidoc_file +Dir.chdir workdir + +doc = Asciidoctor.load_file asciidoc_file, options +Asciidoctor::PdfRenderer.render doc, pdf_file, theme_data diff --git a/lib/asciidoctor-pdf/implicit_header_processor.rb b/lib/asciidoctor-pdf/implicit_header_processor.rb new file mode 100644 index 00000000..b4e9de91 --- /dev/null +++ b/lib/asciidoctor-pdf/implicit_header_processor.rb @@ -0,0 +1,63 @@ +require 'asciidoctor' +require 'asciidoctor/extensions' + +module Asciidoctor +module Pdf +# An include processor that skips the implicit author line below +# the document title in documents which are included. +class ImplicitHeaderProcessor < ::Asciidoctor::Extensions::IncludeProcessor + def initialize document + @document = document + end + + def process reader, target, attributes + return reader unless File.exist? target + ::File.open target, 'r' do |fd| + # FIXME handle case where doc id is specified above title + if (first_line = fd.readline) && (first_line.start_with? '= ') + # HACK reset counters for each article for Asciidoctor Editions + if (doc = @document).attr? 'env-editions' + doc.counters.each do |(counter_key, counter_val)| + doc.attributes.delete counter_key + end + doc.counters.clear + end + if (second_line = fd.readline) + if AuthorInfoLineRx =~ second_line + # FIXME temporary hack to set author and e-mail attributes; this should handle all attributes in header! + author = [$1, $2, $3].compact * ' ' + email = $4 + reader.push_include fd.readlines, target, target, 3, attributes unless fd.eof? + reader.push_include first_line, target, target, 1, attributes + lines = [%(:author: #{author})] + lines << %(:email: #{email}) if email + reader.push_include lines, target, target, 2, attributes + else + lines = [second_line] + lines += fd.readlines unless fd.eof? + reader.push_include lines, target, target, 2, attributes + reader.push_include first_line, target, target, 1, attributes + end + else + reader.push_include first_line, target, target, 1, attributes + end + else + lines = [first_line] + lines += fd.readlines unless fd.eof? + reader.push_include lines, target, target, 1, attributes + end + end + reader + end + + def handles? target + target != 'bio.adoc' && ((target.end_with? '.adoc') || (target.end_with? '.asciidoc')) + end + + # FIXME this method shouldn't be required + def update_config config + (@config ||= {}).update config + end +end +end +end |
