File: myrbdoc

package info (click to toggle)
qdbm 1.8.78-15
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,076 kB
  • sloc: ansic: 31,454; cpp: 3,623; perl: 2,167; java: 2,079; ruby: 1,690; makefile: 1,315; javascript: 627; sh: 396
file content (194 lines) | stat: -rwxr-xr-x 5,609 bytes parent folder | download | duplicates (8)
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
#! /usr/bin/ruby -w

#================================================================
# myrbdoc
# Document generator for Ruby sources
#================================================================


# main routine
def main
  files = []
  err = false
  destdir = nil
  ptitle = "Ruby API"
  afile = nil
  i = 0
  while(i < ARGV.length)
    if(files.length < 1 && ARGV[i] =~ /^-/)
      if(ARGV[i] == "-d")
        destdir = ARGV[i+=1]
      elsif(ARGV[i] == "-t")
        ptitle = ARGV[i+=1]
      elsif(ARGV[i] == "-a")
        afile = ARGV[i+=1]
      else
        usage()
      end
    else
      files.push(ARGV[i])
    end
    i += 1
  end
  (files.length > 0) || usage()
  (destdir) || (destdir = ".")
  (File::directory?(destdir)) || Dir::mkdir(destdir)
  files.each() do |file|
    makedoc(destdir, ptitle, file)
  end
  makeindex(destdir, ptitle, afile, files)
  return 0
end


# print the usage and exit
def usage
  printf(STDERR, "%s: usage: %s: [-d dir] [-t str] [-a file] file ...\n", $0, $0)
  exit(1)
end


# generate a document
def makedoc(destdir, ptitle, name)
  source = name.gsub(/.*\//, "")
  dest = sprintf("%s/%s.html", destdir, source)
  File::open(name, "r") do |infile|
    File::open(dest, "w") do |outfile|
      writehead(outfile, ptitle, source)
      while(line = infile.gets())
        line.chomp!()
        line.gsub!(/^ */, "")
        if(line =~ /^\x23\x23/)
          group = Array::new()
          while(line = infile.gets())
            line.chomp!()
            line.gsub!(/^ */, "")
            (line =~ /^\x23[^@]/) || break
            (line.length() > 1) || next
            group.push(line)
          end
          if(group.length() > 1)
            printf(outfile, "<dl>\n")
            kbd = group[0] =~ /^\x23:/
            group.each() do |elem|
              elem.gsub!(/^\x23[ :]*/, "")
              elem.gsub!(/&/, "&amp;")
              elem.gsub!(/</, "&lt;")
              elem.gsub!(/>/, "&gt;")
              elem.gsub!(/\x22/, "&quot;")
            end
            if(kbd)
              printf(outfile, "<dt><em>%s</em></dt>\n", group[0])
            else
              printf(outfile, "<dt><kbd>%s</kbd></dt>\n", group[0])
            end
            group.shift()
            if(group.length > 0)
              printf(outfile, "<dd>\n")
              group.each() do |elem|
                printf(outfile, "%s\n", elem)
              end
              printf(outfile, "</dd>\n")
            end
            printf(outfile, "</dl>\n")
          end
        end
      end
      printf(outfile, "<hr />\n")
      writetail(outfile)
    end
  end
end


# generate the index file
def makeindex(destdir, ptitle, afile, files)
  dest = sprintf("%s/index.html", destdir)
  File::open(dest, "w") do |outfile|
    writehead(outfile, ptitle)
    if(afile)
      File::open(afile, "r") do |infile|
        while(line = infile.gets())
          line.chomp!()
          printf(outfile, "%s\n", line)
        end
      end
      printf(outfile, "<hr />\n")
    end
    printf(outfile, "<h2>API</h2>\n")
    printf(outfile, "<ul>\n")
    files.each() do |file|
      file = file.gsub(/.*\//, "")
      printf(outfile, "<li><a href=\"%s.html\">%s</a></li>\n", file, file.gsub(/\..*/, ""))
    end
    printf(outfile, "</ul>\n")
    printf(outfile, "<hr />\n")
    writetail(outfile)
  end
end


# write header
def writehead(outfile, ptitle, name = nil)
  if(name)
    title = sprintf("%s (%s)", name.gsub(/\..*/, ""), ptitle)
    head = sprintf("API of %s", name.gsub(/\..*/, ""))
    navi = "<div class=\"note\">[<a href=\"index.html\">Back</a>]</div>"
  else
    title = sprintf("INDEX (%s)", ptitle)
    head = ptitle
    navi = "<div class=\"note\">Ruby Source Documents</div>"
  end
printf(outfile, "%s", <<__MYEOF)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<link rel="contents" href="./" />
<link rev="made" href="mailto:mikio\@fallabs.com" />
<title>#{title}</title>
<style type="text/css">html { margin: 0em 0em; padding 0em 0em; background: #eeeeee none; }
body { background-color: #eeeeee; color: #111111; padding: 1.5em 2em; margin: 0em 0em;
  border-left: double 3.0em #eedddd; border-right: solid 1.0em #eedddd;
  font-style: normal; font-weight: normal; font-family: serif; }
h1 { margin-top: 0.8em; margin-bottom: 1.3em; font-weight: bold; font-family: sans-serif; }
h2 { margin-top: 1.8em; margin-bottom: 1.1em; font-weight: bold; font-family: sans-serif; }
h3 { margin-top: 1.8em; margin-bottom: 0.8em; font-weight: bold; font-family: sans-serif; }
p,ul,ol,dl { line-height: 140%; }
em { color: #111111; font-style: italic; font-weight: normal; font-family: serif; }
kbd { color: #111111; font-style: normal; font-weight: bold; font-family: monospace; }
a { color: #0022aa; text-decoration: none; }
a:hover { color: #0033ee; text-decoration: underline; }
a.head { color: #111111; text-decoration: none; }
.note { text-align: right; }
</style>
</head>
<body>
#{navi}
<h1>#{head}</h1>
<hr />
__MYEOF
end


# write tail
def writetail(outfile)
  (sec, min, hour, mday, mon, year) = Time::now.to_a
  datestr = sprintf("%04d/%02d/%02d", year, mon, mday)
printf(outfile, "%s", <<__MYEOF)
<div class="note">Generated by #{$0}, #{datestr}.</div>
</body>
</html>
__MYEOF
end


# execute main
#$0.gsub!(/.*\//, "")
exit(main())



# END OF FILE