File: create-reference.rb

package info (click to toggle)
gegl 0.0.18-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 6,784 kB
  • ctags: 4,389
  • sloc: ansic: 45,496; sh: 9,223; ruby: 1,361; makefile: 740; cpp: 509; xml: 256
file content (565 lines) | stat: -rwxr-xr-x 17,252 bytes parent folder | download
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#!/usr/bin/env ruby

# GEGL API creator Øyvind Kolås 2007,
#
# Use under a public domain license.
#

class Argument
    attr_accessor :name, :data_type, :doc
    def initialize
        @data_type=""
        @name=""
    end
    def data_type
        type = @data_type.gsub("const", "").gsub(/\**/,"").gsub(/\s*/, "")
        ret = @data_type.sub(/\s+/, " ")
        url = ""
        
        case type
        when "void"
        when "gint"
        when "guint"
        when "gchar"
        when "gdouble"
        when "gfloat"
        when "gboolean"
        when "gpointer"
        when "GList"
          url="http://developer.gnome.org/doc/API/2.0/glib/glib-Doubly-Linked-Lists.html#GList"
        when "GSList"
          url="http://developer.gnome.org/doc/API/2.0/glib/glib-Singly-Linked-Lists.html#GSList"
        when "GParamSpec"
          url="http://developer.gnome.org/doc/API/2.0/gobject/gobject-GParamSpec.html#GParamSpec"
        when "GOptionGroup"
          url="http://developer.gnome.org/doc/API/2.2/glib/glib-Commandline-option-parser.html#GOptionGroup"
        when "GValue"
          url="http://developer.gnome.org/doc/API/2.0/gobject/gobject-Generic-values.html#GValue"
        else
          url="##{type}"
        end
        if url!=""
          ret.sub(type, "<a href='#{url}'>#{type}</a>")
        else
          ret
        end
    end
end

