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