@@ -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
207215end
0 commit comments