Skip to content

Commit e91ead2

Browse files
committed
Add \[ and \] to emphasis regexp to fix #85, emphasis inside links.
1 parent 871a2f2 commit e91ead2

4 files changed

Lines changed: 52 additions & 29 deletions

File tree

lib/org-ruby/html_output_buffer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def escape_string! str
319319
str.gsub! /@@html:(<[^<>\n]*>)@@/, "\\1"
320320
end
321321

322-
def quote_tags str
322+
def quote_tags(str)
323323
str.gsub /(<[^<>\n]*>)/, "@@html:\\1@@"
324324
end
325325

lib/org-ruby/regexp_helper.rb

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,11 @@ class RegexpHelper
4040
# non-shy groups here, and don't allow newline here.
4141
# newline The maximum number of newlines allowed in an emphasis exp.
4242

43-
attr_reader :org_image_file_regexp
44-
4543
def initialize
4644
# Set up the emphasis regular expression.
4745
@code_snippet_stack = []
4846
@logger = Logger.new(STDERR)
4947
@logger.level = Logger::WARN
50-
build_org_link_regexp
51-
@org_subp_regexp = /([_^])\{(.*?)\}/
52-
@org_footnote_regexp = /\[fn:(.+?)(:(.*))?\]/
53-
@org_footnote_def_regexp = /^\[fn:(.+?)(:(.*))?\]( (.+))?/
5448
end
5549

5650
# Finds all emphasis matches in a string.
@@ -102,21 +96,21 @@ def rewrite_emphasis(str)
10296
end
10397

10498
# rewrite subscript and superscript (_{foo} and ^{bar})
105-
def rewrite_subp(str) # :yields: type ("_" for subscript and "^" for superscript), text
106-
str.gsub! @org_subp_regexp do |_match|
99+
def rewrite_subp(str)
100+
str.gsub!(org_subp_regexp) do |_match|
107101
yield Regexp.last_match(1), Regexp.last_match(2)
108102
end
109103
end
110104

111105
# rewrite footnotes
112-
def rewrite_footnote(str) # :yields: name, definition or nil
113-
str.gsub! @org_footnote_regexp do |_match|
106+
def rewrite_footnote(str)
107+
str.gsub!(org_footnote_regexp) do |_match|
114108
yield Regexp.last_match(1), Regexp.last_match(3)
115109
end
116110
end
117111

118112
def rewrite_footnote_definition(str)
119-
str.gsub! @org_footnote_def_regexp do |_match|
113+
str.gsub!(org_footnote_def_regexp) do |_match|
120114
yield Regexp.last_match(1), Regexp.last_match(5)
121115
end
122116
end
@@ -147,10 +141,10 @@ def rewrite_footnote_definition(str)
147141
# HTML-style link, and that is how things will get recorded in
148142
# +result+.
149143
def rewrite_links(str)
150-
str.gsub! @org_link_regexp do |_match|
151-
yield Regexp.last_match(1), Regexp.last_match(3)
144+
str.gsub!(org_link_regexp) do |_match|
145+
yield Regexp.last_match['url'], Regexp.last_match['friendly_text']
152146
end
153-
str.gsub! @org_angle_link_text_regexp do |_match|
147+
str.gsub!(org_angle_link_text_regexp) do |_match|
154148
yield Regexp.last_match(1), nil
155149
end
156150

@@ -172,10 +166,18 @@ def org_emphasis_regexp
172166
"(?=#{post_emphasis})")
173167
end
174168

169+
def org_link_regexp
170+
/\[\[(?<url>[^\[\]]+)\](\[(?<friendly_text>[^\[\]]+)\])?\]/x
171+
end
172+
173+
def org_image_file_regexp
174+
/\.(gif|jpe?g|p(?:bm|gm|n[gm]|pm)|svgz?|tiff?|x[bp]m)/i
175+
end
176+
175177
private
176178

177179
def pre_emphasis_regexp
178-
'^|\s|[\(\'"\{]'
180+
'^|\s|[\(\'"\{\[]'
179181
end
180182

181183
def markers_regexp
@@ -187,21 +189,27 @@ def border_forbidden
187189
end
188190

189191
def post_emphasis
190-
'\s|[-,\.;:!\?\'"\)\}]|$'
192+
'\s|[-,\.;:!\?\'"\)\}\]]|$'
191193
end
192194

193195
def body_regexp
194196
'.*?(?:\\n.*?){0,1}'
195197
end
196198

197-
def build_org_link_regexp
198-
@org_link_regexp = /\[\[
199-
([^\]\[]+) # This is the URL
200-
\](\[
201-
([^\]\[]+) # This is the friendly text
202-
\])?\]/x
203-
@org_angle_link_text_regexp = /<(\w+:[^\]\s<>]+)>/
204-
@org_image_file_regexp = /\.(gif|jpe?g|p(?:bm|gm|n[gm]|pm)|svgz?|tiff?|x[bp]m)/i
199+
def org_subp_regexp
200+
/([_^])\{(.*?)\}/
201+
end
202+
203+
def org_footnote_regexp
204+
/\[fn:(.+?)(:(.*))?\]/
205+
end
206+
207+
def org_footnote_def_regexp
208+
/^\[fn:(.+?)(:(.*))?\]( (.+))?/
209+
end
210+
211+
def org_angle_link_text_regexp
212+
/<(\w+:[^\]\s<>]+)>/
205213
end
206214
end
207215
end

spec/org-ruby/html_output_buffer_spec.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ module Orgmode
8989
end
9090
end
9191

92-
xcontext 'when tag is table' do
93-
end
94-
9592
context 'when called for second time' do
9693
before(:each) do
9794
buffer.push_mode(mode, indent)

spec/regexp_helper_spec.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
require 'spec_helper'
22

33
describe Orgmode::RegexpHelper do
4+
let(:helper) { Orgmode::RegexpHelper.new }
5+
6+
describe '#org-emphasis-regexp' do
7+
it 'match emphasis expresions' do
8+
expect(helper.org_emphasis_regexp).to match '~code~'
9+
end
10+
example { expect(helper.org_emphasis_regexp).to match '[[a][~a~]]' }
11+
end
12+
13+
describe '#org-link-regexp' do
14+
it 'match org-links' do
15+
expect(helper.org_link_regexp).to match '[[url][description]]'
16+
end
17+
18+
example { expect(helper.org_link_regexp).to match '[[url]]' }
19+
example { expect(helper.org_link_regexp).to match '[[a][~a~]]' }
20+
end
21+
422
it "should recognize simple markup" do
523
e = Orgmode::RegexpHelper.new
624
total = 0
@@ -73,4 +91,4 @@
7391

7492
expect(n).to eql("This string contains a quote using code markup: <code>\"</code>")
7593
end
76-
end # describe Orgmode::RegexpHelper
94+
end

0 commit comments

Comments
 (0)