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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: add25cd5ac828d350ccf78f66898be73e08697a8cae07ade7eed136477665d1a
4
- data.tar.gz: 25dfd70414948ad46abd0436b94b1c5283391a2be26ae9f87a7fdcf1238b97eb
3
+ metadata.gz: 1bd6519db873bccdf6e70d074fef2de91fed77183cf0bc3d5c98c7141a9ed401
4
+ data.tar.gz: 6c1437518072c4a8f4e638de7d4d2afb57f9486118393d426ca72a37b5295e55
5
5
  SHA512:
6
- metadata.gz: d8fb1a470a66603332ab42a70b010547775d078497fd79bf060349f232fae89b99fc6f83a841331b79b98d71e0e3054737141b6c71e4c098799bee2dcf854b3c
7
- data.tar.gz: e90f324a7f615941a5ed9c066f3e88ebd6f9094ee6cdf20bf0f92b7affa0b77b4cadcf6b3d4aa2ff68e602e8f8a884f8019596106dc99f8111de7e4dfa0938b7
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,
@@ -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 quote font if it doesnt exist
64
- if @options["quote_font"].nil?
65
- text = text.gsub("name='QUOTE_FONT' ", "")
66
- @options.delete("quote_font")
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>")
@@ -2,6 +2,6 @@
2
2
 
3
3
  module PrawndownExt
4
4
  module Ext
5
- VERSION = "0.1.6"
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.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - PunishedFelix