diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2014-02-27 19:54:13 -0700 |
|---|---|---|
| committer | Dan Allen <dan.j.allen@gmail.com> | 2014-02-27 19:57:49 -0700 |
| commit | 771e2d25b9610d93632b593d6da1f6c00ee2a0f4 (patch) | |
| tree | 1e1df320be0204e643da5da8581373db33beb9a0 | |
| parent | 9b4ce14a6ba57bfad05bf8709471da47d4eae454 (diff) | |
allow custom converter to set backend info such as outfilesuffix
- add DSL for custom converter to use to set backend info
- add test to ensure outfilesuffix is set properly
| -rw-r--r-- | lib/asciidoctor/converter.rb | 68 | ||||
| -rw-r--r-- | lib/asciidoctor/converter/composite.rb | 8 | ||||
| -rw-r--r-- | lib/asciidoctor/converter/factory.rb | 2 | ||||
| -rw-r--r-- | lib/asciidoctor/converter/template.rb | 7 | ||||
| -rw-r--r-- | lib/asciidoctor/document.rb | 45 | ||||
| -rw-r--r-- | test/attributes_test.rb | 3 |
6 files changed, 109 insertions, 24 deletions
diff --git a/lib/asciidoctor/converter.rb b/lib/asciidoctor/converter.rb index 9698dbf1..d819f542 100644 --- a/lib/asciidoctor/converter.rb +++ b/lib/asciidoctor/converter.rb @@ -15,6 +15,10 @@ module Asciidoctor # class TextConverter # include Asciidoctor::Converter # register_for 'text' + # def initialize backend, opts + # super + # outfilesuffix '.txt' + # end # def convert node, transform = nil # case (transform ||= node.node_name) # when 'document' @@ -63,6 +67,54 @@ module Asciidoctor end end + module BackendInfo + def backend_info + @backend_info ||= setup_backend_info + end + + def setup_backend_info + raise ::ArgumentError, %(Cannot determine backend for converter: #{self.class}) unless @backend + base = @backend.sub TrailingDigitsRx, '' + if (ext = DEFAULT_EXTENSIONS[base]) + type = ext[1..-1] + else + # QUESTION should we be forcing the basebackend to html if unknown? + base = 'html' + ext = '.html' + type = 'html' + end + { + 'basebackend' => base, + 'outfilesuffix' => ext, + 'filetype' => type, + } + end + + def filetype value = nil + if value + backend_info['filetype'] = value + else + backend_info['filetype'] + end + end + + def basebackend value = nil + if value + backend_info['basebackend'] = value + else + backend_info['basebackend'] + end + end + + def outfilesuffix value = nil + if value + backend_info['outfilesuffix'] = value + else + backend_info['outfilesuffix'] + end + end + end + class << self # Mixes the {Config Converter::Config} module into any class that includes the {Converter} module. # @@ -75,7 +127,8 @@ module Asciidoctor end include Config - + include BackendInfo + # Public: Creates a new instance of Converter # # backend - The String backend format to which this converter converts. @@ -84,8 +137,19 @@ module Asciidoctor # Returns a new instance of [Converter] def initialize backend, opts = {} @backend = backend + setup_backend_info end - + +=begin + # Public: Invoked when this converter is added to the chain of converters in a {CompositeConverter}. + # + # owner - The CompositeConverter instance + # + # Returns nothing + def composed owner + end +=end + # Public: Converts an {AbstractNode} using the specified transform. If a # transform is not specified, implementations typically derive one from the # {AbstractNode#node_name} property. diff --git a/lib/asciidoctor/converter/composite.rb b/lib/asciidoctor/converter/composite.rb index d6f7fc99..09fdf4f9 100644 --- a/lib/asciidoctor/converter/composite.rb +++ b/lib/asciidoctor/converter/composite.rb @@ -3,11 +3,15 @@ module Asciidoctor # objects passed to the constructor. Selects the first {Converter} that # identifies itself as the handler for a given transform. class Converter::CompositeConverter < Converter::Base + # Get the Array of Converter objects in the chain attr_reader :converters - def initialize *converters - @converters = converters.flatten.compact + def initialize backend, *converters + @backend = backend + (@converters = converters.flatten.compact).each do |converter| + converter.composed self if converter.respond_to? :composed + end @converter_map = {} end diff --git a/lib/asciidoctor/converter/factory.rb b/lib/asciidoctor/converter/factory.rb index 11378082..89491cbf 100644 --- a/lib/asciidoctor/converter/factory.rb +++ b/lib/asciidoctor/converter/factory.rb @@ -200,7 +200,7 @@ module Asciidoctor end template_converter = TemplateConverter.new backend, opts[:template_dirs], opts # QUESTION should we omit the composite converter if built_in_converter is nil? - CompositeConverter.new template_converter, base_converter + CompositeConverter.new backend, template_converter, base_converter end end end diff --git a/lib/asciidoctor/converter/template.rb b/lib/asciidoctor/converter/template.rb index 160f9772..ffa9ba65 100644 --- a/lib/asciidoctor/converter/template.rb +++ b/lib/asciidoctor/converter/template.rb @@ -78,6 +78,13 @@ module Asciidoctor #create_handlers end +=begin + # Public: Called when this converter is added to a composite converter. + def composed parent + # TODO set the backend info determined during the scan + end +=end + # Internal: Scans the template directories specified in the constructor for Tilt-supported # templates, loads the templates and stores the in a Hash that is accessible via the # {TemplateConverter#templates} method. diff --git a/lib/asciidoctor/document.rb b/lib/asciidoctor/document.rb index eb45358c..85fdb7cb 100644 --- a/lib/asciidoctor/document.rb +++ b/lib/asciidoctor/document.rb @@ -775,24 +775,40 @@ class Document < AbstractBlock if (resolved_name = BACKEND_ALIASES[new_backend]) new_backend = resolved_name end - new_basebackend = new_backend.sub TrailingDigitsRx, '' - if (page_width = DEFAULT_PAGE_WIDTHS[new_basebackend]) - attrs['pagewidth'] = page_width - else - attrs.delete 'pagewidth' - end if current_backend attrs.delete %(backend-#{current_backend}) if current_doctype attrs.delete %(backend-#{current_backend}-doctype-#{current_doctype}) end end - attrs['backend'] = new_backend - attrs[%(backend-#{new_backend})] = '' if current_doctype attrs[%(doctype-#{current_doctype})] = '' attrs[%(backend-#{new_backend}-doctype-#{current_doctype})] = '' end + attrs['backend'] = new_backend + attrs[%(backend-#{new_backend})] = '' + # (re)initialize converter + if (@converter = create_converter).is_a? Converter::BackendInfo + new_basebackend = @converter.basebackend + attrs['outfilesuffix'] = @converter.outfilesuffix + new_filetype = @converter.filetype + else + new_basebackend = new_backend.sub TrailingDigitsRx, '' + # QUESTION should we be forcing the basebackend to html if unknown? + new_outfilesuffix = DEFAULT_EXTENSIONS[new_basebackend] || '.html' + new_filetype = new_outfilesuffix[1..-1] + attrs['outfilesuffix'] = new_outfilesuffix unless attribute_locked? 'outfilesuffix' + end + if (current_filetype = attrs['filetype']) + attrs.delete %(filetype-#{current_filetype}) + end + attrs['filetype'] = new_filetype + attrs[%(filetype-#{new_filetype})] = '' + if (page_width = DEFAULT_PAGE_WIDTHS[new_basebackend]) + attrs['pagewidth'] = page_width + else + attrs.delete 'pagewidth' + end if new_basebackend != current_basebackend if current_basebackend attrs.delete %(basebackend-#{current_basebackend}) @@ -804,17 +820,8 @@ class Document < AbstractBlock attrs[%(basebackend-#{new_basebackend})] = '' attrs[%(basebackend-#{new_basebackend}-doctype-#{current_doctype})] = '' if current_doctype end - ext = DEFAULT_EXTENSIONS[new_basebackend] || '.html' - new_file_type = ext[1..-1] - current_file_type = attrs['filetype'] - attrs['outfilesuffix'] = ext unless attribute_locked? 'outfilesuffix' - attrs.delete %(filetype-#{current_file_type}) if current_file_type - attrs['filetype'] = new_file_type - attrs[%(filetype-#{new_file_type})] = '' - # clear cached value + # clear cached backend value @backend = nil - # (re)initialize converter - @converter = create_converter end end @@ -833,7 +840,7 @@ class Document < AbstractBlock attrs[%(doctype-#{new_doctype})] = '' attrs[%(backend-#{current_backend}-doctype-#{new_doctype})] = '' if current_backend attrs[%(basebackend-#{current_basebackend}-doctype-#{new_doctype})] = '' if current_basebackend - # clear cached value + # clear cached doctype value @doctype = nil end end diff --git a/test/attributes_test.rb b/test/attributes_test.rb index f6b588a8..ad3000ed 100644 --- a/test/attributes_test.rb +++ b/test/attributes_test.rb @@ -162,6 +162,7 @@ content 'backend' => 'html5', 'backend-html5' => '', 'backend-html5-doctype-article' => '', + 'outfilesuffix' => '.html', 'basebackend' => 'html', 'basebackend-html' => '', 'basebackend-html-doctype-article' => '', @@ -189,6 +190,7 @@ content 'backend' => 'docbook5', 'backend-docbook5' => '', 'backend-docbook5-doctype-book' => '', + 'outfilesuffix' => '.xml', 'basebackend' => 'docbook', 'basebackend-docbook' => '', 'basebackend-docbook-doctype-book' => '', @@ -218,6 +220,7 @@ content 'backend' => 'docbook5', 'backend-docbook5' => '', 'backend-docbook5-doctype-book' => '', + 'outfilesuffix' => '.xml', 'basebackend' => 'docbook', 'basebackend-docbook' => '', 'basebackend-docbook-doctype-book' => '', |
