Class: TextFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/text_formatter.rb

Overview

Several methods take a language_code argument. This is the language code (like ‘en_us’) required by Text::Hyphen.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.text_hyphenator(language_code) ⇒ Object

Get a Text::Hyphen instance for a specified language



78
79
80
81
82
# File 'lib/text_formatter.rb', line 78

def TextFormatter.text_hyphenator(language_code)
    @@hyphenators = Hash.new unless defined? @@hyphenators
    @@hyphenators[language_code] ||= Text::Hyphen.new(:language => language_code, :left => 2, :right => 2)
    return @@hyphenators[language_code]
end

Instance Method Details

#chop_word(word, size) ⇒ Object

Simply chop overly long “words” into chunks no longer than the specified size



67
68
69
70
71
72
73
74
75
# File 'lib/text_formatter.rb', line 67

def chop_word(word, size)
    chopped = []
    while (!word.nil? && word.length > size) do
        chopped.push word[0,size]
        word = word[size..-1]
    end
    chopped.push(word) # get the last chunk
    return chopped
end

#hyphenate_word(word, size, language_code) ⇒ Object

Try to hyphenate a word into chunks just barely short enough to fit within the specified size



38
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
# File 'lib/text_formatter.rb', line 38

def hyphenate_word(word, size, language_code)
    # handle pre-hyphenated words, which hyphenate_to balks on
    hyphenated = word.split(/-/)
    if hyphenated.size > 1
        hyphenated.collect!{|w| w.length > size ? self.hyphenate_word(w, size, language_code) : w}
        hyphenated.flatten!
        last = hyphenated.pop
        hyphenated.collect!{|w| w[-1,1].eql?('-') ? w : "#{w}-"}
        hyphenated.push(last)
        return hyphenated
    end

    h = TextFormatter.text_hyphenator(language_code)
    hyphenated = h.hyphenate_to(word, size).compact

    loop_limit = word.length
    while (hyphenated.size > 1 && hyphenated.last.length > size) do
        last = hyphenated.pop
        part1, part2 = h.hyphenate_to(last, size).compact
        hyphenated.push(part1)
        hyphenated.push(part2) unless part2.nil?

        loop_limit -= 1
        break if last.eql?(part1) || (loop_limit < 0)
    end
    return hyphenated
end

#split_long_words(string, max_width, language_code, append_newlines = false) ⇒ Object

Replace all words longer than max_width with the results of split_word. Leading and trailing whitespace is eliminated and inline whitespace runs are replaced with a space.



17
18
19
20
# File 'lib/text_formatter.rb', line 17

def split_long_words(string, max_width, language_code, append_newlines = false)
    words = string.split
    words.collect{|w| w.length > max_width ? split_word(w, max_width, language_code, append_newlines) : w}.flatten.join(' ')
end

#split_word(word, size, langauge_code, append_newlines = true) ⇒ Object

Split one word and return it as an array of substrings no longer than the specified size. Splitting results consist of a hyphenated version (if possible) or by cutting the word into segments no longer than max_width (if hyphenation fails, such as for the word ‘xxxxxxxxxxx’) Every substring except the last will have a newline appended unless append_newlines is false. The result for a ‘word’ string containing whitespace is undefined.



27
28
29
30
31
32
33
34
35
# File 'lib/text_formatter.rb', line 27

def split_word(word, size, langauge_code, append_newlines = true)
    hyphenated = hyphenate_word(word,size,langauge_code)
    hyphenated_chopped = hyphenated.collect{|w| w.length > size ? chop_word(w, size) : w}.flatten
    if append_newlines
        hyphenated_chopped.collect!{|w| "#{w}\n"} 
        hyphenated_chopped.last.chop!
    end
    return hyphenated_chopped
end