lib/caracal/renderers/styles_renderer.rb in caracal-0.2.0 vs lib/caracal/renderers/styles_renderer.rb in caracal-0.2.1
- old
+ new
@@ -5,33 +5,34 @@
module Caracal
module Renderers
class StylesRenderer < XmlRenderer
-
+
#-------------------------------------------------------------
# Public Methods
#-------------------------------------------------------------
-
- # This method produces the xml required for the `word/styles.xml`
+
+ # This method produces the xml required for the `word/styles.xml`
# sub-document.
#
def to_xml
builder = ::Nokogiri::XML::Builder.with(declaration_xml) do |xml|
xml.send '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.send 'w:docDefaults' do
xml.send 'w:rPrDefault' do
xml.send 'w:rPr' do
xml.send 'w:rFonts', font_options(s)
xml.send 'w:b', { 'w:val' => (s.style_bold ? '1' : '0') }
xml.send 'w:i', { 'w:val' => (s.style_italic ? '1' : '0') }
+ xml.send 'w:caps', { 'w:val' => (s.style_caps ? '1' : '0') }
xml.send 'w:smallCaps', { 'w:val' => '0' }
xml.send 'w:strike', { 'w:val' => '0' }
xml.send 'w:color', { 'w:val' => s.style_color }
xml.send 'w:sz', { 'w:val' => s.style_size }
xml.send 'w:u', { 'w:val' => (s.style_underline ? 'single' : 'none') }
@@ -57,14 +58,14 @@
xml.send 'w:pPr' do
xml.send '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.send 'w:style', { 'w:styleId' => s.style_id, 'w:type' => 'paragraph' } do
xml.send 'w:name', { 'w:val' => s.style_name }
xml.send 'w:basedOn', { 'w:val' => s.style_base }
xml.send 'w:next', { 'w:val' => s.style_next }
@@ -78,19 +79,20 @@
end
xml.send 'w:rPr' do
xml.send 'w:rFonts', font_options(s) unless s.style_font.nil?
xml.send 'w:b', { 'w:val' => (s.style_bold ? '1' : '0') } unless s.style_bold.nil?
xml.send 'w:i', { 'w:val' => (s.style_italic ? '1' : '0') } unless s.style_italic.nil?
+ xml.send 'w:caps', { 'w:val' => (s.style_caps ? '1' : '0') } unless s.style_caps.nil?
xml.send 'w:color', { 'w:val' => s.style_color } unless s.style_color.nil?
xml.send 'w:sz', { 'w:val' => s.style_size } unless s.style_size.nil?
xml.send 'w:u', { 'w:val' => (s.style_underline ? 'single' : 'none') } unless s.style_underline.nil?
end
end
end
-
+
#============ TABLE STYLES ================================
-
+
xml.send 'w:style', { 'w:styleId' => 'DefaultTable', 'w:type' => 'table' } do
xml.send 'w:basedOn', { 'w:val' => 'TableNormal' }
xml.send 'w:tblPr' do
xml.send 'w:tblStyleRowBandSize', { 'w:val' => '1' }
xml.send 'w:tblStyleColBandSize', { 'w:val' => '1' }
@@ -103,44 +105,44 @@
end
%w(neCell nwCell seCell swCell).each do |type|
xml.send 'w:tblStylePr', { 'w:type' => type }
end
end
-
+
end
end
builder.to_xml(save_options)
end
-
-
-
+
+
+
#-------------------------------------------------------------
# Private Methods
- #-------------------------------------------------------------
+ #-------------------------------------------------------------
private
-
+
def font_options(style)
name = style.style_font
{ 'w:cs' => name, 'w:hAnsi' => name, 'w:eastAsia' => name, 'w:ascii' => name }
end
-
+
def spacing_options(style, default=false)
top = (default) ? style.style_top.to_i : style.style_top
bottom = (default) ? style.style_bottom.to_i : style.style_bottom
line = style.style_line
-
+
options = nil
if [top, bottom, line].compact.size > 0
options = {}
options['w:lineRule'] = 'auto'
options['w:before'] = top unless top.nil?
options['w:after'] = bottom unless bottom.nil?
options['w:line'] = line unless line.nil?
end
options
end
-
+
def root_options
{
'xmlns:mc' => 'http://schemas.openxmlformats.org/markup-compatibility/2006',
'xmlns:o' => 'urn:schemas-microsoft-com:office:office',
'xmlns:r' => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships',
@@ -156,9 +158,9 @@
'xmlns:c' => 'http://schemas.openxmlformats.org/drawingml/2006/chart',
'xmlns:lc' => 'http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas',
'xmlns:dgm' => 'http://schemas.openxmlformats.org/drawingml/2006/diagram'
}
end
-
+
end
end
-end
\ No newline at end of file
+end