Skip to content

Commit 0e91834

Browse files
authored
Merge pull request #8 from ricardoricho/image-description
Image description
2 parents d4c954b + 1825376 commit 0e91834

11 files changed

Lines changed: 237 additions & 18 deletions

File tree

lib/org-ruby.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# internal requires
22
require 'org-ruby/version'
33
require 'orgmode/elements/document'
4+
require 'orgmode/elements/link'
45
require 'org-ruby/parser'
56
require 'org-ruby/regexp_helper'
67
require 'org-ruby/line'

lib/org-ruby/html_output_buffer.rb

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -431,24 +431,8 @@ def rewrite_links(str)
431431
text.sub!(/\Afile(|\+emacs|\+sys):(?=[^\s]+\Z)/, "")
432432
end
433433

434-
# We don't add a description for images in links, because its
435-
# empty value forces the image to be inlined.
436-
defi ||= link unless link =~ @re_help.org_image_file_regexp
437-
438-
if defi =~ @re_help.org_image_file_regexp
439-
defi = quote_tags "<img src=\"#{defi}\" alt=\"#{defi}\" />"
440-
end
441-
442-
if defi
443-
link = options[:link_abbrevs][link] if options[:link_abbrevs].has_key?(link)
444-
target = document.targets.find do |target|
445-
target[:content] == defi
446-
end
447-
link = "#tg.#{target[:index]}" if target
448-
quote_tags("<a href=\"#{link}\">") + defi + quote_tags("</a>")
449-
else
450-
quote_tags "<img src=\"#{link}\" alt=\"#{link}\" />"
451-
end
434+
org_link = Orgmode::Elements::Link.new(document, link, defi)
435+
quote_tags org_link.html_tag
452436
end
453437
end
454438

lib/org-ruby/image_regexp.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Orgmode
2+
module ImageRegexp
3+
def image_file
4+
/\.(gif|jpe?g|p(?:bm|gm|n[gm]|pm)|svgz?|tiff?|x[bp]m)/i
5+
end
6+
end
7+
end

lib/org-ruby/regexp_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'org-ruby/line_regexp'
22
require 'org-ruby/headline_regexp'
3+
require 'org-ruby/image_regexp'
34

45
module Orgmode
56

@@ -21,6 +22,7 @@ module Orgmode
2122
class RegexpHelper
2223
extend LineRegexp
2324
extend HeadlineRegexp
25+
extend ImageRegexp
2426

2527
######################################################################
2628
# EMPHASIS

lib/orgmode/elements/link.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
module Orgmode
2+
module Elements
3+
class Link
4+
attr_reader :url, :description, :document
5+
6+
def initialize(document, url, description = nil)
7+
@document = document
8+
@url = expand(url, document.link_abbreviations)
9+
@description = description
10+
end
11+
12+
def html_tag
13+
return image_tag if target_image?
14+
return target_tag unless document.targets.empty?
15+
16+
"<a href=\"#{url}\">#{description || url}</a>"
17+
end
18+
19+
private
20+
21+
def expand(url, abbreviations)
22+
return url if abbreviations.nil?
23+
return url unless abbreviations.has_key?(url)
24+
25+
abbreviations[url]
26+
end
27+
28+
def description_img_tag
29+
"<img src=\"#{description}\" alt=\"#{description}\" />"
30+
end
31+
32+
def find_target(targets = [])
33+
targets.find do |target|
34+
target[:content] == description || target[:content] == url
35+
end
36+
end
37+
38+
def image_file?(file)
39+
RegexpHelper.image_file.match(file)
40+
end
41+
42+
def image_tag
43+
if !image_file?(url)
44+
"<a href=\"#{url}\">#{description_img_tag}</a>"
45+
elsif image_file? description
46+
description_img_tag
47+
elsif description.nil?
48+
"<img src=\"#{url}\" alt=\"#{url}\" />"
49+
else
50+
"<img src=\"#{url}\" alt=\"#{description}\" />"
51+
end
52+
end
53+
54+
def target_image?
55+
return @target_image unless @target_image.nil?
56+
57+
@target_image = image_file?(url) || image_file?(description) || false
58+
end
59+
60+
def target_tag
61+
target = find_target(document.targets)
62+
link = target ? "#tg.#{target[:index]}" : url
63+
64+
"<a href=\"#{link}\">#{description || url}</a>"
65+
end
66+
end
67+
end
68+
end

