#!/usr/bin/ruby require "rest" class DocBookPrinter < Printer attr_accessor :output_dir def initialize super() @output_dir = "docbook" @xml_examples = Hash.new @xml_schemas = Hash.new @section = 0 @html_tag_mapping = { "tt" => nil, "em" => "emphasis", "b" => "emphasis", } end def do_prepare unless File.exists? @output_dir Dir.mkdir @output_dir end @index = File.new @output_dir + "/rest_api_appendix.xml", "w" @xml = Builder::XmlMarkup.new :target => @index, :indent => 2 @xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8" @xml.declare! :DOCTYPE, :appendix, :PUBLIC, "-//Novell//DTD NovDoc XML V1.0//EN", "novdocx.dtd" do |x| x.declare! :ENTITY, :%, :'NOVDOC.DEACTIVATE.IDREF', "IGNORE" x.declare! :ENTITY, :%, :entities, :SYSTEM, "entity-decl.ent" x << " %entities;\n" end @xml.comment! "This file was generated by restility at #{Time.now}" @xml << "\n" end def do_finish @xml << "\n" puts "Written #{@index.path}" @index.close end def print_section section if !section.root? level = section.level - 1 if level.zero? @xml.title section @xml.para section.print_children self else @xml.tag! "sect#{level}", "id" => "app.rest_api_doc.sect#{level}.#{@section}" do @section += 1 @xml.title section section.print_children self end end else section.print_children self end end def print_request request @xml.variablelist do @xml.varlistentry do @xml.term do @xml.literal request.to_s end @xml.listitem do request.parameters.each do |p| @xml.para do @xml.emphasis p.name @xml << " (optional)" if p.optional @xml << " - #{p.description}" if p.description && !p.description.empty? end end request.print_children self end end end end def replace_html_tags text @html_tag_mapping.each do |html, docbook| if docbook.nil? text.gsub! "<#{html}>", "" else text.gsub! "<#{html}>", "<#{docbook}>" text.gsub! "", "" end end end def print_text text @xml.para do |p| text.text.each do |t| replace_html_tags t p << t << "\n" end end end def print_parameter parameter end def print_host host @xml.para "Host: " + host.name end def print_result result @xml.para "Result: #{result.name}" end def print_body body @xml.para "Body: #{body.name}" end def print_xmlresult result print_xml_links result.name, result.schema end def print_xmlbody body print_xml_links body.name, body.schema end def print_xml_links xmlname, schema example = xmlname + ".xml" if !schema || schema.empty? schema = xmlname + ".xsd" end if XmlFile.exist? example @xml.screen File.read(XmlFile.find_file example) end if XmlFile.exist? schema @xml.screen File.read(XmlFile.find_file schema) end end def print_contents contents # empty, the content is generated by docbook itself @xml.para end def print_version version @xml.para "API Version: #{version}" end end