class String
    def htmlify
        self.gsub("\n\n","\n</p><p>").gsub("XXX","<span style='color:red;font-weight:bold;'>XXX</span>").gsub("NULL","<span class='keyword'>NULL</span>").gsub(/#([a-zA-Z_]*)/, "<a href='#\\1'>\\1</a>").gsub(/@([a-zA-Z_]*)/, "<span class='arg_name'>\\1</span>").gsub("TRUE", "<span class='keyword'>TRUE</span>")
    end
end

class Section
    attr_accessor :name, :doc, :sample
    def initialize
        @doc=""
        @sample=""
        @name=""
    end
    def menu_entry
        if @name == "GeglBuffer" or
           @name == "GeglNode" or
           @name == "GeglColor" or
           @name == "GeglRectangle" or
           @name == "GeglProcessor" or
           @name == "XML" or
           @name == "The GEGL API" then
          return "<div style='padding-top:0.5em'>#{@name}</div>"
        else
          return "#{@name}"
        end
    end
    def to_s
        ret = "#{@name}\n"
    end
    def sample_html
        @sample.gsub('&','&amp;').gsub('<','&lt;').gsub('>','&gt;').gsub(/(.)(#.*)$/, "\\1<span style='color:gray;text-style:italic'>\\2</span>") 
    end
    def to_html
        ret = "<div class='sect'>
                 <h2>#{@name}</h2>"
            ret += "<div class='sect_doc'>"
            
            ret += "<p>"
            ret += @doc.htmlify
            ret += "</p>"
            ret += "<pre class='sample_code'>#{sample_html}</pre>"
            ret += "</div>"
        ret += "</div>"
    end
end

class Function
    attr_accessor :name, :return_type, :return_doc, :doc, :arguments, :sample
    def initialize
        @arguments = Array.new
        @doc=""
        @return_type=""
        @return_doc=""
        @name=""
        @sample=""
    end
    def menu_entry
        "&nbsp;&nbsp;#{@name}"
    end
    def sample_html
        @sample.gsub('&','&amp;').gsub('<','&lt;').gsub('>','&gt;').gsub(/(.)(#.*)$/, "\\1<span style='color:gray;text-style:italic'>\\2</span>") 
    end
    def add_arg arg
        @arguments << arg
    end
    def to_s
        ret = "#{@return_type} #{@name}\n"
        @arguments.each {|arg|
            ret += "\t#{arg.data_type} #{arg.name} : #{arg.doc}\n"
        }
        ret += " _#{@doc}\n"
        ret += " _#{@return_doc}\n"
        ret += "\n"
    end

    def return_type
        type = @return_type.gsub("const", "").gsub(/\**/,"").gsub(/\s*/, "")
        ret = @return_type.sub(/\s+/, "&nbsp;")
        url = ""
        
        case type
        when "void"
        when "gint"
        when "guint"
        when "gchar"
        when "gdouble"
        when "gfloat"
        when "gboolean"
        when "gpointer"
        when "GList"
          url="http://developer.gnome.org/doc/API/2.0/glib/glib-Doubly-Linked-Lists.html#GList"
        when "GSList"
          url="http://developer.gnome.org/doc/API/2.0/glib/glib-Singly-Linked-Lists.html#GSList"
        when "GParamSpec"
          url="http://developer.gnome.org/doc/API/2.0/gobject/gobject-GParamSpec.html#GParamSpec"
        when "GOptionGroup"
          url="http://developer.gnome.org/doc/API/2.2/glib/glib-Commandline-option-parser.html#GOptionGroup"
        when "GValue"
          url="http://developer.gnome.org/doc/API/2.0/gobject/gobject-Generic-values.html#GValue"
        else
          url="##{type}"
        end
        if url!=""
          ret.sub(type, "<a href='#{url}'>#{type}</a>")
        else
          ret
        end
    end


    def to_html
        ret = "<div class='function'>
                 <!--<h3>#{@name}</h3>-->
                 <div class='function_signature'>
                   <div class='function_header'>
                   <div class='return_type'>#{self.return_type.sub(/const/,"<span class='const'>const</span>").gsub("*","<span class='pointer'>*</span>")}</div>
                   <div class='function_name'>#{@name}</div>
                   </div>"
        ret += "<div class='function_args'>"

        first=true
        i=0
        if @arguments.length==0
            ret += "<div class='argument'><div class='arg_type'><span class='const'>(void)</span></div></div>\n"
        end
        @arguments.each {|arg|
            i=i+1
            if(first and i==@arguments.length)
            ret += "<div class='argument'>
                      <div class='arg_type'><span class='const'>(</span>#{arg.data_type.sub(/const/,"<span class='const'>const</span>").gsub("*","<span class='pointer'>*</span>")}</div>
                      <div class='arg_name'>#{arg.name.sub("...","<span class='varargs'>..., NULL</span>")}<span class='const'>)</span></div>
                    </div>\n"
            elsif(first)
                first=false
            ret += "<div class='argument'>
                      <div class='arg_type'><span class='const'>(</span>#{arg.data_type.sub(/const/,"<span class='const'>const</span>").gsub("*","<span class='pointer'>*</span>")}</div>
                      <div class='arg_name'>#{arg.name.sub("...","<span class='varargs'>..., NULL</span>")}<span class='const'>,</span></div>
                    </div>\n"
            elsif(i==@arguments.length)
            ret += "<div class='argument'>
                      <div class='arg_type'>#{arg.data_type.sub(/const/,"<span class='const'>const</span>").gsub("*","<span class='pointer'>*</span>")}</div>
                      <div class='arg_name'>#{arg.name.sub("...","<span class='varargs'>..., NULL</span>")}<span class='const'>)</span></div>
                    </div>\n"
            else
            ret += "<div class='argument'>
                      <div class='arg_type'>#{arg.data_type.sub(/const/,"<span class='const'>const</span>").gsub("*","<span class='pointer'>*</span>")}</div>
                      <div class='arg_name'>#{arg.name.sub("...","<span class='varargs'>..., NULL</span>")}<span class='const'>,</span></div>
                    </div>\n"
            end
        }

        ret += "</div>"
        ret += "<div class='float_breaker'></div>"
        ret += "</div>"
        ret += "<div class='function_doc'>"
        
        ret += "<p>"
        ret += @doc.htmlify;
        ret += "</p>"

        if @arguments.length > 0
            ret += "<p><b>Arguments:</b></p>"
            ret += "<table>\n"
            @arguments.each { |arg|
                ret += "<tr><td style='padding-right:1em;vertical-align:top'><span class='arg_name'>#{arg.name}</span></td>"
                ret += "<td>#{arg.doc.htmlify}</td></tr>"
            }
            ret += "</table>\n"
        end

        ret += "</div>"
        ret += "<div class='return_doc'>#{@return_doc.htmlify.sub("Returns", "<b>Returns</b>")}</div>"
        ret += "<pre class='sample_code'>#{sample_html}</pre>"
        ret += "</div>"
    end
end

CSS="div.function{
    margin-top: 2em;
  }
  div.function_signature{
      background-color: #ddd;
      padding: 0.5em;
  }
  div.function_header{
      position: relative;
      top: 0;
      left: 0;
      display:block;
  }
  div.return_type{
        display: block;
        font-style: italic;
        color: #22F;
        float:left;
        padding-right: 1em;
  }
  div.function_name{
     display: block;
     float: left;
     font-weight: bold;
  }
  div.function_args{
        float: right;
      display:block;
  }
  div.argument{
     display: block;
     clear: left;
  }
  div.arg_type{
        display: block;
        font-style: italic;
        color: #22F;
        width: 10em;
        float: left;
  }
  div.arg_name{
        color: #050;
        display: block;
        float: left;
  }
  span.arg_name{
        color: #050;
  }
  div.arg_doc{
        display: none;
  }
  div.function_doc{
        display: block;
        margin-top: 2em;
  }
  div.float_breaker{
     clear: both;
  }
  div.return_doc{
        margin-top: 2em;
  }
  span.const{
        color: grey;
  }
  span.pointer{
        color: red;
  }
  span.varargs{
        color: #F22;
        font-style: italic;
  }
  span.keyword {
        color: #22F;
        font-family: mono;
  }

  div.sect{
    margin-top: 2em;
  }
  div.sect_doc{
        display: block;
        margin-top: 2em;
  }
"

elements = []
function = nil
state = :none
arg_no=0

(ARGV.length-1).times {
    |file_no|
line_no=0
IO.foreach(ARGV[file_no]) {
    |line|
    line_no = line_no+1

    case state
    when :none
        state = :start if (line =~ /\/\*\*/)
        state = :section if (line =~ /\/\*\*\*/)
    when :section
        line =~ / \* (.*):/
        elements << function if (function!=nil)
        function=Section.new
        function.name=$1.to_s
        state = :section_doc
    when :start
        line =~ / \* (.*):/
        elements << function if (function!=nil)
        function=Function.new
        function.name=$1.to_s
        state = :args
        arg_no=-1
    when :args
        if line =~ /.*@(.*):(.*)/
            arg_no=arg_no+1
            argument=Argument.new
            argument.name=$1
            argument.doc=""+$2.to_s
            function.add_arg argument
        elsif line =~ / \*(.*)/
            if $1.chomp==''
                state=:more
            else
              function.arguments[arg_no].doc = function.arguments[arg_no].doc + $1.to_s + "\n" if arg_no != -1
            end
        else
            state=:more
        end

    when :section_doc
        if line =~ /^ \* ---/
            state=:section_sample
        elsif line =~ /\*\//
            state=:none
        else
            line =~ /.*\*(.*)/
            function.doc = function.doc + $1.to_s + "\n"
        end
    when :section_sample
        if line =~ /\*\//
            state=:none
        else
            line =~ /.*\* (.*)/
            function.sample = function.sample + $1.to_s + "\n"
        end
    when :function_sample
        if line =~ /\*\//
            state=:fun
            arg_no=0
        else
            line =~ /.*\* (.*)/
            function.sample = function.sample + $1.to_s + "\n"
        end
    when :more
        if line =~ /^ \* ---/
            state=:function_sample
        elsif line =~ /.*(Returns.*)/
            function.return_doc = $1 + "\n"
            state=:more_return
        elsif line =~ /\*\//
            state=:fun
            arg_no=0
        else
            line =~ /.*\*(.*)/
            function.doc = function.doc + $1.to_s + "\n"
        end

    when :more_return
        if line =~ /^ \* ---/
            state=:function_sample
        elsif line =~ /\*\//
            state=:fun
            arg_no=0
        else
            line =~ /.*\*(.*)/
            function.return_doc = function.return_doc + $1.to_s + "\n"
        end

    when :fun  # might be getting more data on current fun
        if line=~ /^\s*((?:const)?\s*[a-zA-Z\d_]*\s*\**\s*)([a-z_\d]*)\s*\(\s*((?:const)?\s*[a-zA-Z\d_]*\s*\**)\s*([a-zA-Z\d_]*)(,?)/
            #.*\(([a-zA-Z_\s\*])([a-zA-Z\d]*)/
            function.return_type=$1
            name=$2
            argtype=$3
            argname=$4
            comma=$5
            arg_no=0

            if name!=function.name and function.name!='GeglProcessor'
                puts "#{line_no}:function name mismatch #{name}!=#{function.name}"
            end
            if argtype=='void'
                state=:none
            elsif argtype==''
                puts "#{line_no}: empty argument list"
                state=:none
            else
                if argname==''
                   puts "#{line_no}: expected argument name"
                   state=:none
                end
                if (function.arguments[arg_no].name!=argname)
                   puts "#{line_no}: #{function.arguments[arg_no].name}!=#{argname}"
                end
                function.arguments[arg_no].data_type=argtype
                arg_no = arg_no+1
            end
        elsif line=~ /^\s*((?:const)?\s*[a-zA-Z\d_]*\s*\**\s*)([a-z_\d]*)/
            argtype=$1
            argname=$2
            if (argtype!='' and argname!='' and function.arguments.length>arg_no)
                if (function.arguments[arg_no].name!=argname)
                   puts "#{line_no}: #{function.arguments[arg_no].name}!=#{argname}"
                end
                function.arguments[arg_no].data_type=argtype
                arg_no = arg_no+1
            end
        else
            #puts "we're in fun and got: #{line}"
        end
        state = :start if (line =~ /\/\*\*/)
        state = :section if (line =~ /\/\*\*\*/)
    else
        state=:none
    end
}
}

if ARGV.length<2
    puts "usage: #{$0} <header1 [header2 ..]> <output.html>"
    exit
end

=begin
File.open(ARGV[1], "w") {|file|
    file.puts "<html><body><head><style type='text/css'>#{CSS}</style></head>"
    elements.each {|fun| file.puts fun.to_html
    file.puts "</body></html>"
    }
}
=end


File.open(ARGV[ARGV.length-1], "w") {|file|

file.puts "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd\">
<html>          
  <head>
    <title>GEGL API</title>
    <meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />

    <style type='text/css'>
       @import url(\"gegl.css\");
       div#toc ul {
          font-size: 70%;
       }
       h3 {
         margin-top: 2em;
         margin-bottom: 0;
         padding-bottom: 0;
         padding-top: 0.2em;
         border-top: 1px solid grey;
       }
       h4 {
         margin-top: 1.5em;
         margin-bottom: 0;
         padding-bottom: 0.5em;
         padding-top: 0.2em;
         border-top: 1px solid grey;
       }
       #{CSS}
    </style>
    </head>
    <body>"

file.puts "<div id='toc'>
      <div class='print'>
         <h3>Contents</h3>
      </div>
      <ul>
        <li><a href='index.html' style='padding-top: 0.5em;'>GEGL</a></li>
        <li><a href='index.html#Documentation' style='padding-top: 0.5em;'>Documentation</a></li>
        <li><a href='index.html#_glossary'>&nbsp;&nbsp;Glossary</a></li>
        <li><a href='operations.html'>&nbsp;&nbsp;Operations</a></li>

"
    elements.each { |element|
      file.puts "<li><a href='\##{element.name.gsub(' ','_')}'>#{element.menu_entry}</a></li>"
    }
    file.puts "</ul></div>\n"
   
    file.puts "<div class='paper'><div class='content'>" 
    elements.each {|element| 
        if !element.name.empty?
        file.puts "<div class='section' id='section_#{element.name.gsub(' ','_')}'><a name='#{element.name.gsub(' ','_')}'></a>#{element.to_html}</div>"
        end
    }
    file.puts "</div></div>
  </body>
</html>"

}

File.open("gegl.devhelp", "w") {|file|
    file.puts "<?xml version='1.0' encoding='utf-8' standalone='no'?>
<!DOCTYPE book PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' ''>
<book xmlns='http://www.devhelp.net/book' title='GEGL Reference Manual' link='api.html' author='' name='gegl'>"
    file.puts "<chapters>"
    elements.each { |element|
       if element.is_a? Section and !element.name.empty?
         file.puts "<sub name='#{element.name}' link='api.html\##{element.name.gsub(' ','_')}'/>"
       end
    }
    file.puts "<sub name='Operations' link='operations.html'/>"
    file.puts "</chapters>"
    file.puts "<functions>"

    elements.each { |element|
       if element.is_a? Function and !element.name.empty?
         file.puts "<function name='#{element.name}' link='api.html\##{element.name.gsub(' ','_')}'/>"
       end
       if element.is_a? Section and !element.name.empty? and element.name=~ /^Gegl/
         file.puts "<function name='#{element.name}' link='api.html\##{element.name.gsub(' ','_')}'/>"
       end


    }

    IO.foreach("operations.html"){ |line|
        if line =~ /^<li><a href='#op_.*'>.*<\/a><\/li>/
            opname=line.gsub(/.*op_/,'').gsub(/'.*/,'').strip
            file.puts "<function name='gegl-#{opname}' link='operations.html#op_#{opname}'/>"
        end
    }

    file.puts "</functions>"
    file.puts "</book>"
}