Skip to content

Commit a0ab4ac

Browse files
committed
New tags: table, tr, td
1 parent c1181a7 commit a0ab4ac

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

lib/prawn_html/tag.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Tag
99
'StrikeThrough' => Callbacks::StrikeThrough
1010
}.freeze
1111

12-
TAG_CLASSES = %w[A B Blockquote Body Br Code Del Div H Hr I Img Li Mark Ol P Pre Small Span Sub Sup U Ul].freeze
12+
TAG_CLASSES = %w[A B Blockquote Body Br Code Del Div H Hr I Img Li Mark Ol P Pre Small Span Sub Sup Table Td Tr U Ul].freeze
1313

1414
def_delegators :@attrs, :styles, :update_styles
1515

lib/prawn_html/tags/table.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
module PrawnHtml
4+
module Tags
5+
class Table < Tag
6+
ELEMENTS = [:table].freeze
7+
8+
attr_accessor :rows
9+
10+
def block?
11+
true
12+
end
13+
14+
def new_cell
15+
@col_index += 1
16+
@rows[@row_index] << ''
17+
end
18+
19+
def new_row
20+
@row_index += 1
21+
@col_index = -1
22+
@rows << []
23+
end
24+
25+
def update_content(content)
26+
@rows[@row_index][@col_index] = content
27+
end
28+
29+
def tag_opening(pdf: nil, context: nil)
30+
super.tap do
31+
context.current_table = self
32+
@row_index = -1
33+
@rows = []
34+
end
35+
end
36+
37+
def tag_closing(pdf: nil, context: nil)
38+
super.tap do
39+
pdf.table(@rows)
40+
end
41+
end
42+
end
43+
end
44+
end

lib/prawn_html/tags/td.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
module PrawnHtml
4+
module Tags
5+
class Td < Tag
6+
ELEMENTS = [:td].freeze
7+
8+
def block?
9+
true
10+
end
11+
12+
def tag_opening(pdf: nil, context: nil)
13+
super.tap do
14+
context.current_table&.new_cell
15+
end
16+
end
17+
end
18+
end
19+
end

lib/prawn_html/tags/tr.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
module PrawnHtml
4+
module Tags
5+
class Tr < Tag
6+
ELEMENTS = [:tr].freeze
7+
8+
def block?
9+
true
10+
end
11+
12+
def tag_opening(pdf: nil, context: nil)
13+
super.tap do
14+
context.current_table&.new_row
15+
end
16+
end
17+
end
18+
end
19+
end

0 commit comments

Comments
 (0)