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.(File.dirname(__FILE__) + '/../xml/dita_grammar.xml')
Class Method Summary collapse
-
.grammar ⇒ GrammarClass
Returns Dita grammar as standalone object.
Instance Method Summary collapse
-
#load(path) ⇒ Doc
XML document.
-
#row(ary) ⇒ Element
Correctly formatted row.
-
#save(path, output = doc) ⇒ Doc
Same as @param output.
-
#table(column_info, rows = []) ⇒ Element
Valid Dita <table>.
-
#topic(title, content = nil) ⇒ Element
Valid Dita <topic>.
Class Method Details
.grammar ⇒ 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.
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.
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.
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>.
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>.
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 |