prawndown-ext 0.1.6 → 0.1.7
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 +41 -12
- data/lib/prawndown/version.rb +1 -1
- data/lib/prawndown-ext.rb +36 -16
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1bd6519db873bccdf6e70d074fef2de91fed77183cf0bc3d5c98c7141a9ed401
|
4
|
+
data.tar.gz: 6c1437518072c4a8f4e638de7d4d2afb57f9486118393d426ca72a37b5295e55
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f998d15bf130fe474b49c511933814ef921f5357206ab8dc04f627464d443618a0e3b2f83b2b411244736b49689f423d27ad6cd16793ba724608c83e658cca9
|
7
|
+
data.tar.gz: 654ab49b3a3e398ca247609ce5e4035494c7c83cacce614a96cb7b02d729b41dfc15fb53c683bd97a6d86365400986af220e931ef7ea921b720b7f8dc85d4b2f
|
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,21 +24,33 @@ 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,
|
16
34
|
}
|
17
35
|
|
18
36
|
MATCHERS = {
|
19
|
-
## Regular markdown
|
20
|
-
/^# (.+)/ => '<font size="HEADER1_SIZE"><b>\1</b></font>', # Header 1
|
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
|
26
37
|
/<iframe ([^\[]+)<\/iframe>/ => '', # Embeds are just removed
|
27
38
|
/(\*\*|__)(.*?)\1/ => '<b>\2</b>', # Bold
|
28
39
|
/(\*|_)(.*?)\1/ => '<i>\2</i>', # Italic
|
29
40
|
/\~\~(.*?)\~\~/ => '<strikethrough>\1</strikethrough>', # Strikethrough
|
41
|
+
## Regular markdown
|
42
|
+
## Header 1
|
43
|
+
/^# (.+)/ => '<command_break>{"command":"header1","margin":QUOTE_MARGIN,"text":"<font name=\'HEADER1_FONT\' size=\'HEADER1_SIZE\'><b>\1</b></font>"}<command_break>',
|
44
|
+
## Header 2
|
45
|
+
/^## (.+)/ => '<command_break>{"command":"header2","margin":QUOTE_MARGIN,"text":"<font name=\'HEADER2_FONT\' size=\'HEADER2_SIZE\'><b>\1</b></font>"}<command_break>',
|
46
|
+
## Header 3
|
47
|
+
/^### (.+)/ => '<command_break>{"command":"header3","margin":QUOTE_MARGIN,"text":"<font name=\'HEADER3_FONT\' size=\'HEADER3_SIZE\'><b>\1</b></font>"}<command_break>',
|
48
|
+
## Header 4
|
49
|
+
/^#### (.+)/ => '<command_break>{"command":"header4","margin":QUOTE_MARGIN,"text":"<font name=\'HEADER4_FONT\' size=\'HEADER4_SIZE\'><b>\1</b></font>"}<command_break>',
|
50
|
+
## Header 5
|
51
|
+
/^##### (.+)/ => '<command_break>{"command":"header5","margin":QUOTE_MARGIN,"text":"<font name=\'HEADER5_FONT\' size=\'HEADER5_SIZE\'><b>\1</b></font>"}<command_break>',
|
52
|
+
## Header 6
|
53
|
+
/^###### (.+)/ => '<command_break>{"command":"header6","margin":QUOTE_MARGIN,"text":"<font name=\'HEADER6_FONT\' size=\'HEADER6_SIZE\'><b>\1</b></font>"}<command_break>',
|
30
54
|
|
31
55
|
# Command Break items
|
32
56
|
# These split into multiple commands for output
|
@@ -60,10 +84,15 @@ module PrawndownExt
|
|
60
84
|
end
|
61
85
|
|
62
86
|
def replace_options text
|
63
|
-
# remove
|
64
|
-
|
65
|
-
|
66
|
-
@options.
|
87
|
+
# remove nil options if it doesnt exist
|
88
|
+
|
89
|
+
DELETE_NAMES.each do |option|
|
90
|
+
if @options.key?(option)
|
91
|
+
if @options[option].nil?
|
92
|
+
text = text.gsub("name='" + option.upcase + "' ", "")
|
93
|
+
@options.delete(option)
|
94
|
+
end
|
95
|
+
end
|
67
96
|
end
|
68
97
|
|
69
98
|
# remove quote spacing if it doesnt exist
|
@@ -90,7 +119,7 @@ module PrawndownExt
|
|
90
119
|
result = _match.inject(@text) do |final_string, (markdown_matcher, prawn_tag)|
|
91
120
|
final_string.gsub(markdown_matcher, prawn_tag)
|
92
121
|
end
|
93
|
-
|
122
|
+
|
94
123
|
result = replace_options result
|
95
124
|
|
96
125
|
result = result.split("<command_break>")
|
data/lib/prawndown/version.rb
CHANGED
data/lib/prawndown-ext.rb
CHANGED
@@ -10,38 +10,53 @@ 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
|
47
|
+
def self.cl_img args, pdf, options
|
34
48
|
pdf.image(args["path"],
|
35
49
|
width: pdf.bounds.width,
|
36
50
|
position: :center)
|
37
51
|
end
|
38
52
|
|
39
53
|
|
40
|
-
def exec
|
41
|
-
|
54
|
+
def exec args, pdf, options
|
42
55
|
if args.key?("command")
|
43
56
|
if COMMAND.include?(args["command"])
|
44
|
-
|
57
|
+
|
58
|
+
COMMAND[args["command"]].call(args, pdf, options)
|
59
|
+
|
45
60
|
end
|
46
61
|
end
|
47
62
|
|
@@ -62,16 +77,21 @@ module PrawndownExt
|
|
62
77
|
# markdown '# Welcome to Prawndown!'
|
63
78
|
# markdown '**Important:** We _hope_ you enjoy your stay!'
|
64
79
|
# end
|
65
|
-
def markdown(string, options:
|
80
|
+
def markdown(string, options: {})
|
81
|
+
if !options.key?("default_line_spacing")
|
82
|
+
options["default_line_spacing"] = 0
|
83
|
+
end
|
84
|
+
|
66
85
|
processed = PrawndownExt::Parser.new(string, options).to_prawn
|
67
86
|
|
68
|
-
processed.each do |output|
|
87
|
+
processed.each do |output|
|
69
88
|
|
70
89
|
begin
|
71
|
-
|
72
|
-
|
90
|
+
object = JSON.parse(output.strip)
|
91
|
+
|
92
|
+
CommandInterface.new.exec object, self, options
|
73
93
|
rescue
|
74
|
-
text unescape_text(output), inline_format: true
|
94
|
+
text unescape_text(output), inline_format: true, leading: options["default_line_spacing"].to_f
|
75
95
|
end
|
76
96
|
|
77
97
|
end
|