Skip to content

Commit 9ac9402

Browse files
committed
Match abbreviations across line breaks and other whitespace
1 parent 6f53282 commit 9ac9402

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

lib/kramdown/parser/base.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ def initialize(source, options)
5454
@options = Kramdown::Options.merge(options)
5555
@root = Element.new(:root, nil, nil, encoding: (source.encoding rescue nil), location: 1,
5656
options: {}, abbrev_defs: {}, abbrev_attr: {})
57+
58+
@root.options[:abbrev_defs].default_proc = @root.options[:abbrev_attr].default_proc =
59+
lambda do |h, k|
60+
k_mod = k.gsub(/[\s\p{Z}]+/, " ")
61+
k != k_mod ? h[k_mod] : nil
62+
end
5763
@warnings = []
5864
@text_type = :text
5965
end

lib/kramdown/parser/kramdown/abbreviation.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ def replace_abbreviations(el, regexps = nil)
4242
return if @root.options[:abbrev_defs].empty?
4343
unless regexps
4444
sorted_abbrevs = @root.options[:abbrev_defs].keys.sort {|a, b| b.length <=> a.length }
45-
regexps = [Regexp.union(*sorted_abbrevs.map {|k| /#{Regexp.escape(k)}/ })]
45+
regexps = [Regexp.union(*sorted_abbrevs.map do |k|
46+
/#{Regexp.escape(k).gsub(/\\\s/, "[\\s\\p{Z}]+").force_encoding(Encoding::UTF_8)}/
47+
end)]
4648
regexps << /(?=(?:\W|^)#{regexps.first}(?!\w))/ # regexp should only match on word boundaries
4749
end
4850
el.children.map! do |child|

test/test_files.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,11 @@ def tidy_output(out)
282282
define_method("test_whether_#{conv_class}_modifies_tree_with_file_#{text_file.tr('.', '_')}") do
283283
doc = Kramdown::Document.new(File.read(text_file), options)
284284
options_before = Marshal.load(Marshal.dump(doc.options))
285+
abbrev_proc = doc.root.options[:abbrev_defs].default_proc
286+
doc.root.options[:abbrev_defs].default_proc = doc.root.options[:abbrev_attr].default_proc = nil
285287
tree_before = Marshal.load(Marshal.dump(doc.root))
288+
doc.root.options[:abbrev_defs].default_proc = doc.root.options[:abbrev_attr].default_proc =
289+
abbrev_proc
286290
Kramdown::Converter.const_get(conv_class).convert(doc.root, doc.options)
287291
assert_equal(options_before, doc.options)
288292
assert_tree_not_changed(tree_before, doc.root)

test/testcases/span/abbreviations/abbrev.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@
1919

2020
<p>This is <abbr class="testit test" title="Some text here">awesome</abbr>.</p>
2121

22+
<p>hello <abbr title="baz">foo bar</abbr> babble.</p>
23+
24+
<p>hello <abbr title="baz">foo
25+
bar</abbr> babble.</p>

test/testcases/span/abbreviations/abbrev.text

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,10 @@ This is awesome.
3232
{:.testit}
3333
*[awesome]: Some text here
3434
{:.test}
35+
36+
*[foo bar]: baz
37+
38+
hello foo bar babble.
39+
40+
hello foo
41+
bar babble.

0 commit comments

Comments
 (0)