prawn_components 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ba3fe2c34017a0063369b7780dd3a3188a031eafc0845b3fe59b974bab47c4b1
4
+ data.tar.gz: c68115920c2bfdd0b171eb5b2f26b3d104eda194433081ecbbb9626811cccc81
5
+ SHA512:
6
+ metadata.gz: d015bf6cd3808c464e2883cbe53c2f3096fb1d51b444f9b6a233ffd751041f20f78b1f0586cfb85fefd1bba17ef06f87232077ef140a4c889f1d3ace3ab621f3
7
+ data.tar.gz: 46c507ebd9f66925aa9d6b25a524722faad3762344212df77b86b3a62426130e890a598f58c6ebca8a9d30c6193ea9d4ffa0268c1c40d8139cdc1e1a9038a25e
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
data/fonts/consola.ttf ADDED
Binary file
Binary file
Binary file
Binary file
Binary file
data/fonts/georgia.ttf ADDED
Binary file
@@ -0,0 +1,34 @@
1
+ module PrawnComponents
2
+ module Components
3
+ module CodeBlock
4
+ def code_block(value, language)
5
+ formatter = Rouge::Formatters::Prawn.new
6
+ lexer = get_lexer(language)
7
+
8
+ default_leading(5)
9
+ span(540) do
10
+ move_down(5)
11
+ span(520, position: 10) do
12
+ move_down(10)
13
+ font 'Consolas' do
14
+ formatted_text(formatter.format(lexer.new.lex(value)))
15
+ end
16
+ end
17
+
18
+ move_down(30)
19
+ end
20
+
21
+ default_leading(5)
22
+ end
23
+
24
+ private
25
+
26
+ def get_lexer(language)
27
+ name = "Rouge::Lexers::#{language.capitalize}"
28
+ Object.const_get(name)
29
+ rescue NameError
30
+ Rouge::Lexers::Ruby
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ module PrawnComponents
2
+ module Components
3
+ module H1
4
+ def h1(value, options = {})
5
+ callback = options.key?(:callback) ? options[:callback] : lambda { |x| x }
6
+
7
+ font('Helvetica') do
8
+ text(callback.call(value), size: 20, style: :bold, inline_format: true)
9
+ end
10
+ stroke do
11
+ move_down 10
12
+ stroke_color 'F2F5F8'
13
+ line_width 1
14
+ horizontal_line(0, 540)
15
+ end
16
+ move_down(20)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ module PrawnComponents
2
+ module Components
3
+ module H2
4
+ def h2(value, options = {})
5
+ callback = options.key?(:callback) ? options[:callback] : lambda { |x| x }
6
+
7
+ font('Helvetica') do
8
+ text(callback.call(value), size: 18, style: :bold, inline_format: true)
9
+ end
10
+ move_down(10)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module PrawnComponents
2
+ module Components
3
+ module H3
4
+ def h3(value, options = {})
5
+ callback = options.key?(:callback) ? options[:callback] : lambda { |x| x }
6
+
7
+ font('Helvetica') do
8
+ text(callback.call(value), size: 16, style: :bold, inline_format: true)
9
+ end
10
+ move_down(10)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module PrawnComponents
2
+ module Components
3
+ module H4
4
+ def h4(value, options = {})
5
+ callback = options.key?(:callback) ? options[:callback] : lambda { |x| x }
6
+
7
+ font('Helvetica') do
8
+ text(callback.call(value), size: 14, style: :bold, inline_format: true)
9
+ end
10
+ move_down(10)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ module PrawnComponents
2
+ module Components
3
+ module Ol
4
+ def ol(value, options = {})
5
+ value.each_with_index do |val, i|
6
+ start_new_page if value.last != val && cursor.to_i < 64
7
+ indent(31) do
8
+ parsed_value = options.key?(:callback) ? options[:callback].call(val) : val
9
+ text("#{i + 1}.\xC2\xA0\ #{parsed_value}", size: 13, inline_format: true, align: :justify)
10
+ end
11
+
12
+ move_down(5)
13
+ end
14
+
15
+ move_down(10)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+ module PrawnComponents
2
+ module Components
3
+ module Paragraph
4
+ def paragraph(value)
5
+ text(value, size: 13, inline_format: true, align: :justify)
6
+ move_down(10)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,32 @@
1
+ module PrawnComponents
2
+ module Components
3
+ module Quote
4
+ def quote(value, options = {})
5
+ callback = options.key?(:callback) ? options[:callback] : lambda { |x| x }
6
+ parsed_value = if value.is_a?(Array)
7
+ value.map { |e| [callback.call(e)] }
8
+ else
9
+ value.split("\n").map { |e| [callback.call(e)] }
10
+ end
11
+
12
+ quote_rows = parsed_value
13
+ text_color = '6A737D'
14
+ border_color = 'DFE2E5'
15
+ padding = [5, 0, 5, 15]
16
+ border_width = 3
17
+
18
+ table(quote_rows,
19
+ cell_style: {
20
+ text_color: text_color,
21
+ borders: [:left],
22
+ border_color: border_color,
23
+ border_width: border_width,
24
+ padding: padding,
25
+ inline_format: true
26
+ }
27
+ )
28
+ move_down(20)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,78 @@
1
+ # pdf.v1_table_of_contents(
2
+ # [
3
+ # [
4
+ # 'Preface',
5
+ # {
6
+ # page: 1,
7
+ # headings: {
8
+ # 'Introduction' => '2',
9
+ # 'Chapter 1' => '3',
10
+ # 'Chapter 2' => '4',
11
+ # }
12
+ # }
13
+ # ],
14
+ # [
15
+ # 'Introduction',
16
+ # {
17
+ # page: 2,
18
+ # headings: {
19
+ # 'Introduction' => '2',
20
+ # 'Chapter 1' => '3',
21
+ # 'Chapter 2' => '4',
22
+ # }
23
+ # }
24
+ # ]
25
+ # ]
26
+ # )
27
+ module PrawnComponents
28
+ module Components
29
+ module TableOfContents
30
+ def table_of_contents(value)
31
+ value.each do |(chapter_title, data)|
32
+ table_data = []
33
+ start_page = data[:page]
34
+ table_data << [
35
+ { content: chapter_title, font_style: :bold },
36
+ { content: start_page.to_s, font_style: :bold, align: :right }
37
+ ]
38
+ data[:headings].each do |(title, page)|
39
+ str = "\xC2\xA0\xC2\xA0\xC2\xA0\xC2\xA0#{title}\xC2\xA0\xC2\xA0"
40
+ line_size = 140
41
+ 1.upto(line_size - str.size) do |i|
42
+ if i.odd?
43
+ str << "."
44
+ else
45
+ str << "\xC2\xA0"
46
+ end
47
+ end
48
+ length_indicator = data[:headings].keys.sort_by { |s| s.size }.reverse.first.size
49
+ diff = length_indicator - title.size
50
+
51
+ if diff > 0
52
+ next_diff = diff
53
+ next_diff = 2 if diff == 1
54
+ 1.upto(next_diff + 1) do |i|
55
+ if i.odd?
56
+ str << "\xC2\xA0"
57
+ else
58
+ str << "."
59
+ end
60
+ end
61
+ end
62
+
63
+ table_data << [
64
+ { content: title },
65
+ { content: page.to_s, align: :right }
66
+ ]
67
+ end
68
+
69
+ table(table_data,
70
+ cell_style: { border_width: 0, padding: [10, 10, 5, 10] },
71
+ width: 560
72
+ )
73
+ move_down(30)
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,19 @@
1
+ module PrawnComponents
2
+ module Components
3
+ module Ul
4
+ def ul(value, options = {})
5
+ value.each_with_index do |val, i|
6
+ start_new_page if value.last != val && cursor.to_i < 64
7
+ indent(31) do
8
+ parsed_value = options.key?(:callback) ? options[:callback].call(val) : val
9
+ text("• #{parsed_value}", size: 13, inline_format: true, align: :justify)
10
+ end
11
+
12
+ move_down(5)
13
+ end
14
+
15
+ move_down(10)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ module PrawnComponents
2
+ module Version
3
+ module_function
4
+
5
+ def to_s
6
+ "0.0.1"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,77 @@
1
+ require 'prawn/table'
2
+ require 'prawn'
3
+ require 'rouge'
4
+
5
+ require_relative 'prawn_rouge_formatter/fragment'
6
+ require_relative 'prawn_rouge_formatter/fragment_inline_block_styles'
7
+ require_relative 'prawn_rouge_formatter/fragment_styles'
8
+ require_relative 'prawn_rouge_formatter/rouge_theme_finder'
9
+ require_relative 'prawn_rouge_formatter/normalize_color'
10
+ require_relative 'prawn_rouge_formatter/rouge_theme_finder'
11
+ require_relative 'prawn_rouge_formatter/parser_configuration'
12
+ require_relative 'rouge/formatters/prawn'
13
+
14
+ require_relative 'prawn_components/components/h1'
15
+ require_relative 'prawn_components/components/h2'
16
+ require_relative 'prawn_components/components/h3'
17
+ require_relative 'prawn_components/components/h4'
18
+ require_relative 'prawn_components/components/paragraph'
19
+ require_relative 'prawn_components/components/code_block'
20
+ require_relative 'prawn_components/components/quote'
21
+ require_relative 'prawn_components/components/ul'
22
+ require_relative 'prawn_components/components/ol'
23
+ require_relative 'prawn_components/components/table_of_contents'
24
+
25
+ Prawn::Document.class_eval do
26
+ include PrawnComponents::Components::H1
27
+ include PrawnComponents::Components::H2
28
+ include PrawnComponents::Components::H3
29
+ include PrawnComponents::Components::H4
30
+ include PrawnComponents::Components::Paragraph
31
+ include PrawnComponents::Components::CodeBlock
32
+ include PrawnComponents::Components::Quote
33
+ include PrawnComponents::Components::Ul
34
+ include PrawnComponents::Components::Ol
35
+ include PrawnComponents::Components::TableOfContents
36
+ end
37
+
38
+ module PrawnComponents
39
+ class << self
40
+ def initialize_fonts(pdf:)
41
+ dir = Gem::Specification.find_by_name('prawn_components').gem_dir
42
+ fonts_dir = "#{dir}/fonts/"
43
+
44
+ pdf.font_families.update(
45
+ "Helvetica" => {
46
+ bold: "#{fonts_dir}Helvetica-Bold.ttf",
47
+ italic: "#{fonts_dir}Helvetica-Oblique.ttf",
48
+ normal: "#{fonts_dir}Helvetica.ttf"
49
+ }
50
+ )
51
+
52
+ pdf.font_families.update(
53
+ "Consolas" => {
54
+ bold: "#{fonts_dir}consolab.ttf",
55
+ italic: "#{fonts_dir}consolai.ttf",
56
+ normal: "#{fonts_dir}consola.ttf"
57
+ }
58
+ )
59
+
60
+ pdf.font_families.update(
61
+ "Georgia" => {
62
+ bold: "#{fonts_dir}georgia-bold.ttf",
63
+ italic: "#{fonts_dir}georgia-italic.ttf",
64
+ normal: "#{fonts_dir}georgia.ttf"
65
+ }
66
+ )
67
+
68
+ pdf.font_families.update(
69
+ "Inter" => {
70
+ bold: "#{fonts_dir}Inter-Bold.ttf",
71
+ normal: "#{fonts_dir}Inter-Regular.ttf",
72
+ italic: "#{fonts_dir}Inter-Italic.otf"
73
+ }
74
+ )
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,48 @@
1
+ module PrawnRougeFormatter
2
+ class Fragment
3
+ def initialize(token, theme, value)
4
+ @token = token
5
+ @theme = theme
6
+ @value = value
7
+ end
8
+
9
+ def self.call(token:, theme:, value: nil)
10
+ new(token, theme, value).call
11
+ end
12
+
13
+ def call
14
+ return default_fragment unless style_rules
15
+
16
+ fragment = default_fragment
17
+
18
+ # if (bg = PrawnRougeFormatter::NormalizeColor.fetch(style_rules.bg)) && bg != @background_color
19
+
20
+ # fragment[:background_color] = bg
21
+
22
+ # fragment = ::PrawnRougeFormatter::FragmentInlineBlockStyles.call(
23
+ # options: fragment, token: @token, rules: style_rules
24
+ # )
25
+ # end
26
+
27
+ if (fg = PrawnRougeFormatter::NormalizeColor.fetch(style_rules.fg))
28
+ fragment[:color] = fg
29
+ end
30
+
31
+ if styles = PrawnRougeFormatter::FragmentStyles.call(rules: style_rules)
32
+ fragment[:styles] = styles
33
+ end
34
+
35
+ fragment
36
+ end
37
+
38
+ private
39
+
40
+ def default_fragment
41
+ @value ? { text: @value, size: 13 } : {}
42
+ end
43
+
44
+ def style_rules
45
+ @style_rules ||= @theme.style_for(@token)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,22 @@
1
+ module PrawnRougeFormatter
2
+ class FragmentInlineBlockStyles
3
+ LineOrientedTokens = [
4
+ ::Rouge::Token::Tokens::Generic::Inserted,
5
+ ::Rouge::Token::Tokens::Generic::Deleted,
6
+ ::Rouge::Token::Tokens::Generic::Heading,
7
+ ::Rouge::Token::Tokens::Generic::Subheading
8
+ ]
9
+
10
+ def self.call(options:, token:, rules:)
11
+ if LineOrientedTokens.include? token
12
+ options[:inline_block] = true unless rules[:inline_block] == false
13
+ options[:extend] = true unless rules[:extend] == false
14
+ else
15
+ options[:inline_block] = true if rules[:inline_block]
16
+ options[:extend] = true if rules[:extend]
17
+ end
18
+
19
+ options
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,10 @@
1
+ module PrawnRougeFormatter
2
+ class FragmentStyles
3
+ def self.call(rules:)
4
+ styles = %i[italic underline].select { |s| rules[s] }
5
+ return if styles.size.zero?
6
+
7
+ styles.to_set
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ module PrawnRougeFormatter
2
+ class NormalizeColor
3
+ def self.fetch(code)
4
+ return unless code
5
+
6
+ normalized = (code.start_with? '#') ? code[1..-1] : code
7
+ normalized = normalized.each_char.map {|c| c * 2 }.join if normalized.length == 3
8
+ normalized
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ Tokens = ::Rouge::Token::Tokens
2
+ LineOrientedTokens = [
3
+ ::Rouge::Token::Tokens::Generic::Inserted,
4
+ ::Rouge::Token::Tokens::Generic::Deleted,
5
+ ::Rouge::Token::Tokens::Generic::Heading,
6
+ ::Rouge::Token::Tokens::Generic::Subheading
7
+ ]
8
+
9
+ LF = %(\n)
10
+ NoBreakSpace = %(\u00a0)
11
+ InnerIndent = %(\n )
12
+ GuardedIndent = %(\u00a0)
13
+ GuardedInnerIndent = %(\n\u00a0)
@@ -0,0 +1,28 @@
1
+ module PrawnRougeFormatter
2
+ class RougeThemeFinder
3
+ # Use Github theme by default
4
+ def initialize(options)
5
+ @options = options
6
+ end
7
+
8
+ def self.call(options = {})
9
+ new(options).call
10
+ end
11
+
12
+ def call(options = {})
13
+ (theme || default_theme).new
14
+ end
15
+
16
+ private
17
+
18
+ def default_theme
19
+ ::Rouge::Themes::Github
20
+ end
21
+
22
+ def theme
23
+ return unless @options[:theme]
24
+
25
+ ::Rouge::Theme.find(@options[:theme])
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,52 @@
1
+ module Rouge
2
+ module Formatters
3
+ class Prawn < Formatter
4
+ tag 'prawn'
5
+
6
+ def initialize opts = {}
7
+ @theme = ::PrawnRougeFormatter::RougeThemeFinder.call(opts)
8
+ @linenum_fragment_base = create_fragment(
9
+ Tokens::Generic::Lineno
10
+ ).merge(linenum: true)
11
+ end
12
+
13
+ # Override format method so fragments don't get flatted to a string
14
+ # and to add an options Hash.
15
+ def format tokens, opts = {}
16
+ stream tokens, opts
17
+ end
18
+
19
+ def stream tokens, opts = {}
20
+ start_of_line = true
21
+ tokens.map do |tok, val|
22
+ # match one or more consecutive endlines
23
+ if val == LF || (val == (LF * val.length))
24
+ start_of_line = true
25
+ { text: val }
26
+ else
27
+ val[0] = GuardedIndent if start_of_line && (val.start_with? ' ')
28
+ val.gsub! InnerIndent, GuardedInnerIndent if val.include? InnerIndent
29
+ # QUESTION do we need the call to create_fragment if val contains only spaces? consider bg
30
+ #fragment = create_fragment tok, val
31
+ fragment = val.rstrip.empty? ? { text: val } : create_fragment(tok, val)
32
+ # NOTE we assume if the fragment ends in a line feed, the intention was to match a line-oriented form
33
+ fragment[:line_oriented] = true if (start_of_line = val.end_with? LF)
34
+ fragment
35
+ end
36
+ end
37
+ end
38
+
39
+ def create_linenum_fragment linenum
40
+ @linenum_fragment_base.merge text: %(#{linenum} )
41
+ end
42
+
43
+ private
44
+
45
+ def create_fragment(token, value = nil)
46
+ PrawnRougeFormatter::Fragment.call(
47
+ token: token, theme: @theme, value: value
48
+ )
49
+ end
50
+ end
51
+ end
52
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prawn_components
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Paweł Dąbrowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-11-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: prawn
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.2.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 2.2.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: prawn-table
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.2.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.2.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: rouge
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 3.30.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 3.30.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: redcarpet
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 3.6.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 3.6.0
83
+ description: Craft beautiful PDFs with Prawn and components
84
+ email: contact@paweldabrowski.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - fonts/Helvetica-Bold.ttf
90
+ - fonts/Helvetica-Oblique.ttf
91
+ - fonts/Helvetica.ttf
92
+ - fonts/Inter-Bold.ttf
93
+ - fonts/Inter-Italic.otf
94
+ - fonts/Inter-Regular.ttf
95
+ - fonts/consola.ttf
96
+ - fonts/consolab.ttf
97
+ - fonts/consolai.ttf
98
+ - fonts/georgia-bold.ttf
99
+ - fonts/georgia-italic.ttf
100
+ - fonts/georgia.ttf
101
+ - lib/prawn_components.rb
102
+ - lib/prawn_components/components/code_block.rb
103
+ - lib/prawn_components/components/h1.rb
104
+ - lib/prawn_components/components/h2.rb
105
+ - lib/prawn_components/components/h3.rb
106
+ - lib/prawn_components/components/h4.rb
107
+ - lib/prawn_components/components/ol.rb
108
+ - lib/prawn_components/components/paragraph.rb
109
+ - lib/prawn_components/components/quote.rb
110
+ - lib/prawn_components/components/table_of_contents.rb
111
+ - lib/prawn_components/components/ul.rb
112
+ - lib/prawn_components/version.rb
113
+ - lib/prawn_rouge_formatter/fragment.rb
114
+ - lib/prawn_rouge_formatter/fragment_inline_block_styles.rb
115
+ - lib/prawn_rouge_formatter/fragment_styles.rb
116
+ - lib/prawn_rouge_formatter/normalize_color.rb
117
+ - lib/prawn_rouge_formatter/parser_configuration.rb
118
+ - lib/prawn_rouge_formatter/rouge_theme_finder.rb
119
+ - lib/rouge/formatters/prawn.rb
120
+ homepage:
121
+ licenses: []
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubygems_version: 3.4.10
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Craft beautiful PDFs with Prawn and components
142
+ test_files: []