Class: Prawn::Text::Formatted::Parser

Inherits:
Object
  • Object
show all
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

Class Method Details

.escape(text) ⇒ String

Escape characters that can interfere with inline format parsing.

Parameters:

  • text (String)

Returns:

  • (String)


270
271
272
# File 'lib/prawn/text/formatted/parser.rb', line 270

def self.escape(text)
  text.gsub(Regexp.union(ESCAPE_CHARS.keys), ESCAPE_CHARS)
end

.format(string, *_args) ⇒ Array<Hash>

Parse formatted string.

Parameters:

  • string (String)

Returns:

  • (Array<Hash>)

    Text fragments.



48
49
50
51
# File 'lib/prawn/text/formatted/parser.rb', line 48

def self.format(string, *_args)
  tokens = string.gsub(%r{<br\s*/?>}, "\n").scan(PARSER_REGEX)
  array_from_tokens(tokens)
end

.to_string(array) ⇒ String

Serialize text fragments to an inline format string.

Parameters:

  • array (Array<Hash>)

Returns:

  • (String)


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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/prawn/text/formatted/parser.rb', line 57

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
    .map { |hash|
      prefix = ''
      suffix = ''
      hash[:styles]&.each do |style|
        prefix += prefixes[style]
        suffix = suffixes[style] + suffix
      end

      font = hash[:font] ? " name='#{hash[:font]}'" : nil
      size = hash[:size] ? " size='#{hash[:size]}'" : nil
      character_spacing =
        if hash[:character_spacing]
          " character_spacing='#{hash[:character_spacing]}'"
        end
      if font || size || character_spacing
        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 += "<link#{link}#{anchor}>"
        suffix = '</link>'
      end

      if hash[:color]
        prefix +=
          if hash[:color].is_a?(Array)
            "<color c='#{hash[:color][0]}' " \
              "m='#{hash[:color][1]}' " \
              "y='#{hash[:color][2]}' " \
              "k='#{hash[:color][3]}'>"
          else
            "<color rgb='#{hash[:color]}'>"
          end
        suffix = '</color>'
      end

      string = escape(hash[:text])
      prefix + string + suffix
    }
    .join
end

.unescape(text) ⇒ String

Unescape characters that can interfere with inline format parsing.

Parameters:

  • text (String)

Returns:

  • (String)


278
279
280
# File 'lib/prawn/text/formatted/parser.rb', line 278

def self.unescape(text)
  text.gsub(Regexp.union(UNESCAPE_CHARS.keys), UNESCAPE_CHARS)
end