diff --git a/lib/org-ruby.rb b/lib/org-ruby.rb
index 39e5167..0aebfae 100644
--- a/lib/org-ruby.rb
+++ b/lib/org-ruby.rb
@@ -1,6 +1,7 @@
# internal requires
require 'org-ruby/version'
require 'orgmode/elements/document'
+require 'orgmode/elements/link'
require 'org-ruby/parser'
require 'org-ruby/regexp_helper'
require 'org-ruby/line'
diff --git a/lib/org-ruby/html_output_buffer.rb b/lib/org-ruby/html_output_buffer.rb
index bc39c33..ba983cf 100644
--- a/lib/org-ruby/html_output_buffer.rb
+++ b/lib/org-ruby/html_output_buffer.rb
@@ -431,24 +431,8 @@ def rewrite_links(str)
text.sub!(/\Afile(|\+emacs|\+sys):(?=[^\s]+\Z)/, "")
end
- # We don't add a description for images in links, because its
- # empty value forces the image to be inlined.
- defi ||= link unless link =~ @re_help.org_image_file_regexp
-
- if defi =~ @re_help.org_image_file_regexp
- defi = quote_tags ""
- end
-
- if defi
- link = options[:link_abbrevs][link] if options[:link_abbrevs].has_key?(link)
- target = document.targets.find do |target|
- target[:content] == defi
- end
- link = "#tg.#{target[:index]}" if target
- quote_tags("") + defi + quote_tags("")
- else
- quote_tags "
"
- end
+ org_link = Orgmode::Elements::Link.new(document, link, defi)
+ quote_tags org_link.html_tag
end
end
diff --git a/lib/org-ruby/image_regexp.rb b/lib/org-ruby/image_regexp.rb
new file mode 100644
index 0000000..6fc9db4
--- /dev/null
+++ b/lib/org-ruby/image_regexp.rb
@@ -0,0 +1,7 @@
+module Orgmode
+ module ImageRegexp
+ def image_file
+ /\.(gif|jpe?g|p(?:bm|gm|n[gm]|pm)|svgz?|tiff?|x[bp]m)/i
+ end
+ end
+end
diff --git a/lib/org-ruby/regexp_helper.rb b/lib/org-ruby/regexp_helper.rb
index 5a0166d..5d18f8d 100644
--- a/lib/org-ruby/regexp_helper.rb
+++ b/lib/org-ruby/regexp_helper.rb
@@ -1,5 +1,6 @@
require 'org-ruby/line_regexp'
require 'org-ruby/headline_regexp'
+require 'org-ruby/image_regexp'
module Orgmode
@@ -21,6 +22,7 @@ module Orgmode
class RegexpHelper
extend LineRegexp
extend HeadlineRegexp
+ extend ImageRegexp
######################################################################
# EMPHASIS
diff --git a/lib/orgmode/elements/link.rb b/lib/orgmode/elements/link.rb
new file mode 100644
index 0000000..62125ca
--- /dev/null
+++ b/lib/orgmode/elements/link.rb
@@ -0,0 +1,68 @@
+module Orgmode
+ module Elements
+ class Link
+ attr_reader :url, :description, :document
+
+ def initialize(document, url, description = nil)
+ @document = document
+ @url = expand(url, document.link_abbreviations)
+ @description = description
+ end
+
+ def html_tag
+ return image_tag if target_image?
+ return target_tag unless document.targets.empty?
+
+ "#{description || url}"
+ end
+
+ private
+
+ def expand(url, abbreviations)
+ return url if abbreviations.nil?
+ return url unless abbreviations.has_key?(url)
+
+ abbreviations[url]
+ end
+
+ def description_img_tag
+ "
"
+ end
+
+ def find_target(targets = [])
+ targets.find do |target|
+ target[:content] == description || target[:content] == url
+ end
+ end
+
+ def image_file?(file)
+ RegexpHelper.image_file.match(file)
+ end
+
+ def image_tag
+ if !image_file?(url)
+ "#{description_img_tag}"
+ elsif image_file? description
+ description_img_tag
+ elsif description.nil?
+ "
"
+ else
+ "
"
+ end
+ end
+
+ def target_image?
+ return @target_image unless @target_image.nil?
+
+ @target_image = image_file?(url) || image_file?(description) || false
+ end
+
+ def target_tag
+ target = find_target(document.targets)
+ link = target ? "#tg.#{target[:index]}" : url
+
+ "#{description || url}"
+ end
+ end
+ end
+end
diff --git a/spec/html_examples/include-file.html b/spec/html_examples/include-file.html
index ab56cb6..cb8ab74 100644
--- a/spec/html_examples/include-file.html
+++ b/spec/html_examples/include-file.html
@@ -107,6 +107,10 @@
Sample relative link to .svgz:
Image with alt description
+This image should use the description as the alt attribute:
+
This is an inline from a link.
This is similar to the center block:
@@ -134,6 +138,10 @@Sample relative link to .svgz:
Image with alt description
+This image should use the description as the alt attribute:
+
This is an inline from a link.
After
Sample relative link to .svgz:
Image with alt description
+This image should use the description as the alt attribute:
+
This is an inline from a link.