Skip to content

Commit 76d8274

Browse files
committed
Switches the formatting of image links to default Org-mode behavior.
1 parent ed347dc commit 76d8274

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

lib/org-ruby/html_output_buffer.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,7 @@ def inline_formatting(str)
275275
end
276276
end
277277
end
278-
@re_help.rewrite_images str do |link|
279-
"@<a href=\"#{link}\">@<img src=\"#{link}\" />@</a>"
280-
end
281278
@re_help.rewrite_links str do |link, text|
282-
text ||= link
283279
link = link.sub(/^file:(.*)::(.*?)$/) do
284280

285281
# We don't support search links right now. Get rid of it.
@@ -292,10 +288,19 @@ def inline_formatting(str)
292288

293289
link = link.sub(/^file:/i, "") # will default to HTTP
294290

295-
text = text.gsub(/([^\]]*\.(jpg|jpeg|gif|png))/xi) do |img_link|
296-
"@<img src=\"#{img_link}\" />"
291+
# We don't add a description for images in links, because its
292+
# empty value forces the image to be inlined.
293+
text ||= link unless link =~ @re_help.org_image_file_regexp
294+
295+
if text =~ @re_help.org_image_file_regexp
296+
text = "@<img src=\"#{text}\" alt=\"#{text}\" />"
297+
end
298+
299+
if text
300+
"@<a href=\"#{link}\">#{text}@</a>"
301+
else
302+
"@<img src=\"#{link}\" alt=\"#{link}\" />"
297303
end
298-
"@<a href=\"#{link}\">#{text}@</a>"
299304
end
300305
if @output_type == :table_row
301306
str.gsub!(/^\|\s*/, "@<td>")

lib/org-ruby/regexp_helper.rb

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,8 @@ class RegexpHelper
3939
# body-regexp A regexp like \".\" to match a body character. Don't use
4040
# non-shy groups here, and don't allow newline here.
4141
# newline The maximum number of newlines allowed in an emphasis exp.
42-
attr_reader :pre_emphasis
43-
attr_reader :post_emphasis
44-
attr_reader :border_forbidden
45-
attr_reader :body_regexp
46-
attr_reader :max_newlines
47-
attr_reader :markers
4842

49-
attr_reader :org_emphasis_regexp
43+
attr_reader :org_image_file_regexp
5044

5145
def initialize
5246
# Set up the emphasis regular expression.
@@ -162,13 +156,6 @@ def rewrite_links str # :yields: link, text
162156
str # for testing
163157
end
164158

165-
# Rewrites all of the inline image tags.
166-
def rewrite_images str # :yields: image_link
167-
str.gsub! @org_img_regexp do |match|
168-
yield $1
169-
end
170-
end
171-
172159
def restore_code_snippets str
173160
str = str % @code_snippet_stack
174161
@code_snippet_stack = []
@@ -193,10 +180,8 @@ def build_org_link_regexp
193180
\](\[
194181
([^\]\[]+) # This is the friendly text
195182
\])?\]/x
196-
@org_img_regexp = /\[\[
197-
([^\]\[]+\.(jpg|jpeg|gif|png)) # Like a normal URL, but must end with a specified extension
198-
\]\]/xi
199183
@org_angle_link_text_regexp = /<(\w+:[^\]\s<>]+)>/
184+
@org_image_file_regexp = /\.(gif|jpe?g|p(?:bm|gm|n[gm]|pm)|svg|tiff?|x[bp]m)/i
200185
end
201186
end # class Emphasis
202187
end # module Orgmode

lib/org-ruby/textile_output_buffer.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,22 @@ def inline_formatting(input)
5555
end
5656
end
5757
@re_help.rewrite_links input do |link, text|
58-
text ||= link
58+
# We don't add a description for images in links, because its
59+
# empty value forces the image to be inlined.
60+
text ||= link unless link =~ @re_help.org_image_file_regexp
5961
link = link.gsub(/ /, "%%20")
60-
"\"#{text}\":#{link}"
62+
63+
if text =~ @re_help.org_image_file_regexp
64+
text = "!#{text}(#{text})!"
65+
else
66+
text = "\"#{text}\"" if text
67+
end
68+
69+
if text
70+
"#{text}:#{link}"
71+
else
72+
"!#{link}(#{link})!"
73+
end
6174
end
6275
@re_help.rewrite_footnote input do |name, definition|
6376
# textile only support numerical names, so we need to do some conversion

0 commit comments

Comments
 (0)