prawn-table-html 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +20 -0
  3. data/README.md +236 -0
  4. data/lib/prawn-html.rb +180 -0
  5. data/lib/prawn_html/attributes.rb +166 -0
  6. data/lib/prawn_html/callbacks/background.rb +19 -0
  7. data/lib/prawn_html/callbacks/strike_through.rb +18 -0
  8. data/lib/prawn_html/context.rb +100 -0
  9. data/lib/prawn_html/document_renderer.rb +172 -0
  10. data/lib/prawn_html/html_parser.rb +104 -0
  11. data/lib/prawn_html/instance.rb +18 -0
  12. data/lib/prawn_html/pdf_wrapper.rb +145 -0
  13. data/lib/prawn_html/tag.rb +93 -0
  14. data/lib/prawn_html/tags/a.rb +20 -0
  15. data/lib/prawn_html/tags/b.rb +13 -0
  16. data/lib/prawn_html/tags/blockquote.rb +25 -0
  17. data/lib/prawn_html/tags/body.rb +13 -0
  18. data/lib/prawn_html/tags/br.rb +21 -0
  19. data/lib/prawn_html/tags/code.rb +13 -0
  20. data/lib/prawn_html/tags/col.rb +37 -0
  21. data/lib/prawn_html/tags/colgroup.rb +13 -0
  22. data/lib/prawn_html/tags/del.rb +13 -0
  23. data/lib/prawn_html/tags/div.rb +13 -0
  24. data/lib/prawn_html/tags/h.rb +49 -0
  25. data/lib/prawn_html/tags/hr.rb +39 -0
  26. data/lib/prawn_html/tags/i.rb +13 -0
  27. data/lib/prawn_html/tags/img.rb +31 -0
  28. data/lib/prawn_html/tags/li.rb +39 -0
  29. data/lib/prawn_html/tags/mark.rb +13 -0
  30. data/lib/prawn_html/tags/ol.rb +43 -0
  31. data/lib/prawn_html/tags/p.rb +23 -0
  32. data/lib/prawn_html/tags/pre.rb +25 -0
  33. data/lib/prawn_html/tags/small.rb +15 -0
  34. data/lib/prawn_html/tags/span.rb +9 -0
  35. data/lib/prawn_html/tags/sub.rb +13 -0
  36. data/lib/prawn_html/tags/sup.rb +13 -0
  37. data/lib/prawn_html/tags/table.rb +53 -0
  38. data/lib/prawn_html/tags/tbody.rb +13 -0
  39. data/lib/prawn_html/tags/td.rb +43 -0
  40. data/lib/prawn_html/tags/th.rb +43 -0
  41. data/lib/prawn_html/tags/tr.rb +37 -0
  42. data/lib/prawn_html/tags/u.rb +13 -0
  43. data/lib/prawn_html/tags/ul.rb +40 -0
  44. data/lib/prawn_html/utils.rb +139 -0
  45. data/lib/prawn_html/version.rb +5 -0
  46. metadata +131 -0
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrawnHtml
4
+ module Tags
5
+ class Ul < Tag
6
+ ELEMENTS = [:ul].freeze
7
+
8
+ MARGIN_TOP = 15
9
+ MARGIN_LEFT = 40
10
+ MARGIN_BOTTOM = 15
11
+
12
+ def initialize(tag, attributes: {}, options: {})
13
+ super
14
+ @first_level = false
15
+ end
16
+
17
+ def block?
18
+ true
19
+ end
20
+
21
+ def on_context_add(context)
22
+ return if context.map(&:tag).count { |el| el == :ul } > 1
23
+
24
+ @first_level = true
25
+ end
26
+
27
+ def tag_styles
28
+ if @first_level
29
+ <<~STYLES
30
+ margin-top: #{MARGIN_TOP}px;
31
+ margin-left: #{MARGIN_LEFT}px;
32
+ margin-bottom: #{MARGIN_BOTTOM}px;
33
+ STYLES
34
+ else
35
+ "margin-left: #{MARGIN_LEFT}px"
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrawnHtml
4
+ module Utils
5
+ NORMALIZE_STYLES = {
6
+ 'bold' => :bold,
7
+ 'italic' => :italic,
8
+ 'sub' => :subscript,
9
+ 'super' => :superscript,
10
+ 'underline' => :underline
11
+ }.freeze
12
+
13
+ # Setup a background callback
14
+ #
15
+ # @param value [String] HTML string color
16
+ #
17
+ # @return [Array] callback name and argument value
18
+ def callback_background(value, options: nil)
19
+ ['Background', convert_color(value, options: options)]
20
+ end
21
+
22
+ # Setup a strike through callback
23
+ #
24
+ # @param value [String] unused
25
+ #
26
+ # @return [Array] callback name and argument value
27
+ def callback_strike_through(value, options: nil)
28
+ ['StrikeThrough', nil]
29
+ end
30
+
31
+ # Converts a color string
32
+ #
33
+ # Supported formats:
34
+ # - 3 hex digits, ex. `color: #FB1`;
35
+ # - 6 hex digits, ex. `color: #abcdef`;
36
+ # - RGB, ex. `color: RGB(64, 0, 128)`;
37
+ # - color name, ex. `color: red`.
38
+ #
39
+ # @param value [String] HTML string color
40
+ #
41
+ # @return [String] adjusted string color or nil if value is invalid
42
+ def convert_color(value, options: nil)
43
+ val = value.to_s.strip.downcase
44
+ return Regexp.last_match[1] if val.match /\A#([a-f0-9]{6})\Z/ # rubocop:disable Performance/RedundantMatch
45
+
46
+ if val.match /\A#([a-f0-9]{3})\Z/ # rubocop:disable Performance/RedundantMatch
47
+ r, g, b = Regexp.last_match[1].chars
48
+ return (r * 2) + (g * 2) + (b * 2)
49
+ end
50
+ if val.match /\Argb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)\Z/ # rubocop:disable Performance/RedundantMatch
51
+ r, g, b = Regexp.last_match[1..3].map { |v| v.to_i.to_s(16) }
52
+ return "#{r.rjust(2, '0')}#{g.rjust(2, '0')}#{b.rjust(2, '0')}"
53
+ end
54
+
55
+ COLORS[val]
56
+ end
57
+
58
+ # Converts a decimal number string
59
+ #
60
+ # @param value [String] string decimal
61
+ #
62
+ # @return [Float] converted and rounded float number
63
+ def convert_float(value, options: nil)
64
+ val = value&.gsub(/[^0-9.]/, '') || ''
65
+ val.to_f.round(4)
66
+ end
67
+
68
+ # Converts a size string
69
+ #
70
+ # @param value [String] size string
71
+ # @param options [Numeric] container size
72
+ #
73
+ # @return [Float] converted and rounded size
74
+ def convert_size(value, options: nil)
75
+ val = value&.gsub(/[^0-9.]/, '') || ''
76
+ val =
77
+ if options && value&.include?('%')
78
+ val.to_f * options * 0.01
79
+ else
80
+ val.to_f * PrawnHtml::PX
81
+ end
82
+ val.round(4)
83
+ end
84
+
85
+ # Converts a string to symbol
86
+ #
87
+ # @param value [String] string
88
+ #
89
+ # @return [Symbol] symbol
90
+ def convert_symbol(value, options: nil)
91
+ value.to_sym if value && !value.match?(/\A\s*\Z/)
92
+ end
93
+
94
+ # Copy a value without conversion
95
+ #
96
+ # @param value
97
+ #
98
+ # @return value
99
+ def copy_value(value, options: nil)
100
+ value
101
+ end
102
+
103
+ # Filter font family
104
+ #
105
+ # @param value [String] string value
106
+ #
107
+ # @return [Symbol] unquoted font family or nil if the input value is 'inherit'
108
+ def filter_font_family(value, options: nil)
109
+ result = unquote(value, options: options)
110
+ result == 'inherit' ? nil : result
111
+ end
112
+
113
+ # Normalize a style value
114
+ #
115
+ # @param value [String] string value
116
+ # @param accepted_values [Array] allowlist of valid values (symbols)
117
+ #
118
+ # @return [Symbol] style value or nil
119
+ def normalize_style(value, accepted_values)
120
+ val = value&.strip&.downcase
121
+ ret = NORMALIZE_STYLES[val]
122
+ accepted_values.include?(ret) ? ret : nil
123
+ end
124
+
125
+ # Unquotes a string
126
+ #
127
+ # @param value [String] string
128
+ #
129
+ # @return [String] string without quotes at the beginning/ending
130
+ def unquote(value, options: nil)
131
+ (value&.strip || +'').tap do |val|
132
+ val.gsub!(/\A['"]|["']\Z/, '')
133
+ end
134
+ end
135
+
136
+ module_function :callback_background, :callback_strike_through, :convert_color, :convert_float, :convert_size,
137
+ :convert_symbol, :copy_value, :filter_font_family, :normalize_style, :unquote
138
+ end
139
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrawnHtml # :nodoc:
4
+ VERSION = '0.7.1'
5
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prawn-table-html
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mattia Roccoberton
8
+ - Anthony Amar
9
+ - Nerya
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2025-01-08 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: oga
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '3.3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '3.3'
29
+ - !ruby/object:Gem::Dependency
30
+ name: prawn
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: '2.4'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '2.4'
43
+ - !ruby/object:Gem::Dependency
44
+ name: prawn-table
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '0.2'
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '0.2'
57
+ description: HTML renderer for Prawn PDF with table support, forked from Prawn HTML
58
+ email: a.amar@nerya.fr
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - LICENSE.txt
64
+ - README.md
65
+ - lib/prawn-html.rb
66
+ - lib/prawn_html/attributes.rb
67
+ - lib/prawn_html/callbacks/background.rb
68
+ - lib/prawn_html/callbacks/strike_through.rb
69
+ - lib/prawn_html/context.rb
70
+ - lib/prawn_html/document_renderer.rb
71
+ - lib/prawn_html/html_parser.rb
72
+ - lib/prawn_html/instance.rb
73
+ - lib/prawn_html/pdf_wrapper.rb
74
+ - lib/prawn_html/tag.rb
75
+ - lib/prawn_html/tags/a.rb
76
+ - lib/prawn_html/tags/b.rb
77
+ - lib/prawn_html/tags/blockquote.rb
78
+ - lib/prawn_html/tags/body.rb
79
+ - lib/prawn_html/tags/br.rb
80
+ - lib/prawn_html/tags/code.rb
81
+ - lib/prawn_html/tags/col.rb
82
+ - lib/prawn_html/tags/colgroup.rb
83
+ - lib/prawn_html/tags/del.rb
84
+ - lib/prawn_html/tags/div.rb
85
+ - lib/prawn_html/tags/h.rb
86
+ - lib/prawn_html/tags/hr.rb
87
+ - lib/prawn_html/tags/i.rb
88
+ - lib/prawn_html/tags/img.rb
89
+ - lib/prawn_html/tags/li.rb
90
+ - lib/prawn_html/tags/mark.rb
91
+ - lib/prawn_html/tags/ol.rb
92
+ - lib/prawn_html/tags/p.rb
93
+ - lib/prawn_html/tags/pre.rb
94
+ - lib/prawn_html/tags/small.rb
95
+ - lib/prawn_html/tags/span.rb
96
+ - lib/prawn_html/tags/sub.rb
97
+ - lib/prawn_html/tags/sup.rb
98
+ - lib/prawn_html/tags/table.rb
99
+ - lib/prawn_html/tags/tbody.rb
100
+ - lib/prawn_html/tags/td.rb
101
+ - lib/prawn_html/tags/th.rb
102
+ - lib/prawn_html/tags/tr.rb
103
+ - lib/prawn_html/tags/u.rb
104
+ - lib/prawn_html/tags/ul.rb
105
+ - lib/prawn_html/utils.rb
106
+ - lib/prawn_html/version.rb
107
+ homepage:
108
+ licenses:
109
+ - MIT
110
+ metadata:
111
+ rubygems_mfa_required: 'true'
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: 2.5.0
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubygems_version: 3.5.22
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Prawn Table PDF - HTML renderer, forked from Prawn HTML
131
+ test_files: []