File: mkdoc.rb

package info (click to toggle)
ruby-pgplot 0.1.9-3
  • links: PTS, VCS
  • area: contrib
  • in suites: buster, stretch
  • size: 604 kB
  • ctags: 144
  • sloc: ruby: 1,607; makefile: 76; ansic: 57; sh: 17
file content (415 lines) | stat: -rw-r--r-- 7,353 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
# -*- coding: utf-8 -*-

class Formatter
  def initialize(stream)
    @stream = stream
  end
  def <<(a)
    @stream << a
  end
  def print(*a)
    @stream << String(a)
  end
  def stream
    @stream
  end
end

class FormatterRD < Formatter
  def printhead
    @stream << "
=begin
= Ruby/PGPLOT method descriptions
This page contains descriptions of Ruby/PGPLOT method
whose arguments are modified from original PGPLOT subroutines.
"
  end

  def printtrail
    print "
<<< trailer
=end
"
  end

  def subject(x)
    @stream <<
      x.sub(/^\s*(PG\w+)/){'=== ((*%s*))' % $1.downcase}
  end

  def methoddoc(x)
    s=''
    x=[x] if String===x
    x.each{|line| s << line}
    @stream << s
  end
  def docbegin; end
  def docend; end
end


class FormatterHTML < Formatter
  def encode(a)
    a = a.gsub(/&/,'&amp')
    a.gsub!(/</,'&lt')
    a.gsub!(/>/,'&gt')
    a.gsub!(/\b(PG(?!PLOT)[A-Z0-9]+)/) {
      '<a href="#%s">%s</a>' % [$1,$1]
    }
    a
  end

  def <<(a)
    @stream << encode(a)
  end

  def printhead
    @stream <<'
<?xml version="1.0" ?>
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>index</title>
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
 body { background: white; color: black }
dl {
  margin-left: 1em;
  margin-right: 2em;
  padding: 2pt 1em 2pt 0.5em;
  border-width: thin 0 thin thin;
  border-color: #aaaaff;
  background-color: #eeeeff
}
h1 {
  padding: 0.2em 0.5em 0.2em 0.5em;
  border-style: solid;
  border-width: thin thick thin thick;
  border-color: #bbbbff;
  color: #444444;
  background-color: #eeeeff;
}
h2 {
  padding: 0 0.75em 0 0.5em;
  border-style: solid;
  text-indent: 0em;
  background-color: #eeffee;
  border-color: #88bb88;
  color: #444444;
  border-width: 0 0 thin 7pt;
}
pre {
  white-space:  pre;
  padding:      0.5em;
  border-style: solid;
  border-color: #CCCCCC;
  border-width: 1px 2px 2px 1px;
}
h3 {
  padding: 0 2em 0pt 0.25em;
  border-style: solid;
  border-color: #bb88ff;
  color: #444444;
  border-width: 0 0 thin thick;
}
--></style>
</head>
<body bgcolor="#ffffff">
<h1>Ruby/PGPLOT method descriptions</h1>
Most part of this document is from the original PGPLOT distribution
and copyrighted by Dr. Tim Pearson.
<b>Do not</b> redistribute this document
separately from Ruby/PGPLOT distribution
without his permission.
<hr>
<h2>Index</h2>
'
  end

  def printtrail
    @stream << <<EOL
