Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/org-ruby.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
20 changes: 2 additions & 18 deletions lib/org-ruby/html_output_buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<img src=\"#{defi}\" alt=\"#{defi}\" />"
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("<a href=\"#{link}\">") + defi + quote_tags("</a>")
else
quote_tags "<img src=\"#{link}\" alt=\"#{link}\" />"
end
org_link = Orgmode::Elements::Link.new(document, link, defi)
quote_tags org_link.html_tag
end
end

Expand Down
7 changes: 7 additions & 0 deletions lib/org-ruby/image_regexp.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions lib/org-ruby/regexp_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'org-ruby/line_regexp'
require 'org-ruby/headline_regexp'
require 'org-ruby/image_regexp'

module Orgmode

Expand All @@ -21,6 +22,7 @@ module Orgmode
class RegexpHelper
extend LineRegexp
extend HeadlineRegexp
extend ImageRegexp

######################################################################
# EMPHASIS
Expand Down
68 changes: 68 additions & 0 deletions lib/orgmode/elements/link.rb
Original file line number Diff line number Diff line change
@@ -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?

"<a href=\"#{url}\">#{description || url}</a>"
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
"<img src=\"#{description}\" alt=\"#{description}\" />"
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)
"<a href=\"#{url}\">#{description_img_tag}</a>"
elsif image_file? description
description_img_tag
elsif description.nil?
"<img src=\"#{url}\" alt=\"#{url}\" />"
else
"<img src=\"#{url}\" alt=\"#{description}\" />"
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

"<a href=\"#{link}\">#{description || url}</a>"
end
end
end
end
8 changes: 8 additions & 0 deletions spec/html_examples/include-file.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ <h2>Within a center block</h2>
<p><img src="images/animate-dom-01-f.svg" alt="images/animate-dom-01-f.svg" /></p>
<p>Sample relative link to .svgz:</p>
<p><img src="images/conform-viewers-01-t.svgz" alt="images/conform-viewers-01-t.svgz" /></p>
<p>Image with alt description</p>
<p>This image should use the description as the alt attribute:</p>
<p><img src="/images/emacs-logo.png" alt="A screenshot of our new UI" /></p>
<p>This is an inline <img src="images/emacs-logo.svg" alt="emacs-logo" /> from a link.</p>
</div>
<h2>Within a blockquote</h2>
<p>This is similar to the center block:</p>
Expand Down Expand Up @@ -134,6 +138,10 @@ <h2>Within a blockquote</h2>
<p><img src="images/animate-dom-01-f.svg" alt="images/animate-dom-01-f.svg" /></p>
<p>Sample relative link to .svgz:</p>
<p><img src="images/conform-viewers-01-t.svgz" alt="images/conform-viewers-01-t.svgz" /></p>
<p>Image with alt description</p>
<p>This image should use the description as the alt attribute:</p>
<p><img src="/images/emacs-logo.png" alt="A screenshot of our new UI" /></p>
<p>This is an inline <img src="images/emacs-logo.svg" alt="emacs-logo" /> from a link.</p>
<p>After</p>
</blockquote>
<h2>Within an example block, it should not be possible to include a file.</h2>
Expand Down
4 changes: 4 additions & 0 deletions spec/html_examples/inline-images.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@
<p><img src="images/animate-dom-01-f.svg" alt="images/animate-dom-01-f.svg" /></p>
<p>Sample relative link to .svgz:</p>
<p><img src="images/conform-viewers-01-t.svgz" alt="images/conform-viewers-01-t.svgz" /></p>
<p>Image with alt description</p>
<p>This image should use the description as the alt attribute:</p>
<p><img src="/images/emacs-logo.png" alt="A screenshot of our new UI" /></p>
<p>This is an inline <img src="images/emacs-logo.svg" alt="emacs-logo" /> from a link.</p>
8 changes: 8 additions & 0 deletions spec/html_examples/inline-images.org
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ Sample relative link to .svg:
Sample relative link to .svgz:

[[file:images/conform-viewers-01-t.svgz]]

