Module: Documatic::OpenDocumentText::Helper

Includes:
ERB::Util
Included in:
Component
Defined in:
lib/documatic/open_document_text/helper.rb

Instance Method Summary collapse

Instance Method Details

#image(full_path, opts = {}) ⇒ Object

Insert a reference to an image file with the minimal set of options (:width and :height in centimetres).



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/documatic/open_document_text/helper.rb', line 67

def image(full_path, opts = {})
  image_name = template.add_image(full_path)
  output = '<draw:frame text:anchor-type="as-char" '
  opts[:width] && output << %Q(svg:width="#{opts[:width]}cm" )
  opts[:height] && output << %Q(svg:height="#{opts[:height]}cm")
  output << '>'
  output << %Q(<draw:image xlink:href="Pictures/#{image_name}" )
  output <<
    'xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"/>'
  output << '</draw:frame>'
end

#line_break(lines) ⇒ Object

Turns an array of strings into a single, escaped string with OpenDocument line breaks (<text:line-break/>), omitting any blank lines. Perfect for address blocks etc.



35
36
37
38
39
40
41
42
# File 'lib/documatic/open_document_text/helper.rb', line 35

def line_break(lines)
  lines_esc = lines.collect do |line|
    ERB::Util.h(line)
  end
  return (lines_esc.find_all do |line|
            line && line.to_s.length > 0
          end).join('<text:line-break/>')
end

#para(stylename, content = nil) ⇒ Object

Inserts a paragraph (<text:p>) containing the provided content; or nothing if no content is provided. This helper should be invoked from within “Ruby Block” because paragraphs are block-level elements.

Note that the content is not escaped by default because you might want to include other tags in the content. You should use ERB::Util.h() to escape any content that could possibly contain XML characters.



19
20
21
22
# File 'lib/documatic/open_document_text/helper.rb', line 19

def para(stylename, content = nil)
  end_element = ( content ? ">#{content}</text:p>" : "/>" )
  %Q(<text:p text:style-name="#{stylename}"#{end_element})
end

#partial(filename, assigns = {}) ⇒ Object

Inserts a partial into the document at the chosen position. This helper should be invoked from within “Ruby Block” because it inserts unescaped block-level material in the current template.

The assigns hash is passed through to the partial for binding.

This method will add the provided partial to the Documatic::Partial cache if it hasn’t yet been loaded; or if it has been loaded then the existing partial will be re-used.



54
55
56
57
58
59
60
61
62
63
# File 'lib/documatic/open_document_text/helper.rb', line 54

def partial(filename, assigns = {})
  if template.partials.has_key?(filename)
    p = template.partials[filename]
  else
    p = Documatic::OpenDocumentText::Partial.new(filename)
    template.add_partial(filename, p)
  end
  assigns.merge!(:template => template, :master => master)
  p.process(assigns)
end

#span(stylename, content) ⇒ Object

Inserts a text span (<text:span>) containing the provided content. This helper should be invoked from within “Ruby Literal” because the tag is a text-level element that shouldn’t be escaped. However the content is escaped.



28
29
30
# File 'lib/documatic/open_document_text/helper.rb', line 28

def span(stylename, content)
  %Q(<text:span text:style-name="#{stylename}">#{ERB::Util.h(content)}</text:span>)
end