Class: Caracal::Renderers::StylesRenderer

Inherits:
XmlRenderer
  • Object
show all
Defined in:
lib/caracal/renderers/styles_renderer.rb

Instance Attribute Summary

Attributes inherited from XmlRenderer

#document

Instance Method Summary collapse

Methods inherited from XmlRenderer

#initialize, render

Constructor Details

This class inherits a constructor from Caracal::Renderers::XmlRenderer

Instance Method Details

#to_xmlObject

This method produces the xml required for the ‘word/styles.xml` sub-document.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
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
# File 'lib/caracal/renderers/styles_renderer.rb', line 18

def to_xml
  builder = ::Nokogiri::XML::Builder.with(declaration_xml) do |xml|
    xml['w'].styles root_options do

      #============ DEFAULT STYLES ================================

      unless s = document.default_style
        raise Caracal::Errors::NoDefaultStyleError 'Document must declare a default paragraph style.'
      end
      xml['w'].docDefaults do
        xml['w'].rPrDefault do
          xml['w'].rPr do
            xml['w'].rFonts font_options(s)
            xml['w'].b({ 'w:val' => (s.style_bold ? '1' : '0') })
            xml['w'].i({ 'w:val' => (s.style_italic ? '1' : '0') })
            xml['w'].caps({ 'w:val' => (s.style_caps ? '1' : '0') })
            xml['w'].smallCaps({ 'w:val' => '0' })
            xml['w'].strike({ 'w:val' => '0' })
            xml['w'].color({ 'w:val' => s.style_color })
            xml['w'].sz({ 'w:val' => s.style_size })
            xml['w'].u({ 'w:val' => (s.style_underline ? 'single' : 'none') })
            xml['w'].vertAlign({ 'w:val' => 'baseline' })
          end
        end
        xml['w'].pPrDefault do
          xml['w'].pPr do
            xml['w'].keepNext({ 'w:val' => '0' })
            xml['w'].keepLines({ 'w:val' => '0' })
            xml['w'].widowControl({ 'w:val' => '1' })
            xml['w'].spacing(spacing_options(s, true))
            xml['w'].ind(indentation_options(s, true))
            xml['w'].jc({ 'w:val' => s.style_align.to_s })
          end
        end
      end
      xml['w'].style({ 'w:styleId' => s.style_id, 'w:type' => 'paragraph', 'w:default' => '1' }) do
        xml['w'].name({ 'w:val' => s.style_name })
      end
      xml['w'].style({ 'w:styleId' => 'TableNormal', 'w:type' => 'table', 'w:default' => '1' }) do
        xml['w'].name({ 'w:val' => 'Table Normal'})
        xml['w'].pPr do
          xml['w'].spacing({ 'w:lineRule' => 'auto', 'w:line' => (s.style_size * 20 * 1.15), 'w:before' => '0', 'w:after' => '0' })
        end
      end
      default_id = s.style_id


      #============ PARAGRAPH STYLES ================================

      document.styles.reject { |s| s.style_id == default_id }.each do |s|
        xml['w'].style({ 'w:styleId' => s.style_id, 'w:type' => 'paragraph' }) do
          xml['w'].name({ 'w:val' => s.style_name })
          xml['w'].basedOn({ 'w:val' => s.style_base })
          xml['w'].next({ 'w:val' => s.style_next })
          xml['w'].pPr do
            xml['w'].keepNext({ 'w:val' => '0' })
            xml['w'].keepLines({ 'w:val' => '0' })
            xml['w'].widowControl({ 'w:val' => '1' })
            xml['w'].spacing(spacing_options(s)) unless spacing_options(s).nil?
            xml['w'].contextualSpacing({ 'w:val' => '1' })
            xml['w'].jc({ 'w:val' => s.style_align.to_s }) unless s.style_align.nil?
            xml['w'].ind(indentation_options(s)) unless indentation_options(s).nil?
          end
          xml['w'].rPr do
            xml['w'].rFonts(font_options(s)) unless s.style_font.nil?
            xml['w'].b({ 'w:val' => (s.style_bold ? '1' : '0') } ) unless s.style_bold.nil?
            xml['w'].i({ 'w:val' => (s.style_italic ? '1' : '0') }) unless s.style_italic.nil?
            xml['w'].caps({ 'w:val' => (s.style_caps ? '1' : '0') }) unless s.style_caps.nil?
            xml['w'].color({ 'w:val' => s.style_color }) unless s.style_color.nil?
            xml['w'].sz({ 'w:val' => s.style_size }) unless s.style_size.nil?
            xml['w'].u({ 'w:val' => (s.style_underline ? 'single' : 'none') }) unless s.style_underline.nil?
          end
        end
      end

      #============ TABLE STYLES ================================

      xml['w'].style({ 'w:styleId' => 'DefaultTable', 'w:type' => 'table' }) do
        xml['w'].basedOn({ 'w:val' => 'TableNormal' })
        xml['w'].tblPr do
          xml['w'].tblStyleRowBandSize({ 'w:val' => '1' })
          xml['w'].tblStyleColBandSize({ 'w:val' => '1' })
        end
        %w(band1Horz band1Vert band2Horz band2Vert).each do |type|
          xml['w'].tblStylePr({ 'w:type' => type })
        end
        %w(firstCol firstRow lastCol lastRow).each do |type|
          xml['w'].tblStylePr({ 'w:type' => type })
        end
        %w(neCell nwCell seCell swCell).each do |type|
          xml['w'].tblStylePr({ 'w:type' => type })
        end
      end

    end
  end
  builder.to_xml(save_options)
end