Skip to content

Commit

Permalink
add widths to table cells
Browse files Browse the repository at this point in the history
  • Loading branch information
dj2 committed Feb 26, 2011
1 parent e98f206 commit 4eb37f7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
4 changes: 1 addition & 3 deletions bin/rtf_parse
Expand Up @@ -86,13 +86,11 @@ doc.sections.each do |section|
mods = section[:modifiers]

if mods[:table]
# pp mods[:table]

str << "<table>\n"
mods[:table].rows.each do |row|
str << "<tr>\n"
row.cells.each do |cell|
str << "<td>\n"
str << "<td width=\"#{cell.width}\">\n"
cell.sections.each do |sect|
format(str, sect)
end
Expand Down
10 changes: 0 additions & 10 deletions lib/ruby-rtf/parser.rb
Expand Up @@ -411,16 +411,6 @@ def add_modifier_section(mods = {}, text = nil)
def add_section!(mods = {})
if current_section[:text].empty?
current_section[:modifiers].merge!(mods)

elsif current_section[:modifiers][:row]
row = current_section[:modifiers][:row]
force_section!

@section_stack.pop
cell = row.add_cell
current_section[:modifiers][:row] = row
@section_stack.push(cell.sections)

else
force_section!(mods)
end
Expand Down
25 changes: 18 additions & 7 deletions lib/ruby-rtf/table.rb
Expand Up @@ -25,29 +25,40 @@ class Row
def initialize(table)
@table = table
@end_positions = []
@cells = []
add_cell

@cells = [RubyRTF::Table::Row::Cell.new(self, 0)]
end

def current_cell
@cells.last
end

def add_cell
return @cells.last if (@cells.length > 0) && @cells.last.sections.empty?
return @cells.last if @cells.last.sections.empty?

@cells << RubyRTF::Table::Row::Cell.new(self)
@cells << RubyRTF::Table::Row::Cell.new(self, @cells.length)
@cells.last
end

class Cell
attr_accessor :sections, :row
attr_accessor :sections, :row, :idx

def initialize(row)
def initialize(row, idx)
@row = row
@idx = idx
@sections = []
end

def width
gap = row.table.half_gap
left_margin = row.table.left_margin

end_pos = row.end_positions[idx]
prev_pos = idx == 0 ? 0 : row.end_positions[idx - 1]

end_pos - prev_pos - (2 * gap) - left_margin
end
end
end
end
end
end

0 comments on commit 4eb37f7

Please sign in to comment.