Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions html2textile.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ Gem::Specification.new do |s|

s.require_path = 'lib'
s.files = Dir.glob("{lib}/**/*") + %w(example.rb README.mdown)

end
8 changes: 3 additions & 5 deletions lib/html2textile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
# parser.feed(input_html)
# puts parser.to_textile
class HTMLToTextileParser < SgmlParser

# TDH removed span from quicktags and set p to empty string
# removed blockquote pair
# removed blockquote pair
PAIRS = { 'p' => ''}
QUICKTAGS = { 'b' => '*', 'strong' => '*',
QUICKTAGS = { 'b' => '*', 'strong' => '*', 'span' => '',
'i' => '_', 'em' => '_', 'cite' => '??', 's' => '-',
'sup' => '^', 'sub' => '~', 'code' => '@'}

Expand Down Expand Up @@ -120,7 +118,7 @@ def handle_data(data)

%w[1 2 3 4 5 6].each do |num|
define_method "start_h#{num}" do |attributes|
make_block_start_pair("h#{num}", attributes)
make_block_start_pair("h#{num}. ", attributes)
end

define_method "end_h#{num}" do
Expand Down
49 changes: 49 additions & 0 deletions test/html2textile_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'test/unit'
require_relative '../lib/html2textile'

class HTMLToTextileParserTest < Test::Unit::TestCase

def to_textile(html)
@parser = HTMLToTextileParser.new
@parser.feed(html)
@parser.to_textile
end

def test_should_parse_bold
assert_equal ' *bold* ', to_textile('<b>bold</b>')
end

def test_should_parse_italic
assert_equal ' _italic_ ', to_textile('<i>italic</i>')
end

def test_bold_with_spaces
assert_equal ' *b o l d* ', to_textile('<b> b o l d </b>')
end

def test_should_parse_bold_with_new_lines_inside
assert_equal ' *b o l d* ', to_textile("<b> b o\nl d\n</b>")
end

def test_should_parse_italic_with_spaces
assert_equal ' _i t a l i c_ ', to_textile('<i> i t a l i c</i>')
end

def test_should_parse_headings
assert_equal("h1. Heading 1\n\n", to_textile('<h1>Heading 1</h1>'))
assert_equal("h2. Heading 2\n\n", to_textile('<h2>Heading 2</h2>'))
assert_equal("h3. Heading 3\n\n", to_textile('<h3>Heading 3</h3>'))
end

def test_should_parse_heading_with_color
assert_equal "h1. Hello world \n\n",
to_textile('<h1>Hello <span style="color: red">world</span></h1>')
end

def test_should_parse_heading_with_spaces_and_new_lines
assert_equal "h1. Th is is a headi ng\n\n",
to_textile('<h1> Th
is is a headi ng </h1>')
end

end