*** empty log message ***
[m17n/m17n-docs.git] / utils / mkman.rb
1 #! /usr/local/bin/ruby
2 #¼Â¹Ô¤¹¤ë¤È/tmp/doxyman¤ò¤Ä¤«¤Ã¤Æ¥Õ¥¡¥¤¥ë¤òʬ¤±¡¢
3 #¤â¤È¤Îdirectory¤Ë¤«¤­¤Ê¤ª¤¹¡£
4 # see also ¤ÎÃæ¿È¤ò woman ¸þ¤±¤Ë½ñ¤­´¹¤¨¤ë¡£
5
6 usr_dev_ja=$1
7
8 currentdir = Dir.pwd
9 doxywork = currentdir+"doxywork"+usr_dev_ja+"man3"
10 doxyman3 = currentdir+usr_dev_ja+"man/man3"
11 doxyman3m = currentdir+usr_dev_ja+"man/man3m"
12
13 headertexts = open("doxyhead.txt","r").readlines
14  $fdheader = headertexts[0]
15  $flheader = headertexts[1]
16  $ddheader = headertexts[2]
17  $fielddheader = headertexts[3]
18  $fieldlheader = headertexts[4]
19
20 ####  to find data structure documentation files and rewriting them 
21
22 def writeuntilsectionheader(buf, text, index)
23       for line in text[index .. text.size] 
24           if line =~ /^.S(H|S)/ 
25              return
26           end 
27           buf.push(line)
28      end
29  end
30
31 def  writedocumentation(buf, text, index)
32       for line in text[index .. text.size] 
33           if line =~ /^.SH/ 
34              return
35           end 
36            if 
37                line =~ /^.SS\s"(.+)"/
38                buf.push(".PP\n\\fB".concat($1).concat("\\fP\n"))
39                else
40                buf.push(line)
41             end
42       end
43  end
44
45
46 def datastructure(struct)
47
48    text = open(struct.concat(".3"),"r").readlines
49
50    buf = []
51
52 #  title
53     if tline = text.index("\.SH NAME\n")
54      if text[tline+1] =~ /^([\w\s]+)\s\\-\s(.+)$/ 
55         buf.push(".SS ".concat($1).concat("\n"))
56         buf.push(".PP\n")
57         buf.push($2.concat("\n"))
58         nl = tline + 2
59         writeuntilsectionheader(buf, text, nl)
60      else
61         if 
62         text[tline+1] =~ /^([\w\s]+)\s\\-/ 
63         buf.push(".SS ".concat($1).concat("\n"))
64         buf.push(".PP\n")
65         else
66         buf.push(text[tline+1])
67         buf.push(".PP\n")   
68         end
69      end
70     end
71
72      if ddline = text.index(text.find{|i| i = $ddheader})
73        nl = ddline + 1 
74        writeuntilsectionheader(buf, text, nl)
75    end
76
77 # public types
78     if ptline = text.index("\.SS \"Public Types\"\n")
79        buf.push("\\fBPublic Types:\\fP\n")
80        nl = ptline + 1 
81        writeuntilsectionheader(buf, text, nl)
82        buf.push(".PP\n")
83    end
84
85 # member enumeration documentation
86    if medline = text.index("\.SH \"MEMBER ENUMERATION DOCUMENTATION\"\n")
87       buf.push("\.PP\n\\fBMEMBER ENUMERATION DOCUMENTATION:\\fP\n")
88        nl = medline + 1 
89        writedocumentation(buf, text, nl)
90    end
91
92 # data fields
93     if dfline = text.index($fieldlheader)
94        buf.push("\\fBData Fields:\\fP\n")
95        nl = dfline + 1 
96        writeuntilsectionheader(buf, text, nl)
97        buf.push(".PP\n")
98    end
99
100 # field documentation
101    if fdline = text.index($fielddheader)
102       buf.push("\.PP\n\\fBFIELD DOCUMENTATION:\\fP\n")
103        nl = fdline + 1 
104        writedocumentation(buf, text, nl)
105    end
106
107 return buf
108 end
109
110 ###############################
111 ### ¥Õ¥¡¥¤¥ëʬ¤±
112
113 def documentfunc(title, func_text, short_text)
114   func_text.grep(/^\.SS/){|i| 
115  #¼«Ê¬¤è¤ê¸å¤í¤À¤±
116       func_rest =  func_text[func_text.index(i)..func_text.size] 
117       documentfunc2(i, title, func_rest, short_text)}
118 end
119
120 def documentfunc2 (dstart, title, func_text, short_text)
121  #func_text¤ÎºÇ½é¤Î¹ÔÃæ¤Î´Ø¿ô̾
122    dstart =~ /\s([a-z_]+)\s\(/
123    return if $1 == nil
124    fname = $1
125    ffname = "\\fB".concat($1.concat("\\fP"))
126  #short_text¤Î´Ø¿ô̾¤Î£²¹Ô¸å¤¬brief¡£
127    brief =  short_text[short_text.index(short_text.find{|i| i.index(ffname)}) + 2]
128  #´Ø¿ô¤´¤È¤Î¥Õ¥¡¥¤¥ë¤òºî¤ë¡£
129    file = open(doxywork+fname+".3", "w")
130    file.puts("@function")
131  #¥Ø¥Ã¥À
132    /^\.TH \"([^"]*)\"\s/ =~ title
133    oname = $1
134    newtitle = title.gsub(oname, fname.chop.chop)
135    file.puts(newtitle)
136  #@brief
137    file.print("@brief ") 
138    if /^.RI/ =~ brief 
139       file.puts(brief) 
140      else file.print("\n") 
141    end
142  #¥í¥ó¥°
143    file.puts(func_text[0])
144    for line in func_text[1 .. func_text.size]
145      break if /.SS/ =~ line
146      file.puts(line)
147    end  
148    file.flush
149    end
150
151 ####rewriting each man file
152 ### rewriting a man file for a function
153
154 def frewrite(text)
155 # let the library name appear in the header 
156   buf = [text[0].gsub!("\" \"", "\" \"\" \"")]
157
158   title =  text[0].split(" ")[1].chop!.reverse.chop!.reverse
159
160   if text.index("\\fBReturns: \\fP\n")  == nil
161    print  title, ": Returns not described\n" 
162    end  
163   if text.index("\\fBErrors: \\fP\n")  == nil
164    print  title, ": Errors not described\n" 
165    end
166
167   buf.push(".ad l\n.nh\n.SH NAME\n")
168 #  if @brief is given  
169   if text[1] =~ /\"\\fI(.+)\\fP\"/
170      buf.push(title.concat(" \- ").concat($1))
171      else buf.push(title)
172   end
173
174   synopsys = text.find{|line| line =~ /\.SS/} 
175   buf.push("\n\n.SH SYNOPSIS").push(formatsynopsys(synopsys)).push("\n")
176
177   description = text[text.index(synopsys)+2..text.size]
178   if 
179     description == []
180     print title, ": No description found\n"
181     else
182     buf.push("\n.SH DESCRIPTION\n").push(womanrewrite(desrewrite(description)))
183   end
184  return buf
185 end
186
187 ####synopsys section of a function
188
189 def formatsynopsys(line)
190   line.chop!.chop!.reverse!.chop!.chop!.chop!.chop!.chop!.reverse!
191
192 #  line.gsub!(/\\fP\s*/,"\n.ft\n")
193 #  line.gsub!(/\\fB/,"\n.ft B\n")
194
195   line.gsub!(/\s(\w*)\)/){"\n\\fI" << $1 << "\\fP)"}
196   line.gsub!(/\s(\w*),/){"\n\\fI" << $1 << "\\fP,"}
197   line.gsub!(/\s(\w*)\s\(/){"\n\\fB" << $1 << "\\fP ("}
198 end
199
200 ####non-synopsys section of a function
201
202 def desrewrite(text)
203
204 #removing identation
205   text.grep(/^\\fB.+\\fP/){|line| 
206                      ind = text.index(line)
207                      text.delete_at(ind+1)  if text[ind+1] == (".in +1c\n")}
208
209
210   text.each_with_index{|line,i|
211
212 #removing "More..."  hyperlink
213   line.gsub!(/More.../,"")
214 # ? ad hoc 
215   line.gsub!(/^\.TP/,"")
216
217 #headers
218   line.gsub!(/^\\fBReturn value:\\fP/,"\n.SH RETURNS\n.PP")
219   line.gsub!(/^\\fBSee Also:\\fP/,"\n.SH \"SEE ALSO\"\n.PP")
220   line.gsub!(/^\\fBErrors:\\fP/,"\n.SH ERRORS\n.PP")
221
222   line.gsub!(/^\\fB(.+)\\fP/){"\n.SS " << $1}
223
224 #removing indentation
225  if text[i - 1] =~ /^.PP/
226     if line =~ /^\s./
227    line.reverse!.chop!.reverse!
228   end
229  end
230
231 # removing the results of doxygen bug 
232 # \fP required for . , ; and <> 
233    line.gsub!(/\s*(\\fP)+\s*(,|\.|;)\s+/){"\\fP" << $2 << "\n.ft R\n"}
234
235    line.gsub!(/(\\fP)+\s*>/,"\\fP>")
236    line.gsub!(/<\s+\\f(P|I|B|C|)/){"<\\f" << $1}
237
238
239    line.gsub!(/\s*(\\fP)+\s+/,"\n.ft R\n")
240
241    line.gsub!(/\s+\\f(I|C)\\fB\s*/,"\n.ft B\n")
242    line.gsub!(/\s+\\f(B|I)\\fC\s*/,"\n.ft C\n")
243    line.gsub!(/\s+\\f(B|C)\\fI\s*/,"\n.ft I\n")
244    line.gsub!(/\s+\\fB\s*/,"\n.ft B\n")
245    line.gsub!(/\s+\\fC\s*/,"\n.ft C\n")
246    line.gsub!(/\s+\\fI\s*/,"\n.ft I\n")
247
248   }
249
250 return text
251 end
252
253 def womanrewrite(text)
254
255   if sasectionstart  = text.index(text.find{|line| line =~ /^\.SH\s"SEE ALSO"/})
256      aftersasection = text[sasectionstart+1 .. text.size]
257     if sasectionend = aftersasection.index(aftersasection.find{|line| line =~ /^\.SH/})
258         for line in text[sasectionstart+1 .. sasectionend + sasectionstart]
259             line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
260         end
261       else
262         for line in text[sasectionstart+1 .. text.size]
263             line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
264        end
265      end
266   end
267   return text
268 end
269
270 # rewriting a man file for a non-function
271
272 def orewrite(text)
273   buf = []
274   structures = ["\.SH \"Data Structure Documentation\"\n"]
275
276   if ddind = text.index($ddheader)
277      if odind = text.index(text.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/})
278     unless
279      text[ddind+1 .. odind-1].find{|line| line =~ /^[^\.]/}  
280      text = text[0 .. ddind-1] + text[odind .. text.size] 
281   end  end  end
282
283   text.each_with_index{|line,i|
284              line.gsub!(/More.../,"")
285
286      # let the library name appear in the header 
287               if line =~ /^.TH/
288                  line = line.gsub!("\" \"", "\" \"\" \"")
289               end
290
291      # finding structure documentations and merging into "structures"
292              if line =~ /^\.RI\s\"struct\s\\fB(.*)\\fP\"/
293                 structures.push(datastructure($1))
294              end
295
296      #removing indentation
297              if text[i - 1] =~ /^.PP/
298               if line =~ /^\s./
299                line.reverse!.chop!.reverse!
300                end
301              end
302
303      #removing extra "-"
304              if text[i - 1] =~ /^.SH\sNAME/
305                 if line =~ /\\-/
306                   unless line =~ /\\-\s./
307                   line.chop!.chop!.chop!.chop!
308                   end
309                end
310              end
311
312      #removing author section
313              line.gsub!(/^\.SH\s\"AUTHOR\"/,"")
314              line.gsub!("Generated automatically by Doxygen for m17n_test from the source code.","")
315
316              line.gsub!(/\\fP\s+,/,"\\fP,")
317              line.gsub!(/\\fP\s+\./,"\\fP.")
318              line.gsub!(/\\fC\\fB(\w+)\\fP\\fP/){"\\fB" << $1 << "\\fP"}
319
320       buf.push(line)
321   }
322
323   unless structures == ["\.SH \"Data Structure Documentation\"\n"]
324   
325   if dindex = buf.index(buf.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/i})
326      buf = buf[0 .. dindex-1] + structures + buf[dindex .. buf.size]
327      else 
328      buf = buf + structures
329    end
330   end 
331
332  return buf
333 end
334
335 #############################dividing files
336
337 Dir.mkdir "doxywork/man3" unless FileTest.directory? "doxywork/man3"
338 Dir.mkdir "man" unless FileTest.directory? "man"
339 Dir.mkdir "man/man3m" unless FileTest.directory? "man/man3m"
340
341 Dir.chdir("usr/man/man3")
342
343 Dir.open(".").each{|filename|
344      if FileTest.directory? filename 
345         next
346      end   
347
348      if filename =~ /\.c\./
349         next
350      end   
351
352      file = open(filename,"r") 
353      text = file.readlines
354      title = text[0]
355
356    if
357       sfunctionstart = text.index(text.find{|i| i == $flheader})
358         if sfunctionend = text.index(text[sfunctionstart+1 .. text.size].find{|i| i =~ /^\.SS|^\.SH/})
359         short_text = text[sfunctionstart .. sfunctionend - 1] 
360        else
361         short_text = text[sfunctionstart .. text.size - 1] 
362        end
363
364    if lfunctionstart = text.index(text.find{|i| i == $fdheader})
365       if lfunctionend = text.index(text[lfunctionstart+1 .. text.size].find{|i| i =~ /^\.SH/})
366          func_text = text[lfunctionstart .. lfunctionend - 1] 
367          group_text = text[0 .. lfunctionstart - 1] + text[lfunctionend ..text.size]
368        else 
369          func_text = text[lfunctionstart .. text.size]
370          group_text = text[0 .. lfunctionstart - 1]
371       end
372       else 
373        func_text = [] 
374        group_text = text  
375    end
376
377    documentfunc(title, func_text, short_text)
378
379   else
380   
381   group_text = text
382   
383   end
384    
385   filetowrite = open("../../../doxywork/man3/".concat(filename),"w")
386   print "filetowrite=", filetowrite
387   filetowrite.puts(group_text)
388   filetowrite.flush
389 }
390
391 Dir.chdir(currentdir)
392 #############################rewriting files
393
394 Dir.chdir("doxywork/man3")
395
396 Dir.open(".").each{|filename|
397 unless FileTest.directory? filename
398
399    print "PROCESSING: ", filename, "\n"
400
401     file = open(filename,"r") 
402     text = file.readlines
403
404  if /@function/ =~ text[0]  
405     buf = frewrite(text[1..text.size])
406     else buf = orewrite(text)
407  end
408
409   filetowrite = open("../../man/man3m/".concat(filename).concat("m"),"w")
410   filetowrite.puts(buf)
411   filetowrite.flush
412
413 end
414 }
415
416
417 # Dir.chdir("/tmp/doxyman")
418
419 # Dir.open(".").each{|f|  File.delete(f) if FileTest.file?(f)}