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
- #colspec(attrs) ⇒ Object
- #entry(content, attrs = {}) ⇒ Object
-
#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
#colspec(attrs) ⇒ Object
56 57 58 |
# File 'lib/ruby-dita/table.rb', line 56 def colspec(attrs) Element.new('colspec', attrs) end |
#entry(content, attrs = {}) ⇒ Object
61 62 63 |
# File 'lib/ruby-dita/table.rb', line 61 def entry(content, attrs={}) Element.new('entry', attrs, [content]) end |
#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.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/ruby-dita/table.rb', line 9 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 |e| if e.is_a?(Element) and e.name == 'entry' e else entry e 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>.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ruby-dita/table.rb', line 24 def table(column_info, rows=[]) t = Element.new('table') 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) ⇒ 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 << content if content t end |