Class: Llmsherpa::Table
Overview
This conversion assumes the presence of similarly functional TableCell, Paragraph, ListItem, and Section classes or modules in Ruby.
Instance Attribute Summary
Attributes inherited from Block
#bbox, #block_idx, #block_json, #children, #left, #level, #page_idx, #parent, #sentences, #tag, #top
Instance Method Summary collapse
-
#initialize(table_json, _parent) ⇒ Table
constructor
Initializes a Table with child table rows and headers.
-
#to_html(_include_children = false, _recurse = false) ⇒ Object
Returns html for a <table> with html from all the rows in the table as <tr>.
-
#to_text(_include_children = false, _recurse = false) ⇒ Object
Returns text of a table with text from all the rows in the table delimited by ‘n’.
Methods inherited from Block
#add_child, #chunks, #iter_children, #paragraphs, #parent_chain, #parent_text, #sections, #tables, #to_context_text
Constructor Details
#initialize(table_json, _parent) ⇒ Table
Initializes a Table with child table rows and headers
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/llmsherpa/blocks.rb', line 304 def initialize(table_json, _parent) super(table_json) @rows = [] @headers = [] @name = table_json["name"] return unless table_json.include?("table_rows") table_json["table_rows"].each do |row_json| if row_json["type"] == "table_header" row = TableHeader.new(row_json) @headers << row else row = TableRow.new(row_json) @rows << row end end end |
Instance Method Details
#to_html(_include_children = false, _recurse = false) ⇒ Object
Returns html for a <table> with html from all the rows in the table as <tr>
328 329 330 331 332 333 334 |
# File 'lib/llmsherpa/blocks.rb', line 328 def to_html(_include_children = false, _recurse = false) html_str = "<table>" @headers.each { |header| html_str += header.to_html } @rows.each { |row| html_str += row.to_html } html_str += "</table>" html_str end |
#to_text(_include_children = false, _recurse = false) ⇒ Object
Returns text of a table with text from all the rows in the table delimited by ‘n’
323 324 325 |
# File 'lib/llmsherpa/blocks.rb', line 323 def to_text(_include_children = false, _recurse = false) "#{@headers.map(&:to_text).join("\n")}\n#{@rows.map(&:to_text).join("\n")}" end |