*** empty log message ***
[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   residue = []
157   func_text.grep(/^\.SS/){|i| 
158  #¼«Ê¬¤È¤½¤Î¸å¤í
159     func_rest =  func_text[func_text.index(i) ..func_text.size]  
160     if funclast = func_rest.index(func_rest[1 .. func_rest.size].find{|m| m =~ /^\.SS/})
161         func_desc = func_rest[0 .. funclast - 1] 
162        else
163         func_desc = func_rest
164        end
165
166  #fname: func_text¤ÎºÇ½é¤Î¹ÔÃæ¤Î´Ø¿ô̾ 
167    i =~ /\s([a-z0-9_]+)\s\(/
168    if $1 == nil
169      else 
170       fname = $1
171       ffname = "\\fB".concat($1.concat("\\fP"))
172   #short_text¤Î´Ø¿ô̾¤Î£²¹Ô¸å¤¬brief¡£
173       if  short_text.find{|i| i.index(ffname)}
174           brief =  short_text[short_text.index(short_text.find{|i| i.index(ffname)}) + 2]
175           documentfunc2(fname, title, func_desc, brief)
176         else  # obsolete function ¤Î¾ì¹ç
177           residue.push(func_desc)
178        end
179     end
180    }
181  return residue
182 end
183
184 def documentfunc2 (fname, title, func_desc, brief)
185
186  #´Ø¿ô¤´¤È¤Î¥Õ¥¡¥¤¥ë¤òºî¤ë¡£
187    file = open($doxywork+fname+$manext, "w")
188    file.puts("@function")
189  #¥Ø¥Ã¥À
190    /^\.TH \"([^"]*)\"\s/ =~ title
191    oname = $1
192 ##?????   newtitle = title.gsub(oname, fname.chop.chop)
193    newtitle = title.gsub(oname, fname)
194    file.puts(newtitle)
195  #@brief
196    file.print("@brief ") 
197    if /^.RI/ =~ brief 
198       file.puts(brief) 
199      else file.print("\n") 
200    end
201  #¥í¥ó¥°
202    file.puts(func_desc)
203 #   for line in func_desc[1 .. func_desc.size]
204 #     break if /.SS/ =~ line
205 #     file.puts(line)
206 #   end  
207    file.flush
208
209    end
210
211 ####rewriting each man file
212 ### rewriting a man file for a function
213
214 def frewrite(text)
215
216 # let the library name appear in the header 
217   buf = [text[0].gsub!(/\" \"/, "\" \"\" \"")]
218
219   title =  text[0].split(" ")[1].chop!.reverse.chop!.reverse
220
221   buf.push(".ad l\n.nh\n.SH NAME\n")
222 #  if @brief is given  
223   if text[1] =~ /\"\\fI(.+)\\fP\"/
224      buf.push(title.concat(" \- ").concat($1))
225      else buf.push(title)
226   end
227
228   synopsys = text.find{|line| line =~ /\.SS/} 
229       buf.push("\n\n.SH SYNOPSIS").push(formatsynopsys(synopsys)).push("\n")
230
231       description = text[text.index(synopsys)+2..text.size]
232     if       
233        description == []
234        print title, ": No description found\n"
235    else       
236        descriptiontext = womanrewrite(desrewrite(description))
237        buf.push("\n.SH DESCRIPTION\n").push(descriptiontext)
238    end
239  return buf
240 end
241
242 ####synopsys section of a function
243
244 def formatsynopsys(line)
245   line.chop!.chop!.reverse!.chop!.chop!.chop!.chop!.chop!.reverse!
246
247 #  line.gsub!(/\\fP\s*/,"\n.ft\n")
248 #  line.gsub!(/\\fB/,"\n.ft B\n")
249
250   line.gsub!(/\s(\w*)\)/){"\n\\fI" << $1 << "\\fP)"}
251   line.gsub!(/\s(\w*),/){"\n\\fI" << $1 << "\\fP,"}
252   line.gsub!(/\s(\w*)\s\(/){"\n\\fB" << $1 << "\\fP ("}
253 end
254
255 ####non-synopsys section of a function
256
257 def desrewrite(text)
258   returndescribed = false 
259   errordescribed = false
260
261 #removing identation
262   text.grep(/^\\fB.+\\fP/){|line| 
263                      ind = text.index(line)
264                      text.delete_at(ind+1)  if text[ind+1] == (".in +1c\n")}
265
266 ### letting verbatim end in place Part1
267   verbatim = false
268
269   text.each_with_index{|line,i|
270
271 ### TEST 6/24
272   line.gsub!(/^\.RS 4/,"")
273
274 ### letting verbatim end in place Part2
275   if line =~ /^\.nf/
276      verbatim = true
277      end
278
279   if verbatim == true
280        if line =~ /^\.PP/
281           line.gsub!(/^\.PP/,".fi")
282           verbatim = false
283         end
284   end
285
286 #removing "More..."  hyperlink
287   line.gsub!(/More.../,"")
288 # ? ad hoc 
289   line.gsub!(/^\.TP/,"")
290
291
292 #headers
293   if line =~ /Return\svalue:/
294      returndescribed = true
295   end 
296   line.gsub!(/^\\fBReturn value:\\fP/,"\n.SH RETURN VALUE\n.PP")
297   if line =~ /Errors:/  
298      errordescribed = true
299   end
300   line.gsub!(/^\\fBErrors:\\fP/,"\n.SH ERRORS\n.PP")
301   line.gsub!(/^\\fBSee Also:\\fP/,"\n.SH \"SEE ALSO\"\n.PP")
302
303   line.gsub!(/^\\fB(.+)[^\)]\\fP/){"\n.SS " << $1}
304  # [^\)] in the pattern is added to avoid the first function in see also section. 
305
306 #removing indentation
307  if text[i - 1] =~ /^.PP/
308     if line =~ /^\s./
309    line.reverse!.chop!.reverse!
310   end
311  end
312
313 # removing the results of doxygen bug 
314 # \fP required for . , ; and <> 
315    line.gsub!(/\s*(\\fP)+\s*(,|\.|;)\s+/){"\\fP" << $2 << "\n.ft R\n"}
316
317    line.gsub!(/(\\fP)+\s*>/,"\\fP>")
318    line.gsub!(/<\s+\\f(P|I|B|C|)/){"<\\f" << $1}
319
320
321    line.gsub!(/\s*(\\fP)+\s+/,"\n.ft R\n")
322
323    line.gsub!(/\s+\\f(I|C)\\fB\s*/,"\n.ft B\n")
324    line.gsub!(/\s+\\f(B|I)\\fC\s*/,"\n.ft C\n")
325    line.gsub!(/\s+\\f(B|C)\\fI\s*/,"\n.ft I\n")
326    line.gsub!(/\s+\\fB\s*/,"\n.ft B\n")
327    line.gsub!(/\s+\\fC\s*/,"\n.ft C\n")
328    line.gsub!(/\s+\\fI\s*/,"\n.ft I\n")
329
330   }
331
332 unless returndescribed == true 
333        print (" return not described \n")
334 end 
335 unless errordescribed == true 
336        print (" errors not described \n")
337 end 
338
339 return text
340 end
341
342 def womanrewrite(text)
343
344   if sasectionstart  = text.index(text.find{|line| line =~ /^\.SH\s"SEE ALSO"/})
345      aftersasection = text[sasectionstart+1 .. text.size]
346     if sasectionend = aftersasection.index(aftersasection.find{|line| line =~ /^\.SH/})
347         for line in text[sasectionstart+1 .. sasectionend + sasectionstart]
348             line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
349         end
350       else
351         for line in text[sasectionstart+1 .. text.size]
352             line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
353        end
354      end
355   end
356   return text
357 end
358
359 # rewriting a man file for a non-function
360
361 def orewrite(text)
362   buf = []
363   structures = ["\.SH \"Data Structure Documentation\"\n"]
364
365   if ddind = text.index($ddheader)
366      if odind = text.index(text.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/})
367     unless
368      text[ddind+1 .. odind-1].find{|line| line =~ /^[^\.]/}  
369      text = text[0 .. ddind-1] + text[odind .. text.size] 
370   end  end  end
371
372   text.each_with_index{|line,i|
373              line.gsub!(/More.../,"")
374      
375      ### let verbatim end in place
376         line.gsub!(/^.nf/,".NF")
377
378      ### test1/16/2004   changes the type of list, and indentation
379         if line =~ /^.IP/
380            line = ".TP"
381            text[i+2] = ""
382            end
383
384         if line =~ /^.TP/
385            text[i+2] = ""
386            end
387      ### end of test1/20/2004
388
389      # let the library name appear in the header 
390               if line =~ /^.TH/
391                  line = line.gsub!(/\" \"/, "\" \"\" \"")
392               end
393
394      # finding structure documentations and merging into "structures"
395              if line =~ /^\.RI\s\"struct\s\\fB(.*)\\fP\"/
396                 structures.push(datastructure($1))
397              end
398
399      #removing indentation
400              if text[i - 1] =~ /^.PP/
401               if line =~ /^\s./
402                line.reverse!.chop!.reverse!
403                end
404              end
405
406      #removing extra "-"
407              if text[i - 1] =~ /^.SH\sNAME/
408                 if line =~ /\\-/
409                   unless line =~ /\\-\s./
410                   line.chop!.chop!.chop!.chop!
411                   end
412                end
413              end
414
415      #removing author section
416              line.gsub!(/^\.SH\s\"AUTHOR\"/,"")
417              line.gsub!(/Generated automatically by Doxygen for m17n_test from the source code\./,"")
418
419              line.gsub!(/\\fP\s+,/,"\\fP,")
420              line.gsub!(/\\fP\s+\./,"\\fP.")
421              line.gsub!(/\\fC\\fB(\w+)\\fP\\fP/){"\\fB" << $1 << "\\fP"}
422
423       buf.push(line)
424   }
425
426   unless structures == ["\.SH \"Data Structure Documentation\"\n"]
427   
428   if dindex = buf.index(buf.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/i})
429      buf = buf[0 .. dindex-1] + structures + buf[dindex .. buf.size]
430      else 
431      buf = buf + structures
432    end
433   end 
434
435  return buf
436 end
437
438 #############################dividing files
439
440 Dir.mkdir $doxywork unless FileTest.directory? $doxywork
441
442 Dir.chdir($srcdir)
443
444 Dir.open(".").each{|filename|
445
446      if FileTest.directory? filename 
447         next
448      end   
449
450      if filename =~ /\.[ch]\./
451         next
452      end   
453
454      if filename =~ /\.txt\./
455         next
456      end   
457
458      print "RUBY DIVIDING: ", filename, "\n"
459
460      file = open(filename,"r") 
461      text = file.readlines
462      title = text[0]
463
464    if
465       sfunctionstart = text.index(text.find{|i| i == $flheader})
466         if sfunctionend = text.index(text[sfunctionstart+1 .. text.size].find{|i| i =~ /^\.SS|^\.SH/})
467         short_text = text[sfunctionstart .. sfunctionend - 1] 
468        else
469         short_text = text[sfunctionstart .. text.size - 1] 
470        end
471
472     if lfunctionstart = text.index(text.find{|i| i == $fdheader})
473        if lfunctionend = text.index(text[lfunctionstart+1 .. text.size].find{|i| i =~ /^\.SH/})
474          func_text = text[lfunctionstart .. lfunctionend - 1] 
475          group_text = text[0 .. lfunctionstart - 1] + text[lfunctionend ..text.size]
476         else 
477          func_text = text[lfunctionstart .. text.size]
478          group_text = text[0 .. lfunctionstart - 1]
479        end
480       else 
481        func_text = [] 
482        group_text = text  
483     end
484
485   residue = documentfunc(title, func_text, short_text)
486
487   if residue == []
488      else 
489      group_text = group_text + [".SH \"Function Documentation\"\n.PP\n"] + residue
490   end
491
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 }