### Home Classes Methods

### In Files

- lib/prawn/text/formatted/parser.rb

### Parent

Object

### Methods

- ::to_array
- ::to_string

### Files

- COPYING
- GPLv2
- GPLv3
- LICENSE

### Class/Module Index

Quicksearch

- Prawn
- Prawn::Core
- Prawn::Core::Page
- Prawn::Core::Page::GraphicsState
- Prawn::Core::Reference
- Prawn::Document
- Prawn::Document::BoundingBox
- Prawn::Document::ColumnBox
- Prawn::Document::GraphicsState
- Prawn::Document::Grid
- Prawn::Document::Internals
- Prawn::Document::PageGeometry
- Prawn::Document::Security
- Prawn::Document::Snapshot
- Prawn::Encoding
- Prawn::Errors
- Prawn::Font
- Prawn::Font::AFM
- Prawn::Font::DFont
- Prawn::Font::TTF
- Prawn::GraphicState
- Prawn::GraphicStateStack
- Prawn::Graphics
- Prawn::Graphics::CapStyle
- Prawn::Graphics::Color
- Prawn::Graphics::Dash
- Prawn::Graphics::Gradient
- Prawn::Graphics::JoinStyle
- Prawn::Graphics::Transformation
- Prawn::Graphics::Transparency
- Prawn::Images
- Prawn::Images::JPG
- Prawn::Images::PNG
- Prawn::Layout
- Prawn::Measurements
- Prawn::Outline
- Prawn::Stamp
- Prawn::Table
- Prawn::Table::Cell
- Prawn::Table::Cell::InTable
- Prawn::Table::Cell::Subtable
- Prawn::Table::Cell::Text
- Prawn::Table::Cells
- Prawn::Text
- Prawn::Text::Box
- Prawn::Text::Formatted
- Prawn::Text::Formatted::Box
- Prawn::Text::Formatted::Fragment
- Prawn::Text::Formatted::Parser
- Arcfour
- Numeric

No matching classes.

# Prawn::Text::Formatted::Parser

### Public Class Methods

to_array(string) click to toggle source

    # File lib/prawn/text/formatted/parser.rb, line 16
    def self.to_array(string)
      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]+"
      regex = Regexp.new(regex_string, Regexp::MULTILINE)
      tokens = string.scan(regex)
      self.array_from_tokens(tokens)
    end

to_string(array) click to toggle source

    # File lib/prawn/text/formatted/parser.rb, line 36
    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("&", "&amp;").gsub(">", "&gt;").gsub("<", "&lt;")
        prefix + string + suffix
      end.join
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.