prawndown-ext 0.1.6 → 0.1.9
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/lib/prawndown/parser.rb +50 -13
- data/lib/prawndown/version.rb +1 -1
- data/lib/prawndown-ext.rb +54 -19
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cde96e249677d9a8796584e537c0466f30cf98764c16eafe3fd08b82fcecebee
|
4
|
+
data.tar.gz: 664c9251c77bfe9a783d2ac26ecaf4a506c0f87e604a097ba49c6ae4c613015a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a2a036fb2d2a05537dd8d780b675b9574751afab2f9449630de714586a39c2451d0ed22149299b9cb86e15e362f05839ba2736bff650e770db1e4fb4d7d3c702
|
7
|
+
data.tar.gz: 95ba8579b3addb4a791a8e6d00eb91a0d98d2f36cf5f133a2349a7b4b2579c391dfe024b7f279f138ddcc68cefa21c5ade556c81e85a85035f362014a7a5630d
|
data/lib/prawndown/parser.rb
CHANGED
@@ -2,6 +2,18 @@ module PrawndownExt
|
|
2
2
|
# Markdown to Prawn parser
|
3
3
|
class Parser
|
4
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
|
+
|
5
17
|
DEFAULT_OPTIONS = {
|
6
18
|
"header1_size" => 28,
|
7
19
|
"header2_size" => 24,
|
@@ -12,28 +24,48 @@ module PrawndownExt
|
|
12
24
|
"quote_size" => 14,
|
13
25
|
"quote_font_spacing" => nil,
|
14
26
|
"quote_font" => nil,
|
27
|
+
"header1_font" => nil,
|
28
|
+
"header2_font" => nil,
|
29
|
+
"header3_font" => nil,
|
30
|
+
"header4_font" => nil,
|
31
|
+
"header5_font" => nil,
|
32
|
+
"header6_font" => nil,
|
15
33
|
"quote_margin" => 20,
|
34
|
+
"header1_margin" => 4,
|
35
|
+
"header2_margin" => 4,
|
36
|
+
"header3_margin" => 4,
|
37
|
+
"header4_margin" => 4,
|
38
|
+
"header5_margin" => 4,
|
39
|
+
"header6_margin" => 4,
|
16
40
|
}
|
17
41
|
|
18
42
|
MATCHERS = {
|
19
|
-
|
20
|
-
|
21
|
-
/^## (.+)/ => '<font size="HEADER2_SIZE"><b>\1</b></font>', # Header 2
|
22
|
-
/^### (.+)/ => '<font size="HEADER3_SIZE"><b>\1</b></font>', # Header 3
|
23
|
-
/^#### (.+)/ => '<font size="HEADER4_SIZE"><b>\1</b></font>', # Header 4
|
24
|
-
/^##### (.+)/ => '<font size="HEADER5_SIZE"><b>\1</b></font>', # Header 5
|
25
|
-
/^###### (.+)/ => '<font size="HEADER6_SIZE"><b>\1</b></font>', # Header 6
|
43
|
+
# Removes carriage returns, they cause issues
|
44
|
+
/\r/ => '',
|
26
45
|
/<iframe ([^\[]+)<\/iframe>/ => '', # Embeds are just removed
|
27
46
|
/(\*\*|__)(.*?)\1/ => '<b>\2</b>', # Bold
|
28
47
|
/(\*|_)(.*?)\1/ => '<i>\2</i>', # Italic
|
29
48
|
/\~\~(.*?)\~\~/ => '<strikethrough>\1</strikethrough>', # Strikethrough
|
49
|
+
## Regular markdown
|
50
|
+
## Header 1
|
51
|
+
/(^# )(.+)/ => '<command_break>{"command":"header1","margin":HEADER1_MARGIN,"text":"<font name=\'HEADER1_FONT\' size=\'HEADER1_SIZE\'><b>\2</b></font>"}<command_break>',
|
52
|
+
## Header 2
|
53
|
+
/(^## )(.*)/ => '<command_break>{"command":"header2","margin":HEADER2_MARGIN,"text":"<font name=\'HEADER2_FONT\' size=\'HEADER2_SIZE\'><b>\2</b></font>"}<command_break>',
|
54
|
+
## Header 3
|
55
|
+
/(^### )(.*)/ => '<command_break>{"command":"header3","margin":HEADER3_MARGIN,"text":"<font name=\'HEADER3_FONT\' size=\'HEADER3_SIZE\'><b>\2</b></font>"}<command_break>',
|
56
|
+
## Header 4
|
57
|
+
/(^#### )(.*)/ => '<command_break>{"command":"header4","margin":HEADER4_MARGIN,"text":"<font name=\'HEADER4_FONT\' size=\'HEADER4_SIZE\'><b>\2</b></font>"}<command_break>',
|
58
|
+
## Header 5
|
59
|
+
/(^##### )(.*)/ => '<command_break>{"command":"header5","margin":HEADER5_MARGIN,"text":"<font name=\'HEADER5_FONT\' size=\'HEADER5_SIZE\'><b>\2</b></font>"}<command_break>',
|
60
|
+
## Header 6
|
61
|
+
/(^###### )(.*)/ => '<command_break>{"command":"header6","margin":HEADER6_MARGIN,"text":"<font name=\'HEADER6_FONT\' size=\'HEADER6_SIZE\'><b>\2</b></font>"}<command_break>',
|
30
62
|
|
31
63
|
# Command Break items
|
32
64
|
# These split into multiple commands for output
|
33
65
|
|
34
66
|
# Images
|
35
67
|
/!\[([^\[]+)\]\(([^\)]+)\)/ => '<command_break>{"command":"img", "alt":"\1", "path":"\2"}<command_break>',
|
36
|
-
/^> (.+)/ => '<command_break>{"command":"quote","margin":QUOTE_MARGIN,"text":"<font name=\'QUOTE_FONT\' character_spacing=\'QUOTE_FONT_SPACING\' size=\'QUOTE_SIZE\'
|
68
|
+
/^> (.+)/ => '<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
|
37
69
|
|
38
70
|
# Stuff to process last
|
39
71
|
/\[([^\[]+)\]\(([^\)]+)\)/ => '<link href="\2">\1</link>', # Link
|
@@ -60,10 +92,15 @@ module PrawndownExt
|
|
60
92
|
end
|
61
93
|
|
62
94
|
def replace_options text
|
63
|
-
# remove
|
64
|
-
|
65
|
-
|
66
|
-
@options.
|
95
|
+
# remove nil options if it doesnt exist
|
96
|
+
|
97
|
+
DELETE_NAMES.each do |option|
|
98
|
+
if @options.key?(option)
|
99
|
+
if @options[option].nil?
|
100
|
+
text = text.gsub("name='" + option.upcase + "' ", "")
|
101
|
+
@options.delete(option)
|
102
|
+
end
|
103
|
+
end
|
67
104
|
end
|
68
105
|
|
69
106
|
# remove quote spacing if it doesnt exist
|
@@ -90,7 +127,7 @@ module PrawndownExt
|
|
90
127
|
result = _match.inject(@text) do |final_string, (markdown_matcher, prawn_tag)|
|
91
128
|
final_string.gsub(markdown_matcher, prawn_tag)
|
92
129
|
end
|
93
|
-
|
130
|
+
|
94
131
|
result = replace_options result
|
95
132
|
|
96
133
|
result = result.split("<command_break>")
|
data/lib/prawndown/version.rb
CHANGED
data/lib/prawndown-ext.rb
CHANGED
@@ -10,38 +10,67 @@ module PrawndownExt
|
|
10
10
|
class CommandInterface
|
11
11
|
|
12
12
|
COMMAND = {
|
13
|
-
"text" => -> (args, pdf) { cl_text(args, pdf) },
|
14
|
-
"img" => -> (args, pdf) { cl_img(args, pdf) },
|
15
|
-
"quote" => -> (args,pdf) { cl_text_box(args, pdf) },
|
13
|
+
"text" => -> (args, pdf, options) { cl_text(args, pdf, options) },
|
14
|
+
"img" => -> (args, pdf, options) { cl_img(args, pdf, options) },
|
15
|
+
"quote" => -> (args,pdf, options) { cl_text_box(args, pdf, options) },
|
16
|
+
"header1" => -> (args,pdf, options) { cl_text_box(args, pdf, options) },
|
17
|
+
"header2" => -> (args,pdf, options) { cl_text_box(args, pdf, options) },
|
18
|
+
"header3" => -> (args,pdf, options) { cl_text_box(args, pdf, options) },
|
19
|
+
"header4" => -> (args,pdf, options) { cl_text_box(args, pdf, options) },
|
20
|
+
"header5" => -> (args,pdf, options) { cl_text_box(args, pdf, options) },
|
21
|
+
"header6" => -> (args,pdf, options) { cl_text_box(args, pdf, options) },
|
16
22
|
}
|
17
23
|
|
18
|
-
def self.cl_text args, pdf
|
24
|
+
def self.cl_text args, pdf, options
|
19
25
|
|
20
|
-
pdf.text args["text"], inline_format: true
|
26
|
+
pdf.text args["text"], inline_format: true, leading: options["default_line_spacing"].to_f
|
21
27
|
|
22
28
|
end
|
23
29
|
|
24
|
-
def self.cl_text_box args, pdf
|
30
|
+
def self.cl_text_box args, pdf, options
|
31
|
+
if !options.key?(args["command"] + "_line_spacing")
|
32
|
+
options[args["command"] + "_line_spacing"] = 0
|
33
|
+
end
|
34
|
+
|
35
|
+
if !options.key?("margin")
|
36
|
+
args["margin"] = 0
|
37
|
+
end
|
38
|
+
|
25
39
|
pdf.pad args["margin"] do
|
26
40
|
pdf.indent args["margin"], args["margin"] do
|
27
|
-
pdf.text args["text"], inline_format: true
|
41
|
+
pdf.text args["text"], inline_format: true, leading: options[args["command"] + "_line_spacing"].to_f
|
28
42
|
end
|
29
43
|
end
|
30
44
|
|
31
45
|
end
|
32
46
|
|
33
|
-
def self.cl_img args, pdf
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
47
|
+
def self.cl_img args, pdf, options
|
48
|
+
if options.key?("no_image")
|
49
|
+
if options["no_image"]
|
50
|
+
return
|
51
|
+
end
|
52
|
+
end
|
38
53
|
|
54
|
+
file = args["path"]
|
55
|
+
|
56
|
+
if !File.file?(file)
|
57
|
+
file = "." + file
|
58
|
+
end
|
59
|
+
|
60
|
+
if File.extname(file) != ".gif"
|
61
|
+
pdf.image(file,
|
62
|
+
width: pdf.bounds.width,
|
63
|
+
position: :center)
|
64
|
+
end
|
65
|
+
end
|
39
66
|
|
40
|
-
def exec args, pdf
|
41
67
|
|
68
|
+
def exec args, pdf, options
|
42
69
|
if args.key?("command")
|
43
70
|
if COMMAND.include?(args["command"])
|
44
|
-
|
71
|
+
|
72
|
+
COMMAND[args["command"]].call(args, pdf, options)
|
73
|
+
|
45
74
|
end
|
46
75
|
end
|
47
76
|
|
@@ -62,16 +91,22 @@ module PrawndownExt
|
|
62
91
|
# markdown '# Welcome to Prawndown!'
|
63
92
|
# markdown '**Important:** We _hope_ you enjoy your stay!'
|
64
93
|
# end
|
65
|
-
def markdown(string, options:
|
94
|
+
def markdown(string, options: {})
|
95
|
+
|
96
|
+
if !options.key?("default_line_spacing")
|
97
|
+
options["default_line_spacing"] = 0
|
98
|
+
end
|
99
|
+
|
66
100
|
processed = PrawndownExt::Parser.new(string, options).to_prawn
|
67
101
|
|
68
|
-
processed.each do |output|
|
102
|
+
processed.each do |output|
|
69
103
|
|
70
104
|
begin
|
71
|
-
|
72
|
-
|
105
|
+
object = JSON.parse(output.strip)
|
106
|
+
|
107
|
+
CommandInterface.new.exec object, self, options
|
73
108
|
rescue
|
74
|
-
text unescape_text(output), inline_format: true
|
109
|
+
text unescape_text(output), inline_format: true, leading: options["default_line_spacing"].to_f
|
75
110
|
end
|
76
111
|
|
77
112
|
end
|
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.
|
4
|
+
version: 0.1.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- PunishedFelix
|
@@ -30,8 +30,8 @@ dependencies:
|
|
30
30
|
- !ruby/object:Gem::Version
|
31
31
|
version: 2.5.0
|
32
32
|
description: Extension of Prawndown to include additional Markdown features. Currently
|
33
|
-
supports custom header sizes,
|
34
|
-
images
|
33
|
+
supports custom header sizes, fonts, and other properties; removing iframe content,
|
34
|
+
and support for images and quotes.
|
35
35
|
email:
|
36
36
|
- labadore1844@gmail.com
|
37
37
|
executables: []
|
@@ -61,5 +61,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
61
|
requirements: []
|
62
62
|
rubygems_version: 3.6.9
|
63
63
|
specification_version: 4
|
64
|
-
summary: Extension of Prawndown to include additional features
|
64
|
+
summary: Extension of Prawndown to include additional features, such as customizing
|
65
|
+
text fonts, sizes, and support for images and quotes.
|
65
66
|
test_files: []
|