prawndown-ext 0.1.3 → 0.1.4
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 +37 -40
- data/lib/prawndown/version.rb +1 -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: 642c5434cd34aa487ce5925919335736ac3ad47225eceed66c1e23d0cef1baf2
|
4
|
+
data.tar.gz: 6567f31eb143ca6bca5135ed3605edc8ba827272351953e2d68488c0eb6f9e79
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 47933fa1144ec46a86dc0a2d5aa48fb4c06d2632075b818d70ecf8a14be1316239ff8a56f735ca01e51bea274d6a741d863e949d694a5ba698dc986b4154e847
|
7
|
+
data.tar.gz: c06b397f36764c87ec95dfe9e3611440720b5074d0c81f0a51f4bd93008d564f543c52a5e2309c23c591c448c55cbe184eedc7e05737b1a488150ce91288b6d6
|
data/lib/prawndown/parser.rb
CHANGED
@@ -1,6 +1,17 @@
|
|
1
1
|
module PrawndownExt
|
2
2
|
# Markdown to Prawn parser
|
3
3
|
class Parser
|
4
|
+
|
5
|
+
DEFAULT_OPTIONS = {
|
6
|
+
"header1_size" => 28,
|
7
|
+
"header2_size" => 24,
|
8
|
+
"header3_size" => 20,
|
9
|
+
"header4_size" => 18,
|
10
|
+
"header5_size" => 16,
|
11
|
+
"header6_size" => 14,
|
12
|
+
"quote_size" => 14
|
13
|
+
}
|
14
|
+
|
4
15
|
MATCHERS = {
|
5
16
|
## Regular markdown
|
6
17
|
/^# (.+)/ => '<font size="HEADER1_SIZE"><b>\1</b></font>', # Header 1
|
@@ -19,7 +30,7 @@ module PrawndownExt
|
|
19
30
|
|
20
31
|
# Images
|
21
32
|
/!\[([^\[]+)\]\(([^\)]+)\)/ => '<command_break>{"command":"img", "alt":"\1", "path":"\2"}<command_break>',
|
22
|
-
/^> (.+)/ => '<command_break>{"command":"quote","margin":20,"text":"
|
33
|
+
/^> (.+)/ => '<command_break>{"command":"quote","margin":20,"text":"<font size=\'QUOTE_SIZE\'>\\1</font>"}<command_break>', # Quote
|
23
34
|
|
24
35
|
# Stuff to process last
|
25
36
|
/\[([^\[]+)\]\(([^\)]+)\)/ => '<link href="\2">\1</link>', # Link
|
@@ -31,57 +42,35 @@ module PrawndownExt
|
|
31
42
|
|
32
43
|
# Initialize a new +Prawndown::Parser+.
|
33
44
|
# +text+ must a a valid Markdown string that only contains supported tags.
|
34
|
-
#
|
35
|
-
# Supported tags are: Header 1-6, bold, italic, strikethrough and link.
|
36
45
|
def initialize(text, options)
|
37
46
|
|
38
47
|
#@text = text.to_s
|
39
48
|
@text = escape_text text.to_s
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
49
|
+
|
50
|
+
# this way a default is always loaded so no weird crashes
|
51
|
+
@options = Marshal.load(Marshal.dump(DEFAULT_OPTIONS))
|
52
|
+
|
53
|
+
options.keys.each do |key|
|
54
|
+
@options[key] = options[key]
|
55
|
+
end
|
67
56
|
|
68
57
|
end
|
69
58
|
|
59
|
+
def replace_options text
|
60
|
+
DEFAULT_OPTIONS.keys.each do |replacer|
|
61
|
+
if @options.key?(replacer)
|
62
|
+
text = text.gsub(replacer.upcase, @options[replacer].to_s)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
text
|
67
|
+
end
|
68
|
+
|
70
69
|
# Parses the Markdown text and outputs a Prawn compatible string
|
71
70
|
def to_prawn
|
72
71
|
|
73
72
|
# variable replacement
|
74
73
|
_match = Marshal.load(Marshal.dump(MATCHERS))
|
75
|
-
|
76
|
-
_match.each {
|
77
|
-
|x| puts
|
78
|
-
x[1].gsub!("HEADER1_SIZE", @header1_size.to_s)
|
79
|
-
x[1].gsub!("HEADER2_SIZE", @header2_size.to_s)
|
80
|
-
x[1].gsub!("HEADER3_SIZE", @header3_size.to_s)
|
81
|
-
x[1].gsub!("HEADER4_SIZE", @header4_size.to_s)
|
82
|
-
x[1].gsub!("HEADER5_SIZE", @header5_size.to_s)
|
83
|
-
x[1].gsub!("HEADER6_SIZE", @header6_size.to_s)
|
84
|
-
}
|
85
74
|
|
86
75
|
result = _match.inject(@text) do |final_string, (markdown_matcher, prawn_tag)|
|
87
76
|
final_string.gsub(markdown_matcher, prawn_tag)
|
@@ -89,6 +78,14 @@ module PrawndownExt
|
|
89
78
|
|
90
79
|
result = result.split("<command_break>")
|
91
80
|
|
81
|
+
i = 0
|
82
|
+
|
83
|
+
# replaces optional values here
|
84
|
+
while i < result.length
|
85
|
+
result[i] = replace_options result[i]
|
86
|
+
i += 1
|
87
|
+
end
|
88
|
+
|
92
89
|
result
|
93
90
|
|
94
91
|
end
|
data/lib/prawndown/version.rb
CHANGED