prawndown-ext 0.1.1 → 0.1.2
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 +47 -34
- data/lib/prawndown/version.rb +1 -1
- data/lib/prawndown-ext.rb +65 -1
- 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: f575daf43b174ed2094fd654fa3766991353c7a557c419ea65f1bdb08e91dcb5
|
4
|
+
data.tar.gz: ff99c756b94a34d22c16a36a62e4f1eeb5ca8f3e8bd98b9d0965002f9bfe5353
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0abd082859ca2e9e78ef6c031301a22fc030d9779e1b7d0fd8513f6594f389fdc933e6859e0a6597598180ef7d042b03ab930a54151db4adf3a940d78c6abcf9
|
7
|
+
data.tar.gz: e224eeddbb4a52086cf69853e53491d238479553b086e20fdb63c94b71133df11416f21a84591e8b6cc8dd26d3be0816f056ca2ea082f8dcf44e964dd9a025b9
|
data/lib/prawndown/parser.rb
CHANGED
@@ -2,56 +2,68 @@ module PrawndownExt
|
|
2
2
|
# Markdown to Prawn parser
|
3
3
|
class Parser
|
4
4
|
MATCHERS = {
|
5
|
-
|
5
|
+
## Regular markdown
|
6
6
|
/^# (.+)/ => '<font size="HEADER1_SIZE"><b>\1</b></font>', # Header 1
|
7
7
|
/^## (.+)/ => '<font size="HEADER2_SIZE"><b>\1</b></font>', # Header 2
|
8
8
|
/^### (.+)/ => '<font size="HEADER3_SIZE"><b>\1</b></font>', # Header 3
|
9
9
|
/^#### (.+)/ => '<font size="HEADER4_SIZE"><b>\1</b></font>', # Header 4
|
10
10
|
/^##### (.+)/ => '<font size="HEADER5_SIZE"><b>\1</b></font>', # Header 5
|
11
11
|
/^###### (.+)/ => '<font size="HEADER6_SIZE"><b>\1</b></font>', # Header 6
|
12
|
-
#/!\[([^\[]+)\]\(([^\)]+)\)/ => '<img href="\2" alt="\1">', # Image
|
13
|
-
/!\[([^\[]+)\]\(([^\)]+)\)/ => '', # Images removed for now
|
14
12
|
/<iframe ([^\[]+)<\/iframe>/ => '', # Embeds are just removed
|
15
|
-
/\[([^\[]+)\]\(([^\)]+)\)/ => '<link href="\2">\1</link>', # Link
|
16
13
|
/(\*\*|__)(.*?)\1/ => '<b>\2</b>', # Bold
|
17
14
|
/(\*|_)(.*?)\1/ => '<i>\2</i>', # Italic
|
18
|
-
/\~\~(.*?)\~\~/ => '<strikethrough>\1</strikethrough>' # Strikethrough
|
15
|
+
/\~\~(.*?)\~\~/ => '<strikethrough>\1</strikethrough>', # Strikethrough
|
16
|
+
|
17
|
+
# Command Break items
|
18
|
+
# These split into multiple commands for output
|
19
|
+
|
20
|
+
# Images
|
21
|
+
/!\[([^\[]+)\]\(([^\)]+)\)/ => '<command_break>{"command":"img", "alt":"\1", "path":"\2"}<command_break>',
|
22
|
+
/^> (.+)/ => '<command_break>{"command":"quote","margin":100,"text":"\\1"}<command_break>', # Quote
|
23
|
+
|
24
|
+
# Stuff to process last
|
25
|
+
/\[([^\[]+)\]\(([^\)]+)\)/ => '<link href="\2">\1</link>', # Link
|
19
26
|
}
|
20
27
|
|
28
|
+
def escape_text text
|
29
|
+
text = text.gsub('"', '\\"')
|
30
|
+
end
|
31
|
+
|
21
32
|
# Initialize a new +Prawndown::Parser+.
|
22
33
|
# +text+ must a a valid Markdown string that only contains supported tags.
|
23
34
|
#
|
24
35
|
# Supported tags are: Header 1-6, bold, italic, strikethrough and link.
|
25
36
|
def initialize(text, options)
|
26
37
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
38
|
+
#@text = text.to_s
|
39
|
+
@text = escape_text text.to_s
|
40
|
+
@header1_size = 28
|
41
|
+
@header2_size = 24
|
42
|
+
@header3_size = 20
|
43
|
+
@header4_size = 18
|
44
|
+
@header5_size = 16
|
45
|
+
@header6_size = 14
|
46
|
+
|
47
|
+
if !options.nil?
|
48
|
+
if options.key?("header1_size")
|
49
|
+
@header1_size = options["header1_size"]
|
50
|
+
end
|
51
|
+
if options.key?("header2_size")
|
52
|
+
@header2_size = options["header2_size"]
|
53
|
+
end
|
54
|
+
if options.key?("header3_size")
|
55
|
+
@header3_size = options["header3_size"]
|
56
|
+
end
|
57
|
+
if options.key?("header4_size")
|
58
|
+
@header4_size = options["header4_size"]
|
59
|
+
end
|
60
|
+
if options.key?("header5_size")
|
61
|
+
@header5_size = options["header5_size"]
|
62
|
+
end
|
63
|
+
if options.key?("header6_size")
|
64
|
+
@header6_size = options["header6_size"]
|
65
|
+
end
|
66
|
+
end
|
55
67
|
|
56
68
|
end
|
57
69
|
|
@@ -74,8 +86,9 @@ module PrawndownExt
|
|
74
86
|
result = _match.inject(@text) do |final_string, (markdown_matcher, prawn_tag)|
|
75
87
|
final_string.gsub(markdown_matcher, prawn_tag)
|
76
88
|
end
|
77
|
-
|
78
89
|
|
90
|
+
result = result.split("<command_break>")
|
91
|
+
|
79
92
|
result
|
80
93
|
|
81
94
|
end
|
data/lib/prawndown/version.rb
CHANGED
data/lib/prawndown-ext.rb
CHANGED
@@ -1,9 +1,61 @@
|
|
1
1
|
require 'prawn'
|
2
2
|
require "prawndown/version"
|
3
3
|
require "prawndown/parser"
|
4
|
+
require 'json'
|
4
5
|
|
5
6
|
module PrawndownExt
|
7
|
+
|
6
8
|
module Interface
|
9
|
+
|
10
|
+
class CommandInterface
|
11
|
+
|
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) },
|
16
|
+
}
|
17
|
+
|
18
|
+
def self.cl_text args, pdf
|
19
|
+
|
20
|
+
pdf.text args["text"]
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.cl_text_box args, pdf
|
25
|
+
w_size = (pdf.bounds.width - args["margin"]).to_i
|
26
|
+
position = [((pdf.bounds.width - w_size) * 0.5).to_i, pdf.cursor]
|
27
|
+
|
28
|
+
pdf.bounding_box(position, width: w_size) do
|
29
|
+
|
30
|
+
pdf.text(args["text"])
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.cl_img args, pdf
|
37
|
+
pdf.image(args["path"],
|
38
|
+
width: pdf.bounds.width,
|
39
|
+
position: :center)
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
def exec args, pdf
|
44
|
+
|
45
|
+
if args.key?("command")
|
46
|
+
if COMMAND.include?(args["command"])
|
47
|
+
COMMAND[args["command"]].call(args, pdf)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
def unescape_text text
|
56
|
+
text = text.gsub('\\"', '\"')
|
57
|
+
end
|
58
|
+
|
7
59
|
# Renders Markdown in the current document
|
8
60
|
#
|
9
61
|
# It supports header 1-6, bold text, italic text, strikethrough and links
|
@@ -14,8 +66,20 @@ module PrawndownExt
|
|
14
66
|
# markdown '**Important:** We _hope_ you enjoy your stay!'
|
15
67
|
# end
|
16
68
|
def markdown(string, options: nil)
|
17
|
-
|
69
|
+
processed = PrawndownExt::Parser.new(string, options).to_prawn
|
70
|
+
|
71
|
+
processed.each do |output|
|
72
|
+
|
73
|
+
begin
|
74
|
+
CommandInterface.new.exec JSON.parse(output.strip), self
|
75
|
+
|
76
|
+
rescue
|
77
|
+
text unescape_text(output), inline_format: true
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
18
81
|
end
|
82
|
+
|
19
83
|
end
|
20
84
|
end
|
21
85
|
|