doc.scrub!(WikiTags.new(tags))
doc.scrub!(:newline_block_elements)
- Loofah.remove_extraneous_whitespace(doc.text).strip.squeeze(' ').gsub(/^ +/, '')
+ Loofah.remove_extraneous_whitespace(doc.text(:encode_special_chars => false)).strip.squeeze(' ').gsub(/^ +/, '')
end
class WikiTags < ::Loofah::Scrubber
when String
node.add_next_sibling Nokogiri::XML::Text.new(formatting, node.document)
node.remove
+ when Proc
+ node.add_next_sibling formatting.call(node)
+ node.remove
else
CONTINUE
end
'h3' => {:pre => "\n\n### ", :post => "\n\n"},
'h4' => {:pre => "\n\n#### ", :post => "\n\n"},
'h5' => {:pre => "\n\n##### ", :post => "\n\n"},
- 'h6' => {:pre => "\n\n###### ", :post => "\n\n"}
+ 'h6' => {:pre => "\n\n###### ", :post => "\n\n"},
+ 'a' => lambda {|node| node.content.present? ? %| [#{node.content}](#{node.attributes['href'].value}) | : %| #{node.attributes['href'].value} |}
)
end
end
'h3' => {:pre => "\n\nh3. ", :post => "\n\n"},
'h4' => {:pre => "\n\nh4. ", :post => "\n\n"},
'h5' => {:pre => "\n\nh5. ", :post => "\n\n"},
- 'h6' => {:pre => "\n\nh6. ", :post => "\n\n"}
+ 'h6' => {:pre => "\n\nh6. ", :post => "\n\n"},
+ 'a' => lambda {|node| node.content.present? ? %| "#{node.content}":#{node.attributes['href'].value} | : %| #{node.attributes['href'].value} |}
)
end
end
def test_should_convert_tags
assert_equal 'A **simple** html snippet.',
@parser.to_text('<p>A <b>simple</b> html snippet.</p>')
+
+ assert_equal 'foo [bar](http://example.com/) baz',
+ @parser.to_text('foo<a href="http://example.com/">bar</a>baz')
+ assert_equal 'foo http://example.com/ baz',
+ @parser.to_text('foo<a href="http://example.com/"></a>baz')
end
end
def test_should_convert_tags
assert_equal 'A *simple* html snippet.',
@parser.to_text('<p>A <b>simple</b> html snippet.</p>')
+
+ assert_equal 'foo "bar":http://example.com/ baz',
+ @parser.to_text('foo<a href="http://example.com/">bar</a>baz')
+ assert_equal 'foo http://example.com/ baz',
+ @parser.to_text('foo<a href="http://example.com/"></a>baz')
end
end