summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2013-06-23 21:48:18 -0700
committerDan Allen <dan.j.allen@gmail.com>2013-06-23 21:48:18 -0700
commit5ba3a6b820adbda9ba9a8ad1306d7658b5cd24f5 (patch)
tree5b1061f6e5d035df298235e977685605e5343d50 /lib
parent011d4c66815f23b7fbc65982fd39141f495fa2f5 (diff)
parent0596513744a3245d35a08d3ca8a189b42fc381a4 (diff)
Merge pull request #443 from mojavelinux/style-instance-variable
resolves #436 move style to instance variable
Diffstat (limited to 'lib')
-rw-r--r--lib/asciidoctor/abstract_block.rb4
-rw-r--r--lib/asciidoctor/abstract_node.rb20
-rw-r--r--lib/asciidoctor/backends/docbook45.rb25
-rw-r--r--lib/asciidoctor/backends/html5.rb31
-rw-r--r--lib/asciidoctor/lexer.rb3
-rw-r--r--lib/asciidoctor/substituters.rb2
-rw-r--r--lib/asciidoctor/table.rb30
7 files changed, 73 insertions, 42 deletions
diff --git a/lib/asciidoctor/abstract_block.rb b/lib/asciidoctor/abstract_block.rb
index 9999c021..21d5e6c7 100644
--- a/lib/asciidoctor/abstract_block.rb
+++ b/lib/asciidoctor/abstract_block.rb
@@ -9,6 +9,9 @@ class AbstractBlock < AbstractNode
# Public: Set the String block title.
attr_writer :title
+ # Public: Get/Set the String style (block type qualifier) for this block.
+ attr_accessor :style
+
# Public: Get/Set the caption for this block
attr_accessor :caption
@@ -18,6 +21,7 @@ class AbstractBlock < AbstractNode
@id = nil
@title = nil
@caption = nil
+ @style = nil
if context == :document
@level = 0
elsif !parent.nil? && !self.is_a?(Section)
diff --git a/lib/asciidoctor/abstract_node.rb b/lib/asciidoctor/abstract_node.rb
index 7e5ed72e..0d9ec60f 100644
--- a/lib/asciidoctor/abstract_node.rb
+++ b/lib/asciidoctor/abstract_node.rb
@@ -174,6 +174,26 @@ class AbstractNode
@document.renderer
end
+ # Public: A convenience method that checks if the role attribute is specified
+ def role?
+ self.attr?('role')
+ end
+
+ # Public: A convenience method that returns the value of the role attribute
+ def role
+ self.attr('role')
+ end
+
+ # Public: A convenience method that checks if the reftext attribute is specified
+ def reftext?
+ self.attr?('reftext')
+ end
+
+ # Public: A convenience method that returns the value of the reftext attribute
+ def reftext
+ self.attr('reftext')
+ end
+
# Public: Construct a reference or data URI to an icon image for the
# specified icon name.
#
diff --git a/lib/asciidoctor/backends/docbook45.rb b/lib/asciidoctor/backends/docbook45.rb
index a3547acb..58e8a3be 100644
--- a/lib/asciidoctor/backends/docbook45.rb
+++ b/lib/asciidoctor/backends/docbook45.rb
@@ -26,7 +26,7 @@ module Asciidoctor
end
def common_attrs_erb
- %q(<%= template.common_attrs(@id, (attr 'role'), (attr 'reftext')) %>)
+ %q(<%= template.common_attrs(@id, role, reftext) %>)
end
def content(node)
@@ -161,7 +161,7 @@ class SectionTemplate < BaseTemplate
else
tag = sec.document.doctype == 'book' && sec.level <= 1 ? (sec.level == 0 ? 'part' : 'chapter') : 'section'
end
- %(<#{tag}#{common_attrs(sec.id, (sec.attr 'role'), (sec.attr 'reftext'))}>
+ %(<#{tag}#{common_attrs(sec.id, sec.role, sec.reftext)}>
#{sec.title? ? "<title>#{sec.title}</title>" : nil}
#{sec.content.chomp}
</#{tag}>\n)
@@ -193,7 +193,7 @@ class BlockParagraphTemplate < BaseTemplate
end
def result(node)
- paragraph(node.id, (node.attr 'style', nil, false), (node.attr 'role'), (node.attr 'reftext'), (node.title? ? node.title : nil), node.content)
+ paragraph(node.id, node.style, node.role, node.reftext, (node.title? ? node.title : nil), node.content)
end
def template
@@ -214,7 +214,7 @@ end
class BlockUlistTemplate < BaseTemplate
def template
@template ||= @eruby.new <<-EOF
-<%#encoding:UTF-8%><% if attr? :style, 'bibliography' %>
+<%#encoding:UTF-8%><% if @style == 'bibliography' %>
<bibliodiv#{common_attrs_erb}>#{title_tag}
<% content.each do |li| %>
<bibliomixed>
@@ -244,7 +244,7 @@ end
class BlockOlistTemplate < BaseTemplate
def template
@template ||= @eruby.new <<-EOF
-<%#encoding:UTF-8%><orderedlist#{common_attrs_erb}#{attribute('numeration', :style)}>#{title_tag}
+<%#encoding:UTF-8%><orderedlist#{common_attrs_erb}#{attribute('numeration', '@style')}>#{title_tag}
<% content.each do |li| %>
<listitem>
<simpara><%= li.text %></simpara>
@@ -306,7 +306,7 @@ class BlockDlistTemplate < BaseTemplate
continuing = false;
entries = content
last_index = entries.length - 1
-if attr? :style, 'horizontal'
+if @style == 'horizontal'
%><<%= (tag = title? ? 'table' : 'informaltable') %>#{common_attrs_erb} tabstyle="horizontal" frame="none" colsep="0" rowsep="0">#{title_tag}
<tgroup cols="2">
<colspec colwidth="<%= attr :labelwidth, 15 %>*"/>
@@ -344,7 +344,7 @@ if attr? :style, 'horizontal'
</tgroup>
</<%= tag %>><%
else
- tags = (template.class::LIST_TAGS[attr :style] || template.class::LIST_TAGS['labeled'])
+ tags = (template.class::LIST_TAGS[@style] || template.class::LIST_TAGS['labeled'])
if tags[:list]
%><<%= tags[:list] %>#{common_attrs_erb}>#{title_tag}<%
end
@@ -392,8 +392,7 @@ end
class BlockOpenTemplate < BaseTemplate
def result(node)
- open_block(node, node.id, (node.attr 'style', nil, false),
- (node.attr 'role'), (node.attr 'reftext'), node.title? ? node.title : nil)
+ open_block(node, node.id, node.style, node.role, node.reftext, node.title? ? node.title : nil)
end
def open_block(node, id, style, role, reftext, title)
@@ -431,7 +430,7 @@ class BlockListingTemplate < BaseTemplate
@template ||= @eruby.new <<-EOF
<%#encoding:UTF-8%><%
if !title?
- if (attr? 'style', 'source') && (attr? 'language')
+ if @style == 'source' && (attr? 'language')
%><programlisting#{common_attrs_erb}#{attribute('language', :language)} linenumbering="<%= (attr? :linenums) ? 'numbered' : 'unnumbered' %>"><%= template.preserve_endlines(content, self) %></programlisting><%
else
%><screen#{common_attrs_erb}><%= template.preserve_endlines(content, self) %></screen><%
@@ -439,7 +438,7 @@ if !title?
else
%><formalpara#{common_attrs_erb}>#{title_tag false}
<para><%
- if (attr? 'style', 'source') && (attr? 'language') %>
+ if @style == 'source' && (attr? 'language') %>
<programlisting language="<%= attr 'language' %>" linenumbering="<%= (attr? :linenums) ? 'numbered' : 'unnumbered' %>"><%= template.preserve_endlines(content, self) %></programlisting><%
else %>
<screen><%= template.preserve_endlines(content, self) %></screen><%
@@ -554,7 +553,7 @@ class BlockTableTemplate < BaseTemplate
cell_content = ''
if tblsec == :head %><% cell_content = cell.text %><%
else %><%
- case (cell.attr :style)
+ case cell.style
when :asciidoc %><% cell_content = cell.content %><%
when :verse %><% cell_content = %(<literallayout>\#{template.preserve_endlines(cell.text, self)}</literallayout>) %><%
when :literal %><% cell_content = %(<literallayout class="monospaced">\#{template.preserve_endlines(cell.text, self)}</literallayout>) %><%
@@ -642,7 +641,7 @@ class InlineQuotedTemplate < BaseTemplate
end
def result(node)
- quote_text(node.text, node.type, (node.attr 'role'))
+ quote_text(node.text, node.type, node.role)
end
def template
diff --git a/lib/asciidoctor/backends/html5.rb b/lib/asciidoctor/backends/html5.rb
index 99099c48..f1b75a87 100644
--- a/lib/asciidoctor/backends/html5.rb
+++ b/lib/asciidoctor/backends/html5.rb
@@ -5,12 +5,13 @@ class BaseTemplate
# create template matter to insert a style class from the role attribute if specified
def role_class
- attrvalue('role')
+ %(<%= role? ? " \#{role}" : nil %>)
end
# create template matter to insert a style class from the style attribute if specified
def style_class(sibling = true)
- attrvalue('style', sibling, false)
+ delimiter = sibling ? ' ' : ''
+ %(<%= @style && "#{delimiter}\#{@style}" %>)
end
def title_div(opts = {})
@@ -208,7 +209,7 @@ class BlockTocTemplate < BaseTemplate
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')
+ role = node.role? ? node.role : (doc.attr 'toc-class', 'toc')
%(<div#{id_attr} class="#{role}">
<div#{title_id_attr} class="title">#{title}</div>
@@ -271,7 +272,7 @@ class SectionTemplate < BaseTemplate
%(<h1#{id} class="sect0">#{anchor}#{link_start}#{sec.title}#{link_end}</h1>
#{sec.content}\n)
else
- role = (sec.attr? 'role') ? " #{sec.attr 'role'}" : nil
+ role = sec.role? ? " #{sec.role}" : nil
if sec.numbered
sectnum = "#{sec.sectnum} "
else
@@ -312,7 +313,7 @@ class BlockDlistTemplate < BaseTemplate
continuing = false
entries = content
last_index = entries.length - 1
-if attr? 'style', 'qanda', false
+if @style == 'qanda'
%><div#{id} class="qlist#{style_class}#{role_class}"><%
if title? %>
<div class="title"><%= title %></div><%
@@ -342,7 +343,7 @@ end %>
end %>
</ol>
</div><%
-elsif attr? 'style', 'horizontal', false
+elsif @style == 'horizontal'
%><div#{id} class="hdlist#{role_class}"><%
if title? %>
<div class="title"><%= title %></div><%
@@ -391,7 +392,7 @@ end %>
<dl><%
entries.each_with_index do |(dt, dd), index|
last = (index == last_index) %>
-<dt<%= !(attr? 'style', nil, false) ? %( class="hdlist1") : nil %>><%= dt.text %></dt><%
+<dt<%= @style.nil? ? %( class="hdlist1") : nil %>><%= dt.text %></dt><%
unless dd.nil? %>
<dd><%
if dd.text? %>
@@ -416,7 +417,7 @@ class BlockListingTemplate < BaseTemplate
<%#encoding:UTF-8%><div#{id} class="listingblock#{role_class}">
#{title_div :caption => true}
<div class="content monospaced"><%
-if attr? 'style', 'source', false
+if @style == 'source'
language = (language = (attr 'language')) ? %(\#{language} language-\#{language}) : nil
nowrap = !(@document.attr? 'prewrap') || (option? 'nowrap')
case attr 'source-highlighter'
@@ -492,7 +493,7 @@ class BlockParagraphTemplate < BaseTemplate
end
def result(node)
- paragraph(node.id, (node.attr 'role'), (node.title? ? node.title : nil), node.content)
+ paragraph(node.id, node.role, (node.title? ? node.title : nil), node.content)
end
def template
@@ -528,7 +529,7 @@ end
class BlockOpenTemplate < BaseTemplate
def result(node)
- open_block(node, node.id, (node.attr 'style', nil, false), (node.attr 'role'), node.title? ? node.title : nil, node.content)
+ open_block(node, node.id, node.style, node.role, node.title? ? node.title : nil, node.content)
end
def open_block(node, id, style, role, title, content)
@@ -647,9 +648,9 @@ class BlockOlistTemplate < BaseTemplate
def template
@template ||= @eruby.new <<-EOS
-<%#encoding:UTF-8%><% style = attr 'style', nil, false %><div#{id} class="olist#{style_class}#{role_class}">
+<%#encoding:UTF-8%><div#{id} class="olist#{style_class}#{role_class}">
#{title_div}
-<ol class="<%= style %>"<%= (type = ::Asciidoctor::ORDERED_LIST_KEYWORDS[style]) ? %( type="\#{type}") : nil %>#{attribute('start', :start)}><%
+<ol class="<%= @style %>"<%= (type = ::Asciidoctor::ORDERED_LIST_KEYWORDS[@style]) ? %( type="\#{type}") : nil %>#{attribute('start', :start)}><%
content.each do |item| %>
<li>
<p><%= item.text %></p><%
@@ -726,7 +727,7 @@ if (attr :rowcount) >= 0 %>
if tsec == :head
cell_content = cell.text
else
- case (cell.attr 'style', nil, false)
+ case cell.style
when :asciidoc
cell_content = %(<div>\#{cell.content}</div>)
when :verse
@@ -888,7 +889,7 @@ class InlineQuotedTemplate < BaseTemplate
end
def result(node)
- quote_text(node.text, node.type, (node.attr 'role'))
+ quote_text(node.text, node.type, node.role)
end
def template
@@ -952,7 +953,7 @@ class InlineAnchorTemplate < BaseTemplate
when :ref
%(<a id="#{target}"></a>)
when :link
- %(<a href="#{target}"#{(node.attr? 'role') ? " class=\"#{node.attr 'role'}\"" : nil}#{(node.attr? 'window') ? " target=\"#{node.attr 'window'}\"" : nil}>#{text}</a>)
+ %(<a href="#{target}"#{node.role? ? " class=\"#{node.role}\"" : nil}#{(node.attr? 'window') ? " target=\"#{node.attr 'window'}\"" : nil}>#{text}</a>)
when :bibref
%(<a id="#{target}"></a>[#{target}])
end
diff --git a/lib/asciidoctor/lexer.rb b/lib/asciidoctor/lexer.rb
index e6b10484..abafbc2a 100644
--- a/lib/asciidoctor/lexer.rb
+++ b/lib/asciidoctor/lexer.rb
@@ -695,6 +695,9 @@ class Lexer
block.id ||= attributes['id'] if attributes.has_key?('id')
block.title = attributes['title'] unless block.title?
block.caption ||= attributes.delete('caption')
+ # TODO eventualy remove the style attribute from the attributes hash
+ #block.style = attributes.delete('style')
+ block.style = attributes['style']
# AsciiDoc always use [id] as the reftext in HTML output,
# but I'd like to do better in Asciidoctor
if block.id && block.title? && !attributes.has_key?('reftext')
diff --git a/lib/asciidoctor/substituters.rb b/lib/asciidoctor/substituters.rb
index 0c3acb4e..59e51a19 100644
--- a/lib/asciidoctor/substituters.rb
+++ b/lib/asciidoctor/substituters.rb
@@ -95,7 +95,7 @@ module Substituters
def apply_literal_subs(lines)
if attr? 'subs'
apply_subs(lines.join, resolve_subs(attr 'subs'))
- elsif @document.attributes['basebackend'] == 'html' && attr('style') == 'source' &&
+ elsif @document.attributes['basebackend'] == 'html' && @style == 'source' &&
@document.attributes['source-highlighter'] == 'coderay' && attr?('language')
sub_callouts(highlight_source(lines.join))
else
diff --git a/lib/asciidoctor/table.rb b/lib/asciidoctor/table.rb
index 64e50b0e..057d7005 100644
--- a/lib/asciidoctor/table.rb
+++ b/lib/asciidoctor/table.rb
@@ -113,7 +113,7 @@ class Table < AbstractBlock
if !rows.body.empty? && @has_header_option
head = rows.body.shift
# styles aren't applied to header row
- head.each {|c| c.attributes.delete('style') }
+ head.each {|c| c.style = nil }
# QUESTION why does AsciiDoc use an array for head? is it
# possible to have more than one based on the syntax?
rows.head = [head]
@@ -143,8 +143,12 @@ Table::Rows = Struct.new(:head, :foot, :body)
# Public: Methods to manage the columns of an AsciiDoc table. In particular, it
# keeps track of the column specs
class Table::Column < AbstractNode
+ # Public: Get/Set the Symbol style for this column.
+ attr_accessor :style
+
def initialize(table, index, attributes = {})
super(table, :column)
+ @style = attributes['style']
attributes['colnumber'] = index + 1
attributes['width'] ||= 1
attributes['halign'] ||= 'left'
@@ -177,6 +181,8 @@ end
# Public: Methods for managing the a cell in an AsciiDoc table.
class Table::Cell < AbstractNode
+ # Public: Get/Set the Symbol style for this cell (default: nil)
+ attr_accessor :style
# Public: An Integer of the number of columns this cell will span (default: nil)
attr_accessor :colspan
@@ -193,25 +199,24 @@ class Table::Cell < AbstractNode
def initialize(column, text, attributes = {})
super(column, :cell)
@text = text
+ @style = nil
@colspan = nil
@rowspan = nil
# TODO feels hacky
if !column.nil?
+ @style = column.attributes['style']
update_attributes(column.attributes)
end
if !attributes.nil?
- if attributes.has_key? 'colspan'
- @colspan = attributes['colspan']
- attributes.delete('colspan')
- end
- if attributes.has_key? 'rowspan'
- @rowspan = attributes['rowspan']
- attributes.delete('rowspan')
- end
+ @colspan = attributes.delete('colspan')
+ @rowspan = attributes.delete('rowspan')
+ # TODO eventualy remove the style attribute from the attributes hash
+ #@style = attributes.delete('style') if attributes.has_key? 'style'
+ @style = attributes['style'] if attributes.has_key? 'style'
update_attributes(attributes)
end
# only allow AsciiDoc cells in non-header rows
- if @attributes['style'] == :asciidoc && !column.table.header_row?
+ if @style == :asciidoc && !column.table.header_row?
# FIXME hide doctitle from nested document; temporary workaround to fix
# nested document seeing doctitle and assuming it has its own document title
parent_doctitle = @document.attributes.delete('doctitle')
@@ -227,12 +232,11 @@ class Table::Cell < AbstractNode
# Public: Handles the body data (tbody, tfoot), applying styles and partitioning into paragraphs
def content
- style = attr('style')
- if style == :asciidoc
+ if @style == :asciidoc
@inner_document.render
else
text.split(BLANK_LINE_PATTERN).map {|p|
- !style || style == :header ? p : Inline.new(parent, :quoted, p, :type => attr('style')).render
+ !@style || @style == :header ? p : Inline.new(parent, :quoted, p, :type => @style).render
}
end
end