prawndown-ext 0.1.5 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9faf10d4050bcb223f71cfb4e70709b24472e7a07e6bf7c98ccbf06d3008a621
4
- data.tar.gz: 22e2ffd2f536b89d52d2a89a2257b41015a053ea7e9212ae4cd76afbd29bb0f0
3
+ metadata.gz: 1bd6519db873bccdf6e70d074fef2de91fed77183cf0bc3d5c98c7141a9ed401
4
+ data.tar.gz: 6c1437518072c4a8f4e638de7d4d2afb57f9486118393d426ca72a37b5295e55
5
5
  SHA512:
6
- metadata.gz: 1c8fe0e1339f041eafa92742a300e219dab5b7ef60c28dfe27a5995e76b7b2aaa81564ec5351b78aadc48e402c8ec93cc96cd1758a777f4bbdb5b220739cdfea
7
- data.tar.gz: b5bd8aa3b0223302771abbd4692e9fc439e6329a5cad6045f4714c60bad1e27a5c9cfe5065f0a5b02f8af25f9519cf1952370491bca958f4fd44e091fb0910e1
6
+ metadata.gz: 4f998d15bf130fe474b49c511933814ef921f5357206ab8dc04f627464d443618a0e3b2f83b2b411244736b49689f423d27ad6cd16793ba724608c83e658cca9
7
+ data.tar.gz: 654ab49b3a3e398ca247609ce5e4035494c7c83cacce614a96cb7b02d729b41dfc15fb53c683bd97a6d86365400986af220e931ef7ea921b720b7f8dc85d4b2f
@@ -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,
@@ -9,28 +21,43 @@ module PrawndownExt
9
21
  "header4_size" => 18,
10
22
  "header5_size" => 16,
11
23
  "header6_size" => 14,
12
- "quote_size" => 14
24
+ "quote_size" => 14,
25
+ "quote_font_spacing" => nil,
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,
33
+ "quote_margin" => 20,
13
34
  }
14
35
 
15
36
  MATCHERS = {
16
- ## Regular markdown
17
- /^# (.+)/ => '<font size="HEADER1_SIZE"><b>\1</b></font>', # Header 1
18
- /^## (.+)/ => '<font size="HEADER2_SIZE"><b>\1</b></font>', # Header 2
19
- /^### (.+)/ => '<font size="HEADER3_SIZE"><b>\1</b></font>', # Header 3
20
- /^#### (.+)/ => '<font size="HEADER4_SIZE"><b>\1</b></font>', # Header 4
21
- /^##### (.+)/ => '<font size="HEADER5_SIZE"><b>\1</b></font>', # Header 5
22
- /^###### (.+)/ => '<font size="HEADER6_SIZE"><b>\1</b></font>', # Header 6
23
37
  /<iframe ([^\[]+)<\/iframe>/ => '', # Embeds are just removed
24
38
  /(\*\*|__)(.*?)\1/ => '<b>\2</b>', # Bold
25
39
  /(\*|_)(.*?)\1/ => '<i>\2</i>', # Italic
26
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>',
27
54
 
28
55
  # Command Break items
29
56
  # These split into multiple commands for output
30
57
 
31
58
  # Images
32
59
  /!\[([^\[]+)\]\(([^\)]+)\)/ => '<command_break>{"command":"img", "alt":"\1", "path":"\2"}<command_break>',
33
- /^> (.+)/ => '<command_break>{"command":"quote","margin":20,"text":"<font size=\'QUOTE_SIZE\'>\\1</font>"}<command_break>', # Quote
60
+ /^> (.+)/ => '<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
34
61
 
35
62
  # Stuff to process last
36
63
  /\[([^\[]+)\]\(([^\)]+)\)/ => '<link href="\2">\1</link>', # Link
@@ -57,6 +84,23 @@ module PrawndownExt
57
84
  end
58
85
 
59
86
  def replace_options text
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
96
+ end
97
+
98
+ # remove quote spacing if it doesnt exist
99
+ if @options["quote_font_spacing"].nil?
100
+ text = text.gsub("character_spacing='QUOTE_FONT_SPACING' ", "")
101
+ @options.delete("quote_font_spacing")
102
+ end
103
+
60
104
  DEFAULT_OPTIONS.keys.each do |replacer|
61
105
  if @options.key?(replacer)
62
106
  text = text.gsub(replacer.upcase, @options[replacer].to_s)
@@ -75,7 +119,7 @@ module PrawndownExt
75
119
  result = _match.inject(@text) do |final_string, (markdown_matcher, prawn_tag)|
76
120
  final_string.gsub(markdown_matcher, prawn_tag)
77
121
  end
78
-
122
+
79
123
  result = replace_options result
80
124
 
81
125
  result = result.split("<command_break>")
@@ -2,6 +2,6 @@
2
2
 
3
3
  module PrawndownExt
4
4
  module Ext
5
- VERSION = "0.1.5"
5
+ VERSION = "0.1.7"
6
6
  end
7
7
  end
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 args, pdf
41
-
54
+ def exec args, pdf, options
42
55
  if args.key?("command")
43
56
  if COMMAND.include?(args["command"])
44
- COMMAND[args["command"]].call(args, pdf)
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: nil)
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
- CommandInterface.new.exec JSON.parse(output.strip), self
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
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.5
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - PunishedFelix