lib/caracal/renderers/document_renderer.rb in caracal-1.0.13 vs lib/caracal/renderers/document_renderer.rb in caracal-1.1.0
- old
+ new
@@ -15,31 +15,31 @@
# This method produces the xml required for the `word/document.xml`
# sub-document.
#
def to_xml
builder = ::Nokogiri::XML::Builder.with(declaration_xml) do |xml|
- xml.send 'w:document', root_options do
- xml.send 'w:background', { 'w:color' => 'FFFFFF' }
- xml.send 'w:body' do
+ xml['w'].document root_options do
+ xml['w'].background({ 'w:color' => 'FFFFFF' })
+ xml['w'].body do
#============= CONTENTS ===================================
document.contents.each do |model|
method = render_method_for_model(model)
send(method, xml, model)
end
#============= PAGE SETTINGS ==============================
- xml.send 'w:sectPr' do
+ xml['w'].sectPr do
if document.page_number_show
if rel = document.find_relationship('footer1.xml')
- xml.send 'w:footerReference', { 'r:id' => rel.formatted_id, 'w:type' => 'default' }
+ xml['w'].footerReference({ 'r:id' => rel.formatted_id, 'w:type' => 'default' })
end
end
- xml.send 'w:pgSz', page_size_options
- xml.send 'w:pgMar', page_margin_options
+ xml['w'].pgSz page_size_options
+ xml['w'].pgMar page_margin_options
end
end
end
end
@@ -71,26 +71,26 @@
attrs = model.run_attributes.delete_if { |k, v| v.nil? }
if paragraph_level && attrs.empty?
# skip
else
- xml.send 'w:rPr' do
+ xml['w'].rPr do
unless attrs.empty?
- xml.send 'w:rStyle', { 'w:val' => attrs[:style] } unless attrs[:style].nil?
- xml.send 'w:color', { 'w:val' => attrs[:color] } unless attrs[:color].nil?
- xml.send 'w:sz', { 'w:val' => attrs[:size] } unless attrs[:size].nil?
- xml.send 'w:b', { 'w:val' => (attrs[:bold] ? '1' : '0') } unless attrs[:bold].nil?
- xml.send 'w:i', { 'w:val' => (attrs[:italic] ? '1' : '0') } unless attrs[:italic].nil?
- xml.send 'w:u', { 'w:val' => (attrs[:underline] ? 'single' : 'none') } unless attrs[:underline].nil?
- xml.send 'w:shd', { 'w:fill' => attrs[:bgcolor], 'w:val' => 'clear' } unless attrs[:bgcolor].nil?
- xml.send 'w:vertAlign', { 'w:val' => attrs[:vertical_align] } unless attrs[:vertical_align].nil?
+ xml['w'].rStyle( { 'w:val' => attrs[:style] }) unless attrs[:style].nil?
+ xml['w'].color( { 'w:val' => attrs[:color] }) unless attrs[:color].nil?
+ xml['w'].sz( { 'w:val' => attrs[:size] }) unless attrs[:size].nil?
+ xml['w'].b( { 'w:val' => (attrs[:bold] ? '1' : '0') }) unless attrs[:bold].nil?
+ xml['w'].i( { 'w:val' => (attrs[:italic] ? '1' : '0') }) unless attrs[:italic].nil?
+ xml['w'].u( { 'w:val' => (attrs[:underline] ? 'single' : 'none') }) unless attrs[:underline].nil?
+ xml['w'].shd( { 'w:fill' => attrs[:bgcolor], 'w:val' => 'clear' }) unless attrs[:bgcolor].nil?
+ xml['w'].vertAlign( { 'w:val' => attrs[:vertical_align] }) unless attrs[:vertical_align].nil?
unless attrs[:font].nil?
f = attrs[:font]
- xml.send 'w:rFonts', { 'w:ascii' => f, 'w:hAnsi' => f, 'w:eastAsia' => f, 'w:cs' => f }
+ xml['w'].rFonts( { 'w:ascii' => f, 'w:hAnsi' => f, 'w:eastAsia' => f, 'w:cs' => f })
end
end
- xml.send 'w:rtl', { 'w:val' => '0' }
+ xml['w'].rtl({ 'w:val' => '0' })
end
end
end
end
@@ -138,71 +138,71 @@
rel = document.relationship({ type: :image, target: model.image_url, data: model.image_data })
rel_id = rel.relationship_id
rel_name = rel.formatted_target
- xml.send 'w:p', paragraph_options do
- xml.send 'w:pPr' do
- xml.send 'w:spacing', { 'w:lineRule' => 'auto', 'w:line' => ds.style_line }
- xml.send 'w:contextualSpacing', { 'w:val' => '0' }
- xml.send 'w:jc', { 'w:val' => model.image_align.to_s }
- xml.send 'w:rPr'
+ xml['w'].p paragraph_options do
+ xml['w'].pPr do
+ xml['w'].spacing({ 'w:lineRule' => 'auto', 'w:line' => ds.style_line })
+ xml['w'].contextualSpacing({ 'w:val' => '0' })
+ xml['w'].jc({ 'w:val' => model.image_align.to_s })
+ xml['w'].rPr
end
- xml.send 'w:r', run_options do
- xml.send 'w:drawing' do
- xml.send 'wp:inline', { distR: model.formatted_right, distT: model.formatted_top, distB: model.formatted_bottom, distL: model.formatted_left } do
- xml.send 'wp:extent', { cx: model.formatted_width, cy: model.formatted_height }
- xml.send 'wp:effectExtent', { t: 0, b: 0, r: 0, l: 0 }
- xml.send 'wp:docPr', { id: rel_id, name: rel_name }
- xml.send 'a:graphic' do
- xml.send 'a:graphicData', { uri: 'http://schemas.openxmlformats.org/drawingml/2006/picture' } do
- xml.send 'pic:pic' do
- xml.send 'pic:nvPicPr' do
- xml.send 'pic:cNvPr', { id: rel_id, name: rel_name }
- xml.send 'pic:cNvPicPr'
+ xml['w'].r run_options do
+ xml['w'].drawing do
+ xml['wp'].inline({ distR: model.formatted_right, distT: model.formatted_top, distB: model.formatted_bottom, distL: model.formatted_left }) do
+ xml['wp'].extent({ cx: model.formatted_width, cy: model.formatted_height })
+ xml['wp'].effectExtent({ t: 0, b: 0, r: 0, l: 0 })
+ xml['wp'].docPr({ id: rel_id, name: rel_name })
+ xml['a'].graphic do
+ xml['a'].graphicData({ uri: 'http://schemas.openxmlformats.org/drawingml/2006/picture' }) do
+ xml['pic'].pic do
+ xml['pic'].nvPicPr do
+ xml['pic'].cNvPr({ id: rel_id, name: rel_name })
+ xml['pic'].cNvPicPr
end
- xml.send 'pic:blipFill' do
- xml.send 'a:blip', { 'r:embed' => rel.formatted_id }
- xml.send 'a:srcRect'
- xml.send 'a:stretch' do
- xml.send 'a:fillRect'
+ xml['pic'].blipFill do
+ xml['a'].blip({ 'r:embed' => rel.formatted_id })
+ xml['a'].srcRect
+ xml['a'].stretch do
+ xml['a'].fillRect
end
end
- xml.send 'pic:spPr' do
- xml.send 'a:xfrm' do
- xml.send 'a:ext', { cx: model.formatted_width, cy: model.formatted_height }
+ xml['pic'].spPr do
+ xml['a'].xfrm do
+ xml['a'].ext({ cx: model.formatted_width, cy: model.formatted_height })
end
- xml.send 'a:prstGeom', { prst: 'rect' }
- xml.send 'a:ln'
+ xml['a'].prstGeom({ prst: 'rect' })
+ xml['a'].ln
end
end
end
end
end
end
end
- xml.send 'w:r', run_options do
- xml.send 'w:rPr' do
- xml.send 'w:rtl', { 'w:val' => '0' }
+ xml['w'].r run_options do
+ xml['w'].rPr do
+ xml['w'].rtl({ 'w:val' => '0' })
end
end
end
end
def render_linebreak(xml, model)
- xml.send 'w:r' do
- xml.send 'w:br'
+ xml['w'].r do
+ xml['w'].br
end
end
def render_link(xml, model)
rel = document.relationship({ target: model.link_href, type: :link })
- xml.send 'w:hyperlink', { 'r:id' => rel.formatted_id } do
- xml.send 'w:r', run_options do
+ xml['w'].hyperlink({ 'r:id' => rel.formatted_id }) do
+ xml['w'].r run_options do
render_run_attributes(xml, model, false)
- xml.send 'w:t', { 'xml:space' => 'preserve' }, model.link_content
+ xml['w'].t({ 'xml:space' => 'preserve' }, model.link_content)
end
end
end
def render_list(xml, model)
@@ -218,20 +218,20 @@
def render_listitem(xml, model, list_num)
ls = document.find_list_style(model.list_item_type, model.list_item_level)
hanging = ls.style_left.to_i - ls.style_indent.to_i - 1
- xml.send 'w:p', paragraph_options do
- xml.send 'w:pPr' do
- xml.send 'w:numPr' do
- xml.send 'w:ilvl', { 'w:val' => model.list_item_level }
- xml.send 'w:numId', { 'w:val' => list_num }
+ xml['w'].p paragraph_options do
+ xml['w'].pPr do
+ xml['w'].numPr do
+ xml['w'].ilvl({ 'w:val' => model.list_item_level })
+ xml['w'].numId({ 'w:val' => list_num })
end
- xml.send 'w:ind', { 'w:left' => ls.style_left, 'w:hanging' => hanging }
- xml.send 'w:contextualSpacing', { 'w:val' => '1' }
- xml.send 'w:rPr' do
- xml.send 'w:u', { 'w:val' => 'none' }
+ xml['w'].ind({ 'w:left' => ls.style_left, 'w:hanging' => hanging })
+ xml['w'].contextualSpacing({ 'w:val' => '1' })
+ xml['w'].rPr do
+ xml['w'].u({ 'w:val' => 'none' })
end
end
model.runs.each do |run|
method = render_method_for_model(run)
send(method, xml, run)
@@ -239,30 +239,30 @@
end
end
def render_pagebreak(xml, model)
if model.page_break_wrap
- xml.send 'w:p', paragraph_options do
- xml.send 'w:r', run_options do
- xml.send 'w:br', { 'w:type' => 'page' }
+ xml['w'].p paragraph_options do
+ xml['w'].r run_options do
+ xml['w'].br({ 'w:type' => 'page' })
end
end
else
- xml.send 'w:r', run_options do
- xml.send 'w:br', { 'w:type' => 'page' }
+ xml['w'].r run_options do
+ xml['w'].br({ 'w:type' => 'page' })
end
end
end
def render_paragraph(xml, model)
run_props = [:color, :size, :bold, :italic, :underline].map { |m| model.send("paragraph_#{ m }") }.compact
- xml.send 'w:p', paragraph_options do
- xml.send 'w:pPr' do
- xml.send 'w:pStyle', { 'w:val' => model.paragraph_style } unless model.paragraph_style.nil?
- xml.send 'w:contextualSpacing', { 'w:val' => '0' }
- xml.send 'w:jc', { 'w:val' => model.paragraph_align } unless model.paragraph_align.nil?
+ xml['w'].p paragraph_options do
+ xml['w'].pPr do
+ xml['w'].pStyle({ 'w:val' => model.paragraph_style }) unless model.paragraph_style.nil?
+ xml['w'].contextualSpacing({ 'w:val' => '0' })
+ xml['w'].jc({ 'w:val' => model.paragraph_align }) unless model.paragraph_align.nil?
render_run_attributes(xml, model, true)
end
model.runs.each do |run|
method = render_method_for_model(run)
send(method, xml, run)
@@ -271,75 +271,75 @@
end
def render_rule(xml, model)
options = { 'w:color' => model.rule_color, 'w:sz' => model.rule_size, 'w:val' => model.rule_line, 'w:space' => model.rule_spacing }
- xml.send 'w:p', paragraph_options do
- xml.send 'w:pPr' do
- xml.send 'w:pBdr' do
- xml.send 'w:top', options
+ xml['w'].p paragraph_options do
+ xml['w'].pPr do
+ xml['w'].pBdr do
+ xml['w'].top options
end
end
end
end
def render_text(xml, model)
- xml.send 'w:r', run_options do
+ xml['w'].r run_options do
render_run_attributes(xml, model, false)
- xml.send 'w:t', { 'xml:space' => 'preserve' }, model.text_content
+ xml['w'].t({ 'xml:space' => 'preserve' }, model.text_content)
end
end
def render_table(xml, model)
borders = %w(top left bottom right horizontal vertical).select do |m|
model.send("table_border_#{ m }_size") > 0
end
- xml.send 'w:tbl' do
- xml.send 'w:tblPr' do
- xml.send 'w:tblStyle', { 'w:val' => 'DefaultTable' }
- xml.send 'w:bidiVisual', { 'w:val' => '0' }
- xml.send 'w:tblW', { 'w:w' => model.table_width.to_f, 'w:type' => 'dxa' }
- xml.send 'w:tblInd', { 'w:w' => '0.0', 'w:type' => 'dxa' }
- xml.send 'w:jc', { 'w:val' => model.table_align }
+ xml['w'].tbl do
+ xml['w'].tblPr do
+ xml['w'].tblStyle({ 'w:val' => 'DefaultTable' })
+ xml['w'].bidiVisual({ 'w:val' => '0' })
+ xml['w'].tblW({ 'w:w' => model.table_width.to_f, 'w:type' => 'dxa' })
+ xml['w'].tblInd({ 'w:w' => '0.0', 'w:type' => 'dxa' })
+ xml['w'].jc({ 'w:val' => model.table_align })
unless borders.empty?
- xml.send 'w:tblBorders' do
+ xml['w'].tblBorders do
borders.each do |m|
options = {
'w:color' => model.send("table_border_#{ m }_color"),
'w:val' => model.send("table_border_#{ m }_line"),
'w:sz' => model.send("table_border_#{ m }_size"),
'w:space' => model.send("table_border_#{ m }_spacing")
}
- xml.send "w:#{ Caracal::Core::Models::BorderModel.formatted_type(m) }", options
+ xml['w'].method_missing "#{ Caracal::Core::Models::BorderModel.formatted_type(m) }", options
end
end
end
- xml.send 'w:tblLayout', { 'w:type' => 'fixed' }
- xml.send 'w:tblLook', { 'w:val' => '0600' }
+ xml['w'].tblLayout({ 'w:type' => 'fixed' })
+ xml['w'].tblLook({ 'w:val' => '0600' })
end
- xml.send 'w:tblGrid' do
+ xml['w'].tblGrid do
model.rows.first.each do |tc|
- xml.send 'w:gridCol', { 'w:w' => tc.cell_width }
+ xml['w'].gridCol({ 'w:w' => tc.cell_width })
end
- xml.send 'w:tblGridChange', { 'w:id' => '0' } do
- xml.send 'w:tblGrid' do
+ xml['w'].tblGridChange({ 'w:id' => '0' }) do
+ xml['w'].tblGrid do
model.rows.first.each do |tc|
- xml.send 'w:gridCol', { 'w:w' => tc.cell_width }
+ xml['w'].gridCol({ 'w:w' => tc.cell_width })
end
end
end
end
model.rows.each do |row|
- xml.send 'w:tr' do
+ xml['w'].tr do
row.each do |tc|
- xml.send 'w:tc' do
- xml.send 'tcPr' do
- xml.send 'w:shd', { 'w:fill' => tc.cell_background }
- xml.send 'w:vAlign', { 'w:val' => tc.cell_vertical_align }
- xml.send 'w:tcMar' do
+ xml['w'].tc do
+ xml['w'].tcPr do
+ xml['w'].shd({ 'w:fill' => tc.cell_background })
+ xml['w'].vAlign({ 'w:val' => tc.cell_vertical_align })
+ xml['w'].tcMar do
%w(top left bottom right).each do |d|
- xml.send "w:#{ d }", { 'w:w' => tc.send("cell_margin_#{ d }").to_f, 'w:type' => 'dxa' }
+ xml['w'].method_missing "#{ d }", { 'w:w' => tc.send("cell_margin_#{ d }").to_f, 'w:type' => 'dxa' }
end
end
end
tc.contents.each do |m|
method = render_method_for_model(m)