prawndown-ext 0.1.14 → 0.1.15

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e81c1189fb2fec119b406c1f56d8fbc18470b77eab3fbe6457fa97bcea2f187d
4
- data.tar.gz: 8334d33e581d317302353c26b15d1bd58bdf8d955acb9713584e763264de27ec
3
+ metadata.gz: 2a36350204d8f370f63b72c19e2c62b01fabd6f91da0f0692030c91fa62b3080
4
+ data.tar.gz: 386b3f53a8192f86ba5098b96d4546a70fb1a7a1633ae0953d44efe0f235f568
5
5
  SHA512:
6
- metadata.gz: 04e7eafb5f0ef9d0c0b01053a7c89a9a1a5ec899d8c8cecc8ba4a450494d0818ee7a474d17f264b84cd5c3190f2850e599143ec8d2bd4f10b28664ee1a4bfb2a
7
- data.tar.gz: f97af900e4fc8e2b464b9c3edafa7c41bcd270d0fdf72676d584b38842cfe67acd624f1324371c21f5aacc2070e96e58a4762d60490d0219985cdc24805e12aa
6
+ metadata.gz: dd33059ec96a336eb4da186b927255367065a73439ebfd342a2fa2779b554cb41ca46daf48bd28028ead472bde09d0cab4d9e3d3cd1384f77173bb9407ea72d0
7
+ data.tar.gz: 4547aa854d79817b71db117e51369e452d4baec7b34c3619b3561db83bda9e8efcaa67a4e98662840ab06834dec4fb49af342aff84a5c50360a3dbc4c74ec36c
@@ -0,0 +1,200 @@
1
+ require 'redcarpet'
2
+
3
+ module PrawndownExt
4
+ class Render < Redcarpet::Render::Base
5
+
6
+ # These are used as a collection of nil properties
7
+ # for font names.
8
+ DELETE_NAMES = [
9
+ "quote_font",
10
+ "code_font",
11
+ "header1_font",
12
+ "header2_font",
13
+ "header3_font",
14
+ "header4_font",
15
+ "header5_font",
16
+ "header6_font",
17
+ ]
18
+
19
+ DEFAULT_OPTIONS = {
20
+ "header1_line_space" => 5,
21
+ "header2_line_space" => 5,
22
+ "header3_line_space" => 5,
23
+ "header4_line_space" => 5,
24
+ "header5_line_space" => 5,
25
+ "header6_line_space" => 5,
26
+ "header1_size" => 28,
27
+ "header2_size" => 24,
28
+ "header3_size" => 20,
29
+ "header4_size" => 18,
30
+ "header5_size" => 16,
31
+ "header6_size" => 14,
32
+ "quote_size" => 14,
33
+ "quote_font_spacing" => nil,
34
+ "quote_font" => "quote",
35
+ "code_size" => 12,
36
+ "code_font_spacing" => nil,
37
+ "code_font" => "code",
38
+ "header1_font" => "header1",
39
+ "header2_font" => "header2",
40
+ "header3_font" => "header3",
41
+ "header4_font" => "header4",
42
+ "header5_font" => "header5",
43
+ "header6_font" => "header6",
44
+ "quote_margin" => 20,
45
+ "header1_margin" => 4,
46
+ "header2_margin" => 4,
47
+ "header3_margin" => 4,
48
+ "header4_margin" => 4,
49
+ "header5_margin" => 4,
50
+ "header6_margin" => 4,
51
+ "img_dir" => "",
52
+ }
53
+
54
+ # custom matchers that are not specific to markdown
55
+ MATCHERS = {
56
+ /\r/ => '', # Removes carriage returns, they cause issues
57
+ /<div class=\"webpage-exclude\">([\s|\S]*)<\/div>/ => '\1', # Web output excluded has tags removed
58
+ /<iframe ([^\[]+)<\/iframe>/ => '', # Embeds are just removed
59
+ /<div class=\"output-exclude\".*>([\s|\S]*)<\/div>/ => '', # Output excluded also removed
60
+ /<br><br>/ => '<command_break>{"command":"newpage"}<command_break>' # skips to the next section
61
+ }
62
+
63
+ def block_quote(quote)
64
+ %(<command_break>{"command":"quote","margin":QUOTE_MARGIN,"text":"<font name=\'QUOTE_FONT\' character_spacing=\'QUOTE_FONT_SPACING\' size=\'QUOTE_SIZE\'>#{quote}</font>"}<command_break>)
65
+ end
66
+
67
+ def block_code(code)
68
+ %(<command_break>{"command":"code","margin":CODE_MARGIN,"text":"<font name=\'CODE_FONT\' character_spacing=\'CODE_FONT_SPACING\' size=\'CODE_SIZE\'>#{code}</font>"}<command_break>)
69
+ end
70
+
71
+ def block_code(code)
72
+ %(<command_break>{"command":"code","margin":CODE_MARGIN,"text":"<font name=\'CODE_FONT\' character_spacing=\'CODE_FONT_SPACING\' size=\'CODE_SIZE\'>#{code}</font>"}<command_break>)
73
+ end
74
+
75
+ def footnote_def(content, number)
76
+ %(\n#{number}. #{content})
77
+ end
78
+
79
+ def footnote_ref(text)
80
+ %(<sup>#{text}</sup>)
81
+ end
82
+
83
+ def footnotes(text)
84
+ %(#{text})
85
+ end
86
+
87
+ def header(text, header_level)
88
+
89
+ if header_level <= 3
90
+ %(<command_break>{"command":"header#{header_level}","margin":HEADER#{header_level}_MARGIN,"text":"<font name=\'HEADER#{header_level}_FONT\' size=\'HEADER#{header_level}_SIZE\'><b>#{text}</b></font>"}<command_break>)
91
+ else
92
+ %(<command_break>{"command":"header#{header_level}","margin":HEADER#{header_level}_MARGIN,"text":"<font name=\'HEADER#{header_level}_FONT\' size=\'HEADER#{header_level}_SIZE\'>#{text}</font>"}<command_break>)
93
+ end
94
+ end
95
+
96
+ def emphasis(text)
97
+ %(<i>#{text}</i>)
98
+ end
99
+
100
+ def quote(text)
101
+ %(#{text})
102
+ end
103
+
104
+ def double_emphasis(text)
105
+ %(<b>#{text}</b>)
106
+ end
107
+
108
+ def triple_emphasis(text)
109
+ %(<i><b>#{text}</b></i>)
110
+ end
111
+
112
+ def underline(text)
113
+ %(<u>#{text}</u>)
114
+ end
115
+
116
+ def strikethrough(text)
117
+ %(<strikethrough>#{text}</strikethrough>)
118
+ end
119
+
120
+ def superscript(text)
121
+ %(<sup>#{text}</sup>)
122
+ end
123
+
124
+ def link(link, title, content)
125
+ %(<link href="#{link}">#{content}</link>)
126
+ end
127
+
128
+ def image(link, title, alt_text)
129
+ %(<command_break>{"command":"img", "title":"#{title}", "alt":"#{alt_text}", "path":"#{link}"}<command_break>)
130
+ end
131
+
132
+ def paragraph(text)
133
+ %(#{text})
134
+ end
135
+
136
+ def linebreak
137
+ "\n.LP\n"
138
+ end
139
+
140
+ # Initialize a new +Prawndown::Parser+.
141
+ # +text+ must a a valid Markdown string that only contains supported tags.
142
+ def set_options(options)
143
+
144
+ # this way a default is always loaded so no weird crashes
145
+ @options = Marshal.load(Marshal.dump(DEFAULT_OPTIONS))
146
+
147
+ options.keys.each do |key|
148
+ @options[key] = options[key]
149
+ end
150
+
151
+ end
152
+
153
+ def replace_options text
154
+ # remove nil options if it doesnt exist
155
+
156
+ DELETE_NAMES.each do |option|
157
+ if @options.key?(option)
158
+ if @options[option].nil?
159
+ text = text.gsub("name='" + option.upcase + "' ", "")
160
+ @options.delete(option)
161
+ end
162
+ end
163
+
164
+ end
165
+
166
+ # remove quote spacing if it doesnt exist
167
+ if @options["quote_font_spacing"].nil?
168
+ text = text.gsub("character_spacing='QUOTE_FONT_SPACING' ", "")
169
+ @options.delete("quote_font_spacing")
170
+ end
171
+
172
+ DEFAULT_OPTIONS.keys.each do |replacer|
173
+
174
+ text = text.gsub(replacer.upcase, @options[replacer].to_s)
175
+
176
+ end
177
+
178
+ text
179
+ end
180
+
181
+ # Parses the Markdown text and outputs a Prawn compatible string
182
+ def finalize_input input
183
+
184
+ # variable replacement
185
+ _match = Marshal.load(Marshal.dump(MATCHERS))
186
+
187
+ result = _match.inject(input) do |final_string, (markdown_matcher, prawn_tag)|
188
+ final_string.gsub(markdown_matcher, prawn_tag)
189
+ end
190
+
191
+ result = replace_options result
192
+
193
+ result = result.split("<command_break>")
194
+
195
+ result
196
+
197
+ end
198
+
199
+ end
200
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module PrawndownExt
4
4
  module Ext
5
- VERSION = "0.1.14"
5
+ VERSION = "0.1.15"
6
6
  end
7
7
  end
data/lib/prawndown-ext.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'prawn'
2
2
  require "prawndown/version"
3
- require "prawndown/parser"
3
+ require "prawndown/new-parser"
4
4
  require 'json'
5
5
 
6
6
  module PrawndownExt
@@ -43,7 +43,7 @@ module PrawndownExt
43
43
 
44
44
  margin = (options[args["command"] + "_horizontal_margin"] * 0.5).floor
45
45
  half_margin = (options[args["command"] + "_vertical_margin"] * 0.5).floor
46
-
46
+
47
47
  pdf.pad half_margin do
48
48
  pdf.indent margin, margin do
49
49
  pdf.text args["text"], inline_format: true, leading: options[args["command"] + "_line_spacing"].to_f
@@ -96,6 +96,7 @@ module PrawndownExt
96
96
  end
97
97
 
98
98
  pdf.pad options["image_pad"] do
99
+
99
100
  if height.nil? && !width.nil?
100
101
  pdf.image(file,
101
102
  width: [pdf.bounds.width, width].min,
@@ -156,7 +157,29 @@ module PrawndownExt
156
157
  options["default_line_spacing"] = 0
157
158
  end
158
159
 
159
- processed = PrawndownExt::Parser.new(string, options).to_prawn
160
+ #processed = PrawndownExt::Parser.new(string, options).to_prawn
161
+
162
+ md = Redcarpet::Markdown.new(PrawndownExt::Render, render_options = {
163
+ :footnotes => true,
164
+ :quote => false,
165
+ :space_after_headers => true,
166
+ :lax_spacing => true,
167
+ :strikethrough => true,
168
+ :disable_indented_code_blocks => true,
169
+ :autolink => false,
170
+ :fenced_code_blocks => true,
171
+ :no_intra_emphasis => true,
172
+
173
+
174
+ })
175
+
176
+ text = md.render(string)
177
+
178
+ renderer = PrawndownExt::Render.new()
179
+
180
+ renderer.set_options options
181
+
182
+ processed = renderer.finalize_input text
160
183
 
161
184
  processed.each do |output|
162
185
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prawndown-ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.14
4
+ version: 0.1.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - PunishedFelix
@@ -29,6 +29,26 @@ dependencies:
29
29
  - - ">="
30
30
  - !ruby/object:Gem::Version
31
31
  version: 2.5.0
32
+ - !ruby/object:Gem::Dependency
33
+ name: redcarpet
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - "~>"
37
+ - !ruby/object:Gem::Version
38
+ version: 3.6.1
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 3.6.1
42
+ type: :runtime
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: 3.6.1
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 3.6.1
32
52
  description: Extension of Prawndown to include additional Markdown features. Currently
33
53
  supports custom header sizes, fonts, and other properties; removing iframe content,
34
54
  and support for images and quotes.
@@ -39,7 +59,7 @@ extensions: []
39
59
  extra_rdoc_files: []
40
60
  files:
41
61
  - lib/prawndown-ext.rb
42
- - lib/prawndown/parser.rb
62
+ - lib/prawndown/new-parser.rb
43
63
  - lib/prawndown/version.rb
44
64
  homepage: https://github.com/badgernested/prawndown-ext
45
65
  licenses:
@@ -1,149 +0,0 @@
1
- module PrawndownExt
2
- # Markdown to Prawn parser
3
- class Parser
4
-
5
- # These are used as a collection of nil properties
6
- # for font names.
7
- DELETE_NAMES = [
8
- "quote_font",
9
- "header1_font",
10
- "header2_font",
11
- "header3_font",
12
- "header4_font",
13
- "header5_font",
14
- "header6_font",
15
- ]
16
-
17
- DEFAULT_OPTIONS = {
18
- "header1_line_space" => 5,
19
- "header2_line_space" => 5,
20
- "header3_line_space" => 5,
21
- "header4_line_space" => 5,
22
- "header5_line_space" => 5,
23
- "header6_line_space" => 5,
24
- "header1_size" => 28,
25
- "header2_size" => 24,
26
- "header3_size" => 20,
27
- "header4_size" => 18,
28
- "header5_size" => 16,
29
- "header6_size" => 14,
30
- "quote_size" => 14,
31
- "quote_font_spacing" => nil,
32
- "quote_font" => "quote",
33
- "header1_font" => "header1",
34
- "header2_font" => "header2",
35
- "header3_font" => "header3",
36
- "header4_font" => "header4",
37
- "header5_font" => "header5",
38
- "header6_font" => "header6",
39
- "quote_margin" => 20,
40
- "header1_margin" => 4,
41
- "header2_margin" => 4,
42
- "header3_margin" => 4,
43
- "header4_margin" => 4,
44
- "header5_margin" => 4,
45
- "header6_margin" => 4,
46
- "img_dir" => "",
47
- }
48
-
49
- MATCHERS = {
50
- # Removes carriage returns, they cause issues
51
- /\r/ => '',
52
- /<iframe ([^\[]+)<\/iframe>/ => '', # Embeds are just removed
53
- /(\*\*|__)(.*?)\1/ => '<b>\2</b>', # Bold
54
- /(\*|_)(.*?)\1/ => '<i>\2</i>', # Italic
55
- /\~\~(.*?)\~\~/ => '<strikethrough>\1</strikethrough>', # Strikethrough
56
- ## Regular markdown
57
- ## Header 1
58
- /(^# )(.+)/ => '<command_break>{"command":"header1","margin":HEADER1_MARGIN,"text":"<font name=\'HEADER1_FONT\' size=\'HEADER1_SIZE\'><b>\2</b></font>"}<command_break>',
59
- ## Header 2
60
- /(^## )(.*)/ => '<command_break>{"command":"header2","margin":HEADER2_MARGIN,"text":"<font name=\'HEADER2_FONT\' size=\'HEADER2_SIZE\'><b>\2</b></font>"}<command_break>',
61
- ## Header 3
62
- /(^### )(.*)/ => '<command_break>{"command":"header3","margin":HEADER3_MARGIN,"text":"<font name=\'HEADER3_FONT\' size=\'HEADER3_SIZE\'><b>\2</b></font>"}<command_break>',
63
- ## Header 4
64
- /(^#### )(.*)/ => '<command_break>{"command":"header4","margin":HEADER4_MARGIN,"text":"<font name=\'HEADER4_FONT\' size=\'HEADER4_SIZE\'><b>\2</b></font>"}<command_break>',
65
- ## Header 5
66
- /(^##### )(.*)/ => '<command_break>{"command":"header5","margin":HEADER5_MARGIN,"text":"<font name=\'HEADER5_FONT\' size=\'HEADER5_SIZE\'><b>\2</b></font>"}<command_break>',
67
- ## Header 6
68
- /(^###### )(.*)/ => '<command_break>{"command":"header6","margin":HEADER6_MARGIN,"text":"<font name=\'HEADER6_FONT\' size=\'HEADER6_SIZE\'><b>\2</b></font>"}<command_break>',
69
-
70
- # Command Break items
71
- # These split into multiple commands for output
72
-
73
- # Images
74
- /!\[([^\[]+)\]\(([^\)]+)\)/ => '<command_break>{"command":"img", "alt":"\1", "path":"\2"}<command_break>',
75
- /^> (.+)/ => '<command_break>{"command":"quote","margin":QUOTE_MARGIN,"text":"<font name=\'QUOTE_FONT\' character_spacing=\'QUOTE_FONT_SPACING\' size=\'QUOTE_SIZE\'>\1</font>"}<command_break>', # Quote
76
-
77
- # Stuff to process last
78
- /\[([^\[]+)\]\(([^\)]+)\)/ => '<link href="\2">\1</link>', # Link
79
-
80
- # Special commands exclusive to prawndown-ext to control output
81
- # Two breaks in a row signifies a new page
82
- /<br><br>/ => '<command_break>{"command":"newpage"}<command_break>'
83
- }
84
-
85
- def escape_text text
86
- text = text.gsub('"', '\\"')
87
- end
88
-
89
- # Initialize a new +Prawndown::Parser+.
90
- # +text+ must a a valid Markdown string that only contains supported tags.
91
- def initialize(text, options)
92
-
93
- #@text = text.to_s
94
- @text = escape_text text.to_s
95
-
96
- # this way a default is always loaded so no weird crashes
97
- @options = Marshal.load(Marshal.dump(DEFAULT_OPTIONS))
98
-
99
- options.keys.each do |key|
100
- @options[key] = options[key]
101
- end
102
-
103
- end
104
-
105
- def replace_options text
106
- # remove nil options if it doesnt exist
107
-
108
- DELETE_NAMES.each do |option|
109
- if @options.key?(option)
110
- if @options[option].nil?
111
- text = text.gsub("name='" + option.upcase + "' ", "")
112
- @options.delete(option)
113
- end
114
- end
115
-
116
- end
117
-
118
- # remove quote spacing if it doesnt exist
119
- if @options["quote_font_spacing"].nil?
120
- text = text.gsub("character_spacing='QUOTE_FONT_SPACING' ", "")
121
- @options.delete("quote_font_spacing")
122
- end
123
-
124
- DEFAULT_OPTIONS.keys.each do |replacer|
125
- text = text.gsub(replacer.upcase, @options[replacer].to_s)
126
- end
127
-
128
- text
129
- end
130
-
131
- # Parses the Markdown text and outputs a Prawn compatible string
132
- def to_prawn
133
-
134
- # variable replacement
135
- _match = Marshal.load(Marshal.dump(MATCHERS))
136
-
137
- result = _match.inject(@text) do |final_string, (markdown_matcher, prawn_tag)|
138
- final_string.gsub(markdown_matcher, prawn_tag)
139
- end
140
-
141
- result = replace_options result
142
-
143
- result = result.split("<command_break>")
144
-
145
- result
146
-
147
- end
148
- end
149
- end