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