spec/html_examples/include-file.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ <h2>Within a center block</h2>
107107
<p><img src="images/animate-dom-01-f.svg" alt="images/animate-dom-01-f.svg" /></p>
108108
<p>Sample relative link to .svgz:</p>
109109
<p><img src="images/conform-viewers-01-t.svgz" alt="images/conform-viewers-01-t.svgz" /></p>
110+
<p>Image with alt description</p>
111+
<p>This image should use the description as the alt attribute:</p>
112+
<p><img src="/images/emacs-logo.png" alt="A screenshot of our new UI" /></p>
113+
<p>This is an inline <img src="images/emacs-logo.svg" alt="emacs-logo" /> from a link.</p>
110114
</div>
111115
<h2>Within a blockquote</h2>
112116
<p>This is similar to the center block:</p>
@@ -134,6 +138,10 @@ <h2>Within a blockquote</h2>
134138
<p><img src="images/animate-dom-01-f.svg" alt="images/animate-dom-01-f.svg" /></p>
135139
<p>Sample relative link to .svgz:</p>
136140
<p><img src="images/conform-viewers-01-t.svgz" alt="images/conform-viewers-01-t.svgz" /></p>
141+
<p>Image with alt description</p>
142+
<p>This image should use the description as the alt attribute:</p>
143+
<p><img src="/images/emacs-logo.png" alt="A screenshot of our new UI" /></p>
144+
<p>This is an inline <img src="images/emacs-logo.svg" alt="emacs-logo" /> from a link.</p>
137145
<p>After</p>
138146
</blockquote>
139147
<h2>Within an example block, it should not be possible to include a file.</h2>

spec/html_examples/inline-images.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@
2020
<p><img src="images/animate-dom-01-f.svg" alt="images/animate-dom-01-f.svg" /></p>
2121
<p>Sample relative link to .svgz:</p>
2222
<p><img src="images/conform-viewers-01-t.svgz" alt="images/conform-viewers-01-t.svgz" /></p>
23+
<p>Image with alt description</p>
24+
<p>This image should use the description as the alt attribute:</p>
25+
<p><img src="/images/emacs-logo.png" alt="A screenshot of our new UI" /></p>
26+
<p>This is an inline <img src="images/emacs-logo.svg" alt="emacs-logo" /> from a link.</p>

spec/html_examples/inline-images.org

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,11 @@ Sample relative link to .svg:
3030
Sample relative link to .svgz:
3131

3232
[[file:images/conform-viewers-01-t.svgz]]
33+
34+
Image with alt description
35+
36+
This image should use the description as the alt attribute:
37+
38+
[[file:/images/emacs-logo.png][A screenshot of our new UI]]
39+
40+
This is an inline [[file:images/emacs-logo.svg][emacs-logo]] from a link.

spec/org-ruby/image_regexp_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'spec_helper'
2+
3+
module Orgmode
4+
RSpec.describe ImageRegexp do
5+
class DummyRegexp
6+
include ImageRegexp
7+
end
8+
let(:regexp) { DummyRegexp.new }
9+
10+
describe 'image_file' do
11+
it { expect(regexp.image_file).to match 'file.jpg' }
12+
it { expect(regexp.image_file).to match 'file.jpeg' }
13+
it { expect(regexp.image_file).to match 'file.png' }
14+
it { expect(regexp.image_file).to match 'file.svg' }
15+
it { expect(regexp.image_file).to match 'some/path/file.gif' }
16+
it { expect(regexp.image_file).to match 'other.svgz' }
17+
it { expect(regexp.image_file).to match 'tiffany.tiff' }
18+
it { expect(regexp.image_file).to match 'xx.xpm' }
19+
it { expect(regexp.image_file).to match 'yy.xbm' }
20+
21+
it { expect(regexp.image_file).not_to match 'file' }
22+
it { expect(regexp.image_file).not_to match 'path/file/' }
23+
it { expect(regexp.image_file).not_to match 'file.pdf' }
24+
it { expect(regexp.image_file).not_to match 'some/file.xml' }
25+
end
26+
end
27+
end

spec/orgmode/elements/document_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'spec_helper'
2+
23
module Orgmode
34
module Elements
45
RSpec.describe Document do

0 commit comments

Comments
 (0)