Module: Dita

Includes:
Duxml
Defined in:
lib/ruby-dita.rb,
lib/ruby-dita/table.rb,
lib/ruby-dita/topic.rb

Constant Summary collapse

GRAMMAR_PATH =
File.expand_path(File.dirname(__FILE__) + '/../xml/dita_grammar.xml')

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.grammarGrammarClass

Returns Dita grammar as standalone object

Returns:

  • (GrammarClass)

    returns Dita grammar as standalone object



29
30
31
# File 'lib/ruby-dita.rb', line 29

def self.grammar
  Ox.parse_obj File.read GRAMMAR_PATH
end

Instance Method Details

#load(path) ⇒ Doc

Returns XML document.

Parameters:

  • path (String)

    path of existing or new Dita file

Returns:

  • (Doc)

    XML document



12
13
14
# File 'lib/ruby-dita.rb', line 12

def load(path)
  super(path, GRAMMAR_PATH)
end

#row(ary) ⇒ Element

Returns correctly formatted row.

Parameters:

  • ary (Array)

    array of row entries or entry values

Returns:

  • (Element)

    correctly formatted row



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby-dita/table.rb', line 37

def row(ary)
  return ary if ary.all? do |a| a.respond_to?(:name) and a.name == 'row' end
  Element.new('row') <<
      ary.collect do |entry|
        if entry.is_a?(Element) and entry.name == 'entry'
          entry
        else
          Element.new('entry') << entry
        end
      end
end

#save(path, output = doc) ⇒ Doc

Returns same as @param output.

Parameters:

  • path (String)

    where to save output file

  • output (Doc) (defaults to: doc)

    document to save; if output.grammar is same as Dita.grammar, adds Dita doctype declaration

Returns:

  • (Doc)

    same as @param output



19
20
21
22
23
24
25
26
# 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 = []) ⇒ Element

Returns valid Dita <table>.

Parameters:

  • 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

  • rows (Array) (defaults to: [])

    array of rows with which to populate table; can either be XML <row> elements or Strings representing values

Returns:

  • (Element)

    valid Dita <table>



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby-dita/table.rb', line 8

def table(column_info, rows=[])
  t = Element.new('table')
  headings = []
  case column_info
    when Array
      headings = column_info if column_info.first.is_a?(String)
      t << column_info if column_info.first.is_a?(Element)
    when Hash
      headings = column_info.keys
      t << column_info.values
    else
      # TODO raise error?

  end
  tgroup = Element.new('tgroup')
  if headings.any?
    tgroup << Element.new('thead', [Element.new('row')])
    headings.each do |h|
      tgroup.thead.row << Element.new('entry', [h])
    end
  end

  tgroup[:cols] = column_info.size.to_s
  tgroup << Element.new('tbody')
  tgroup.tbody << rows.collect do |r| row r end
  t << tgroup
end

#topic(title, content = nil) ⇒ Element

Returns valid Dita <topic>.

Parameters:

  • title (String, Element)

    string or XML rich-text

  • content (Array) (defaults to: nil)

    optional topic body content in array form

Returns:

  • (Element)

    valid Dita <topic>



9
10
11
12
13
14
15
# 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 << Element.new('body', content) if content
  t
end