lib/asciidoctor/extensions.rb in asciidoctor-1.5.6.2 vs lib/asciidoctor/extensions.rb in asciidoctor-1.5.7

- old
+ new

@@ -1,5 +1,8 @@ +# NOTE .to_s hides require from Opal +require 'asciidoctor'.to_s unless defined? Asciidoctor + # encoding: UTF-8 module Asciidoctor # Extensions provide a way to participate in the parsing and converting # phases of the AsciiDoc processor or extend the AsciiDoc syntax. # @@ -105,41 +108,50 @@ # Use the id attribute to assign an explicit ID or set the value to false to # disable automatic ID generation (when sectids document attribute is set). # opts - An optional Hash of options (default: {}): # :level - [Integer] The level to assign to this section; defaults to # one greater than the parent level (optional). - # :numbered - [Boolean] A flag to force numbering, which falls back to the + # :numbered - [Boolean] A flag to force numbering, which falls back to the # state of the sectnums document attribute (optional). # # Returns a [Section] node with all properties properly initialized. def create_section parent, title, attrs, opts = {} doc = parent.document - doctype, level = doc.doctype, (opts[:level] || parent.level + 1) + book = (doctype = doc.doctype) == 'book' + level = opts[:level] || parent.level + 1 if (style = attrs.delete 'style') - if style == 'abstract' && doctype == 'book' + if book && style == 'abstract' sectname, level = 'chapter', 1 else sectname, special = style, true level = 1 if level == 0 end - elsif doctype == 'book' - sectname = level == 0 ? 'part' : (level == 1 ? 'chapter' : 'section') + elsif book + sectname = level == 0 ? 'part' : (level > 1 ? 'section' : 'chapter') elsif doctype == 'manpage' && (title.casecmp 'synopsis') == 0 sectname, special = 'synopsis', true else sectname = 'section' end - sect = Section.new parent, level, false + sect = Section.new parent, level sect.title, sect.sectname = title, sectname if special sect.special = true - sect.numbered = true if opts.fetch :numbered, (style == 'appendix') - elsif opts.fetch :numbered, (level > 0 && (doc.attributes.key? 'sectnums')) - sect.numbered = sect.special ? (parent.context == :section && parent.numbered) : true + if opts.fetch :numbered, (style == 'appendix') + sect.numbered = true + elsif !(opts.key? :numbered) && (doc.attr? 'sectnums', 'all') + sect.numbered = book && level == 1 ? :chapter : true + end + elsif level > 0 + if opts.fetch :numbered, (doc.attr? 'sectnums') + sect.numbered = sect.special ? parent.numbered && true : true + end + else + sect.numbered = true if opts.fetch :numbered, (book && (doc.attr? 'partnums')) end unless (id = attrs.delete 'id') == false - sect.id = attrs['id'] = id || ((doc.attributes.key? 'sectids') ? (Section.generate_id sect.title, doc) : nil) + sect.id = attrs['id'] = id || ((doc.attr? 'sectids') ? (Section.generate_id sect.title, doc) : nil) end sect.update_attributes attrs sect end @@ -150,11 +162,11 @@ # Public: Creates an image block node and links it to the specified parent. # # parent - The parent Block (Block, Section, or Document) of this new image block. # attrs - A Hash of attributes to control how the image block is built. # Use the target attribute to set the source of the image. - # Use the alt attribute to specify an alternate text for the image. + # Use the alt attribute to specify an alternative text for the image. # opts - An optional Hash of options (default: {}) # # Returns a [Block] node with all properties properly initialized. def create_image_block parent, attrs, opts = {} unless (target = attrs['target']) @@ -641,17 +653,16 @@ # stored in the registry during parsing. class Registry # Public: Returns the {Asciidoctor::Document} on which the extensions in this registry are being used. attr_reader :document - # Public: Returns the Array of {Group} classes, instances and/or Procs that have been registered. + # Public: Returns the Hash of {Group} classes, instances, and/or Procs that have been registered with this registry. attr_reader :groups def initialize groups = {} @groups = groups - @preprocessor_extensions = @tree_processor_extensions = @postprocessor_extensions = @include_processor_extensions = @docinfo_processor_extensions = nil - @block_extensions = @block_macro_extensions = @inline_macro_extensions = nil + @preprocessor_extensions = @tree_processor_extensions = @postprocessor_extensions = @include_processor_extensions = @docinfo_processor_extensions = @block_extensions = @block_macro_extensions = @inline_macro_extensions = nil @document = nil end # Public: Activates all the global extension {Group}s and the extension {Group}s # associated with this registry. @@ -659,23 +670,25 @@ # document - the {Asciidoctor::Document} on which the extensions are to be used. # # Returns the instance of this [Registry]. def activate document @document = document - (Extensions.groups.values + @groups.values).each do |group| - case group - when ::Proc - case group.arity - when 0, -1 - instance_exec(&group) - when 1 - group.call self + unless (ext_groups = Extensions.groups.values + @groups.values).empty? + ext_groups.each do |group| + case group + when ::Proc + case group.arity + when 0, -1 + instance_exec(&group) + when 1 + group.call self + end + when ::Class + group.new.activate self + else + group.activate self end - when ::Class - group.new.activate self - else - group.activate self end end self end @@ -703,11 +716,11 @@ # # as a name of a Preprocessor subclass # preprocessor 'FrontMatterPreprocessor' # # # as a method block # preprocessor do - # process |doc, reader| + # process do |doc, reader| # ... # end # end # # Returns the [Extension] stored in the registry that proxies the @@ -755,11 +768,11 @@ # # as a name of a TreeProcessor subclass # tree_processor 'ShellTreeProcessor' # # # as a method block # tree_processor do - # process |document| + # process do |document| # ... # end # end # # Returns the [Extension] stored in the registry that proxies the @@ -812,11 +825,11 @@ # # as a name of a Postprocessor subclass # postprocessor 'AnalyticsPostprocessor' # # # as a method block # postprocessor do - # process |document, output| + # process do |document, output| # ... # end # end # # Returns the [Extension] stored in the registry that proxies the @@ -864,11 +877,11 @@ # # as a name of a Postprocessor subclass # include_processor 'GitIncludeProcessor' # # # as a method block # include_processor do - # process |document, output| + # process do |document, output| # ... # end # end # # Returns the [Extension] stored in the registry that proxies the @@ -916,11 +929,11 @@ # # as a name of a DocinfoProcessor subclass # docinfo_processor 'MetaRobotsDocinfoProcessor' # # # as a method block # docinfo_processor do - # process |doc| + # process do |doc| # at_location :footer # 'footer content' # end # end # @@ -1005,18 +1018,18 @@ # block 'ShoutBlock', :shout # # # as a method block # block do # named :shout - # process |parent, reader, attrs| + # process do |parent, reader, attrs| # ... # end # end # # # as a method block with an explicit block name # block :shout do - # process |parent, reader, attrs| + # process do |parent, reader, attrs| # ... # end # end # # Returns an instance of the [Extension] proxy object that is stored in the @@ -1094,18 +1107,18 @@ # block_macro 'GistBlockMacro', :gist # # # as a method block # block_macro do # named :gist - # process |parent, target, attrs| + # process do |parent, target, attrs| # ... # end # end # # # as a method block with an explicit macro name # block_macro :gist do - # process |parent, target, attrs| + # process do |parent, target, attrs| # ... # end # end # # Returns an instance of the [Extension] proxy object that is stored in the @@ -1166,11 +1179,11 @@ # # # as an InlineMacroProcessor subclass # inline_macro ChromeInlineMacro # # # as an InlineMacroProcessor subclass with an explicit macro name - # inline_macro ChromeInineMacro, :chrome + # inline_macro ChromeInlineMacro, :chrome # # # as an instance of an InlineMacroProcessor subclass # inline_macro ChromeInlineMacro.new # # # as an instance of an InlineMacroProcessor subclass with an explicit macro name @@ -1178,23 +1191,23 @@ # # # as a name of an InlineMacroProcessor subclass # inline_macro 'ChromeInlineMacro' # # # as a name of an InlineMacroProcessor subclass with an explicit macro name - # inline_macro 'ChromeInineMacro', :chrome + # inline_macro 'ChromeInlineMacro', :chrome # # # as a method block # inline_macro do # named :chrome - # process |parent, target, attrs| + # process do |parent, target, attrs| # ... # end # end # # # as a method block with an explicit macro name # inline_macro :chrome do - # process |parent, target, attrs| + # process do |parent, target, attrs| # ... # end # end # # Returns an instance of the [Extension] proxy object that is stored in the @@ -1475,19 +1488,38 @@ end # Public: Resolves the Class object for the qualified name. # # Returns Class - def class_for_name qualified_name - resolved = ::Object - (qualified_name.split '::').each do |name| - unless name.empty? || ((resolved.const_defined? name) && ::Module === (resolved = resolved.const_get name)) - raise ::NameError, %(Could not resolve class for name: #{qualified_name}) + if RUBY_MIN_VERSION_2 + def class_for_name qualified_name + resolved = ::Object.const_get qualified_name, false + raise unless ::Class === resolved + resolved + rescue + raise ::NameError, %(Could not resolve class for name: #{qualified_name}) + end + elsif RUBY_MIN_VERSION_1_9 + def class_for_name qualified_name + resolved = (qualified_name.split '::').reduce ::Object do |current, name| + name.empty? ? current : (current.const_get name, false) end + raise unless ::Class === resolved + resolved + rescue + raise ::NameError, %(Could not resolve class for name: #{qualified_name}) end - raise ::NameError, %(Could not resolve class for name: #{qualified_name}) unless ::Class === resolved - resolved + else + def class_for_name qualified_name + resolved = (qualified_name.split '::').reduce ::Object do |current, name| + # NOTE on Ruby 1.8, const_defined? only checks for constant in current scope + name.empty? ? current : ((current.const_defined? name) ? (current.const_get name) : raise) + end + raise unless ::Class === resolved + resolved + rescue + raise ::NameError, %(Could not resolve class for name: #{qualified_name}) + end end end - end end