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