prawndown-ext 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 118fcc0fe7bfc07e14992421fdfdaf3e2f2202a5d4f9b5c7436825191859f7cb
4
+ data.tar.gz: c552ea7ed3617c370900cf70c72a48f9f2b9a248679655ed6f21be19c94422e7
5
+ SHA512:
6
+ metadata.gz: 600aa7e6bdd7dad7d8e3ea132ae5379288ebed17ef97a54c7d63b84b9a20708aeda504696f71d29f5b7cc05442c4238633b042e9e1049c3681476f5b035464ff
7
+ data.tar.gz: f6064acfb29cbd45cfd0e5d192ea2dbe802789e256d32c68d1b1e3b9d9aee4c0d886ed97762eeb57f9d977390b23a566b8a6463090145f9cbbf3ec1f4bacf11f
@@ -0,0 +1,83 @@
1
+ module PrawndownExt
2
+ # Markdown to Prawn parser
3
+ class Parser
4
+ MATCHERS = {
5
+ #/^> (.+)/ => '<quote>\1</quote>', # Quote
6
+ /^# (.+)/ => '<font size="HEADER1_SIZE"><b>\1</b></font>', # Header 1
7
+ /^## (.+)/ => '<font size="HEADER2_SIZE"><b>\1</b></font>', # Header 2
8
+ /^### (.+)/ => '<font size="HEADER3_SIZE"><b>\1</b></font>', # Header 3
9
+ /^#### (.+)/ => '<font size="HEADER4_SIZE"><b>\1</b></font>', # Header 4
10
+ /^##### (.+)/ => '<font size="HEADER5_SIZE"><b>\1</b></font>', # Header 5
11
+ /^###### (.+)/ => '<font size="HEADER6_SIZE"><b>\1</b></font>', # Header 6
12
+ #/!\[([^\[]+)\]\(([^\)]+)\)/ => '<img href="\2" alt="\1">', # Image
13
+ /!\[([^\[]+)\]\(([^\)]+)\)/ => '', # Images removed for now
14
+ /<iframe ([^\[]+)<\/iframe>/ => '', # Embeds are just removed
15
+ /\[([^\[]+)\]\(([^\)]+)\)/ => '<link href="\2">\1</link>', # Link
16
+ /(\*\*|__)(.*?)\1/ => '<b>\2</b>', # Bold
17
+ /(\*|_)(.*?)\1/ => '<i>\2</i>', # Italic
18
+ /\~\~(.*?)\~\~/ => '<strikethrough>\1</strikethrough>' # Strikethrough
19
+ }
20
+
21
+ # Initialize a new +Prawndown::Parser+.
22
+ # +text+ must a a valid Markdown string that only contains supported tags.
23
+ #
24
+ # Supported tags are: Header 1-6, bold, italic, strikethrough and link.
25
+ def initialize(text, options)
26
+
27
+ @text = text.to_s
28
+ @header1_size = 28
29
+ @header2_size = 24
30
+ @header3_size = 20
31
+ @header4_size = 18
32
+ @header5_size = 16
33
+ @header6_size = 14
34
+
35
+ if !options.nil?
36
+ if options.key?(:header1_size)
37
+ @header1_size = options[:header1_size]
38
+ end
39
+ if options.key?(:header2_size)
40
+ @header2_size = options[:header2_size]
41
+ end
42
+ if options.key?(:header3_size)
43
+ @header3_size = options[:header3_size]
44
+ end
45
+ if options.key?(:header4_size)
46
+ @header4_size = options[:header4_size]
47
+ end
48
+ if options.key?(:header5_size)
49
+ @header5_size = options[:header5_size]
50
+ end
51
+ if options.key?(:header6_size)
52
+ @header6_size = options[:header6_size]
53
+ end
54
+ end
55
+
56
+ end
57
+
58
+ # Parses the Markdown text and outputs a Prawn compatible string
59
+ def to_prawn
60
+
61
+ # variable replacement
62
+ _match = Marshal.load(Marshal.dump(MATCHERS))
63
+
64
+ _match.each {
65
+ |x| puts
66
+ x[1].gsub!("HEADER1_SIZE", @header1_size.to_s)
67
+ x[1].gsub!("HEADER2_SIZE", @header2_size.to_s)
68
+ x[1].gsub!("HEADER3_SIZE", @header3_size.to_s)
69
+ x[1].gsub!("HEADER4_SIZE", @header4_size.to_s)
70
+ x[1].gsub!("HEADER5_SIZE", @header5_size.to_s)
71
+ x[1].gsub!("HEADER6_SIZE", @header6_size.to_s)
72
+ }
73
+
74
+ result = _match.inject(@text) do |final_string, (markdown_matcher, prawn_tag)|
75
+ final_string.gsub(markdown_matcher, prawn_tag)
76
+ end
77
+
78
+
79
+ result
80
+
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrawndownExt
4
+ module Ext
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,22 @@
1
+ require 'prawn'
2
+ require "prawndown/version"
3
+ require "prawndown/parser"
4
+
5
+ module PrawndownExt
6
+ module Interface
7
+ # Renders Markdown in the current document
8
+ #
9
+ # It supports header 1-6, bold text, italic text, strikethrough and links
10
+ # It supports the same options as +Prawn::Document#text+
11
+ #
12
+ # Prawn::Document.generate('markdown.pdf') do
13
+ # markdown '# Welcome to Prawndown!'
14
+ # markdown '**Important:** We _hope_ you enjoy your stay!'
15
+ # end
16
+ def markdown(string, options: nil)
17
+ text PrawndownExt::Parser.new(string, options).to_prawn, options.merge(inline_format: true)
18
+ end
19
+ end
20
+ end
21
+
22
+ Prawn::Document.extensions << PrawndownExt::Interface
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prawndown-ext
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - PunishedFelix
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: prawn
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.5'
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: 2.5.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '2.5'
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 2.5.0
32
+ description: Extension of Prawndown to include additional Markdown features. Currently
33
+ supports custom header sizes, removing iframe content. Need to write more to make
34
+ images work.
35
+ email:
36
+ - labadore1844@gmail.com
37
+ executables: []
38
+ extensions: []
39
+ extra_rdoc_files: []
40
+ files:
41
+ - lib/prawndown-ext.rb
42
+ - lib/prawndown/parser.rb
43
+ - lib/prawndown/version.rb
44
+ homepage: https://github.com/badgernested/prawndown-ext
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 3.1.0
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.6.9
63
+ specification_version: 4
64
+ summary: Extension of Prawndown to include additional features.
65
+ test_files: []