Image with alt description

This image should use the description as the alt attribute:

[[file:/images/emacs-logo.png][A screenshot of our new UI]]

This is an inline [[file:images/emacs-logo.svg][emacs-logo]] from a link.
27 changes: 27 additions & 0 deletions spec/org-ruby/image_regexp_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'spec_helper'

module Orgmode
RSpec.describe ImageRegexp do
class DummyRegexp
include ImageRegexp
end
let(:regexp) { DummyRegexp.new }

describe 'image_file' do
it { expect(regexp.image_file).to match 'file.jpg' }
it { expect(regexp.image_file).to match 'file.jpeg' }
it { expect(regexp.image_file).to match 'file.png' }
it { expect(regexp.image_file).to match 'file.svg' }
it { expect(regexp.image_file).to match 'some/path/file.gif' }
it { expect(regexp.image_file).to match 'other.svgz' }
it { expect(regexp.image_file).to match 'tiffany.tiff' }
it { expect(regexp.image_file).to match 'xx.xpm' }
it { expect(regexp.image_file).to match 'yy.xbm' }

it { expect(regexp.image_file).not_to match 'file' }
it { expect(regexp.image_file).not_to match 'path/file/' }
it { expect(regexp.image_file).not_to match 'file.pdf' }
it { expect(regexp.image_file).not_to match 'some/file.xml' }
end
end
end
1 change: 1 addition & 0 deletions spec/orgmode/elements/document_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'

module Orgmode
module Elements
RSpec.describe Document do
Expand Down
109 changes: 109 additions & 0 deletions spec/orgmode/elements/link_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
require 'spec_helper'

module Orgmode
module Elements
RSpec.describe Link do
describe 'initialize' do
let(:document) { Document.new }
let(:url) { "This is a url" }
let(:link) { Link.new document, url }

it 'has a document' do
expect(link.document).not_to be_nil
end

it 'has an url' do
expect(link.url).not_to be_nil
end

it 'has a description' do
expect(link.description).to be_nil
end

context 'when description is present' do
let(:description) { "this is the description" }
let(:link) { Link.new document, url, description }

it 'set a description value' do
expect(link.description).to eq description
end
end

context 'when document has link abbrevitions' do
let(:url) { "abbrev"}

before do
line = Line.new "#+LINK: abbrev long_url"
document.store_link_abbreviation(line)
end

it 'expand url abbreviation' do
abbreviate_url = "long_url"
expect(link.url).to eq abbreviate_url
end
end
end

describe '#html_tag' do
let(:document) { Document.new }
let(:url) { "Any url" }
let(:description) { nil }
let(:link) { Link.new document, url, description }

context 'when description is nil' do
it 'return a html link with the url as description' do
html_link = "<a href=\"#{url}\">#{url}</a>"
expect(link.html_tag).to eq html_link
end
end

context 'when description is not nil' do
let(:description) { "this is the description"}

it 'return an html link to url with description as text' do
html_link = "<a href=\"#{url}\">#{description}</a>"
expect(link.html_tag).to eq html_link
end
end

context 'when url is an image' do
let(:url) { "an/image/path.png" }

context 'when description is text' do
let(:description) { "Text description"}

it 'return an img tag' do
img_tag = "<img src=\"#{url}\" alt=\"#{description}\" />"
expect(link.html_tag).to eq img_tag
end
end
end

context 'when url is not an image but description is' do
let(:url) { "link/path" }
let(:description) { "an_image.jpeg" }

it 'return an img tag wrapper in a link' do
img_tag = "<img src=\"#{description}\" alt=\"#{description}\" />"
link_tag = "<a href=\"#{url}\">#{img_tag}</a>"
expect(link.html_tag).to eq link_tag
end
end

context 'when document has targets' do
let(:url) { "target" }

before do
line = Line.new "<<target>>"
document.store_target(line)
end

it 'set url to numerated target' do
target_tag = "<a href=\"#tg.1\">target</a>"
expect(link.html_tag).to eq target_tag
end
end
end
end
end
end