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