diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2015-10-16 23:53:23 -0600 |
|---|---|---|
| committer | Dan Allen <dan.j.allen@gmail.com> | 2015-10-17 23:54:38 -0600 |
| commit | 0e4836171a6ebdc56ca059727ea5fd4f5f4abed2 (patch) | |
| tree | fbe94052b5576496b9969ad7826904f409cff2b2 | |
| parent | 1d35b43aa4df688b61011a5725c4feda4fc632e8 (diff) | |
resolves #1510 use value of docinfo attribute to enable docinfo behavior
- use docinfo attribute to enable docinfo behavior
- rename :header docinfo location to :head
- add permutation tests for docinfo
| -rw-r--r-- | lib/asciidoctor/converter/docbook5.rb | 4 | ||||
| -rw-r--r-- | lib/asciidoctor/document.rb | 51 | ||||
| -rw-r--r-- | lib/asciidoctor/extensions.rb | 6 | ||||
| -rw-r--r-- | test/document_test.rb | 45 | ||||
| -rw-r--r-- | test/extensions_test.rb | 6 |
5 files changed, 62 insertions, 50 deletions
diff --git a/lib/asciidoctor/converter/docbook5.rb b/lib/asciidoctor/converter/docbook5.rb index ab2ceee6..b33e1791 100644 --- a/lib/asciidoctor/converter/docbook5.rb +++ b/lib/asciidoctor/converter/docbook5.rb @@ -680,8 +680,8 @@ module Asciidoctor result << %(</revision> </revhistory>) end - unless (header_docinfo = doc.docinfo :header).empty? - result << header_docinfo + unless (head_docinfo = doc.docinfo).empty? + result << head_docinfo end result << %(<orgname>#{doc.attr 'orgname'}</orgname>) if doc.attr? 'orgname' end diff --git a/lib/asciidoctor/document.rb b/lib/asciidoctor/document.rb index 200244f0..bdd6358b 100644 --- a/lib/asciidoctor/document.rb +++ b/lib/asciidoctor/document.rb @@ -1113,7 +1113,7 @@ class Document < AbstractBlock # will be determined based on the basebackend. (default: nil) # # returns The contents of the docinfo file(s) - def docinfo(location = :header, ext = nil) + def docinfo(location = :head, ext = nil) if safe >= SafeMode::SECURE '' else @@ -1123,28 +1123,39 @@ class Document < AbstractBlock content = nil - 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)) - if (docinfosubs ||= resolve_docinfo_subs) - content = (docinfosubs == :attributes) ? sub_attributes(content) : apply_subs(content, docinfosubs) - end + if (docinfo = @attributes['docinfo']).nil_or_empty? + if @attributes.key? 'docinfo2' + docinfo = ['private', 'shared'] + elsif @attributes.key? 'docinfo1' + docinfo = ['shared'] + else + docinfo = docinfo ? ['private'] : nil end + else + docinfo = docinfo.split(',').map(&:strip) end - if (docinfo || docinfo2) && @attributes.key?('docname') - 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)) - if (docinfosubs ||= resolve_docinfo_subs) - content2 = (docinfosubs == :attributes) ? sub_attributes(content2) : apply_subs(content2, docinfosubs) + if docinfo + docinfo_filename = %(docinfo#{qualifier}#{ext}) + unless (docinfo & ['shared', %(shared-#{location})]).empty? + 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)) + if (docinfosubs ||= resolve_docinfo_subs) + content = (docinfosubs == :attributes) ? sub_attributes(content) : apply_subs(content, docinfosubs) + end + end + end + + unless @attributes['docname'].nil_or_empty? || (docinfo & ['private', %(private-#{location})]).empty? + 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)) + if (docinfosubs ||= resolve_docinfo_subs) + content2 = (docinfosubs == :attributes) ? sub_attributes(content2) : apply_subs(content2, docinfosubs) + end + content = content ? %(#{content}#{EOL}#{content2}) : content2 end - content = content ? %(#{content}#{EOL}#{content2}) : content2 end end @@ -1168,7 +1179,7 @@ class Document < AbstractBlock end end - def docinfo_processors?(location = :header) + def docinfo_processors?(location = :head) if @docinfo_processor_extensions.key?(location) # false means we already performed a lookup and didn't find any @docinfo_processor_extensions[location] != false diff --git a/lib/asciidoctor/extensions.rb b/lib/asciidoctor/extensions.rb index 4e3d6162..1acb1f2c 100644 --- a/lib/asciidoctor/extensions.rb +++ b/lib/asciidoctor/extensions.rb @@ -258,7 +258,7 @@ module Extensions def initialize config = {} super config - @config[:location] ||= :header + @config[:location] ||= :head end def process document @@ -796,7 +796,7 @@ module Extensions # Public: Checks whether any {DocinfoProcessor} extensions have been registered. # - # location - A Symbol for selecting docinfo extensions at a given location (:header or :footer) (default: nil) + # location - A Symbol for selecting docinfo extensions at a given location (:head or :footer) (default: nil) # # Returns a [Boolean] indicating whether any DocinfoProcessor extensions are registered. def docinfo_processors? location = nil @@ -814,7 +814,7 @@ module Extensions # Public: Retrieves the {Extension} proxy objects for all the # DocinfoProcessor instances stored in this registry. # - # location - A Symbol for selecting docinfo extensions at a given location (:header or :footer) (default: nil) + # location - A Symbol for selecting docinfo extensions at a given location (:head or :footer) (default: nil) # # Returns an [Array] of Extension proxy objects. def docinfo_processors location = nil diff --git a/test/document_test.rb b/test/document_test.rb index 0c0bddf5..7519b50f 100644 --- a/test/document_test.rb +++ b/test/document_test.rb @@ -753,29 +753,30 @@ text test 'should include docinfo files for html backend' do sample_input_path = fixture_path('basic.asciidoc') - output = Asciidoctor.convert_file sample_input_path, :to_file => false, - :header_footer => true, :safe => Asciidoctor::SafeMode::SERVER, :attributes => {'docinfo' => ''} - assert !output.empty? - assert_css 'script[src="modernizr.js"]', output, 1 - assert_css 'meta[http-equiv="imagetoolbar"]', output, 0 - assert_css 'body > a#top', output, 0 - assert_css 'body > script', output, 1 - - output = Asciidoctor.convert_file sample_input_path, :to_file => false, - :header_footer => true, :safe => Asciidoctor::SafeMode::SERVER, :attributes => {'docinfo1' => ''} - assert !output.empty? - assert_css 'script[src="modernizr.js"]', output, 0 - assert_css 'meta[http-equiv="imagetoolbar"]', output, 1 - assert_css 'body > a#top', output, 1 - assert_css 'body > script', output, 0 + cases = { + 'docinfo' => { :head_script => 1, :meta => 0, :top_link => 0, :footer_script => 1 }, + 'docinfo=private' => { :head_script => 1, :meta => 0, :top_link => 0, :footer_script => 1 }, + 'docinfo1' => { :head_script => 0, :meta => 1, :top_link => 1, :footer_script => 0 }, + 'docinfo=shared' => { :head_script => 0, :meta => 1, :top_link => 1, :footer_script => 0 }, + 'docinfo2' => { :head_script => 1, :meta => 1, :top_link => 1, :footer_script => 1 }, + 'docinfo docinfo2' => { :head_script => 1, :meta => 1, :top_link => 1, :footer_script => 1 }, + 'docinfo=private,shared' => { :head_script => 1, :meta => 1, :top_link => 1, :footer_script => 1 }, + 'docinfo=private-head' => { :head_script => 1, :meta => 0, :top_link => 0, :footer_script => 0 }, + 'docinfo=shared-head' => { :head_script => 0, :meta => 1, :top_link => 0, :footer_script => 0 }, + 'docinfo=private-footer' => { :head_script => 0, :meta => 0, :top_link => 0, :footer_script => 1 }, + 'docinfo=shared-footer' => { :head_script => 0, :meta => 0, :top_link => 1, :footer_script => 0 }, + 'docinfo=private-head\ ,\ shared-footer' => { :head_script => 1, :meta => 0, :top_link => 1, :footer_script => 0 } + } - output = Asciidoctor.convert_file sample_input_path, :to_file => false, - :header_footer => true, :safe => Asciidoctor::SafeMode::SERVER, :attributes => {'docinfo2' => ''} - assert !output.empty? - assert_css 'script[src="modernizr.js"]', output, 1 - assert_css 'meta[http-equiv="imagetoolbar"]', output, 1 - assert_css 'body > a#top', output, 1 - assert_css 'body > script', output, 1 + cases.each do |attr_val, markup| + output = Asciidoctor.convert_file sample_input_path, :to_file => false, + :header_footer => true, :safe => Asciidoctor::SafeMode::SERVER, :attributes => %(linkcss copycss! #{attr_val}) + assert !output.empty? + assert_css 'script[src="modernizr.js"]', output, markup[:head_script] + assert_css 'meta[http-equiv="imagetoolbar"]', output, markup[:meta] + assert_css 'body > a#top', output, markup[:top_link] + assert_css 'body > script', output, markup[:footer_script] + end end test 'should include docinfo footer even if nofooter attribute is set' do diff --git a/test/extensions_test.rb b/test/extensions_test.rb index 8edbed8c..d93b8b6b 100644 --- a/test/extensions_test.rb +++ b/test/extensions_test.rb @@ -125,7 +125,7 @@ end class MetaAppDocinfoProcessor < Asciidoctor::Extensions::DocinfoProcessor use_dsl - at_location :header + at_location :head def process document '<meta name="application-name" content="Asciidoctor App">' @@ -310,7 +310,7 @@ context 'Extensions' do registry.docinfo_processor SampleDocinfoProcessor registry.activate Asciidoctor::Document.new assert registry.docinfo_processors? - assert registry.docinfo_processors?(:header) + assert registry.docinfo_processors?(:head) extensions = registry.docinfo_processors assert_equal 1, extensions.size assert extensions.first.is_a? Asciidoctor::Extensions::ProcessorExtension @@ -687,7 +687,7 @@ sample content doc = document_from_string input, :safe => :server assert_equal '<meta name="robots" content="index,follow"> -<meta name="application-name" content="Asciidoctor App">', doc.docinfo(:header) +<meta name="application-name" content="Asciidoctor App">', doc.docinfo assert_equal '<script><!-- analytics code --></script>', doc.docinfo(:footer) ensure Asciidoctor::Extensions.unregister_all |
