Skip to content

Commit

Permalink
Add \[ and \] to emphasis regexp to fix #85, emphasis inside links.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoricho committed Nov 15, 2021
1 parent 871a2f2 commit e91ead2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/org-ruby/html_output_buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def escape_string! str
str.gsub! /@@html:(<[^<>\n]*>)@@/, "\\1"
end

def quote_tags str
def quote_tags(str)
str.gsub /(<[^<>\n]*>)/, "@@html:\\1@@"
end

Expand Down
56 changes: 32 additions & 24 deletions lib/org-ruby/regexp_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,11 @@ class RegexpHelper
# non-shy groups here, and don't allow newline here.
# newline The maximum number of newlines allowed in an emphasis exp.

attr_reader :org_image_file_regexp

def initialize
# Set up the emphasis regular expression.
@code_snippet_stack = []
@logger = Logger.new(STDERR)
@logger.level = Logger::WARN
build_org_link_regexp
@org_subp_regexp = /([_^])\{(.*?)\}/
@org_footnote_regexp = /\[fn:(.+?)(:(.*))?\]/
@org_footnote_def_regexp = /^\[fn:(.+?)(:(.*))?\]( (.+))?/
end

# Finds all emphasis matches in a string.
Expand Down Expand Up @@ -102,21 +96,21 @@ def rewrite_emphasis(str)
end

# rewrite subscript and superscript (_{foo} and ^{bar})
def rewrite_subp(str) # :yields: type ("_" for subscript and "^" for superscript), text
str.gsub! @org_subp_regexp do |_match|
def rewrite_subp(str)
str.gsub!(org_subp_regexp) do |_match|
yield Regexp.last_match(1), Regexp.last_match(2)
end
end

# rewrite footnotes
def rewrite_footnote(str) # :yields: name, definition or nil
str.gsub! @org_footnote_regexp do |_match|
def rewrite_footnote(str)
str.gsub!(org_footnote_regexp) do |_match|
yield Regexp.last_match(1), Regexp.last_match(3)
end
end

def rewrite_footnote_definition(str)
str.gsub! @org_footnote_def_regexp do |_match|
str.gsub!(org_footnote_def_regexp) do |_match|
yield Regexp.last_match(1), Regexp.last_match(5)
end
end
Expand Down Expand Up @@ -147,10 +141,10 @@ def rewrite_footnote_definition(str)
# HTML-style link, and that is how things will get recorded in
# +result+.
def rewrite_links(str)
str.gsub! @org_link_regexp do |_match|
yield Regexp.last_match(1), Regexp.last_match(3)
str.gsub!(org_link_regexp) do |_match|
yield Regexp.last_match['url'], Regexp.last_match['friendly_text']
end
str.gsub! @org_angle_link_text_regexp do |_match|
str.gsub!(org_angle_link_text_regexp) do |_match|
yield Regexp.last_match(1), nil
end

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

def org_link_regexp
/\[\[(?<url>[^\[\]]+)\](\[(?<friendly_text>[^\[\]]+)\])?\]/x
end

def org_image_file_regexp
/\.(gif|jpe?g|p(?:bm|gm|n[gm]|pm)|svgz?|tiff?|x[bp]m)/i
end

private

def pre_emphasis_regexp
'^|\s|[\(\'"\{]'
'^|\s|[\(\'"\{\[]'
end

def markers_regexp
Expand All @@ -187,21 +189,27 @@ def border_forbidden
end

def post_emphasis
'\s|[-,\.;:!\?\'"\)\}]|$'
'\s|[-,\.;:!\?\'"\)\}\]]|$'
end

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

def build_org_link_regexp
@org_link_regexp = /\[\[
([^\]\[]+) # This is the URL
\](\[
([^\]\[]+) # This is the friendly text
\])?\]/x
@org_angle_link_text_regexp = /<(\w+:[^\]\s<>]+)>/
@org_image_file_regexp = /\.(gif|jpe?g|p(?:bm|gm|n[gm]|pm)|svgz?|tiff?|x[bp]m)/i
def org_subp_regexp
/([_^])\{(.*?)\}/
end

def org_footnote_regexp
/\[fn:(.+?)(:(.*))?\]/
end

def org_footnote_def_regexp
/^\[fn:(.+?)(:(.*))?\]( (.+))?/
end

def org_angle_link_text_regexp
/<(\w+:[^\]\s<>]+)>/
end
end
end
3 changes: 0 additions & 3 deletions spec/org-ruby/html_output_buffer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ module Orgmode
end
end

xcontext 'when tag is table' do
end

context 'when called for second time' do
before(:each) do
buffer.push_mode(mode, indent)
Expand Down
20 changes: 19 additions & 1 deletion spec/regexp_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
require 'spec_helper'

describe Orgmode::RegexpHelper do
let(:helper) { Orgmode::RegexpHelper.new }

describe '#org-emphasis-regexp' do
it 'match emphasis expresions' do
expect(helper.org_emphasis_regexp).to match '~code~'
end
example { expect(helper.org_emphasis_regexp).to match '[[a][~a~]]' }
end

describe '#org-link-regexp' do
it 'match org-links' do
expect(helper.org_link_regexp).to match '[[url][description]]'
end

example { expect(helper.org_link_regexp).to match '[[url]]' }
example { expect(helper.org_link_regexp).to match '[[a][~a~]]' }
end

it "should recognize simple markup" do
e = Orgmode::RegexpHelper.new
total = 0
Expand Down Expand Up @@ -73,4 +91,4 @@

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

0 comments on commit e91ead2

Please sign in to comment.