Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #537 from mojavelinux/inline-icon-macro
resolves #529 add inline icon macro
  • Loading branch information
mojavelinux committed Aug 8, 2013
2 parents 71c7cad + 22e716f commit a41d93a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 15 deletions.
3 changes: 2 additions & 1 deletion lib/asciidoctor.rb
Expand Up @@ -383,7 +383,8 @@ module SafeMode
# image:filename.png[Alt Text]
# image:http://example.com/images/filename.png[Alt Text]
# image:filename.png[More [Alt\] Text] (alt text becomes "More [Alt] Text")
:image_macro => /\\?image:([^:\[][^\[]*)\[((?:\\\]|[^\]])*?)\]/,
# icon:github[large]
:image_macro => /\\?(?:image|icon):([^:\[][^\[]*)\[((?:\\\]|[^\]])*?)\]/,

# indexterm:[Tigers,Big cats]
# (((Tigers,Big cats)))
Expand Down
2 changes: 1 addition & 1 deletion lib/asciidoctor/backends/docbook45.rb
Expand Up @@ -729,7 +729,7 @@ def template
@template ||= @eruby.new <<-EOF
<%#encoding:UTF-8%><inlinemediaobject>
<imageobject>
<imagedata fileref="<%= image_uri(@target) %>"#{attribute('width', :width)}#{attribute('depth', :height)}/>
<imagedata fileref="<%= @type == 'icon' ? icon_uri(@target) : image_uri(@target) %>"#{attribute('width', :width)}#{attribute('depth', :height)}/>
</imageobject>
<textobject><phrase><%= attr :alt %></phrase></textobject>
</inlinemediaobject>
Expand Down
55 changes: 46 additions & 9 deletions lib/asciidoctor/backends/html5.rb
Expand Up @@ -74,7 +74,7 @@ def template
end
if attr? 'icons', 'font'
if !(attr 'iconfont-remote', '').nil? %>
<link rel="stylesheet" href="<%= attr 'iconfont-cdn', 'http://cdnjs.cloudflare.com/ajax/libs/font-awesome/3.2.1/css' %>/<%= attr 'iconfont-name', 'font-awesome' %>.min.css"><%
<link rel="stylesheet" href="<%= attr 'iconfont-cdn', 'http://cdnjs.cloudflare.com/ajax/libs/font-awesome/3.2.1/css/font-awesome.min.css' %>"><%
else %>
<link rel="stylesheet" href="<%= normalize_web_path(%(\#{attr 'iconfont-name', 'font-awesome'}.css), (attr 'stylesdir', '')) %>"><%
end
Expand Down Expand Up @@ -1000,15 +1000,52 @@ def template
end

class InlineImageTemplate < BaseTemplate
def image(target, type, node)
if type == 'icon' && (node.document.attr? 'icons', 'font')
style_class = "icon-#{target}"
if node.attr? 'size'
style_class = "#{style_class} icon-#{node.attr 'size'}"
end
if node.attr? 'rotate'
style_class = "#{style_class} icon-rotate-#{node.attr 'rotate'}"
end
if node.attr? 'flip'
style_class = "#{style_class} icon-flip-#{node.attr 'flip'}"
end
img = %(<i class="#{style_class}"></i>)
span = false
else
if type == 'icon'
resolved_target = node.icon_uri target
else
resolved_target = node.image_uri target
end

attrs = ['alt', 'width', 'height', 'title'].map {|name|
if node.attr? name
%( #{name}="#{node.attr name}")
else
nil
end
}.join

img = %(<img src="#{resolved_target}"#{attrs}>)
span = true
end

if node.attr? 'link'
img = %(<a class="image" href="#{node.attr 'link'}"#{(node.attr? 'window') ? " target=\"#{node.attr 'window'}\"" : nil}>#{img}</a>)
end

span ? %(<span class="image#{node.role? ? " #{node.role}" : nil}">#{img}</span>) : img
end

def result(node)
image(node.target, node.type, node)
end

def template
# care is taken here to avoid a space inside the optional <a> tag
@template ||= @eruby.new <<-EOS
<%#encoding:UTF-8%><span class="image#{role_class}"><%
if attr? :link %><a class="image" href="<%= attr :link %>"><%
end %><img src="<%= image_uri(@target) %>" alt="<%= attr :alt %>"#{attribute('width', :width)}#{attribute('height', :height)}#{attribute('title', :title)}><%
if attr? :link%></a><% end
%></span>
EOS
:invoke_result
end
end

Expand Down
19 changes: 15 additions & 4 deletions lib/asciidoctor/substituters.rb
Expand Up @@ -450,7 +450,7 @@ def sub_macros(text)
end
end

if found[:macroish] && result.include?('image:')
if found[:macroish] && (result.include?('image:') || result.include?('icon:'))
# image:filename.png[Alt Text]
result.gsub!(REGEXP[:image_macro]) {
# alias match for Ruby 1.8.7 compat
Expand All @@ -459,13 +459,24 @@ def sub_macros(text)
if m[0].start_with? '\\'
next m[0][1..-1]
end

raw_attrs = unescape_bracketed_text m[2]
if m[0].start_with? 'icon:'
type = 'icon'
posattrs = ['size']
else
type = 'image'
posattrs = ['alt', 'width', 'height']
end
target = sub_attributes(m[1])
@document.register(:images, target)
attrs = parse_attributes(unescape_bracketed_text(m[2]), ['alt', 'width', 'height'])
unless type == 'icon'
@document.register(:images, target)
end
attrs = parse_attributes(raw_attrs, posattrs)
if !attrs['alt']
attrs['alt'] = File.basename(target, File.extname(target))
end
Inline.new(self, :image, nil, :target => target, :attributes => attrs).render
Inline.new(self, :image, nil, :type => type, :target => target, :attributes => attrs).render
}
end

Expand Down
15 changes: 15 additions & 0 deletions test/substitutions_test.rb
Expand Up @@ -496,6 +496,21 @@
assert result.include?('image::tiger.png[]')
end

test 'an icon macro should be interpreted as an icon' do
para = block_from_string('icon:github[]')
assert_equal %{<span class="image"><img src="./images/icons/github.png" alt="github"></span>}, para.sub_macros(para.buffer.join).gsub(/>\s+</, '><')
end

test 'an icon macro should be interpreted as a font-based icon when icons=font' do
para = block_from_string 'icon:github[]', :attributes => {'icons' => 'font'}
assert_equal %{<i class="icon-github"></i>}, para.sub_macros(para.buffer.join).gsub(/>\s+</, '><')
end

test 'an icon macro with a size should be interpreted as a font-based icon with a size when icons=font' do
para = block_from_string 'icon:github[4x]', :attributes => {'icons' => 'font'}
assert_equal %{<i class="icon-github icon-4x"></i>}, para.sub_macros(para.buffer.join).gsub(/>\s+</, '><')
end

test 'a single-line footnote macro should be registered and rendered as a footnote' do
para = block_from_string('Sentence text footnote:[An example footnote.].')
assert_equal %(Sentence text <span class="footnote">[<a id="_footnoteref_1" class="footnote" href="#_footnote_1" title="View footnote.">1</a>]</span>.), para.sub_macros(para.buffer.join)
Expand Down

0 comments on commit a41d93a

Please sign in to comment.