Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
dj2 committed Feb 25, 2011
2 parents 0db2e26 + 9bd8a44 commit 11a5dc0
Show file tree
Hide file tree
Showing 8 changed files with 469 additions and 454 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -7,4 +7,4 @@ tmp/
data/
pkg/

doc.html
*.html
57 changes: 39 additions & 18 deletions bin/rtf_parse
Expand Up @@ -11,17 +11,12 @@ def add(open, close = open)
@suffix = "</#{close}>#{@suffix}"
end

doc = RubyRTF::Parser.parse(File.open(ARGV[0]).read)

STDERR.puts doc

str = '<html><body>'
doc.sections.each do |section|
mods = section[:modifiers]

def format(str, section)
@prefix = ''
@suffix = ''

mods = section[:modifiers]

if mods[:paragraph]
if section[:text].empty?
str << "<p></p>\n"
Expand All @@ -31,30 +26,30 @@ doc.sections.each do |section|

elsif mods[:tab]
str << "&nbsp;&nbsp;&nbsp;&nbsp;"
next
return
elsif mods[:newline]
str << "<br />\n"
next
return
elsif mods[:rquote]
str << "&rsquo;"
next
return
elsif mods[:lquote]
str << "&lsquo;"
next
return
elsif mods[:ldblquote]
str << "&ldquo;"
next
return
elsif mods[:rdblquote]
str << "&rdquo;"
next
return
elsif mods[:emdash]
str << "&mdash;"
next
return
elsif mods[:endash]
str << "&ndash;"
next
return
end
next if section[:text].empty?
return if section[:text].empty?

add('b') if mods[:bold]
add('i') if mods[:italic]
Expand All @@ -81,5 +76,31 @@ doc.sections.each do |section|
str << @prefix + section[:text] + @suffix
end

doc = RubyRTF::Parser.new.parse(File.open(ARGV[0]).read)

STDERR.puts doc

str = '<html><body>'
doc.sections.each do |section|
mods = section[:modifiers]

if mods[:table]
str << "<table>\n"
mods[:table].rows.each do |row|
str << "<tr>\n"
row.each do |cell|
str << "<td>\n"
format(str, cell)
str << "</td>\n"
end
str << "</tr>\n"
end
str << "</table>\n"
next
end

format(str, section)
end

str << "</body></html>"
puts str
puts str
1 change: 1 addition & 0 deletions lib/ruby-rtf.rb
Expand Up @@ -5,5 +5,6 @@

require 'ruby-rtf/font'
require 'ruby-rtf/colour'
require 'ruby-rtf/table'
require 'ruby-rtf/document'
require 'ruby-rtf/parser'
73 changes: 2 additions & 71 deletions lib/ruby-rtf/document.rb
Expand Up @@ -17,85 +17,16 @@ class Document
# @return [Array] The different formatted sections of the document
attr_reader :sections

# @return [Array] The current formatting block to use as the basis for new sections
attr_reader :formatting_stack

# Keys that aren't inherited
BLACKLISTED = [:paragraph, :newline, :tab, :lquote, :rquote, :ldblquote, :rdblquote]

# Creates a new document
#
# @return [RubyRTF::Document] The new document
def initialize
def initialize(sections)
@font_table = []
@colour_table = []
@character_set = :ansi
@default_font = 0

default_mods = {}
@formatting_stack = [default_mods]
@sections = [{:text => '', :modifiers => default_mods}]
end

# Add a new section to the document
# @note If there is no text added to the current section this does nothing
#
# @return [Nil]
def add_section!
return if current_section[:text].empty?
force_section!
end

# Adds a new section to the document regardless if the current section is empty
#
# @return [Nil]
def force_section!
mods = {}
if current_section
formatting_stack.last.each_pair do |k, v|
next if BLACKLISTED.include?(k)
mods[k] = v
end
end
formatting_stack.push(mods)

@sections << {:text => '', :modifiers => mods}
end

# Resets the current section to default formating
#
# @return [Nil]
def reset_current_section!
current_section[:modifiers].clear
end

# Reset the current section to default settings
#
# @return [Nil]
def reset_section!
current_section[:modifiers] = {}
end

# Pop the current top element off the formatting stack.
# @note This will not allow you to remove the defualt formatting parameters
#
# @return [Nil]
def pop_formatting!
formatting_stack.pop if @formatting_stack.length > 1
end

# Removes the last section
#
# @return [Nil]
def remove_current_section!
sections.pop
end

# Retrieve the current section for the document
#
# @return [Hash] The document section data
def current_section
sections.last
@sections = sections
end

# Convert RubyRTF::Document to a string
Expand Down

0 comments on commit 11a5dc0

Please sign in to comment.