Text::Format for Ruby is copyright 2002 - 2005 by Austin Ziegler. It is available under Ruby’s licence, the Perl Artistic licence, or the GNU GPL version 2 (or at your option, any later version). As a special exception, for use with official Rails (provided by the rubyonrails.org development team) and any project created with official Rails, the following alternative MIT-style licence may be used:

Text::Format Licence for Rails and Rails Applications

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

  • The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Methods
Classes and Modules
Class Text::Format::SplitWord
Constants
VERSION = '0.63'
ABBREV = [ 'Mr', 'Mrs', 'Ms', 'Jr', 'Sr' ]
  Local abbreviations. More can be added with Text::Format.abbreviations
LEFT_ALIGN = 0
  Formatting values
RIGHT_ALIGN = 1
RIGHT_FILL = 2
JUSTIFY = 3
SPLIT_FIXED = 1
  Word split modes (only applies when hard_margins is true).
SPLIT_CONTINUATION = 2
SPLIT_HYPHENATION = 4
SPLIT_CONTINUATION_FIXED = SPLIT_CONTINUATION | SPLIT_FIXED
SPLIT_HYPHENATION_FIXED = SPLIT_HYPHENATION | SPLIT_FIXED
SPLIT_HYPHENATION_CONTINUATION = SPLIT_HYPHENATION | SPLIT_CONTINUATION
SPLIT_ALL = SPLIT_HYPHENATION | SPLIT_CONTINUATION | SPLIT_FIXED
LEQ_RE = /[.?!]['"]?$/
Attributes
[RW] abbreviations Defines the current abbreviations as an array. This is only used if extra_space is turned on.

If one is abbreviating "President" as "Pres." (abbreviations = ["Pres"]), then the results of formatting will be as illustrated in the table below:

      extra_space  |  include?        |  !include?
        true       |  Pres. Lincoln   |  Pres.  Lincoln
        false      |  Pres. Lincoln   |  Pres. Lincoln
Default:{}
Used in:format, paragraphs
[R] body_indent The number of spaces to indent all lines after the first line of a paragraph.
                            columns
 <-------------------------------------------------------------->
 <-----------><------><---------------------------><------------>
  left margin  INDENT  text is formatted into here  right margin
Default:0
Used in:format, paragraphs
[R] columns The total width of the format area. The margins, indentation, and text are formatted into this space.
                            COLUMNS
 <-------------------------------------------------------------->
 <-----------><------><---------------------------><------------>
  left margin  indent  text is formatted into here  right margin
Default:72
Used in:format, paragraphs, center
[RW] extra_space Indicates whether sentence terminators should be followed by a single space (false), or two spaces (true).
Default:false
Used in:format, paragraphs
[R] first_indent The number of spaces to indent the first line of a paragraph.
                            columns
 <-------------------------------------------------------------->
 <-----------><------><---------------------------><------------>
  left margin  INDENT  text is formatted into here  right margin
Default:4
Used in:format, paragraphs
[R] format_style Specifies the format style. Allowable values are:
LEFT_ALIGN
Left justified, ragged right.
     |A paragraph that is|
     |left aligned.|
RIGHT_ALIGN
Right justified, ragged left.
     |A paragraph that is|
     |     right aligned.|
RIGHT_FILL
Left justified, right ragged, filled to width by spaces. (Essentially the same as LEFT_ALIGN except that lines are padded on the right.)
     |A paragraph that is|
     |left aligned.      |
JUSTIFY
Fully justified, words filled to width by spaces, except the last line.
     |A paragraph  that|
     |is     justified.|
Default:Text::Format::LEFT_ALIGN
Used in:format, paragraphs
[RW] hard_margins Normally, words larger than the format area will be placed on a line by themselves. Setting this to true will force words larger than the format area to be split into one or more "words" each at most the size of the format area. The first line and the original word will be placed into split_words. Note that this will cause the output to look similar to a format_style of JUSTIFY. (Lines will be filled as much as possible.)
Default:false
Used in:format, paragraphs
[R] hyphenator The object responsible for hyphenating. It must respond to hyphenate_to(word, size) or hyphenate_to(word, size, formatter) and return an array of the word split into two parts; if there is a hyphenation mark to be applied, responsibility belongs to the hyphenator object. The size is the MAXIMUM size permitted, including any hyphenation marks. If the hyphenate_to method has an arity of 3, the formatter will be provided to the method. This allows the hyphenator to make decisions about the hyphenation based on the formatting rules.
Default:nil
Used in:format, paragraphs
[R] left_margin The number of spaces used for the left margin.
                            columns
 <-------------------------------------------------------------->
 <-----------><------><---------------------------><------------>
  LEFT MARGIN  indent  text is formatted into here  right margin
Default:0
Used in:format, paragraphs, center
[RW] nobreak Indicates whether or not the non-breaking space feature should be used.
Default:false
Used in:format, paragraphs
[RW] nobreak_regex A hash which holds the regular expressions on which spaces should not be broken. The hash is set up such that the key is the first word and the value is the second word.

For example, if nobreak_regex contains the following hash:

  { '^Mrs?\.$' => '\S+$', '^\S+$' => '^(?:S|J)r\.$'}

Then "Mr. Jones", "Mrs. Jones", and "Jones Jr." would not be broken. If this simple matching algorithm indicates that there should not be a break at the current end of line, then a backtrack is done until there are two words on which line breaking is permitted. If two such words are not found, then the end of the line will be broken regardless. If there is a single word on the current line, then no backtrack is done and the word is stuck on the end.

Default:{}
Used in:format, paragraphs
[R] right_margin The number of spaces used for the right margin.
                            columns
 <-------------------------------------------------------------->
 <-----------><------><---------------------------><------------>
  left margin  indent  text is formatted into here  RIGHT MARGIN
Default:0
Used in:format, paragraphs, center
[R] split_rules Specifies the split mode; used only when hard_margins is set to true. Allowable values are:
SPLIT_FIXED
The word will be split at the number of characters needed, with no marking at all.
     repre
     senta
     ion
SPLIT_CONTINUATION
The word will be split at the number of characters needed, with a C-style continuation character. If a word is the only item on a line and it cannot be split into an appropriate size, SPLIT_FIXED will be used.
      repr\
      esen\
      tati\
      on
SPLIT_HYPHENATION
The word will be split according to the hyphenator specified in hyphenator. If there is no hyphenator specified, works like SPLIT_CONTINUATION. The example is using TeX::Hyphen. If a word is the only item on a line and it cannot be split into an appropriate size, SPLIT_CONTINUATION mode will be used.
      rep-
      re-
      sen-
      ta-
      tion
Default:Text::Format::SPLIT_FIXED
Used in:format, paragraphs
[R] split_words An array of words split during formatting if hard_margins is set to true.
  #split_words << Text::Format::SplitWord.new(word, first, rest)
[R] tabstop Indicates the number of spaces that a single tab represents.
Default:8
Used in:expand, unexpand, paragraphs
[RW] tag_paragraph Indicates whether the formatting of paragraphs should be done with tagged paragraphs. Useful only with tag_text.
Default:false
Used in:format, paragraphs
[RW] tag_text The array of text to be placed before each paragraph when tag_paragraph is true. When format() is called, only the first element of the array is used. When paragraphs is called, then each entry in the array will be used once, with corresponding paragraphs. If the tag elements are exhausted before the text is exhausted, then the remaining paragraphs will not be tagged. Regardless of indentation settings, a blank line will be inserted between all paragraphs when tag_paragraph is true.
Default:[]
Used in:format, paragraphs
[RW] text The text to be manipulated. Note that value is optional, but if the formatting functions are called without values, this text is what will be formatted.
Default:[]
Used in:All methods
Public Class methods
new(arg = nil, &block)

This constructor takes advantage of a technique for Ruby object construction introduced by Andy Hunt and Dave Thomas (see reference), where optional values are set using commands in a block.

  Text::Format.new {
      columns         = 72
      left_margin     = 0
      right_margin    = 0
      first_indent    = 4
      body_indent     = 0
      format_style    = Text::Format::LEFT_ALIGN
      extra_space     = false
      abbreviations   = {}
      tag_paragraph   = false
      tag_text        = []
      nobreak         = false
      nobreak_regex   = {}
      tabstop         = 8
      text            = nil
  }

As shown above, arg is optional. If arg is specified and is a String, then arg is used as the default value of text. Alternately, an existing Text::Format object can be used or a Hash can be used. With all forms, a block can be specified.

Reference:"Object Construction and Blocks" <www.pragmaticprogrammer.com/ruby/articles/insteval.html>
      # File lib/action_mailer/vendor/text/format.rb, line 963
 963:     def initialize(arg = nil, &block)
 964:       case arg
 965:       when Text::Format
 966:         __create(arg.text) do
 967:           @columns        = arg.columns
 968:           @tabstop        = arg.tabstop
 969:           @first_indent   = arg.first_indent
 970:           @body_indent    = arg.body_indent
 971:           @format_style   = arg.format_style
 972:           @left_margin    = arg.left_margin
 973:           @right_margin   = arg.right_margin
 974:           @extra_space    = arg.extra_space
 975:           @tag_paragraph  = arg.tag_paragraph
 976:           @tag_text       = arg.tag_text
 977:           @abbreviations  = arg.abbreviations
 978:           @nobreak        = arg.nobreak
 979:           @nobreak_regex  = arg.nobreak_regex
 980:           @text           = arg.text
 981:           @hard_margins   = arg.hard_margins
 982:           @split_words    = arg.split_words
 983:           @split_rules    = arg.split_rules
 984:           @hyphenator     = arg.hyphenator
 985:         end
 986:         instance_eval(&block) unless block.nil?
 987:       when Hash
 988:         __create do
 989:           @columns       = arg[:columns]       || arg['columns']       || @columns
 990:           @tabstop       = arg[:tabstop]       || arg['tabstop']       || @tabstop
 991:           @first_indent  = arg[:first_indent]  || arg['first_indent']  || @first_indent
 992:           @body_indent   = arg[:body_indent]   || arg['body_indent']   || @body_indent
 993:           @format_style  = arg[:format_style]  || arg['format_style']  || @format_style
 994:           @left_margin   = arg[:left_margin]   || arg['left_margin']   || @left_margin
 995:           @right_margin  = arg[:right_margin]  || arg['right_margin']  || @right_margin
 996:           @extra_space   = arg[:extra_space]   || arg['extra_space']   || @extra_space
 997:           @text          = arg[:text]          || arg['text']          || @text
 998:           @tag_paragraph = arg[:tag_paragraph] || arg['tag_paragraph'] || @tag_paragraph
 999:           @tag_text      = arg[:tag_text]      || arg['tag_text']      || @tag_text
1000:           @abbreviations = arg[:abbreviations] || arg['abbreviations'] || @abbreviations
1001:           @nobreak       = arg[:nobreak]       || arg['nobreak']       || @nobreak
1002:           @nobreak_regex = arg[:nobreak_regex] || arg['nobreak_regex'] || @nobreak_regex
1003:           @hard_margins  = arg[:hard_margins]  || arg['hard_margins']  || @hard_margins
1004:           @split_rules   = arg[:split_rules] || arg['split_rules'] || @split_rules
1005:           @hyphenator    = arg[:hyphenator] || arg['hyphenator'] || @hyphenator
1006:         end
1007:         instance_eval(&block) unless block.nil?
1008:       when String
1009:         __create(arg, &block)
1010:       when NilClass
1011:         __create(&block)
1012:       else
1013:         raise TypeError
1014:       end
1015:     end
Public Instance methods
==(o)

Compares two Text::Format objects. All settings of the objects are compared except hyphenator. Generated results (e.g., split_words) are not compared, either.

     # File lib/action_mailer/vendor/text/format.rb, line 135
135:     def ==(o)
136:       (@text          ==  o.text)           &&
137:       (@columns       ==  o.columns)        &&
138:       (@left_margin   ==  o.left_margin)    &&
139:       (@right_margin  ==  o.right_margin)   &&
140:       (@hard_margins  ==  o.hard_margins)   &&
141:       (@split_rules   ==  o.split_rules)    &&
142:       (@first_indent  ==  o.first_indent)   &&
143:       (@body_indent   ==  o.body_indent)    &&
144:       (@tag_text      ==  o.tag_text)       &&
145:       (@tabstop       ==  o.tabstop)        &&
146:       (@format_style  ==  o.format_style)   &&
147:       (@extra_space   ==  o.extra_space)    &&
148:       (@tag_paragraph ==  o.tag_paragraph)  &&
149:       (@nobreak       ==  o.nobreak)        &&
150:       (@abbreviations ==  o.abbreviations)  &&
151:       (@nobreak_regex ==  o.nobreak_regex)
152:     end
body_indent=(b)

The number of spaces to indent all lines after the first line of a paragraph. The value provided is silently converted to a positive integer value.

                            columns
 <-------------------------------------------------------------->
 <-----------><------><---------------------------><------------>
  left margin  INDENT  text is formatted into here  right margin
Default:0
Used in:format, paragraphs
     # File lib/action_mailer/vendor/text/format.rb, line 293
293:     def body_indent=(b)
294:       @body_indent = posint(b)
295:     end
center(to_center = nil)

Centers the text, preserving empty lines and tabs.

     # File lib/action_mailer/vendor/text/format.rb, line 908
908:     def center(to_center = nil)
909:       to_center = @text if to_center.nil?
910:       __center([to_center].flatten)
911:     end
columns=(c)

The total width of the format area. The margins, indentation, and text are formatted into this space. The value provided is silently converted to a positive integer.

                            COLUMNS
 <-------------------------------------------------------------->
 <-----------><------><---------------------------><------------>
  left margin  indent  text is formatted into here  right margin
Default:72
Used in:format, paragraphs, center
     # File lib/action_mailer/vendor/text/format.rb, line 187
187:     def columns=(c)
188:       @columns = posint(c)
189:     end
expand(to_expand = nil)

Replaces all tab characters in the text with tabstop spaces.

     # File lib/action_mailer/vendor/text/format.rb, line 914
914:     def expand(to_expand = nil)
915:       to_expand = @text if to_expand.nil?
916:       if to_expand.class == Array
917:         to_expand.collect { |te| __expand(te) }
918:       else
919:         __expand(to_expand)
920:       end
921:     end
first_indent=(f)

The number of spaces to indent the first line of a paragraph. The value provided is silently converted to a positive integer value.

                            columns
 <-------------------------------------------------------------->
 <-----------><------><---------------------------><------------>
  left margin  INDENT  text is formatted into here  right margin
Default:4
Used in:format, paragraphs
     # File lib/action_mailer/vendor/text/format.rb, line 266
266:     def first_indent=(f)
267:       @first_indent = posint(f)
268:     end
format(to_wrap = nil)

Formats text into a nice paragraph format. The text is separated into words and then reassembled a word at a time using the settings of this Format object. If a word is larger than the number of columns available for formatting, then that word will appear on the line by itself.

If to_wrap is nil, then the value of text will be worked on.

     # File lib/action_mailer/vendor/text/format.rb, line 888
888:     def format(to_wrap = nil)
889:       to_wrap = @text if to_wrap.nil?
890:       if to_wrap.class == Array
891:         __format(to_wrap[0])
892:       else
893:         __format(to_wrap)
894:       end
895:     end
format_style=(fs)

Specifies the format style. Allowable values are:

LEFT_ALIGN
Left justified, ragged right.
     |A paragraph that is|
     |left aligned.|
RIGHT_ALIGN
Right justified, ragged left.
     |A paragraph that is|
     |     right aligned.|
RIGHT_FILL
Left justified, right ragged, filled to width by spaces. (Essentially the same as LEFT_ALIGN except that lines are padded on the right.)
     |A paragraph that is|
     |left aligned.      |
JUSTIFY
Fully justified, words filled to width by spaces.
     |A paragraph  that|
     |is     justified.|
Default:Text::Format::LEFT_ALIGN
Used in:format, paragraphs
     # File lib/action_mailer/vendor/text/format.rb, line 544
544:     def format_style=(fs)
545:       raise ArgumentError, "Invalid value provided for format_style." if ((fs < LEFT_ALIGN) || (fs > JUSTIFY))
546:       @format_style = fs
547:     end
hyphenate_to(word, size)

The default implementation of hyphenate_to implements SPLIT_CONTINUATION.

     # File lib/action_mailer/vendor/text/format.rb, line 583
583:     def hyphenate_to(word, size)
584:       [word[0 .. (size - 2)] + "\\", word[(size - 1) .. -1]]
585:     end
hyphenator=(h)

The object responsible for hyphenating. It must respond to hyphenate_to(word, size) and return an array of the word hyphenated into two parts. The size is the MAXIMUM size permitted, including any hyphenation marks.

Default:nil
Used in:format, paragraphs
     # File lib/action_mailer/vendor/text/format.rb, line 335
335:     def hyphenator=(h)
336:       raise ArgumentError, "#{h.inspect} is not a valid hyphenator." unless h.respond_to?(:hyphenate_to)
337:       arity = h.method(:hyphenate_to).arity
338:       raise ArgumentError, "#{h.inspect} must have exactly two or three arguments." unless [2, 3].include?(arity)
339:       @hyphenator = h
340:       @hyphenator_arity = arity
341:     end
justify?()

Indicates that the format style is full justification.

Default:false
Used in:format, paragraphs
     # File lib/action_mailer/vendor/text/format.rb, line 577
577:     def justify?
578:       return @format_style == JUSTIFY
579:     end
left_align?()

Indicates that the format style is left alignment.

Default:true
Used in:format, paragraphs
     # File lib/action_mailer/vendor/text/format.rb, line 553
553:     def left_align?
554:       return @format_style == LEFT_ALIGN
555:     end
left_margin=(left)

The number of spaces used for the left margin. The value provided is silently converted to a positive integer value.

                            columns
 <-------------------------------------------------------------->
 <-----------><------><---------------------------><------------>
  LEFT MARGIN  indent  text is formatted into here  right margin
Default:0
Used in:format, paragraphs, center
     # File lib/action_mailer/vendor/text/format.rb, line 214
214:     def left_margin=(left)
215:       @left_margin = posint(left)
216:     end
paragraphs(to_wrap = nil)

Considers each element of text (provided or internal) as a paragraph. If first_indent is the same as body_indent, then paragraphs will be separated by a single empty line in the result; otherwise, the paragraphs will follow immediately after each other. Uses format to do the heavy lifting.

     # File lib/action_mailer/vendor/text/format.rb, line 902
902:     def paragraphs(to_wrap = nil)
903:       to_wrap = @text if to_wrap.nil?
904:       __paragraphs([to_wrap].flatten)
905:     end
right_align?()

Indicates that the format style is right alignment.

Default:false
Used in:format, paragraphs
     # File lib/action_mailer/vendor/text/format.rb, line 561
561:     def right_align?
562:       return @format_style == RIGHT_ALIGN
563:     end
right_fill?()

Indicates that the format style is right fill.

Default:false
Used in:format, paragraphs
     # File lib/action_mailer/vendor/text/format.rb, line 569
569:     def right_fill?
570:       return @format_style == RIGHT_FILL
571:     end
right_margin=(r)

The number of spaces used for the right margin. The value provided is silently converted to a positive integer value.

                            columns
 <-------------------------------------------------------------->
 <-----------><------><---------------------------><------------>
  left margin  indent  text is formatted into here  RIGHT MARGIN
Default:0
Used in:format, paragraphs, center
     # File lib/action_mailer/vendor/text/format.rb, line 241
241:     def right_margin=(r)
242:       @right_margin = posint(r)
243:     end
split_rules=(s)

Specifies the split mode; used only when hard_margins is set to true. Allowable values are:

SPLIT_FIXED
The word will be split at the number of characters needed, with no marking at all.
     repre
     senta
     ion
SPLIT_CONTINUATION
The word will be split at the number of characters needed, with a C-style continuation character.
      repr\
      esen\
      tati\
      on
SPLIT_HYPHENATION
The word will be split according to the hyphenator specified in hyphenator. If there is no hyphenator specified, works like SPLIT_CONTINUATION. The example is using TeX::Hyphen as the hyphenator.
      rep-
      re-
      sen-
      ta-
      tion

These values can be bitwise ORed together (e.g., SPLIT_FIXED | SPLIT_CONTINUATION) to provide fallback split methods. In the example given, an attempt will be made to split the word using the rules of SPLIT_CONTINUATION; if there is not enough room, the word will be split with the rules of SPLIT_FIXED. These combinations are also available as the following values:

  • SPLIT_CONTINUATION_FIXED
  • SPLIT_HYPHENATION_FIXED
  • SPLIT_HYPHENATION_CONTINUATION
  • SPLIT_ALL
Default:Text::Format::SPLIT_FIXED
Used in:format, paragraphs
     # File lib/action_mailer/vendor/text/format.rb, line 415
415:     def split_rules=(s)
416:       raise ArgumentError, "Invalid value provided for split_rules." if ((s < SPLIT_FIXED) || (s > SPLIT_ALL))
417:       @split_rules = s
418:     end
tabstop=(t)

Indicates the number of spaces that a single tab represents.

Default:8
Used in:expand, unexpand, paragraphs
     # File lib/action_mailer/vendor/text/format.rb, line 501
501:     def tabstop=(t)
502:       @tabstop = posint(t)
503:     end
unexpand(to_unexpand = nil)

Replaces all occurrences of tabstop consecutive spaces with a tab character.

     # File lib/action_mailer/vendor/text/format.rb, line 925
925:     def unexpand(to_unexpand = nil)
926:       to_unexpand = @text if to_unexpand.nil?
927:       if to_unexpand.class == Array
928:         to_unexpand.collect { |te| v << __unexpand(te) }
929:       else
930:         __unexpand(to_unexpand)
931:       end
932:     end