File: rest_htmlprinter.rb

package info (click to toggle)
open-build-service 2.9.4-10
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 30,544 kB
  • sloc: ruby: 73,389; perl: 51,999; xml: 9,782; sh: 5,540; javascript: 2,743; sql: 1,232; python: 435; makefile: 229; cpp: 46
file content (178 lines) | stat: -rwxr-xr-x 3,776 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/ruby

require "rest"

class HtmlPrinter < Printer

  attr_accessor :output_dir

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

  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

  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

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

  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

  def print_text text
    @html.p do |p|
      text.text.each do |t|
        p.span(t)
        p.br
      end
    end
  end

  def print_parameter parameter
  end

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

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

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

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

  def print_xml_links title, xmlname, schema
    example = xmlname + ".xml"
    if ( !schema || schema.empty? )
      schema = xmlname + ".rng"
      schema = xmlname + ".xsd" unless XmlFile.exist? schema
    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

  def print_contents contents
    @html.div do |p|
      p << create_contents_list( contents.root, 1 )
    end
  end

  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 )
      end
      if ( section.level >= min_level )
        endresult += "<ul>\n" + result + "</ul>"
      else
        endresult = result
      end
      if ( section.level > min_level )
        endresult += "</li>"
      end
    end
    endresult 
  end

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

end