Class: Prawn::Text::Formatted::Parser
- Inherits:
-
Object
- Object
- Prawn::Text::Formatted::Parser
- Defined in:
- lib/prawn/text/formatted/parser.rb
Overview
Implements a bi-directional parser between a subset of html and formatted text arrays.
Extension API collapse
-
.escape(text) ⇒ String
Escape characters that can interfere with inline format parsing.
-
.format(string, *_args) ⇒ Array<Hash>
Parse formatted string.
-
.to_string(array) ⇒ String
Serialize text fragments to an inline format string.
-
.unescape(text) ⇒ String
Unescape characters that can interfere with inline format parsing.
Class Method Details
.escape(text) ⇒ String
Escape characters that can interfere with inline format parsing.
Source Code
lib/prawn/text/formatted/parser.rb, line 270
270 | def self.escape(text) |
271 | text.gsub(Regexp.union(ESCAPE_CHARS.keys), ESCAPE_CHARS) |
272 | end
|
.format(string, *_args) ⇒ Array<Hash>
Parse formatted string.
Source Code
lib/prawn/text/formatted/parser.rb, line 48
48 | def self.format(string, *_args) |
49 | tokens = string.gsub(%r{<br\s*/?>}, "\n").scan(PARSER_REGEX) |
50 | array_from_tokens(tokens) |
51 | end
|
.to_string(array) ⇒ String
Serialize text fragments to an inline format string.
Source Code
lib/prawn/text/formatted/parser.rb, line 57
57 | def self.to_string(array) |
58 | prefixes = { |
59 | bold: '<b>', |
60 | italic: '<i>', |
61 | underline: '<u>', |
62 | strikethrough: '<strikethrough>', |
63 | subscript: '<sub>', |
64 | superscript: '<sup>', |
65 | }
|
66 | suffixes = { |
67 | bold: '</b>', |
68 | italic: '</i>', |
69 | underline: '</u>', |
70 | strikethrough: '</strikethrough>', |
71 | subscript: '</sub>', |
72 | superscript: '</sup>', |
73 | }
|
74 | array
|
75 | .map { |hash| |
76 | prefix = '' |
77 | suffix = '' |
78 | hash[:styles]&.each do |style| |
79 | prefix += prefixes[style] |
80 | suffix = suffixes[style] + suffix |
81 | end
|
82 | |
83 | font = hash[:font] ? " name='#{hash[:font]}'" : nil |
84 | size = hash[:size] ? " size='#{hash[:size]}'" : nil |
85 | character_spacing = |
86 | if hash[:character_spacing] |
87 | " character_spacing='#{hash[:character_spacing]}'" |
88 | end
|
89 | if font || size || character_spacing |
90 | prefix += "<font#{font}#{size}#{character_spacing}>" |
91 | suffix = '</font>' |
92 | end
|
93 | |
94 | link = hash[:link] ? " href='#{hash[:link]}'" : nil |
95 | anchor = hash[:anchor] ? " anchor='#{hash[:anchor]}'" : nil |
96 | if link || anchor |
97 | prefix += "<link#{link}#{anchor}>" |
98 | suffix = '</link>' |
99 | end
|
100 | |
101 | if hash[:color] |
102 | prefix += |
103 | if hash[:color].is_a?(Array) |
104 | "<color c='#{hash[:color][0]}' " \ |
105 | "m='#{hash[:color][1]}' " \ |
106 | "y='#{hash[:color][2]}' " \ |
107 | "k='#{hash[:color][3]}'>" |
108 | else
|
109 | "<color rgb='#{hash[:color]}'>" |
110 | end
|
111 | suffix = '</color>' |
112 | end
|
113 | |
114 | string = escape(hash[:text]) |
115 | prefix + string + suffix |
116 | }
|
117 | .join |
118 | end
|
.unescape(text) ⇒ String
Unescape characters that can interfere with inline format parsing.
Source Code
lib/prawn/text/formatted/parser.rb, line 278
278 | def self.unescape(text) |
279 | text.gsub(Regexp.union(UNESCAPE_CHARS.keys), UNESCAPE_CHARS) |
280 | end
|