Object
# File lib/prawn/text/formatted/parser.rb, line 17
17: def self.to_array(string)
18: regex_string = "\n|" +
19: "<b>|</b>|" +
20: "<i>|</i>|" +
21: "<u>|</u>|" +
22: "<strikethrough>|</strikethrough>|" +
23: "<sub>|</sub>|" +
24: "<sup>|</sup>|" +
25: "<link[^>]*>|</link>|" +
26: "<color[^>]*>|</color>|" +
27: "<font[^>]*>|</font>|" +
28: "<strong>|</strong>|" +
29: "<em>|</em>|" +
30: "<a[^>]*>|</a>|" +
31: "[^<\n]+"
32: regex = Regexp.new(regex_string, Regexp::MULTILINE)
33: tokens = string.scan(regex)
34: self.array_from_tokens(tokens)
35: end
# File lib/prawn/text/formatted/parser.rb, line 37
37: def self.to_string(array)
38: prefixes = { :bold => "<b>",
39: :italic => "<i>",
40: :underline => "<u>",
41: :strikethrough => "<strikethrough>",
42: :subscript => "<sub>",
43: :superscript => "<sup>" }
44: suffixes = { :bold => "</b>",
45: :italic => "</i>",
46: :underline => "</u>",
47: :strikethrough => "</strikethrough>",
48: :subscript => "</sub>",
49: :superscript => "</sup>" }
50: array.collect do |hash|
51: prefix = ""
52: suffix = ""
53: if hash[:styles]
54: hash[:styles].each do |style|
55: prefix = prefix + prefixes[style]
56: suffix = suffixes[style] + suffix
57: end
58: end
59:
60: font = hash[:font] ? " name='#{hash[:font]}'" : nil
61: size = hash[:size] ? " size='#{hash[:size]}'" : nil
62: if hash[:character_spacing]
63: character_spacing = " character_spacing='#{hash[:character_spacing]}'"
64: else
65: character_spacing = nil
66: end
67: if font || size || character_spacing
68: prefix = prefix + "<font#{font}#{size}#{character_spacing}>"
69: suffix = "</font>"
70: end
71:
72: link = hash[:link] ? " href='#{hash[:link]}'" : nil
73: anchor = hash[:anchor] ? " anchor='#{hash[:anchor]}'" : nil
74: if link || anchor
75: prefix = prefix + "<link#{link}#{anchor}>"
76: suffix = "</link>"
77: end
78:
79: if hash[:color]
80: if hash[:color].kind_of?(Array)
81: prefix = prefix + "<color c='#{hash[:color][0]}'" +
82: " m='#{hash[:color][1]}'" +
83: " y='#{hash[:color][2]}'" +
84: " k='#{hash[:color][3]}'>"
85: else
86: prefix = prefix + "<color rgb='#{hash[:color]}'>"
87: end
88: suffix = "</color>"
89: end
90:
91: string = hash[:text].gsub("&", "&").gsub(">", ">").gsub("<", "<")
92: prefix + string + suffix
93: end.join
94: end
# File lib/prawn/text/formatted/parser.rb, line 119
119: def self.array_from_tokens(tokens)
120: array = []
121: styles = []
122: colors = []
123: link = nil
124: anchor = nil
125: fonts = []
126: sizes = []
127: character_spacings = []
128:
129: while token = tokens.shift
130: case token
131: when "<b>", "<strong>"
132: styles << :bold
133: when "<i>", "<em>"
134: styles << :italic
135: when "<u>"
136: styles << :underline
137: when "<strikethrough>"
138: styles << :strikethrough
139: when "<sub>"
140: styles << :subscript
141: when "<sup>"
142: styles << :superscript
143: when "</b>", "</strong>"
144: styles.delete(:bold)
145: when "</i>", "</em>"
146: styles.delete(:italic)
147: when "</u>"
148: styles.delete(:underline)
149: when "</strikethrough>"
150: styles.delete(:strikethrough)
151: when "</sub>"
152: styles.delete(:subscript)
153: when "</sup>"
154: styles.delete(:superscript)
155: when "</link>", "</a>"
156: link = nil
157: anchor = nil
158: when "</color>"
159: colors.pop
160: when "</font>"
161: fonts.pop
162: sizes.pop
163: character_spacings.pop
164: else
165: if token =~ /^<link[^>]*>$/ or token =~ /^<a[^>]*>$/
166: matches = /href="([^"]*)"/.match(token) || /href='([^']*)'/.match(token)
167: link = matches[1] unless matches.nil?
168:
169: matches = /anchor="([^"]*)"/.match(token) || /anchor='([^']*)'/.match(token)
170: anchor = matches[1] unless matches.nil?
171: elsif token =~ /^<color[^>]*>$/
172: matches = /rgb="#?([^"]*)"/.match(token) || /rgb='#?([^']*)'/.match(token)
173: colors << matches[1] if matches
174:
175: matches = /c="#?([^"]*)" +m="#?([^"]*)" +y="#?([^"]*)" +k="#?([^"]*)"/.match(token) ||
176: /c='#?([^']*)' +m='#?([^']*)' +y='#?([^']*)' +k='#?([^']*)'/.match(token)
177: colors << [matches[1].to_i, matches[2].to_i, matches[3].to_i, matches[4].to_i] if matches
178:
179: # intend to support rgb="#ffffff" or rgb='#ffffff',
180: # r="255" g="255" b="255" or r='255' g='255' b='255',
181: # and c="100" m="100" y="100" k="100" or
182: # c='100' m='100' y='100' k='100'
183: # color = { :rgb => "#ffffff" }
184: # color = { :r => 255, :g => 255, :b => 255 }
185: # color = { :c => 100, :m => 100, :y => 100, :k => 100 }
186: elsif token =~ /^<font[^>]*>$/
187: matches = /name="([^"]*)"/.match(token) || /name='([^']*)'/.match(token)
188: fonts << matches[1] unless matches.nil?
189:
190: matches = /size="([^"]*)"/.match(token) || /size='([^']*)'/.match(token)
191: sizes << matches[1].to_f unless matches.nil?
192:
193: matches = /character_spacing="([^"]*)"/.match(token) || /character_spacing='([^']*)'/.match(token)
194: character_spacings << matches[1].to_f unless matches.nil?
195: else
196: string = token.gsub("<", "<").gsub(">", ">").gsub("&", "&")
197: array << { :text => string,
198: :styles => styles.dup,
199: :color => colors.last,
200: :link => link,
201: :anchor => anchor,
202: :font => fonts.last,
203: :size => sizes.last,
204: :character_spacing => character_spacings.last }
205: end
206: end
207: end
208: array
209: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.