diff options
| author | Guillaume Grossetie <ggrossetie@gmail.com> | 2014-12-20 03:32:31 -0700 |
|---|---|---|
| committer | Dan Allen <dan.j.allen@gmail.com> | 2014-12-20 03:32:31 -0700 |
| commit | ab23be80baa1cb71f528df116167257bfb11eb9c (patch) | |
| tree | c4a03f685dee8bc42bed0e1ca9571f25426bf33e | |
| parent | 9cb62122dc754bf1cf4988c0ae6b4289a4fe0352 (diff) | |
resolves #405 - add attribute to control which substitutions are performed on docinfo files
- add docinfosubs attribute to control substitutions performed on docinfo files
- don't parse attribute option to cli as an array (interfers with docinfosubs value)
- add method to resolve docinfosubs
- add tests for docinfosubs attribute
| -rw-r--r-- | lib/asciidoctor/cli/options.rb | 23 | ||||
| -rw-r--r-- | lib/asciidoctor/document.rb | 23 | ||||
| -rw-r--r-- | lib/asciidoctor/substitutors.rb | 3 | ||||
| -rw-r--r-- | test/document_test.rb | 10 | ||||
| -rw-r--r-- | test/fixtures/subs-docinfo.html | 2 | ||||
| -rw-r--r-- | test/fixtures/subs.adoc | 7 | ||||
| -rw-r--r-- | test/options_test.rb | 4 |
7 files changed, 50 insertions, 22 deletions
diff --git a/lib/asciidoctor/cli/options.rb b/lib/asciidoctor/cli/options.rb index f2597dff..13116ce4 100644 --- a/lib/asciidoctor/cli/options.rb +++ b/lib/asciidoctor/cli/options.rb @@ -76,24 +76,21 @@ Example: asciidoctor -b html5 source.asciidoc end opts.on('-C', '--compact', 'compact the output by removing blank lines. (No longer in use)') do end - opts.on('-a', '--attribute key[=value],key2[=value2],...', ::Array, - 'a list of document attributes to set in the form of key, key! or key=value pair', - 'unless @ is appended to the value, these attributes take precedence over attributes', - 'defined in the source document') do |attribs| - attribs.each do |attrib| - key, val = attrib.split '=', 2 - # move leading ! to end for internal processing - #if val.nil? && key.start_with?('!') - # key = "#{key[1..-1]}!" - #end - self[:attributes][key] = val || '' - end + opts.on('-a', '--attribute key[=value]', 'a document attribute to set in the form of key, key! or key=value pair', + 'unless @ is appended to the value, this attributes takes precedence over attributes', + 'defined in the source document') do |attr| + key, val = attr.split '=', 2 + # move leading ! to end for internal processing + #if !val && key.start_with?('!') + # key = "#{key[1..-1]}!" + #end + self[:attributes][key] = val || '' end opts.on('-T', '--template-dir DIR', 'a directory containing custom converter templates that override the built-in converter (requires tilt gem)', 'may be specified multiple times') do |template_dir| if self[:template_dirs].nil? self[:template_dirs] = [template_dir] - elsif self[:template_dirs].is_a? ::Array + elsif ::Array === self[:template_dirs] self[:template_dirs].push template_dir else self[:template_dirs] = [self[:template_dirs], template_dir] diff --git a/lib/asciidoctor/document.rb b/lib/asciidoctor/document.rb index 05d6449c..5600219a 100644 --- a/lib/asciidoctor/document.rb +++ b/lib/asciidoctor/document.rb @@ -1107,15 +1107,17 @@ class Document < AbstractBlock content = nil - docinfo = @attributes.key?('docinfo') - docinfo1 = @attributes.key?('docinfo1') - docinfo2 = @attributes.key?('docinfo2') + docinfo = @attributes.key? 'docinfo' + docinfo1 = @attributes.key? 'docinfo1' + docinfo2 = @attributes.key? 'docinfo2' docinfo_filename = %(docinfo#{qualifier}#{ext}) if docinfo1 || docinfo2 docinfo_path = normalize_system_path(docinfo_filename, docinfodir) # NOTE normalizing the lines is essential if we're performing substitutions if (content = read_asset(docinfo_path, :normalize => true)) - content = sub_attributes(content) + if (docinfosubs ||= resolve_docinfo_subs) + content = (docinfosubs == :attributes) ? sub_attributes(content) : apply_subs(content, docinfosubs) + end end end @@ -1123,7 +1125,9 @@ class Document < AbstractBlock docinfo_path = normalize_system_path(%(#{@attributes['docname']}-#{docinfo_filename}), docinfodir) # NOTE normalizing the lines is essential if we're performing substitutions if (content2 = read_asset(docinfo_path, :normalize => true)) - content2 = sub_attributes(content2) + if (docinfosubs ||= resolve_docinfo_subs) + content2 = (docinfosubs == :attributes) ? sub_attributes(content2) : apply_subs(content2, docinfosubs) + end content = content ? %(#{content}#{EOL}#{content2}) : content2 end end @@ -1139,6 +1143,15 @@ class Document < AbstractBlock end end + def resolve_docinfo_subs + if @attributes.key? 'docinfosubs' + subs = resolve_subs @attributes['docinfosubs'], :block, nil, 'docinfo' + subs.empty? ? nil : subs + else + :attributes + end + end + def docinfo_processors?(location = :header) if @docinfo_processor_extensions.key?(location) # false means we already performed a lookup and didn't find any diff --git a/lib/asciidoctor/substitutors.rb b/lib/asciidoctor/substitutors.rb index 4ebfe23e..60bd0bab 100644 --- a/lib/asciidoctor/substitutors.rb +++ b/lib/asciidoctor/substitutors.rb @@ -1318,8 +1318,7 @@ module Substitutors return [] if subs.nil_or_empty? candidates = nil modifiers_present = SubModifierSniffRx =~ subs - subs.split(',').each do |val| - key = val.strip + subs.tr(' ', '').split(',').each do |key| modifier_operation = nil if modifiers_present if (first = key.chr) == '+' diff --git a/test/document_test.rb b/test/document_test.rb index ac2c9c18..ca03d40f 100644 --- a/test/document_test.rb +++ b/test/document_test.rb @@ -817,6 +817,16 @@ text assert_css 'productname', output, 0 assert_css 'copyright', output, 0 end + + test 'should apply explicit substitutions to docinfo files' do + sample_input_path = fixture_path('subs.adoc') + + output = Asciidoctor.convert_file sample_input_path, :to_file => false, + :header_footer => true, :safe => Asciidoctor::SafeMode::SERVER, :attributes => {'docinfo' => '', 'docinfosubs' => 'attributes,replacements', 'linkcss' => ''} + assert !output.empty? + assert_css 'script[src="bootstrap.3.2.0.min.js"]', output, 1 + assert_xpath %(//meta[@name="copyright"][@content="#{entity 169} OpenDevise"]), output, 1 + end end context 'MathJax' do diff --git a/test/fixtures/subs-docinfo.html b/test/fixtures/subs-docinfo.html new file mode 100644 index 00000000..b75acc83 --- /dev/null +++ b/test/fixtures/subs-docinfo.html @@ -0,0 +1,2 @@ +<meta name="copyright" content="(C) OpenDevise"> +<script src="bootstrap.{bootstrap-version}.min.js"></script> diff --git a/test/fixtures/subs.adoc b/test/fixtures/subs.adoc new file mode 100644 index 00000000..c7203896 --- /dev/null +++ b/test/fixtures/subs.adoc @@ -0,0 +1,7 @@ += Document Title +Doc Writer <doc.writer@asciidoc.org> +v1.0, 2013-01-01 +:bootstrap-version: 3.2.0 + +Body content. + diff --git a/test/options_test.rb b/test/options_test.rb index 1cf525ba..123fee59 100644 --- a/test/options_test.rb +++ b/test/options_test.rb @@ -57,9 +57,9 @@ context 'Options' do end test 'standard attribute assignment' do - options = Asciidoctor::Cli::Options.parse!(%w(-a imagesdir=images,icons test/fixtures/sample.asciidoc)) + options = Asciidoctor::Cli::Options.parse!(%w(-a docinfosubs=attributes,replacements -a icons test/fixtures/sample.asciidoc)) - assert_equal 'images', options[:attributes]['imagesdir'] + assert_equal 'attributes,replacements', options[:attributes]['docinfosubs'] assert_equal '', options[:attributes]['icons'] end |
