Class: Prawn::Text::Formatted::Parser
- Inherits:
-
Object
- Object
- Prawn::Text::Formatted::Parser
- Defined in:
- lib/prawn/text/formatted/parser.rb
Constant Summary collapse
- PARSER_REGEX =
begin regex_string = "\n|" + "<b>|</b>|" + "<i>|</i>|" + "<u>|</u>|" + "<strikethrough>|</strikethrough>|" + "<sub>|</sub>|" + "<sup>|</sup>|" + "<link[^>]*>|</link>|" + "<color[^>]*>|</color>|" + "<font[^>]*>|</font>|" + "<strong>|</strong>|" + "<em>|</em>|" + "<a[^>]*>|</a>|" + "[^<\n]+" Regexp.new(regex_string, Regexp::MULTILINE) end
Class Method Summary collapse
-
.array_paragraphs(array) ⇒ Object
:nodoc:.
- .format(string, *args) ⇒ Object
- .to_string(array) ⇒ Object
Class Method Details
.array_paragraphs(array) ⇒ Object
:nodoc:
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/prawn/text/formatted/parser.rb', line 98 def self.array_paragraphs(array) #:nodoc: paragraphs = [] paragraph = [] previous_string = "\n" scan_pattern = /[^\n]+|\n/ array.each do |hash| hash[:text].scan(scan_pattern).each do |string| if string == "\n" paragraph << hash.dup.merge(:text => "\n") if previous_string == "\n" paragraphs << paragraph unless paragraph.empty? paragraph = [] else paragraph << hash.dup.merge(:text => string) end previous_string = string end end paragraphs << paragraph unless paragraph.empty? paragraphs end |
.format(string, *args) ⇒ Object
34 35 36 37 |
# File 'lib/prawn/text/formatted/parser.rb', line 34 def self.format(string, *args) tokens = string.gsub(/<br\s*\/?>/, "\n").scan(PARSER_REGEX) self.array_from_tokens(tokens) end |
.to_string(array) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/prawn/text/formatted/parser.rb', line 39 def self.to_string(array) prefixes = { :bold => "<b>", :italic => "<i>", :underline => "<u>", :strikethrough => "<strikethrough>", :subscript => "<sub>", :superscript => "<sup>" } suffixes = { :bold => "</b>", :italic => "</i>", :underline => "</u>", :strikethrough => "</strikethrough>", :subscript => "</sub>", :superscript => "</sup>" } array.collect do |hash| prefix = "" suffix = "" if hash[:styles] hash[:styles].each do |style| prefix = prefix + prefixes[style] suffix = suffixes[style] + suffix end end font = hash[:font] ? " name='#{hash[:font]}'" : nil size = hash[:size] ? " size='#{hash[:size]}'" : nil if hash[:character_spacing] character_spacing = " character_spacing='#{hash[:character_spacing]}'" else character_spacing = nil end if font || size || character_spacing prefix = prefix + "<font#{font}#{size}#{character_spacing}>" suffix = "</font>" end link = hash[:link] ? " href='#{hash[:link]}'" : nil anchor = hash[:anchor] ? " anchor='#{hash[:anchor]}'" : nil if link || anchor prefix = prefix + "<link#{link}#{anchor}>" suffix = "</link>" end if hash[:color] if hash[:color].kind_of?(Array) prefix = prefix + "<color c='#{hash[:color][0]}'" + " m='#{hash[:color][1]}'" + " y='#{hash[:color][2]}'" + " k='#{hash[:color][3]}'>" else prefix = prefix + "<color rgb='#{hash[:color]}'>" end suffix = "</color>" end string = hash[:text].gsub("&", "&").gsub(">", ">").gsub("<", "<") prefix + string + suffix end.join end |