@@ -44,18 +44,9 @@ class RegexpHelper
4444
4545 def initialize
4646 # Set up the emphasis regular expression.
47- @pre_emphasis = ' \t\(\'"\{'
48- @post_emphasis = '- \t\.,:!\?;\'"\)\}\\\\'
49- @border_forbidden = ' \t\r\n'
50- @body_regexp = '.*?'
51- @max_newlines = 1
52- @body_regexp = "#{ @body_regexp } " +
53- "(?:\\ n#{ @body_regexp } ){0,#{ @max_newlines } }" if @max_newlines > 0
54- @markers = '\*\/_=~\+'
5547 @code_snippet_stack = [ ]
5648 @logger = Logger . new ( STDERR )
5749 @logger . level = Logger ::WARN
58- build_org_emphasis_regexp
5950 build_org_link_regexp
6051 @org_subp_regexp = /([_^])\{ (.*?)\} /
6152 @org_footnote_regexp = /\[ fn:(.+?)(:(.*))?\] /
@@ -65,8 +56,8 @@ def initialize
6556 # Finds all emphasis matches in a string.
6657 # Supply a block that will get the marker and body as parameters.
6758 def match_all ( str )
68- str . scan ( @ org_emphasis_regexp) do |match |
69- yield $2 , $3
59+ str . scan ( org_emphasis_regexp ) do |_match |
60+ yield Regexp . last_match [ 2 ] , Regexp . last_match [ 3 ]
7061 end
7162 end
7263
@@ -95,39 +86,38 @@ def rewrite_emphasis(str)
9586 # escape the percent signs for safe restoring code snippets
9687 str . gsub! ( /%/ , "%%" )
9788 format_str = "%s"
98- str . gsub! @ org_emphasis_regexp do |match |
99- pre = $1
89+ str . gsub! ( org_emphasis_regexp ) do |_match |
90+ pre = Regexp . last_match ( 1 )
10091 # preserve the code snippet from further formatting
101- if $2 == "=" or $2 == "~"
102- inner = yield $2 , $3
92+ inner = yield Regexp . last_match ( 2 ) , Regexp . last_match ( 3 )
93+ if %w[ = ~ ] . include? ( Regexp . last_match ( 2 ) )
10394 # code is not formatted, so turn to single percent signs
10495 inner . gsub! ( /%%/ , "%" )
10596 @code_snippet_stack . push inner
10697 "#{ pre } #{ format_str } "
10798 else
108- inner = yield $2, $3
10999 "#{ pre } #{ inner } "
110100 end
111101 end
112102 end
113103
114104 # rewrite subscript and superscript (_{foo} and ^{bar})
115- def rewrite_subp str # :yields: type ("_" for subscript and "^" for superscript), text
116- str . gsub! @org_subp_regexp do |match |
117- yield $1 , $2
105+ def rewrite_subp ( str ) # :yields: type ("_" for subscript and "^" for superscript), text
106+ str . gsub! @org_subp_regexp do |_match |
107+ yield Regexp . last_match ( 1 ) , Regexp . last_match ( 2 )
118108 end
119109 end
120110
121111 # rewrite footnotes
122- def rewrite_footnote str # :yields: name, definition or nil
123- str . gsub! @org_footnote_regexp do |match |
124- yield $1 , $3
112+ def rewrite_footnote ( str ) # :yields: name, definition or nil
113+ str . gsub! @org_footnote_regexp do |_match |
114+ yield Regexp . last_match ( 1 ) , Regexp . last_match ( 3 )
125115 end
126116 end
127117
128- def rewrite_footnote_definition str
129- str . gsub! @org_footnote_def_regexp do |match |
130- yield $1 , $5
118+ def rewrite_footnote_definition ( str )
119+ str . gsub! @org_footnote_def_regexp do |_match |
120+ yield Regexp . last_match ( 1 ) , Regexp . last_match ( 5 )
131121 end
132122 end
133123
@@ -156,33 +146,52 @@ def rewrite_footnote_definition str
156146 # +http://www.hotmail.com+. In both cases, the block returns an
157147 # HTML-style link, and that is how things will get recorded in
158148 # +result+.
159- def rewrite_links str # :yields: link, text
160- str . gsub! @org_link_regexp do |match |
161- yield $1 , $3
149+ def rewrite_links ( str )
150+ str . gsub! @org_link_regexp do |_match |
151+ yield Regexp . last_match ( 1 ) , Regexp . last_match ( 3 )
162152 end
163- str . gsub! @org_angle_link_text_regexp do |match |
164- yield $1 , nil
153+ str . gsub! @org_angle_link_text_regexp do |_match |
154+ yield Regexp . last_match ( 1 ) , nil
165155 end
166156
167157 str # for testing
168158 end
169159
170- def restore_code_snippets str
160+ def restore_code_snippets ( str )
171161 str = str % @code_snippet_stack
172162 @code_snippet_stack = [ ]
173163 str
174164 end
175165
166+ def org_emphasis_regexp
167+ Regexp . new ( "(#{ pre_emphasis_regexp } )" \
168+ "(#{ markers_regexp } )" \
169+ "(#{ border_forbidden } |" \
170+ "#{ border_forbidden } #{ body_regexp } " \
171+ "#{ border_forbidden } )\\ 2" \
172+ "(?=#{ post_emphasis } )" )
173+ end
174+
176175 private
177176
178- def build_org_emphasis_regexp
179- @org_emphasis_regexp = Regexp . new ( "([#{ @pre_emphasis } ]|^)" +
180- "([#{ @markers } ])(?!\\ 2)" +
181- "([^#{ @border_forbidden } ]|" +
182- "[^#{ @border_forbidden } ]#{ @body_regexp } " +
183- "[^#{ @border_forbidden } ])\\ 2" +
184- "(?=[#{ @post_emphasis } ]|$)" )
185- @logger . debug "Just created regexp: #{ @org_emphasis_regexp } "
177+ def pre_emphasis_regexp
178+ '^|\s|[\(\'"\{]'
179+ end
180+
181+ def markers_regexp
182+ '[\*\/_=~\+]'
183+ end
184+
185+ def border_forbidden
186+ '\S'
187+ end
188+
189+ def post_emphasis
190+ '\s|[-,\.;:!\?\'"\)\}]|$'
191+ end
192+
193+ def body_regexp
194+ '.*?(?:\\n.*?){0,1}'
186195 end
187196
188197 def build_org_link_regexp
@@ -194,5 +203,5 @@ def build_org_link_regexp
194203 @org_angle_link_text_regexp = /<(\w +:[^\] \s <>]+)>/
195204 @org_image_file_regexp = /\. (gif|jpe?g|p(?:bm|gm|n[gm]|pm)|svgz?|tiff?|x[bp]m)/i
196205 end
197- end # class Emphasis
198- end # module Orgmode
206+ end
207+ end
0 commit comments