prawn-html 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/lib/prawn_html/attributes.rb +48 -16
- data/lib/prawn_html/context.rb +5 -9
- data/lib/prawn_html/document_renderer.rb +1 -1
- data/lib/prawn_html/tag.rb +6 -7
- data/lib/prawn_html/tags/a.rb +4 -3
- data/lib/prawn_html/tags/small.rb +4 -4
- data/lib/prawn_html/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3b659b809526c4c961782f613e6946613232dd836ac056ca48d3dabebdbf9a02
|
4
|
+
data.tar.gz: 2549b8b8b872b44f3d6f424236249e7795f97afeadf95299bf06836fb979a13a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b58a21e4424c89db5ff388ab2507ec502ea283ba9e6234d46f3688e477ba21944ee72f813cc9ddb4454426f5e4cc7397531ffa34c74eeb7795a0bbbeaa8d34ed
|
7
|
+
data.tar.gz: 0e3cfad427968577135c8c8791f698b3c5d6e95463b9364d0630d9caf87407f378c3fb725bfe11349cad210d1d0c3bba1a90555ebb9219c620e674fff0413ff8
|
data/README.md
CHANGED
@@ -90,6 +90,8 @@ CSS attributes (dimensional units are ignored and considered in pixel):
|
|
90
90
|
- **top**: see *position (absolute)*
|
91
91
|
- **width**: for *img* tag, support also percentage, ex. `<img src="image.jpg" style="width: 50%; height: 200px"/>`
|
92
92
|
|
93
|
+
The above attributes supports the `initial` value to reset them to their original value.
|
94
|
+
|
93
95
|
For colors, the supported formats are:
|
94
96
|
- 3 hex digits, ex. `color: #FB1`;
|
95
97
|
- 6 hex digits, ex. `color: #abcdef`;
|
@@ -1,10 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'ostruct'
|
4
|
+
require 'set'
|
4
5
|
|
5
6
|
module PrawnHtml
|
6
7
|
class Attributes < OpenStruct
|
7
|
-
attr_reader :styles
|
8
|
+
attr_reader :initial, :styles
|
8
9
|
|
9
10
|
STYLES_APPLY = {
|
10
11
|
block: %i[align bottom leading left margin_left padding_left position right top],
|
@@ -19,13 +20,13 @@ module PrawnHtml
|
|
19
20
|
'color' => { key: :color, set: :convert_color },
|
20
21
|
'font-family' => { key: :font, set: :unquote },
|
21
22
|
'font-size' => { key: :size, set: :convert_size },
|
22
|
-
'font-style' => { key: :styles, set: :append_styles },
|
23
|
-
'font-weight' => { key: :styles, set: :append_styles },
|
23
|
+
'font-style' => { key: :styles, set: :append_styles, values: %i[italic] },
|
24
|
+
'font-weight' => { key: :styles, set: :append_styles, values: %i[bold] },
|
24
25
|
'href' => { key: :link, set: :copy_value },
|
25
26
|
'letter-spacing' => { key: :character_spacing, set: :convert_float },
|
26
27
|
'list-style-type' => { key: :list_style_type, set: :unquote },
|
27
|
-
'text-decoration' => { key: :styles, set: :
|
28
|
-
'vertical-align' => { key: :styles, set: :append_styles },
|
28
|
+
'text-decoration' => { key: :styles, set: :append_styles, values: %i[underline] },
|
29
|
+
'vertical-align' => { key: :styles, set: :append_styles, values: %i[subscript superscript] },
|
29
30
|
'white-space' => { key: :white_space, set: :convert_symbol },
|
30
31
|
# tag opening styles
|
31
32
|
'break-before' => { key: :break_before, set: :convert_symbol },
|
@@ -44,7 +45,9 @@ module PrawnHtml
|
|
44
45
|
'position' => { key: :position, set: :convert_symbol },
|
45
46
|
'right' => { key: :right, set: :convert_size, options: :width },
|
46
47
|
'text-align' => { key: :align, set: :convert_symbol },
|
47
|
-
'top' => { key: :top, set: :convert_size, options: :height }
|
48
|
+
'top' => { key: :top, set: :convert_size, options: :height },
|
49
|
+
# special styles
|
50
|
+
'text-decoration-line-through' => { key: :callback, set: :callback_strike_through }
|
48
51
|
}.freeze
|
49
52
|
|
50
53
|
STYLES_MERGE = %i[margin_left padding_left].freeze
|
@@ -53,6 +56,7 @@ module PrawnHtml
|
|
53
56
|
def initialize(attributes = {})
|
54
57
|
super
|
55
58
|
@styles = {} # result styles
|
59
|
+
@initial = Set.new
|
56
60
|
end
|
57
61
|
|
58
62
|
# Processes the data attributes
|
@@ -74,6 +78,33 @@ module PrawnHtml
|
|
74
78
|
process_styles(hash_styles, options: options) unless hash_styles.empty?
|
75
79
|
end
|
76
80
|
|
81
|
+
# Remove an attribute value from the context styles
|
82
|
+
#
|
83
|
+
# @param context_styles [Hash] hash of the context styles that will be updated
|
84
|
+
# @param rule [Hash] rule from the STYLES_LIST to lookup in the context style for value removal
|
85
|
+
def remove_value(context_styles, rule)
|
86
|
+
if rule[:set] == :append_styles
|
87
|
+
context_styles[rule[:key]] -= rule[:values] if context_styles[:styles]
|
88
|
+
else
|
89
|
+
default = Context::DEFAULT_STYLES[rule[:key]]
|
90
|
+
default ? (context_styles[rule[:key]] = default) : context_styles.delete(rule[:key])
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# Update context styles applying the initial rules (if set)
|
95
|
+
#
|
96
|
+
# @param context_styles [Hash] hash of the context styles that will be updated
|
97
|
+
#
|
98
|
+
# @return [Hash] the update context styles
|
99
|
+
def update_styles(context_styles)
|
100
|
+
initial.each do |rule|
|
101
|
+
next unless rule
|
102
|
+
|
103
|
+
remove_value(context_styles, rule)
|
104
|
+
end
|
105
|
+
context_styles
|
106
|
+
end
|
107
|
+
|
77
108
|
class << self
|
78
109
|
# Merges attributes
|
79
110
|
#
|
@@ -105,29 +136,30 @@ module PrawnHtml
|
|
105
136
|
def process_styles(hash_styles, options:)
|
106
137
|
hash_styles.each do |key, value|
|
107
138
|
rule = evaluate_rule(key, value)
|
139
|
+
next unless rule
|
140
|
+
|
108
141
|
apply_rule!(merged_styles: @styles, rule: rule, value: value, options: options)
|
109
142
|
end
|
110
143
|
@styles
|
111
144
|
end
|
112
145
|
|
113
146
|
def evaluate_rule(rule_key, attr_value)
|
114
|
-
|
115
|
-
if
|
116
|
-
|
117
|
-
|
118
|
-
return { key: :styles, set: :append_styles }
|
119
|
-
end
|
120
|
-
rule
|
147
|
+
key = nil
|
148
|
+
key = 'text-decoration-line-through' if rule_key == 'text-decoration' && attr_value == 'line-through'
|
149
|
+
key ||= rule_key
|
150
|
+
STYLES_LIST[key]
|
121
151
|
end
|
122
152
|
|
123
153
|
def apply_rule!(merged_styles:, rule:, value:, options:)
|
124
|
-
return
|
154
|
+
return (@initial << rule) if value == 'initial'
|
125
155
|
|
126
156
|
if rule[:set] == :append_styles
|
127
|
-
|
157
|
+
val = Utils.normalize_style(value)
|
158
|
+
(merged_styles[rule[:key]] ||= []) << val if val
|
128
159
|
else
|
129
160
|
opts = rule[:options] ? options[rule[:options]] : nil
|
130
|
-
|
161
|
+
val = Utils.send(rule[:set], value, options: opts)
|
162
|
+
merged_styles[rule[:key]] = val if val
|
131
163
|
end
|
132
164
|
end
|
133
165
|
end
|
data/lib/prawn_html/context.rb
CHANGED
@@ -2,7 +2,9 @@
|
|
2
2
|
|
3
3
|
module PrawnHtml
|
4
4
|
class Context < Array
|
5
|
-
|
5
|
+
DEFAULT_STYLES = {
|
6
|
+
size: 16 * PX
|
7
|
+
}.freeze
|
6
8
|
|
7
9
|
attr_reader :previous_tag
|
8
10
|
attr_accessor :last_text_node
|
@@ -54,9 +56,9 @@ module PrawnHtml
|
|
54
56
|
# @return [Hash] the hash of merged styles
|
55
57
|
def merged_styles
|
56
58
|
@merged_styles ||=
|
57
|
-
each_with_object(
|
59
|
+
each_with_object(DEFAULT_STYLES.dup) do |element, res|
|
58
60
|
evaluate_element_styles(element, res)
|
59
|
-
element.update_styles(res)
|
61
|
+
element.update_styles(res)
|
60
62
|
end
|
61
63
|
end
|
62
64
|
|
@@ -71,12 +73,6 @@ module PrawnHtml
|
|
71
73
|
|
72
74
|
private
|
73
75
|
|
74
|
-
def base_styles
|
75
|
-
{
|
76
|
-
size: DEF_FONT_SIZE
|
77
|
-
}
|
78
|
-
end
|
79
|
-
|
80
76
|
def evaluate_element_styles(element, res)
|
81
77
|
styles = element.styles.slice(*Attributes::STYLES_APPLY[:text_node])
|
82
78
|
styles.each do |key, val|
|
@@ -129,7 +129,7 @@ module PrawnHtml
|
|
129
129
|
def adjust_leading(buffer, leading)
|
130
130
|
return leading if leading
|
131
131
|
|
132
|
-
(buffer.map { |item| item[:size] || Context::
|
132
|
+
(buffer.map { |item| item[:size] || Context::DEFAULT_STYLES[:size] }.max * 0.055).round(4)
|
133
133
|
end
|
134
134
|
|
135
135
|
def bounds(buffer, options, block_styles)
|
data/lib/prawn_html/tag.rb
CHANGED
@@ -2,12 +2,17 @@
|
|
2
2
|
|
3
3
|
module PrawnHtml
|
4
4
|
class Tag
|
5
|
+
extend Forwardable
|
6
|
+
|
5
7
|
CALLBACKS = {
|
6
8
|
'Background' => Callbacks::Background,
|
7
9
|
'StrikeThrough' => Callbacks::StrikeThrough
|
8
10
|
}.freeze
|
11
|
+
|
9
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
|
10
13
|
|
14
|
+
def_delegators :@attrs, :styles, :update_styles
|
15
|
+
|
11
16
|
attr_accessor :parent
|
12
17
|
attr_reader :attrs, :tag
|
13
18
|
|
@@ -45,6 +50,7 @@ module PrawnHtml
|
|
45
50
|
attrs.merge_text_styles!(tag_styles, options: options) if respond_to?(:tag_styles)
|
46
51
|
attrs.merge_text_styles!(element_styles, options: options) if element_styles
|
47
52
|
attrs.merge_text_styles!(attrs.style, options: options)
|
53
|
+
attrs.merge_text_styles!(extra_styles, options: options) if respond_to?(:extra_styles)
|
48
54
|
end
|
49
55
|
|
50
56
|
# Styles to apply on tag closing
|
@@ -54,13 +60,6 @@ module PrawnHtml
|
|
54
60
|
styles.slice(*Attributes::STYLES_APPLY[:tag_close])
|
55
61
|
end
|
56
62
|
|
57
|
-
# Styles hash
|
58
|
-
#
|
59
|
-
# @return [Hash] hash of styles
|
60
|
-
def styles
|
61
|
-
attrs.styles
|
62
|
-
end
|
63
|
-
|
64
63
|
# Styles to apply on tag opening
|
65
64
|
#
|
66
65
|
# @return [Hash] hash of styles to apply
|
data/lib/prawn_html/tags/a.rb
CHANGED
@@ -5,12 +5,13 @@ module PrawnHtml
|
|
5
5
|
class A < Tag
|
6
6
|
ELEMENTS = [:a].freeze
|
7
7
|
|
8
|
-
def
|
9
|
-
|
8
|
+
def extra_styles
|
9
|
+
attrs.href ? "href: #{attrs.href}" : nil
|
10
|
+
end
|
10
11
|
|
12
|
+
def tag_styles
|
11
13
|
<<~STYLES
|
12
14
|
color: #00E;
|
13
|
-
href: #{attrs.href};
|
14
15
|
text-decoration: underline;
|
15
16
|
STYLES
|
16
17
|
end
|
@@ -5,10 +5,10 @@ module PrawnHtml
|
|
5
5
|
class Small < Tag
|
6
6
|
ELEMENTS = [:small].freeze
|
7
7
|
|
8
|
-
def update_styles(
|
9
|
-
size = (
|
10
|
-
|
11
|
-
|
8
|
+
def update_styles(context_styles)
|
9
|
+
size = (context_styles[:size] || Context::DEFAULT_STYLES[:size]) * 0.85
|
10
|
+
context_styles[:size] = size
|
11
|
+
super(context_styles)
|
12
12
|
end
|
13
13
|
end
|
14
14
|
end
|
data/lib/prawn_html/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prawn-html
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mattia Roccoberton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-09-
|
11
|
+
date: 2021-09-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oga
|