module Dita

Constants

GRAMMAR_PATH

Public Class Methods

grammar() click to toggle source

@return [GrammarClass] returns Dita grammar as standalone object

# File lib/ruby-dita.rb, line 29
def self.grammar
  Ox.parse_obj File.read GRAMMAR_PATH
end

Public Instance Methods

colspec(attrs) click to toggle source

@param attrs [Hash] attributes of a given column @return [Element] colspec element

# File lib/ruby-dita/table.rb, line 60
def colspec(attrs)
  Element.new('colspec', attrs)
end
entry(content, attrs={}) click to toggle source

@param content [String, Element] content of entry @param attrs [Hash] attributes of entry @return [Element] entry element

# File lib/ruby-dita/table.rb, line 67
def entry(content, attrs={})
  Element.new('entry', attrs, [content])
end
load(path) click to toggle source

@param path [String] path of existing or new Dita file @return [Doc] XML document

Calls superclass method
# File lib/ruby-dita.rb, line 12
def load(path)
  super(path, GRAMMAR_PATH)
end
row(ary, attrs={}) click to toggle source

@param ary [Array] array of row entries or entry values @param attrs [Hash] optional attributes for row @return [Element] correctly formatted row

# File lib/ruby-dita/table.rb, line 10
def row(ary, attrs={})
  return ary if ary.all? do |a| a.respond_to?(:name) and a.name == 'row' end
  Element.new('row', attrs) <<
      ary.collect do |e|
        if e.is_a?(Element) and e.name == 'entry'
          e
        else
          entry e
        end
      end
end
save(path, output=doc) click to toggle source

@param path [String] where to save output file @param output [Doc] document to save; if output.grammar is same as Dita.grammar, adds Dita doctype declaration @return [Doc] same as @param output

Calls superclass method
# File lib/ruby-dita.rb, line 19
def save(path, output=doc)
  if output.grammar.rules.size == Dita.grammar.rules.size # TODO find better way to compare these!!
    File.write(path, %(<!DOCTYPE topic PUBLIC "-//OASIS//DTD DITA Topic//EN" "topic.dtd">#{output.root.to_s}))
  else
    super(path, output)
  end
  output
end
table(column_info, rows, attrs={}) click to toggle source

@param column_info [Array, Hash] if Array of Strings, column headings; if Array of Elements, colspecs; if Hash, keys are strings for column headings; values are <colspec> elements @param rows [Array] array of rows with which to populate table; can either be XML <row> elements or Strings representing values @param attrs [Hash] attributes of table @return [Element] valid Dita <table>

# File lib/ruby-dita/table.rb, line 26
def table(column_info, rows, attrs={})
  t = Element.new('table', attrs)
  headings = []
  tgroup = Element.new('tgroup')
  num_cols = column_info.size.to_s
  case column_info
    when Array
      headings = column_info if column_info.first.is_a?(String)
      colspecs = column_info if column_info.first.is_a?(Element) and column_info.first.name == 'colspec'
    when Hash
      headings = column_info.keys
      colspecs = column_info.values
    else
      # TODO raise error?
  end
  if colspecs
    colspecs.each_with_index do |c, index| c[:colname] = index.to_s end
    tgroup << colspecs
  end
  if headings.any?
    tgroup << Element.new('thead', [Element.new('row')])
    headings.each do |h|
      tgroup.thead.nodes.first << Element.new('entry', [h])
    end
  end

  tgroup[:cols] = num_cols
  tgroup << Element.new('tbody')
  tgroup.tbody << rows.collect do |r| row r end
  t << tgroup
end
topic(title, content=nil) click to toggle source

@param title [String, Element] string or XML rich-text @param content [Array] optional topic content in array form; added as direct children of <topic>, not <body> @return [Element] valid Dita <topic>

# File lib/ruby-dita/topic.rb, line 9
def topic(title, content=nil)
  t = Element.new('topic')
  t[:id] = "topic#{t.object_id.to_s}"
  t << Element.new('title', [title])
  t << content if content
  t
end