Permalink
Browse files

Merged ifad/vjt fork

  • Loading branch information...
2 parents dd9a05a + b3d57cc commit 75cb7cfd17e3b69fc9f5bf4ccf147e3b4de8ccc6 @clbustos clbustos committed Jan 5, 2012
View
5 .gitignore
@@ -1,13 +1,10 @@
doc/*
*~
-.gitignore
pkg
*.rtf
-clbustos-rtf.gemspec
*.sw?
.DS_Store
coverage
-rtf.gemspec.orig
-*.gemspec
pkg
VERSION.yml
+clbustos-rtf.gemspec
View
5 README.rdoc
@@ -47,7 +47,6 @@ the code.
* Check into RTF image handling with a view to adding support for the insertion
of images into a Document.
-
* Provide a complete implementation for the headers and footers.
=== Some Examples
@@ -178,10 +177,14 @@ directory, so check that out too.
== CONTRIBUTORS
+=== Individuals
* Marcello Barnaba
* Claudio Bustos
* Sam Mullen
* Chris O'Sullivan
+===Organizations
+* IFAD
+
COPYRIGHT
Copyright (c) 2009-2012 Peter Wood. See LICENSE for details.
View
1 README.txt
@@ -10,6 +10,7 @@
* [TODO] Write comprehensive tests for OL and UL
* [TODO] Clean up the API
* [TODO] DRY the code
+ * [TODO] Add an HTML-to-RTF converter
Please, please, please: if you come along this library and would lend me an
hand to complete tests, please help. Thank you!
View
1 Rakefile
@@ -5,7 +5,6 @@ require 'rake'
require 'rubygems'
require 'hoe'
require 'rtf'
-
Hoe.plugin :git
View
0 examples/example01.rb 100755 → 100644
File mode changed.
View
0 examples/example02.rb 100755 → 100644
File mode changed.
View
0 examples/example03.rb 100755 → 100644
File mode changed.
View
0 examples/example03.rtf 100755 → 100644
File mode changed.
View
0 examples/example04.rb 100755 → 100644
File mode changed.
View
0 examples/rubyrtf.bmp 100755 → 100644
File mode changed.
View
0 examples/rubyrtf.jpg 100755 → 100644
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
0 examples/rubyrtf.png 100755 → 100644
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
120 ifad-rtf.gemspec
@@ -0,0 +1,120 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{ifad-rtf}
+ s.version = "0.4.2"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Peter Wood", "Claudio Bustos", "Marcello Barnaba"]
+ s.date = %q{2011-06-08}
+ s.description = %q{Ruby RTF is a library that can be used to create rich text format (RTF) documents. RTF is a text based standard for laying out document content.}
+ s.email = %q{m.barnaba@ifad.org}
+ s.extra_rdoc_files = [
+ "LICENSE",
+ "README.rdoc"
+ ]
+ s.files = [
+ "CHANGES",
+ "LICENSE",
+ "README.rdoc",
+ "Rakefile",
+ "VERSION.yml",
+ "examples/example01.rb",
+ "examples/example02.rb",
+ "examples/example03.rb",
+ "examples/example03.rtf",
+ "examples/example04.rb",
+ "examples/rubyrtf.bmp",
+ "examples/rubyrtf.jpg",
+ "examples/rubyrtf.png",
+ "lib/rtf.rb",
+ "lib/rtf/colour.rb",
+ "lib/rtf/converters.rb",
+ "lib/rtf/converters/html.rb",
+ "lib/rtf/font.rb",
+ "lib/rtf/information.rb",
+ "lib/rtf/list.rb",
+ "lib/rtf/node.rb",
+ "lib/rtf/paper.rb",
+ "lib/rtf/style.rb",
+ "test/character_style_test.rb",
+ "test/colour_table_test.rb",
+ "test/colour_test.rb",
+ "test/command_node_test.rb",
+ "test/container_node_test.rb",
+ "test/document_style_test.rb",
+ "test/document_test.rb",
+ "test/fixtures/bitmap1.bmp",
+ "test/fixtures/bitmap2.bmp",
+ "test/fixtures/jpeg1.jpg",
+ "test/fixtures/jpeg2.jpg",
+ "test/fixtures/png1.png",
+ "test/fixtures/png2.png",
+ "test/font_table_test.rb",
+ "test/font_test.rb",
+ "test/footer_node_test.rb",
+ "test/header_node_test.rb",
+ "test/image_node_test.rb",
+ "test/information_test.rb",
+ "test/node_test.rb",
+ "test/paragraph_style_test.rb",
+ "test/style_test.rb",
+ "test/table_cell_node_test.rb",
+ "test/table_node_test.rb",
+ "test/table_row_node_test.rb",
+ "test/test_helper.rb",
+ "test/text_node_test.rb"
+ ]
+ s.homepage = %q{http://github.com/ifad/rtf}
+ s.require_paths = ["lib"]
+ s.rubyforge_project = %q{ruby-statsample}
+ s.rubygems_version = %q{1.3.7}
+ s.summary = %q{Ruby library to create rich text format documents.}
+ s.test_files = [
+ "examples/example01.rb",
+ "examples/example02.rb",
+ "examples/example03.rb",
+ "examples/example04.rb",
+ "test/character_style_test.rb",
+ "test/colour_table_test.rb",
+ "test/colour_test.rb",
+ "test/command_node_test.rb",
+ "test/container_node_test.rb",
+ "test/document_style_test.rb",
+ "test/document_test.rb",
+ "test/font_table_test.rb",
+ "test/font_test.rb",
+ "test/footer_node_test.rb",
+ "test/header_node_test.rb",
+ "test/image_node_test.rb",
+ "test/information_test.rb",
+ "test/node_test.rb",
+ "test/paragraph_style_test.rb",
+ "test/style_test.rb",
+ "test/table_cell_node_test.rb",
+ "test/table_node_test.rb",
+ "test/table_row_node_test.rb",
+ "test/test_helper.rb",
+ "test/text_node_test.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<nokogiri>, ["~> 1.1"])
+ s.add_runtime_dependency(%q<tidy-ext>, ["~> 0.1"])
+ else
+ s.add_dependency(%q<nokogiri>, ["~> 1.1"])
+ s.add_dependency(%q<tidy-ext>, ["~> 0.1"])
+ end
+ else
+ s.add_dependency(%q<nokogiri>, ["~> 1.1"])
+ s.add_dependency(%q<tidy-ext>, ["~> 0.1"])
+ end
+end
+
View
0 lib/rtf.rb 100755 → 100644
File mode changed.
View
0 lib/rtf/colour.rb 100755 → 100644
File mode changed.
View
5 lib/rtf/converters.rb
@@ -0,0 +1,5 @@
+require 'rtf'
+module RTF::Converters
+ # Empty, for now
+end
+require 'rtf/converters/html'
View
123 lib/rtf/converters/html.rb
@@ -0,0 +1,123 @@
+require 'nokogiri'
+require 'tidy'
+
+module RTF::Converters
+ class HTML
+
+ def initialize(html, options = {})
+ html = options[:noclean] ? html : clean(html, options[:tidy_options] || {})
+ @html = Nokogiri::HTML::Document.parse(html)
+ end
+
+ def to_rtf(options = {})
+ to_rtf_document(options).to_rtf
+ end
+
+ def to_rtf_document(options = {})
+ font = Helpers.font(options[:font] || :default)
+ nodes = NodeSet.new @html.css('body').children
+
+ RTF::Document.new(font).tap do |rtf|
+ nodes.to_rtf(rtf)
+ end
+ end
+
+ protected
+ def clean(html, options = {})
+ defaults = {
+ :doctype => 'omit',
+ :bare => true,
+ :clean => true,
+ :drop_empty_paras => true,
+ :logical_emphasis => true,
+ :lower_literals => true,
+ :merge_spans => 1,
+ :merge_divs => 1,
+ :output_html => true,
+ :indent => 0,
+ :wrap => 0,
+ :char_encoding => 'utf8'
+ }
+
+ tidy = Tidy.new defaults.merge(options)
+ tidy.clean(html)
+ end
+
+ module Helpers
+ extend self
+
+ def font(key)
+ RTF::Font.new(*case key
+ when :default then [RTF::Font::ROMAN, 'Times New Roman']
+ when :monospace then [RTF::Font::MODERN, 'Courier New' ]
+ end)
+ end
+
+ def style(key)
+ RTF::CharacterStyle.new.tap do |style|
+ case key.to_sym
+ when :h1
+ style.font_size = 44
+ style.bold = true
+ when :h2
+ style.font_size = 36
+ style.bold = true
+ when :h3
+ style.font_size = 28
+ style.bold = true
+ when :h4
+ style.font_size = 22
+ style.bold = true
+ end
+ end
+ end
+ end
+
+ class NodeSet
+ def initialize(nodeset)
+ @nodeset = nodeset
+ end
+
+ def to_rtf(rtf)
+ @nodeset.each do |node|
+ Node.new(node).to_rtf(rtf)
+ end
+ end
+ end
+
+ class Node # :nodoc:
+ def initialize(node)
+ @node = node
+ end
+
+ def to_rtf(rtf)
+ case @node.name
+ when 'text' then rtf << @node.text.gsub(/\n+/, ' ').strip
+ when 'br' then rtf.line_break
+ when 'b', 'strong' then rtf.bold &recurse
+ when 'i', 'em', 'cite' then rtf.italic &recurse
+ when 'u' then rtf.underline &recurse
+ when 'blockquote', 'p', 'div' then rtf.paragraph &recurse
+ when 'span' then recurse.call(rtf)
+ when 'sup' then rtf.subscript &recurse
+ when 'sub' then rtf.superscript &recurse
+ when 'ul' then rtf.list :bullets, &recurse
+ when 'ol' then rtf.list :decimal, &recurse
+ when 'li' then rtf.item &recurse
+ when 'a' then rtf.link @node[:href], &recurse
+ when 'h1', 'h2', 'h3', 'h4' then rtf.apply(Helpers.style(@node.name), &recurse); rtf.line_break
+ when 'code' then rtf.font Helpers.font(:monospace), &recurse
+ else
+ #puts "Ignoring #{@node.to_html}"
+ end
+
+ return rtf
+ end
+
+ def recurse
+ lambda {|rtf| NodeSet.new(@node.children).to_rtf(rtf)}
+ end
+ end
+
+ end
+end
View
0 lib/rtf/font.rb 100755 → 100644
File mode changed.
View
0 lib/rtf/information.rb 100755 → 100644
File mode changed.
View
0 lib/rtf/node.rb 100755 → 100644
File mode changed.
View
0 lib/rtf/paper.rb 100755 → 100644
File mode changed.
View
0 lib/rtf/style.rb 100755 → 100644
File mode changed.
View
0 test/fixtures/bitmap1.bmp 100755 → 100644
File mode changed.
View
0 test/fixtures/bitmap2.bmp 100755 → 100644
File mode changed.
View
0 test/fixtures/jpeg1.jpg 100755 → 100644
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
0 test/fixtures/jpeg2.jpg 100755 → 100644
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
0 test/fixtures/png1.png 100755 → 100644
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
0 test/fixtures/png2.png 100755 → 100644
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
0 test/test_character_style.rb 100755 → 100644
File mode changed.
View
0 test/test_colour.rb 100755 → 100644
File mode changed.
View
0 test/test_colour_table.rb 100755 → 100644
File mode changed.
View
0 test/test_command_node.rb 100755 → 100644
File mode changed.
View
1 test/test_container_node.rb 100755 → 100644
@@ -59,6 +59,7 @@ def test03
ContainerNode.new(nil).to_rtf
flunk("Successfully called ContainerNode#to_rtf().")
rescue => error
+ #puts error
end
end
end
View
0 test/test_document.rb 100755 → 100644
File mode changed.
View
0 test/test_document_style.rb 100755 → 100644
File mode changed.
View
0 test/test_font.rb 100755 → 100644
File mode changed.
View
0 test/test_font_table.rb 100755 → 100644
File mode changed.
View
0 test/test_footer_node.rb 100755 → 100644
File mode changed.
View
0 test/test_header_node.rb 100755 → 100644
File mode changed.
View
0 test/test_image_node.rb 100755 → 100644
File mode changed.
View
0 test/test_information.rb 100755 → 100644
File mode changed.
View
0 test/test_node.rb 100755 → 100644
File mode changed.
View
0 test/test_paragraph_style.rb 100755 → 100644
File mode changed.
View
0 test/test_style.rb 100755 → 100644
File mode changed.
View
0 test/test_table_cell_node.rb 100755 → 100644
File mode changed.
View
0 test/test_table_node.rb 100755 → 100644
File mode changed.
View
0 test/test_table_row_node.rb 100755 → 100644
File mode changed.
View
0 test/text_node_test.rb 100755 → 100644
File mode changed.

0 comments on commit 75cb7cf

Please sign in to comment.