cf91ded358183c65200aaf2f753ec33063790f9a
[m17n/m17n-docs.git] / utils / mkman.rb
1 #! /usr/local/bin/ruby
2 # Usage: mkman.rb SRCDIR DSTDIR
3
4 #¼Â¹Ô¤¹¤ë¤È/tmp/doxyman¤ò¤Ä¤«¤Ã¤Æ¥Õ¥¡¥¤¥ë¤òʬ¤±¡¢
5 #¤â¤È¤Îdirectory¤Ë¤«¤­¤Ê¤ª¤¹¡£
6 # see also ¤ÎÃæ¿È¤ò woman ¸þ¤±¤Ë½ñ¤­´¹¤¨¤ë¡£
7
8 $manext="."+$*[0]
9
10 #
11 # Setting up directory names.
12 #
13
14 $currentdir = Dir.pwd+"/"
15 $srcdir=$currentdir+$*[1]+"/"
16 $dstdir=$currentdir+$*[2]+"/"
17 $doxywork = $currentdir+"doxywork/"
18 $sampledir="sample/man3/"
19 $headfile = $currentdir+"manhead"
20
21 headbuf = open($headfile, "r").readlines
22
23 #
24 # Extra information about header strings Doxygen generates in a man file.
25 #
26
27 def nextheader(text,index)
28   header = text[index+1..text.size].find{|i| i =~ /\.S(S|H)/} 
29   return header
30   end
31
32 grouptext = open($sampledir+"SampleGroup.3","r").readlines.reverse
33
34 fd = grouptext.index(grouptext.find{|i| i =~ /SampleFunction/})
35 $fdheader = nextheader(grouptext,fd)
36
37 restofthetext =  grouptext[fd+1..grouptext.size]
38
39 fl = restofthetext.index(restofthetext.find{|i| i =~ /SampleFunction/})
40 $flheader = nextheader(restofthetext,fl)
41
42 dd = grouptext.index(grouptext.find{|i| i =~ /long group document/})
43 $ddheader = nextheader(grouptext,dd)
44
45 ####
46 structext = open($sampledir+"SampleStructure.3","r").readlines.reverse
47
48 fieldd = structext.index(structext.find{|i| i =~ /SampleField/})
49 $fielddheader = nextheader(structext,fieldd)
50
51 restofstructext =  structext[fieldd+1..structext.size]
52
53 fieldl = restofstructext.index(restofstructext.find{|i| i =~ /SampleField/})
54 $fieldlheader = nextheader(restofstructext, fieldl)
55
56 #
57 # Main work
58 #
59
60 ####  to find data structure documentation files and rewriting them 
61
62 def writeuntilsectionheader(buf, text, index)
63       for line in text[index .. text.size] 
64           if line =~ /^.S(H|S)/ 
65              return
66           end 
67           buf.push(line)
68      end
69  end
70
71 def  writedocumentation(buf, text, index)
72       for line in text[index .. text.size] 
73           if line =~ /^.SH/ 
74              return
75           end 
76            if 
77                line =~ /^.SS\s"(.+)"/
78                buf.push(".PP\n\\fB".concat($1).concat("\\fP\n"))
79                else
80                buf.push(line)
81             end
82       end
83  end
84
85
86 def datastructure(struct)
87
88    text = open(struct.concat($manext),"r").readlines
89
90    buf = []
91
92 #  title
93     if tline = text.index("\.SH NAME\n")
94      if text[tline+1] =~ /^([\w\s]+)\s\\-\s(.+)$/ 
95         buf.push(".SS ".concat($1).concat("\n"))
96         buf.push(".PP\n")
97         buf.push($2.concat("\n"))
98         nl = tline + 2
99         writeuntilsectionheader(buf, text, nl)
100      else
101         if 
102         text[tline+1] =~ /^([\w\s]+)\s\\-/ 
103         buf.push(".SS ".concat($1).concat("\n"))
104         buf.push(".PP\n")
105         else
106         buf.push(text[tline+1])
107         buf.push(".PP\n")   
108         end
109      end
110     end
111
112      if ddline = text.index(text.find{|i| i = $ddheader})
113        nl = ddline + 1 
114        writeuntilsectionheader(buf, text, nl)
115    end
116
117 # public types
118     if ptline = text.index("\.SS \"Public Types\"\n")
119        buf.push("\\fBPublic Types:\\fP\n")
120        nl = ptline + 1 
121        writeuntilsectionheader(buf, text, nl)
122        buf.push(".PP\n")
123    end
124
125 # member enumeration documentation
126    if medline = text.index("\.SH \"MEMBER ENUMERATION DOCUMENTATION\"\n")
127       buf.push("\.PP\n\\fBMEMBER ENUMERATION DOCUMENTATION:\\fP\n")
128        nl = medline + 1 
129        writedocumentation(buf, text, nl)
130    end
131
132 # data fields
133 #    if dfline = text.index($fieldlheader)
134 #       buf.push("\\fBData Fields:\\fP\n")
135 #       nl = dfline + 1 
136 #       writeuntilsectionheader(buf, text, nl)
137 #       buf.push(".PP\n")
138 #   end
139
140 # field documentation
141    if fdline = text.index($fielddheader)
142       buf.push("\.PP\n\\fBFIELD DOCUMENTATION:\\fP\n")
143        nl = fdline + 1 
144        writedocumentation(buf, text, nl)
145    end
146    
147 #   File.delete(struct) if FileTest.file?(struct)
148
149 return buf
150 end
151
152 ###############################
153 ### ¥Õ¥¡¥¤¥ëʬ¤±
154
155 def documentfunc(title, func_text, short_text)
156   func_text.grep(/^\.SS/){|i| 
157  #¼«Ê¬¤È¤½¤Î¸å¤í
158     func_rest =  func_text[func_text.index(i) ..func_text.size]  
159     if funclast = func_rest.index(func_rest[1 .. func_rest.size].find{|m| m =~ /^\.SS/})
160         func_desc = func_rest[0 .. funclast - 1] 
161        else
162         func_desc = func_rest
163        end
164
165     print "\n"
166     print"----\n"
167     print i
168     print "\n"
169     print func_desc
170
171
172  #fname: func_text¤ÎºÇ½é¤Î¹ÔÃæ¤Î´Ø¿ô̾ 
173    i =~ /\s([a-z0-9_]+)\s\(/
174    if $1 == nil
175      else 
176       fname = $1
177       ffname = "\\fB".concat($1.concat("\\fP"))
178   #short_text¤Î´Ø¿ô̾¤Î£²¹Ô¸å¤¬brief¡£
179       if  short_text.find{|i| i.index(ffname)}
180           brief =  short_text[short_text.index(short_text.find{|i| i.index(ffname)}) + 2]
181           documentfunc2(fname, title, func_desc, brief)
182 #        else
183 #         print "  Cannot find short desc for "
184 #          print ffname
185        end
186     end
187    }
188 end
189
190 def documentfunc2 (fname, title, func_desc, brief)
191
192  #´Ø¿ô¤´¤È¤Î¥Õ¥¡¥¤¥ë¤òºî¤ë¡£
193    file = open($doxywork+fname+$manext, "w")
194    file.puts("@function")
195  #¥Ø¥Ã¥À
196    /^\.TH \"([^"]*)\"\s/ =~ title
197    oname = $1
198 ##?????   newtitle = title.gsub(oname, fname.chop.chop)
199    newtitle = title.gsub(oname, fname)
200    file.puts(newtitle)
201  #@brief
202    file.print("@brief ") 
203    if /^.RI/ =~ brief 
204       file.puts(brief) 
205      else file.print("\n") 
206    end
207  #¥í¥ó¥°
208    file.puts(func_desc)
209 #   for line in func_desc[1 .. func_desc.size]
210 #     break if /.SS/ =~ line
211 #     file.puts(line)
212 #   end  
213    file.flush
214
215    end
216
217 ####rewriting each man file
218 ### rewriting a man file for a function
219
220 def frewrite(text)
221
222 # let the library name appear in the header 
223   buf = [text[0].gsub!(/\" \"/, "\" \"\" \"")]
224
225   title =  text[0].split(" ")[1].chop!.reverse.chop!.reverse
226
227   buf.push(".ad l\n.nh\n.SH NAME\n")
228 #  if @brief is given  
229   if text[1] =~ /\"\\fI(.+)\\fP\"/
230      buf.push(title.concat(" \- ").concat($1))
231      else buf.push(title)
232   end
233
234   synopsys = text.find{|line| line =~ /\.SS/} 
235       buf.push("\n\n.SH SYNOPSIS").push(formatsynopsys(synopsys)).push("\n")
236
237       description = text[text.index(synopsys)+2..text.size]
238     if       
239        description == []
240        print title, ": No description found\n"
241    else       
242        descriptiontext = womanrewrite(desrewrite(description))
243        buf.push("\n.SH DESCRIPTION\n").push(descriptiontext)
244    end
245  return buf
246 end
247
248 ####synopsys section of a function
249
250 def formatsynopsys(line)
251   line.chop!.chop!.reverse!.chop!.chop!.chop!.chop!.chop!.reverse!
252
253 #  line.gsub!(/\\fP\s*/,"\n.ft\n")
254 #  line.gsub!(/\\fB/,"\n.ft B\n")
255
256   line.gsub!(/\s(\w*)\)/){"\n\\fI" << $1 << "\\fP)"}
257   line.gsub!(/\s(\w*),/){"\n\\fI" << $1 << "\\fP,"}
258   line.gsub!(/\s(\w*)\s\(/){"\n\\fB" << $1 << "\\fP ("}
259 end
260
261 ####non-synopsys section of a function
262
263 def desrewrite(text)
264   returndescribed = false 
265   errordescribed = false
266
267 #removing identation
268   text.grep(/^\\fB.+\\fP/){|line| 
269                      ind = text.index(line)
270                      text.delete_at(ind+1)  if text[ind+1] == (".in +1c\n")}
271
272 ### letting verbatim end in place Part1
273   verbatim = false
274
275   text.each_with_index{|line,i|
276
277 ### TEST 6/24
278   line.gsub!(/^\.RS 4/,"")
279
280 ### letting verbatim end in place Part2
281   if line =~ /^\.nf/
282      verbatim = true
283      end
284
285   if verbatim == true
286        if line =~ /^\.PP/
287           line.gsub!(/^\.PP/,".fi")
288           verbatim = false
289         end
290   end
291
292 #removing "More..."  hyperlink
293   line.gsub!(/More.../,"")
294 # ? ad hoc 
295   line.gsub!(/^\.TP/,"")
296
297
298 #headers
299   if line =~ /Return\svalue:/
300      returndescribed = true
301   end 
302   line.gsub!(/^\\fBReturn value:\\fP/,"\n.SH RETURN VALUE\n.PP")
303   if line =~ /Errors:/  
304      errordescribed = true
305   end
306   line.gsub!(/^\\fBErrors:\\fP/,"\n.SH ERRORS\n.PP")
307   line.gsub!(/^\\fBSee Also:\\fP/,"\n.SH \"SEE ALSO\"\n.PP")
308
309   line.gsub!(/^\\fB(.+)[^\)]\\fP/){"\n.SS " << $1}
310  # [^\)] in the pattern is added to avoid the first function in see also section. 
311
312 #removing indentation
313  if text[i - 1] =~ /^.PP/
314     if line =~ /^\s./
315    line.reverse!.chop!.reverse!
316   end
317  end
318
319 # removing the results of doxygen bug 
320 # \fP required for . , ; and <> 
321    line.gsub!(/\s*(\\fP)+\s*(,|\.|;)\s+/){"\\fP" << $2 << "\n.ft R\n"}
322
323    line.gsub!(/(\\fP)+\s*>/,"\\fP>")
324    line.gsub!(/<\s+\\f(P|I|B|C|)/){"<\\f" << $1}
325
326
327    line.gsub!(/\s*(\\fP)+\s+/,"\n.ft R\n")
328
329    line.gsub!(/\s+\\f(I|C)\\fB\s*/,"\n.ft B\n")
330    line.gsub!(/\s+\\f(B|I)\\fC\s*/,"\n.ft C\n")
331    line.gsub!(/\s+\\f(B|C)\\fI\s*/,"\n.ft I\n")
332    line.gsub!(/\s+\\fB\s*/,"\n.ft B\n")
333    line.gsub!(/\s+\\fC\s*/,"\n.ft C\n")
334    line.gsub!(/\s+\\fI\s*/,"\n.ft I\n")
335
336   }
337
338 unless returndescribed == true 
339        print (" return not described \n")
340 end 
341 unless errordescribed == true 
342        print (" errors not described \n")
343 end 
344
345 return text
346 end
347
348 def womanrewrite(text)
349
350   if sasectionstart  = text.index(text.find{|line| line =~ /^\.SH\s"SEE ALSO"/})
351      aftersasection = text[sasectionstart+1 .. text.size]
352     if sasectionend = aftersasection.index(aftersasection.find{|line| line =~ /^\.SH/})
353         for line in text[sasectionstart+1 .. sasectionend + sasectionstart]
354             line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
355         end
356       else
357         for line in text[sasectionstart+1 .. text.size]
358             line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
359        end
360      end
361   end
362   return text
363 end
364
365 # rewriting a man file for a non-function
366
367 def orewrite(text)
368   buf = []
369   structures = ["\.SH \"Data Structure Documentation\"\n"]
370
371   if ddind = text.index($ddheader)
372      if odind = text.index(text.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/})
373     unless
374      text[ddind+1 .. odind-1].find{|line| line =~ /^[^\.]/}  
375      text = text[0 .. ddind-1] + text[odind .. text.size] 
376   end  end  end
377
378   text.each_with_index{|line,i|
379              line.gsub!(/More.../,"")
380      
381      ### let verbatim end in place
382         line.gsub!(/^.nf/,".NF")
383
384      ### test1/16/2004   changes the type of list, and indentation
385         if line =~ /^.IP/
386            line = ".TP"
387            text[i+2] = ""
388            end
389
390         if line =~ /^.TP/
391            text[i+2] = ""
392            end
393      ### end of test1/20/2004
394
395      # let the library name appear in the header 
396               if line =~ /^.TH/
397                  line = line.gsub!(/\" \"/, "\" \"\" \"")
398               end
399
400      # finding structure documentations and merging into "structures"
401              if line =~ /^\.RI\s\"struct\s\\fB(.*)\\fP\"/
402                 structures.push(datastructure($1))
403              end
404
405      #removing indentation
406              if text[i - 1] =~ /^.PP/
407               if line =~ /^\s./
408                line.reverse!.chop!.reverse!
409                end
410              end
411
412      #removing extra "-"
413              if text[i - 1] =~ /^.SH\sNAME/
414                 if line =~ /\\-/
415                   unless line =~ /\\-\s./
416                   line.chop!.chop!.chop!.chop!
417                   end
418                end
419              end
420
421      #removing author section
422              line.gsub!(/^\.SH\s\"AUTHOR\"/,"")
423              line.gsub!(/Generated automatically by Doxygen for m17n_test from the source code\./,"")
424
425              line.gsub!(/\\fP\s+,/,"\\fP,")
426              line.gsub!(/\\fP\s+\./,"\\fP.")
427              line.gsub!(/\\fC\\fB(\w+)\\fP\\fP/){"\\fB" << $1 << "\\fP"}
428
429       buf.push(line)
430   }
431
432   unless structures == ["\.SH \"Data Structure Documentation\"\n"]
433   
434   if dindex = buf.index(buf.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/i})
435      buf = buf[0 .. dindex-1] + structures + buf[dindex .. buf.size]
436      else 
437      buf = buf + structures
438    end
439   end 
440
441  return buf
442 end
443
444 #############################dividing files
445
446 Dir.mkdir $doxywork unless FileTest.directory? $doxywork
447
448 Dir.chdir($srcdir)
449
450 Dir.open(".").each{|filename|
451
452      if FileTest.directory? filename 
453         next
454      end   
455
456      if filename =~ /\.[ch]\./
457         next
458      end   
459
460      if filename =~ /\.txt\./
461         next
462      end   
463
464      print "RUBY DIVIDING: ", filename, "\n"
465
466      file = open(filename,"r") 
467      text = file.readlines
468      title = text[0]
469
470    if
471       sfunctionstart = text.index(text.find{|i| i == $flheader})
472         if sfunctionend = text.index(text[sfunctionstart+1 .. text.size].find{|i| i =~ /^\.SS|^\.SH/})
473         short_text = text[sfunctionstart .. sfunctionend - 1] 
474        else
475         short_text = text[sfunctionstart .. text.size - 1] 
476        end
477
478    if lfunctionstart = text.index(text.find{|i| i == $fdheader})
479       if lfunctionend = text.index(text[lfunctionstart+1 .. text.size].find{|i| i =~ /^\.SH/})
480          func_text = text[lfunctionstart .. lfunctionend - 1] 
481          group_text = text[0 .. lfunctionstart - 1] + text[lfunctionend ..text.size]
482        else 
483          func_text = text[lfunctionstart .. text.size]
484          group_text = text[0 .. lfunctionstart - 1]
485       end
486       else 
487        func_text = [] 
488        group_text = text  
489    end
490
491   documentfunc(title, func_text, short_text)
492   else
493   
494   group_text = text
495   
496   end
497    
498   filetowrite = open($doxywork+filename,"w")
499   filetowrite.puts(group_text)
500   filetowrite.flush
501 }
502
503 #############################rewriting files
504
505 Dir.chdir($dstdir)
506
507 Dir.open(".").each{|f|  File.delete(f) if FileTest.file?(f)}
508
509 Dir.chdir($doxywork)
510
511 Dir.open(".").each{|filename|
512 unless FileTest.directory? filename
513
514      file = open(filename,"r") 
515         text = file.readlines
516
517      if text.include?($fielddheader)
518         next 
519         end
520
521      print "RUBY REWRINTING: ", filename, "\n"
522
523      if /@function/ =~ text[0]  
524         buf = frewrite(text[1..text.size])
525         else buf = orewrite(text)
526       end
527
528      filetowrite = open($dstdir+filename,"w")
529      filetowrite.puts(headbuf)
530      filetowrite.puts(buf)
531      filetowrite.flush
532 end
533 }