class ChupaText::Decomposers::OpenDocumentSpreadsheet::SheetsListener

Constants

TABLE_URI
TEXT_URI

Public Class Methods

new(sheets) click to toggle source
# File lib/chupa-text/decomposers/opendocument-spreadsheet.rb, line 62
def initialize(sheets)
  @sheets = sheets
  @prefix_to_uri = {}
  @uri_to_prefix = {}
  @in_p = false
  @in_shapes = false
end

Public Instance Methods

cdata(content) click to toggle source
# File lib/chupa-text/decomposers/opendocument-spreadsheet.rb, line 140
def cdata(content)
  add_text(content)
end
characters(text) click to toggle source
# File lib/chupa-text/decomposers/opendocument-spreadsheet.rb, line 136
def characters(text)
  add_text(text)
end
end_element(uri, local_name, qname) click to toggle source
# File lib/chupa-text/decomposers/opendocument-spreadsheet.rb, line 108
def end_element(uri, local_name, qname)
  case uri
  when TEXT_URI
    case local_name
    when "p"
      @in_p = false
    end
  when TABLE_URI
    case local_name
    when "table"
      sheet = @sheets.last
      text = ""
      shape_texts = sheet[:shape_texts]
      unless shape_texts.empty?
        text << shape_texts.join("\n") << "\n"
      end
      sheet[:rows].each do |row|
        cell_texts = row.collect {|cell| cell[:text]}
        next if cell_texts.all?(&:empty?)
        text << cell_texts.join("\t") << "\n"
      end
      sheet[:text] = text
    when "shapes"
      @in_shapes = false
    end
  end
end
end_prefix_mapping(prefix) click to toggle source
# File lib/chupa-text/decomposers/opendocument-spreadsheet.rb, line 75
def end_prefix_mapping(prefix)
  uri = @prefix_to_uri.delete(prefix)
  @uri_to_prefix.delete(uri)
end
start_element(uri, local_name, qname, attributes) click to toggle source
# File lib/chupa-text/decomposers/opendocument-spreadsheet.rb, line 80
def start_element(uri, local_name, qname, attributes)
  case uri
  when TEXT_URI
    case local_name
    when "p"
      @in_p = true
    end
  when TABLE_URI
    table_prefix = @uri_to_prefix[TABLE_URI]
    case local_name
    when "table"
      @sheets << {
        name: attributes["#{table_prefix}:name"],
        rows: [],
        shape_texts: [],
      }
    when "table-row"
      @sheets.last[:rows] << []
    when "table-cell"
      @sheets.last[:rows].last << {text: ""}
    when "covered-table-cell"
      @sheets.last[:rows].last << {text: ""}
    when "shapes"
      @in_shapes = true
    end
  end
end
start_prefix_mapping(prefix, uri) click to toggle source
# File lib/chupa-text/decomposers/opendocument-spreadsheet.rb, line 70
def start_prefix_mapping(prefix, uri)
  @prefix_to_uri[prefix] = uri
  @uri_to_prefix[uri] = prefix
end

Private Instance Methods

add_text(text) click to toggle source
# File lib/chupa-text/decomposers/opendocument-spreadsheet.rb, line 145
def add_text(text)
  return unless @in_p
  sheet = @sheets.last
  if @in_shapes
    sheet[:shape_texts] << text
  else
    sheet[:rows].last.last[:text] << text
  end
rescue
  pp [text, @sheets]
  raise
end