Skip to content

Commit de3a069

Browse files
committed
Support for append css
1 parent 966b423 commit de3a069

6 files changed

Lines changed: 43 additions & 6 deletions

File tree

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,23 @@ pdf.render_file('test.pdf')
3535

3636
To check some examples with the PDF output see [examples](examples/) folder.
3737

38+
Alternative form using _PrawnHtml::Instance_ to preserve the context:
39+
40+
```rb
41+
require 'prawn-html'
42+
pdf = Prawn::Document.new(page_size: 'A4')
43+
phtml = PrawnHtml::Instance.new(pdf)
44+
css = <<~CSS
45+
h1 { color: green }
46+
i { color: red }
47+
CSS
48+
phtml.append(css: css)
49+
phtml.append(html: '<h1>Some <i>HTML</i> before</h1>')
50+
pdf.text 'Some Prawn text'
51+
phtml.append(html: '<h1>Some <i>HTML</i> after</h1>')
52+
pdf.render_file('test.pdf')
53+
```
54+
3855
## Supported tags & attributes
3956

4057
HTML tags (using MDN definitions):

examples/instance.pdf

615 Bytes
Binary file not shown.

lib/prawn_html/html_parser.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ def process(html)
3030
renderer.flush
3131
end
3232

33+
# Parses CSS styles
34+
#
35+
# @param text_styles [String] The CSS styles to evaluate
36+
def parse_styles(text_styles)
37+
@raw_styles = text_styles.scan(REGEXP_STYLES).to_h
38+
end
39+
3340
private
3441

3542
attr_reader :document, :ignore, :processing, :renderer, :styles
@@ -62,7 +69,7 @@ def init_element(node)
6269
end
6370

6471
def process_styles(text_styles = nil)
65-
@raw_styles = text_styles.scan(REGEXP_STYLES).to_h if text_styles
72+
parse_styles(text_styles) if text_styles
6673
@raw_styles.each do |selector, rule|
6774
document.css(selector).each do |node|
6875
styles[node] = rule

lib/prawn_html/instance.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ def initialize(pdf)
1010
@html_parser = PrawnHtml::HtmlParser.new(@renderer)
1111
end
1212

13-
def append(html:)
14-
html_parser.process(html)
13+
def append(css: nil, html: nil)
14+
html_parser.parse_styles(css) if css
15+
html_parser.process(html) if html
1516
end
1617
end
1718
end

spec/features/instance_spec.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
it 'preserves the styles in the instance' do
55
pdf = Prawn::Document.new(page_size: 'A4')
66
phtml = PrawnHtml::Instance.new(pdf)
7-
phtml.append(html: '<style>h1 { color: green }</style>')
8-
phtml.append(html: '<h1>Some HTML before</h1>')
7+
css = <<~CSS
8+
h1 { color: green }
9+
i { color: red }
10+
CSS
11+
phtml.append(css: css)
12+
phtml.append(html: '<h1>Some <i>HTML</i> before</h1>')
913
pdf.text 'Some Prawn text'
10-
phtml.append(html: '<h1>Some HTML after</h1>')
14+
phtml.append(html: '<h1>Some <i>HTML</i> after</h1>')
1115

1216
output_file = File.expand_path('../../examples/instance.pdf', __dir__)
1317
pdf.render_file(output_file) unless File.exist?(output_file)

spec/units/prawn_html/html_parser_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@
3838
end
3939
end
4040

41+
describe '#parse_styles' do
42+
subject(:parse_styles) { html_parser.parse_styles(css) }
43+
44+
let(:css) { 'h1 { color: red }' }
45+
46+
it { is_expected.to eq('h1' => 'color: red') }
47+
end
48+
4149
describe '#process' do
4250
subject(:process) { html_parser.process(html) }
4351

0 commit comments

Comments
 (0)