diff options
| author | Dan Allen <dallen@redhat.com> | 2013-04-23 00:33:31 -0600 |
|---|---|---|
| committer | Dan Allen <dallen@redhat.com> | 2013-04-23 14:20:35 -0600 |
| commit | 3a0969cbff400a685a3ba3cadfbea0a2b67aa234 (patch) | |
| tree | 2926993a9e771b7e30138b9f5a3387085737a323 /lib | |
| parent | 8d9241ea92fb8d70ff53b9565f31bbcf56a1348b (diff) | |
resolves #269 implement toc::[] macro
Diffstat (limited to 'lib')
| -rwxr-xr-x | lib/asciidoctor.rb | 4 | ||||
| -rw-r--r-- | lib/asciidoctor/backends/docbook45.rb | 10 | ||||
| -rw-r--r-- | lib/asciidoctor/backends/html5.rb | 34 | ||||
| -rw-r--r-- | lib/asciidoctor/document.rb | 13 | ||||
| -rw-r--r-- | lib/asciidoctor/lexer.rb | 7 |
5 files changed, 65 insertions, 3 deletions
diff --git a/lib/asciidoctor.rb b/lib/asciidoctor.rb index 6b00c41a..a8c825aa 100755 --- a/lib/asciidoctor.rb +++ b/lib/asciidoctor.rb @@ -487,6 +487,10 @@ module Asciidoctor # TODO build from SECTION_LEVELS keys :section_underline => /^(?:=|-|~|\^|\+)+$/, + # toc::[] + # toc::[levels=2] + :toc => /^toc::\[(.*?)\]$/, + # * Foo (up to 5 consecutive asterisks) # - Foo # REVIEW leading space has already been stripped, so may not need in regex diff --git a/lib/asciidoctor/backends/docbook45.rb b/lib/asciidoctor/backends/docbook45.rb index 0920a302..9c4999c1 100644 --- a/lib/asciidoctor/backends/docbook45.rb +++ b/lib/asciidoctor/backends/docbook45.rb @@ -122,6 +122,16 @@ class EmbeddedTemplate < BaseTemplate end end +class BlockTocTemplate < BaseTemplate + def result(node) + '' + end + + def template + :invoke_result + end +end + class BlockPreambleTemplate < BaseTemplate def template @template ||= @eruby.new <<-EOF diff --git a/lib/asciidoctor/backends/html5.rb b/lib/asciidoctor/backends/html5.rb index 51e34e8f..b1bcd1d1 100644 --- a/lib/asciidoctor/backends/html5.rb +++ b/lib/asciidoctor/backends/html5.rb @@ -101,7 +101,7 @@ class DocumentTemplate < BaseTemplate <% if attr? :revdate %><span id="revdate"><%= attr :revdate %></span><% end %> <% if attr? :revremark %><br><span id="revremark"><%= attr :revremark %></span><% end %> <% end %> - <% if attr? :toc %> + <% if (attr? :toc) && (attr? 'toc-placement', 'auto') %> <div id="toc" class="<%= attr 'toc-class', 'toc' %>"> <div id="toctitle"><%= attr 'toc-title' %></div> <%= template.class.outline(self, (attr :toclevels, 2).to_i) %> @@ -150,6 +150,38 @@ class EmbeddedTemplate < BaseTemplate end end +class BlockTocTemplate < BaseTemplate + # id must be unique if this macro is used > once or when built-in one is present + def result(node) + doc = node.document + + return '' unless doc.attr?('toc') + + if node.id + id_attr = %( id="#{node.id}") + title_id_attr = '' + elsif doc.embedded? || !doc.attr?('toc-placement', 'auto') + id_attr = ' id="toc"' + title_id_attr = ' id="toctitle"' + else + id_attr = '' + title_id_attr = '' + end + title = node.title? ? node.title : (doc.attr 'toc-title') + levels = node.attr?('levels') ? node.attr('levels').to_i : doc.attr('toclevels', 2).to_i + role = node.attr?('role') ? node.attr('role') : doc.attr('toc-class', 'toc') + + %(\n<div#{id_attr} class="#{role}"> +<div#{title_id_attr} class="title">#{title}</div> +#{DocumentTemplate.outline(doc, levels)} +</div>) + end + + def template + :invoke_result + end +end + class BlockPreambleTemplate < BaseTemplate def template @template ||= @eruby.new <<-EOS diff --git a/lib/asciidoctor/document.rb b/lib/asciidoctor/document.rb index 380b21d7..bbe6a02b 100644 --- a/lib/asciidoctor/document.rb +++ b/lib/asciidoctor/document.rb @@ -129,9 +129,11 @@ class Document < AbstractBlock @attributes['asciidoctor'] = '' @attributes['asciidoctor-version'] = VERSION - @attributes['sectids'] = '' @attributes['encoding'] = 'UTF-8' - @attributes['notitle'] = '' if !@options[:header_footer] + @attributes['sectids'] = '' + @attributes['notitle'] = '' unless @options[:header_footer] + @attributes['embedded'] = '' unless @options[:header_footer] + @attributes['toc-placement'] = 'auto' # language strings # TODO load these based on language settings @@ -143,9 +145,11 @@ class Document < AbstractBlock @attributes['appendix-caption'] = 'Appendix' @attributes['example-caption'] = 'Example' @attributes['figure-caption'] = 'Figure' + #@attributes['listing-caption'] = 'Listing' @attributes['table-caption'] = 'Table' @attributes['toc-title'] = 'Table of Contents' + # attribute overrides are attributes that can only be set from the commandline @attribute_overrides = options[:attributes] || {} # the only way to set the include-depth attribute is via the document options @@ -330,6 +334,11 @@ class Document < AbstractBlock !@parent_document.nil? end + def embedded? + # QUESTION should this be !@options[:header_footer] ? + @attributes.has_key? 'embedded' + end + # Make the raw source for the Document available. def source @reader.source.join if @reader diff --git a/lib/asciidoctor/lexer.rb b/lib/asciidoctor/lexer.rb index 86270282..19c81b3c 100644 --- a/lib/asciidoctor/lexer.rb +++ b/lib/asciidoctor/lexer.rb @@ -357,6 +357,13 @@ class Lexer # drop the line if target resolves to nothing return nil end + + # NOTE we're letting the toc macro have attributes + elsif (match = this_line.match(REGEXP[:toc])) + block = Block.new(parent, :toc) + block.parse_attributes(match[1], [], :sub_result => false, :into => attributes) + break + end end |
