Class: HtmlPrinter

Inherits:
Printer show all
Defined in:
lib/rest_htmlprinter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Printer

#do_print, #method_missing, #print, #print_document

Constructor Details

#initializeHtmlPrinter

Returns a new instance of HtmlPrinter.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rest_htmlprinter.rb', line 10

def initialize
  super()
  @output_dir = "html"
  @xml_examples = Hash.new
  @xml_schemas = Hash.new

  @docbook_tag_mapping = {
    "command" => "tt",
    "filename" => "tt",
    "emphasis" => "em",
    "replaceable" => "em",
  }

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Printer

Instance Attribute Details

#output_dirObject

Returns the value of attribute output_dir.



8
9
10
# File 'lib/rest_htmlprinter.rb', line 8

def output_dir
  @output_dir
end

Instance Method Details

#create_contents_list(section, min_level) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/rest_htmlprinter.rb', line 163

def create_contents_list section, min_level
  result = ""
  section.children.each do |s|
    if ( s.is_a? Section )
      result += create_contents_list s, min_level      
    end
    if ( s.is_a? Request )
      result += "<li><a href=\"##{s.id}\">" + h( s.to_s ) + "</a></li>\n"
    end
  end
  endresult = ""
  if ( !result.empty? )
    if ( section.level > min_level )
      endresult = "<li>" + h( section.to_s ) + "</li>\n"
    end
    if ( section.level >= min_level )
      endresult += "<ul>\n" + result + "</ul>\n"
    else
      endresult = result
    end
  end
  endresult 
end

#do_finishObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rest_htmlprinter.rb', line 34

def do_finish
  puts "Written #{@index.path}"
  
  @xml_examples.each do |f,b|
    if !XmlFile.exist?( f )
      STDERR.puts "XML Example '#{f}' is missing."
    else
      XmlFile.copy f, @output_dir
    end
  end
  @xml_schemas.each do |f,b|
    if !XmlFile.exist?( f )
      STDERR.puts "XML Schema '#{f}' is missing."
    else
      XmlFile.copy f, @output_dir
    end
  end
  
  @index.close
end

#do_prepareObject



25
26
27
28
29
30
31
32
# File 'lib/rest_htmlprinter.rb', line 25

def do_prepare
  unless File.exists? @output_dir
    Dir.mkdir @output_dir
  end
  @index = File.new( @output_dir + "/index.html", "w" )
  @html = Builder::XmlMarkup.new( :target => @index, :indent => 2 )
  @html.comment! "This file was generated by restility at #{Time.now}"
end


119
120
121
# File 'lib/rest_htmlprinter.rb', line 119

def print_body body
  @html.p "Body: " + body.name
end


156
157
158
159
160
161
# File 'lib/rest_htmlprinter.rb', line 156

def print_contents contents
  @html.tag! "h#{contents.level}", "Table of Contents"
  @html.p do |p|
    p << create_contents_list( contents.root, 1 )
  end
end


111
112
113
# File 'lib/rest_htmlprinter.rb', line 111

def print_host host
  @html.p "Host: " + host.name
end


108
109
# File 'lib/rest_htmlprinter.rb', line 108

def print_parameter parameter
end


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rest_htmlprinter.rb', line 63

def print_request request
  @html.div( "class" => "request" ) do

    @html.p do
      @html.a( "name" => request.id ) do
        @html.b request.to_s
      end
    end

    if false
      host = request.host
      if ( host )
        @html.p "Host: " + host.name
      end
    end

    if request.parameters.size > 0
      @html.p "Arguments:"
      @html.ul do
        request.parameters.each do |p|
          @html.li p.to_s
        end
      end
    end
    request.print_children self

  end
end


115
116
117
# File 'lib/rest_htmlprinter.rb', line 115

def print_result result
  @html.p "Result: " + result.name
end


55
56
57
58
59
60
61
# File 'lib/rest_htmlprinter.rb', line 55

def print_section section
  if ( !section.root? )
    tag = "h#{section.level}"
    @html.tag!( tag, section )
  end
  section.print_children self
end


99
100
101
102
103
104
105
106
# File 'lib/rest_htmlprinter.rb', line 99

def print_text text
  @html.p do |p|
    text.text.each do |t|
      replace_docbook_tags t
      p << t << "\n"
    end
  end
end


187
188
189
# File 'lib/rest_htmlprinter.rb', line 187

def print_version version
  @html.p "Version: " + version.to_s
end


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/rest_htmlprinter.rb', line 131

def print_xml_links title, xmlname, schema
  example = xmlname + ".xml"
  if ( !schema || schema.empty? )
    schema = xmlname + ".xsd"
  end
  @xml_examples[ example ] = true
  @xml_schemas[ schema ] = true
  @html.p do |p|
    p << title
    p << ": "
    has_example = XmlFile.exist? example
    has_schema = XmlFile.exist? schema
    if has_example
      @html.a( "Example", "href" => example )
    end
    if has_schema
      p << " ";
      @html.a( "Schema", "href" => schema )
    end
    if( !has_example && !has_schema )
      p << xmlname
    end
  end
end


127
128
129
# File 'lib/rest_htmlprinter.rb', line 127

def print_xmlbody body
  print_xml_links "Body", body.name, body.schema
end


123
124
125
# File 'lib/rest_htmlprinter.rb', line 123

def print_xmlresult result
  print_xml_links "Result", result.name, result.schema
end

#replace_docbook_tags(text) ⇒ Object



92
93
94
95
96
97
# File 'lib/rest_htmlprinter.rb', line 92

def replace_docbook_tags text
  @docbook_tag_mapping.each do |docbook, html|
    text.gsub! "<#{docbook}>", "<#{html}>"
    text.gsub! "</#{docbook}>", "</#{html}>"
  end
end