diff --git a/html2textile.gemspec b/html2textile.gemspec index 62ee1e3..6e8f136 100644 --- a/html2textile.gemspec +++ b/html2textile.gemspec @@ -14,4 +14,5 @@ Gem::Specification.new do |s| s.require_path = 'lib' s.files = Dir.glob("{lib}/**/*") + %w(example.rb README.mdown) + end diff --git a/lib/html2textile.rb b/lib/html2textile.rb index 01dd007..8b8424a 100644 --- a/lib/html2textile.rb +++ b/lib/html2textile.rb @@ -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' => '@'} @@ -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 diff --git a/test/html2textile_test.rb b/test/html2textile_test.rb new file mode 100644 index 0000000..7f66d35 --- /dev/null +++ b/test/html2textile_test.rb @@ -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('bold') + end + + def test_should_parse_italic + assert_equal ' _italic_ ', to_textile('italic') + end + + def test_bold_with_spaces + assert_equal ' *b o l d* ', to_textile(' b o l d ') + end + + def test_should_parse_bold_with_new_lines_inside + assert_equal ' *b o l d* ', to_textile(" b o\nl d\n") + end + + def test_should_parse_italic_with_spaces + assert_equal ' _i t a l i c_ ', to_textile(' i t a l i c') + end + + def test_should_parse_headings + assert_equal("h1. Heading 1\n\n", to_textile('

Heading 1

')) + assert_equal("h2. Heading 2\n\n", to_textile('

Heading 2

')) + assert_equal("h3. Heading 3\n\n", to_textile('

Heading 3

')) + end + + def test_should_parse_heading_with_color + assert_equal "h1. Hello world \n\n", + to_textile('

Hello world

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

Th + is is a headi ng

') + end + +end \ No newline at end of file