(orewrite): Workaround the doxygen problem of inserting ".PP" before
[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
214    end
215
216 ####rewriting each man file
217 ### rewriting a man file for a function
218
219 def frewrite(text)
220
221 # let the library name appear in the header 
222   buf = [text[0].gsub!(/\" \"/, "\" \"\" \"")]
223
224   title =  text[0].split(" ")[1].chop!.reverse.chop!.reverse
225
226   buf.push(".ad l\n.nh\n.SH NAME\n")
227 #  if @brief is given  
228   if text[1] =~ /\"\\fI(.+)\\fP\"/
229      buf.push(title.concat(" \\- ").concat($1))
230      else buf.push(title.concat(" \\- "))
231   end
232
233   synopsys = text.find{|line| line =~ /\.SS/} 
234       buf.push("\n\n.SH SYNOPSIS").push(formatsynopsys(synopsys)).push("\n")
235
236       description = text[text.index(synopsys)+2..text.size]
237     if       
238        description == []
239        print title, ": No description found\n"
240    else       
241        descriptiontext = womanrewrite(desrewrite(description))
242        buf.push("\n.SH DESCRIPTION\n").push(descriptiontext)
243    end
244  return buf
245 end
246
247 ####synopsys section of a function
248
249 def formatsynopsys(line)
250   line.chop!.chop!.reverse!.chop!.chop!.chop!.chop!.chop!.reverse!
251
252 #  line.gsub!(/\\fP\s*/,"\n.ft\n")
253 #  line.gsub!(/\\fB/,"\n.ft B\n")
254
255   line.gsub!(/\s(\w*)\)/){"\n\\fI" << $1 << "\\fP)"}
256   line.gsub!(/\s(\w*),/){"\n\\fI" << $1 << "\\fP,"}
257   line.gsub!(/\s(\w*)\s\(/){"\n\\fB" << $1 << "\\fP ("}
258 end
259
260 ####non-synopsys section of a function
261
262 def desrewrite(text)
263   returndescribed = false 
264   errordescribed = false
265
266 #removing identation
267   text.grep(/^\\fB.+\\fP/){|line| 
268                      ind = text.index(line)
269                      text.delete_at(ind+1)  if text[ind+1] == (".in +1c\n")}
270
271 ### letting verbatim end in place Part1
272   verbatim = false
273
274   text.each_with_index{|line,i|
275
276 ### TEST 2008/3/3
277   line.gsub!(/\.RS 4/,"\n.RS 4")
278 #  line.gsub!(/^\.RE/,"")
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   line.gsub!(/^\\fBReturn value:\s\\fP/,"\n.SH RETURN VALUE\n.PP")
304   if line =~ /Errors:/  
305      errordescribed = true
306   end
307   line.gsub!(/^\\fBErrors:\\fP/,"\n.SH ERRORS\n.PP")
308   line.gsub!(/^\\fBSee Also:\\fP/,"\n.SH \"SEE ALSO\"\n.PP")
309 #  line.gsub!(/^\\fBSee Also:/,"\n.SH \"SEE ALSO\"\n.PP\n\\fB")
310   line.gsub!(/^\\fBExample:\\fP/,"\n.SH Example:\n.PP\n")
311
312  #test 2008/3/4 is there other headers?   line.gsub!(/^\\fB(.+)[^\)]\\fP/){"\n.SS " << $1}
313  # [^\)] in the pattern is added to avoid the first function in see also section. 
314
315 #removing indentation
316  if text[i - 1] =~ /^.PP/
317     if line =~ /^\s./
318    line.reverse!.chop!.reverse!
319   end
320  end
321
322 # removing the results of doxygen bug 
323 # \fP required for . , ; and <> 
324    line.gsub!(/\s*(\\fP)+\s*(,|\.|;)\s+/){"\\fP" << $2 << "\n.ft R\n"}
325
326    line.gsub!(/(\\fP)+\s*>/,"\\fP>")
327    line.gsub!(/<\s+\\f(P|I|B|C|)/){"<\\f" << $1}
328
329
330    line.gsub!(/\s*(\\fP)+\s+/,"\n.ft R\n")
331
332    line.gsub!(/\s+\\f(I|C)\\fB\s*/,"\n.ft B\n")
333    line.gsub!(/\s+\\f(B|I)\\fC\s*/,"\n.ft C\n")
334    line.gsub!(/\s+\\f(B|C)\\fI\s*/,"\n.ft I\n")
335    line.gsub!(/\s+\\fB\s*/,"\n.ft B\n")
336    line.gsub!(/\s+\\fC\s*/,"\n.ft C\n")
337    line.gsub!(/\s+\\fI\s*/,"\n.ft I\n")
338
339   }
340
341 unless returndescribed == true 
342        print (" return not described \n")
343 end 
344 unless errordescribed == true 
345        print (" errors not described \n")
346 end 
347
348 return text
349 end
350
351 def womanrewrite(text)
352
353   if sasectionstart  = text.index(text.find{|line| line =~ /^\.SH\s"SEE ALSO"/})
354      aftersasection = text[sasectionstart+1 .. text.size]
355     if sasectionend = aftersasection.index(aftersasection.find{|line| line =~ /^\.SH/})
356         for line in text[sasectionstart+1 .. sasectionend + sasectionstart]
357             line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
358         end
359       else
360         for line in text[sasectionstart+1 .. text.size]
361             line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
362        end
363      end
364   end
365   return text
366 end
367
368 # rewriting a man file for a non-function
369
370 def orewrite(text)
371   buf = []
372   structures = ["\.SH \"Data Structure Documentation\"\n"]
373
374   if ddind = text.index($ddheader)
375      if odind = text.index(text.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/})
376     unless
377      text[ddind+1 .. odind-1].find{|line| line =~ /^[^\.]/}  
378      text = text[0 .. ddind-1] + text[odind .. text.size] 
379   end  end  end
380
381   text.each_with_index{|line,i|
382
383      ###  changes the type of list, and indentation
384      #   if line =~ /^.IP/
385      #      line = ".TP"
386      #     text[i+2] = ""
387      #     end
388
389     #    if line =~ /^.TP/
390     #      text[i+2] = ""
391     #      end
392
393      # let the library name appear in the header 
394               if line =~ /^.TH/
395                  line = line.gsub!(/\" \"/, "\" \"\" \"")
396               end
397
398      # finding structure documentations and merging into "structures"
399              if line =~ /^\.RI\s\"struct\s\\fB(.*)\\fP\"/
400                 structures.push(datastructure($1))
401              end
402
403      #removing indentation
404              if text[i - 1] =~ /^.PP/
405               if line =~ /^\s./
406                line.reverse!.chop!.reverse!
407                end
408              end
409
410      #removing extra "-"
411 #            if text[i - 1] =~ /^.SH\sNAME/
412 #               if line =~ /\\-/
413 #                 unless line =~ /\\-\s./
414 #                  line.chop!.chop!.chop!.chop!
415 #                  end
416 #               end
417 #             end
418
419     if text[i-2] =~ /^.SH\sNAME/ && text[i] =~ /^.PP/ && text[i+1] =~ /./
420       # Convert this line sequence:
421       #   .SH NAME
422       #   M-text \- 
423       #   .PP
424       #   M-text objects and API for them.
425       # to:
426       #   .SH NAME
427       #   M-text \- M-text objects and API for them.
428       prevline = buf.pop
429       text[i+1] = prevline.chop!.concat(text[i+1])
430     else
431      #removing author section
432              line.gsub!(/^\.SH\s\"AUTHOR\"/,"")
433              line.gsub!(/Generated automatically by Doxygen for m17n_test from the source code\./,"")
434
435              line.gsub!(/^\\fBSee Also:\\fP/,"\n\\fBSEE ALSO\\fp\n")
436              line.gsub!(/^\\fBReturn value:\\fP/,"\n\\fBRETURN VALUE\\fp\n")
437              line.gsub!(/More.../,"")
438
439              line.gsub!(/\\fP\s+,/,"\\fP,")
440              line.gsub!(/\\fP\s+\./,"\\fP.")
441              line.gsub!(/\\fC\\fB(\w+)\\fP\\fP/){"\\fB" << $1 << "\\fP"}
442       buf.push(line)
443     end
444   }
445
446   unless structures == ["\.SH \"Data Structure Documentation\"\n"]
447   
448   if dindex = buf.index(buf.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/i})
449      buf = buf[0 .. dindex-1] + structures + buf[dindex .. buf.size]
450      else 
451      buf = buf + structures
452    end
453   end 
454
455  return buf
456 end
457
458 #############################dividing files
459
460 Dir.mkdir $doxywork unless FileTest.directory? $doxywork
461
462 Dir.chdir($srcdir)
463
464 Dir.open(".").each{|filename|
465
466      if FileTest.directory? filename 
467         next
468      end   
469
470      if filename =~ /\.[ch]\./
471         next
472      end   
473
474      if filename =~ /\.txt\./
475         next
476      end   
477
478      print "RUBY DIVIDING: ", filename, "\n"
479
480      file = open(filename,"r") 
481      text = file.readlines
482      title = text[0]
483
484    if
485       sfunctionstart = text.index(text.find{|i| i == $flheader})
486         if sfunctionend = text.index(text[sfunctionstart+1 .. text.size].find{|i| i =~ /^\.SS|^\.SH/})
487         short_text = text[sfunctionstart .. sfunctionend - 1] 
488        else
489         short_text = text[sfunctionstart .. text.size - 1] 
490        end
491
492     if lfunctionstart = text.index(text.find{|i| i == $fdheader})
493        if lfunctionend = text.index(text[lfunctionstart+1 .. text.size].find{|i| i =~ /^\.SH/})
494          func_text = text[lfunctionstart .. lfunctionend - 1] 
495          group_text = text[0 .. lfunctionstart - 1] + text[lfunctionend ..text.size]
496         else 
497          func_text = text[lfunctionstart .. text.size]
498          group_text = text[0 .. lfunctionstart - 1]
499        end
500       else 
501        func_text = [] 
502        group_text = text  
503     end
504
505   residue = documentfunc(title, func_text, short_text)
506
507   if residue == []
508      else 
509      group_text = group_text + [".SH \"Function Documentation\"\n.PP\n"] + residue
510   end
511
512   else
513   
514   group_text = text
515   
516   end
517    
518   filetowrite = open($doxywork+filename,"w")
519   filetowrite.puts(group_text)
520   filetowrite.flush
521 }
522
523 #############################rewriting files
524
525 Dir.chdir($dstdir)
526
527 Dir.open(".").each{|f|  File.delete(f) if FileTest.file?(f)}
528
529 Dir.chdir($doxywork)
530
531 Dir.open(".").each{|filename|
532 unless FileTest.directory? filename
533
534      file = open(filename,"r") 
535         text = file.readlines
536
537      if text.include?($fielddheader)
538         next 
539         end
540
541      print "RUBY REWRINTING: ", filename, "\n"
542
543      if /@function/ =~ text[0]  
544         buf = frewrite(text[1..text.size])
545         else buf = orewrite(text)
546       end
547
548      filetowrite = open($dstdir+filename,"w")
549      filetowrite.puts(headbuf)
550      filetowrite.puts(buf)
551      filetowrite.puts(footbuf)
552      filetowrite.flush
553 end
554 }