Class: Rubyword::Element::Text

Inherits:
Base
  • Object
show all
Defined in:
lib/rubyword/element/text.rb

Constant Summary collapse

IndentSize =
200
WordStyleList =
{
  font_size: 'w:sz',
  color: 'w:color',
  underline: 'w:u', 
  blod: 'w:b', 
  all_caps: 'w:caps',
  italic: 'w:i',
  bgcolor: 'w:highlight'
}.freeze
ParagraphStyleList =
{
  text_align: 'w:jc',
  spacing: 'w:spacing',
  indent_left: 'w:ind',
  indent_right: 'w:ind',
  indent_between: 'w:ind'
}.freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#rubyword, #section

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Rubyword::Element::Base

Instance Attribute Details

#textsObject

Returns the value of attribute texts.



5
6
7
# File 'lib/rubyword/element/text.rb', line 5

def texts
  @texts
end

Instance Method Details

#save(text, type, style) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/rubyword/element/text.rb', line 25

def save(text, type, style)
  @texts ||= Queue.new
  @section.titles ||= []
  return if text.nil?
  if type == 'text'
    text(text, style)
  else
    self.send(type.to_sym, text, style)
  end
end

#text(text, style) ⇒ Object



36
37
38
# File 'lib/rubyword/element/text.rb', line 36

def text(text, style)
  @texts << { size: 'normal', text: text.to_s, style: style }
end

#write(section = nil, xml = nil) ⇒ Object



69
70
71
72
73
# File 'lib/rubyword/element/text.rb', line 69

def write(section=nil, xml=nil)
  @xml = xml
  text = self.texts.pop
  eval "write_#{text[:size]}(text)"
end

#write_normal(text) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/rubyword/element/text.rb', line 75

def write_normal(text)
  @xml.send('w:p') do
    write_paragraph_style(text[:style])
    @xml.send('w:r') do
      write_word_style(text[:style])
      @xml.send('w:t', {'xml:space' => 'preserve'}, text[:text])
    end
  end
end

#write_paragraph_style(style) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rubyword/element/text.rb', line 106

def write_paragraph_style(style)
  return unless style.is_a?(Hash)
  @xml.send('w:pPr') {
    style.keys.each do |style_name|
      style_name = style_name.to_sym
      next unless ParagraphStyleList.keys.include?(style_name)
      value =style[style_name]
      attribute = case style_name.to_s
                  when 'spacing'
                    {'w:after' => value}
                  when 'indent_left'
                    {'w:left' => value}
                  when 'indent_right'
                    {'w:right' => value}
                  when 'indent_between'
                    v = value.split '-'
                    next unless v.is_a?(Array)
                    { 'w:left' => v[0].to_i, 'w:right' => v[1].to_i }
                  when !!value == value
                    nil
                  else
                    {'w:val' => value}
                  end
      doc_style = ParagraphStyleList[style_name]
      @xml.send(doc_style, attribute)
    end
  }
end

#write_word_style(style) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rubyword/element/text.rb', line 85

def write_word_style(style)
  if style.is_a?(Hash)
    @xml.send('w:rPr') {
      style.keys.each do |style_name|
        style_name = style_name.to_sym
        if WordStyleList.keys.include?(style_name)
          value =style[style_name]
          attribute = if !!value != value # not a bool type
                        {'w:val' => value}
                      else
                        nil
                      end
          doc_style = WordStyleList[style_name]
          @xml.send(doc_style, attribute)
          @xml.send('w:szCs', attribute) if style_name == :font_size
        end
      end
    }
  end
end