</body>
</html>
EOL
  end

  def subject(x)
    @stream << '<h3>' << x.sub(/^\s*(PG\w+)/) {
      '<a name="%s"><em>%s</em>' % [$1,$1.downcase]
    } << "</a></h3>\n"
    $1.downcase
  end

  def printindex(x)
    @stream << x.chomp.sub(/^\s*(PG\w+)/) {
      '<li><a href="#%s"><code>%s</code></a>' % [$1,$1.downcase]
    } << "\n"
  end

  def methoddoc(x)
    s=''
    if String===x
      x = [x]
    end

    line = x.shift
    if line =~ /\(/
      while line !~ /\(.*\)/m
	line << x.shift
      end
    end

    if line =~ /^---\s*(pg.*)/m
      @stream << "<dl><dt><code>%s</code></dl>\n" % encode($1)
      while x[0]=~/^\s*$/
	x.shift
      end
      if !x.empty?
	@stream << "<pre>\n--- Ruby notes ---\n"
	x.each{|i| @stream << i}
	@stream << "</pre>\n"
      end
    end
    @stream << "<pre>\n"
  end

  def docbegin
    #@stream << "<pre>\n"
  end
  def docend
    @stream << "</pre>\n<hr>\n"
  end
end


class PgDoc

  def autofunclist
    pgfunc_auto = %w(
pgend::
pgbbuf::
pgebuf::
pgpage::
pgpap:1,1:
pgupdt::
pgpanl:1,1:
pgclos::

pgbox:2,1,0,2,1,0:
pgtbox:2,1,0,2,1,0:
pgvsiz:1,1,1,1:
pgvstd::
pgwnad:1,1,1,1:
pgsubp:0,0:

pgwedg:2,1,1,1,1,2:

# Draw Funcs
pgdraw:1,1:
pgmove:1,1:
pgrect:1,1,1,1:
pgarro:1,1,1,1:
pgcirc:1,1,1:
pgpt1:1,1,0:
pgerr1:0,1,1,1,1:
pglab:2,2,2:
pgptxt:1,1,1,1,2:
pgtext:1,1,2:
pgmtxt:2,1,1,1,2:
pgetxt::

pgiden::
pgldev::
pgsave::
pgunsa::
pgeras::

# Set Funcs
pgsch:1:
pgscf:0:
pgsci:0:
pgsfs:0:
pgsls:0:
pgslw:0:
pgsclp:0:
pgsitf:0:
pgslct:0:
pgstbg:0:
pgscr:0,1,1,1:
pgshls:0,1,1,1:
pgsah:0,1,1:
pgscrl:1,1:
pgscir:0,0:
pgscrn:0,2:0
pgshs:1,1,1:
pgsvp:1,1,1,1:
pgswin:1,1,1,1:

# Query Funcs
pgqch::1
pgqcf::0
pgqci::0
pgqfs::0
pgqls::0
pgqlw::0
pgqclp::0
pgqid::0
pgqitf::0
pgqndt::0
pgqtbg::0
pgqcr:0:1,1,1
pgqvp:0:1,1,1,1
pgqwin::1,1,1,1
pgqcol::0,0
pgqcir::0,0
pgqpos::1,1
pgqvsz:0:1,1,1,1

).grep(/:.*:/).collect{|i| i.split(":",3)}

    @autofunc={}
    pgfunc_auto.each do |x|
      @autofunc[x[0]] = x[1..2].collect{|i| i.split(",").size}
    end
  end

  def manualfunclist
    @manualfunc={}
    curfunc=nil
    File.open("../ext/rb_pgplot.c.in").each do |line|
      case line
      when %r'^/\* (PG\w+)'
	curfunc = $1.downcase
	@manualfunc[curfunc] = []
	line.sub!(%r'^/\*\s*','')
      when %r'^\*/'
	curfunc = nil
      else
	if h = @manualfunc[curfunc]
	  h << line
	end
      end
    end
  end

  def mergefunclist
    @mergefunc = @manualfunc.keys
    @autofunc.each_key do |x|
      @mergefunc << x
    end
    @mergefunc.sort!
  end

  def initialize(formatter, srcdir)
    @srcdir = srcdir
    autofunclist
    manualfunclist
    mergefunclist
    @indx = formatter.new('')
    @head = formatter.new('')
    @body = formatter.new('')
    @tail = formatter.new('')
  end


  def defmode()
    funcdef=''
    while @doc[0] !~ /^C/
      line = @doc.shift
      line.chomp!
      if line.sub!(/^\s+SUBROUTINE /,'--- ') ||
	 line.sub!(/^\s+[A-Z0-9\s]+ FUNCTION /,'--- ') ||
	 line.sub!(/^     [^ ]\s*/,'            ')
	funcdef << line.downcase
      end
    end
    #p [funcdef,@doc[0]]

    if @manualfunc[@name]
      # Definded in rb_pgplot.c.in
      @body.methoddoc(@manualfunc[@name])
    else
      # Automatically generated
      #p [@name,@autofunc[@name]]
      nin  = @autofunc[@name][0]
      nout = @autofunc[@name][1]
      funcdef =~ /\(\s*(\w+\s*(?:,\s*\w+\s*)*)\s*\)/
      if s = $1
	a = s.split(/\s*,\s*/)
	funcdef = "---  #{@name}"
	b = a[0,nin].join(', ')
	funcdef << "( " << b << " )" if b.size>0
	b = a[nin,nout]
	funcdef << " #=> " << b.join(', ') if b.size>0
      end
      @body.methoddoc(funcdef)
    end
  end

  def docmode()
    while line = @doc.shift
      case line
      when /^C%/
	# C definition
      when /^C\+/
	defmode()
	while @doc[0] =~ /^C\s*$/
	  @doc.shift
	end
      when /^C/
	line.sub!(/^(     :)/, ' \1')
	line.sub!(/^C.?/,'')
	@body << line << "\n"
      end
    end
  end

  def docextr(fin)
    @doc = []
    fin.each do |line|
      return @doc if line =~ /^C--/
      @doc << line.chomp
    end
    @doc
  end

  def doit
    @head.printhead
    @mergefunc.each do |f|
      if name = f[/(pg\w+)/]
	begin
	  fin = open(@srcdir+'/'+name+'.f')
	rescue
	  fin = nil
	end
	if fin
	  fin.each do |line|
	    if line.sub!(/^C\*/,'')
	      @indx.printindex(line)
	      @name = @body.subject(line)
	      @body.docbegin
	      @doc = docextr(fin)
	      docmode()
	      @body.docend
	    end
	  end
	end
	#@body << "\n\n"
      end
    end
    @tail.printtrail
    b = @head.stream
    b << "<ul>\n" << @indx.stream <<
      "</ul><hr>\n<h2>Descriptions</h2>\n" <<
      @body.stream << @tail.stream
    b
  end

end

#PgDoc.new(FormatterRD.new(File.open(ARGV[0],"w"))).doit
#PgDoc.new(FormatterHTML.new(File.open(ARGV[0],"w"))).doit

File.open(ARGV[1],"w") << PgDoc.new(FormatterHTML,ARGV[0]).doit