*** 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 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    dstart =~ /\s([a-z_]+)\s\(/
127    return if $1 == nil
128    fname = $1
129    ffname = "\\fB".concat($1.concat("\\fP"))
130  #short_text¤Î´Ø¿ô̾¤Î£²¹Ô¸å¤¬brief¡£
131    brief =  short_text[short_text.index(short_text.find{|i| i.index(ffname)}) + 2]
132  #´Ø¿ô¤´¤È¤Î¥Õ¥¡¥¤¥ë¤òºî¤ë¡£
133    file = open($doxywork+fname+".3m", "w")
134    file.puts("@function")
135  #¥Ø¥Ã¥À
136    /^\.TH \"([^"]*)\"\s/ =~ title
137    oname = $1
138    newtitle = title.gsub(oname, fname.chop.chop)
139    file.puts(newtitle)
140  #@brief
141    file.print("@brief ") 
142    if /^.RI/ =~ brief 
143       file.puts(brief) 
144      else file.print("\n") 
145    end
146  #¥í¥ó¥°
147    file.puts(func_text[0])
148    for line in func_text[1 .. func_text.size]
149      break if /.SS/ =~ line
150      file.puts(line)
151    end  
152    file.flush
153    end
154
155 ####rewriting each man file
156 ### rewriting a man file for a function
157
158 def frewrite(text)
159 # let the library name appear in the header 
160   buf = [text[0].gsub!("\" \"", "\" \"\" \"")]
161
162   title =  text[0].split(" ")[1].chop!.reverse.chop!.reverse
163
164   if text.index("\\fBReturns: \\fP\n")  == nil
165    print  title, ": Returns not described\n" 
166    end  
167   if text.index("\\fBErrors: \\fP\n")  == nil
168    print  title, ": Errors not described\n" 
169    end
170
171   buf.push(".ad l\n.nh\n.SH NAME\n")
172 #  if @brief is given  
173   if text[1] =~ /\"\\fI(.+)\\fP\"/
174      buf.push(title.concat(" \- ").concat($1))
175      else buf.push(title)
176   end
177
178   synopsys = text.find{|line| line =~ /\.SS/} 
179   buf.push("\n\n.SH SYNOPSIS").push(formatsynopsys(synopsys)).push("\n")
180
181   description = text[text.index(synopsys)+2..text.size]
182   if 
183     description == []
184     print title, ": No description found\n"
185     else
186     buf.push("\n.SH DESCRIPTION\n").push(womanrewrite(desrewrite(description)))
187   end
188  return buf
189 end
190
191 ####synopsys section of a function
192
193 def formatsynopsys(line)
194   line.chop!.chop!.reverse!.chop!.chop!.chop!.chop!.chop!.reverse!
195
196 #  line.gsub!(/\\fP\s*/,"\n.ft\n")
197 #  line.gsub!(/\\fB/,"\n.ft B\n")
198
199   line.gsub!(/\s(\w*)\)/){"\n\\fI" << $1 << "\\fP)"}
200   line.gsub!(/\s(\w*),/){"\n\\fI" << $1 << "\\fP,"}
201   line.gsub!(/\s(\w*)\s\(/){"\n\\fB" << $1 << "\\fP ("}
202 end
203
204 ####non-synopsys section of a function
205
206 def desrewrite(text)
207
208 #removing identation
209   text.grep(/^\\fB.+\\fP/){|line| 
210                      ind = text.index(line)
211                      text.delete_at(ind+1)  if text[ind+1] == (".in +1c\n")}
212
213
214   text.each_with_index{|line,i|
215
216 #removing "More..."  hyperlink
217   line.gsub!(/More.../,"")
218 # ? ad hoc 
219   line.gsub!(/^\.TP/,"")
220
221 #headers
222   line.gsub!(/^\\fBReturn value:\\fP/,"\n.SH RETURNS\n.PP")
223   line.gsub!(/^\\fBSee Also:\\fP/,"\n.SH \"SEE ALSO\"\n.PP")
224   line.gsub!(/^\\fBErrors:\\fP/,"\n.SH ERRORS\n.PP")
225
226   line.gsub!(/^\\fB(.+)\\fP/){"\n.SS " << $1}
227
228 #removing indentation
229  if text[i - 1] =~ /^.PP/
230     if line =~ /^\s./
231    line.reverse!.chop!.reverse!
232   end
233  end
234
235 # removing the results of doxygen bug 
236 # \fP required for . , ; and <> 
237    line.gsub!(/\s*(\\fP)+\s*(,|\.|;)\s+/){"\\fP" << $2 << "\n.ft R\n"}
238
239    line.gsub!(/(\\fP)+\s*>/,"\\fP>")
240    line.gsub!(/<\s+\\f(P|I|B|C|)/){"<\\f" << $1}
241
242
243    line.gsub!(/\s*(\\fP)+\s+/,"\n.ft R\n")
244
245    line.gsub!(/\s+\\f(I|C)\\fB\s*/,"\n.ft B\n")
246    line.gsub!(/\s+\\f(B|I)\\fC\s*/,"\n.ft C\n")
247    line.gsub!(/\s+\\f(B|C)\\fI\s*/,"\n.ft I\n")
248    line.gsub!(/\s+\\fB\s*/,"\n.ft B\n")
249    line.gsub!(/\s+\\fC\s*/,"\n.ft C\n")
250    line.gsub!(/\s+\\fI\s*/,"\n.ft I\n")
251
252   }
253
254 return text
255 end
256
257 def womanrewrite(text)
258
259   if sasectionstart  = text.index(text.find{|line| line =~ /^\.SH\s"SEE ALSO"/})
260      aftersasection = text[sasectionstart+1 .. text.size]
261     if sasectionend = aftersasection.index(aftersasection.find{|line| line =~ /^\.SH/})
262         for line in text[sasectionstart+1 .. sasectionend + sasectionstart]
263             line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
264         end
265       else
266         for line in text[sasectionstart+1 .. text.size]
267             line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
268        end
269      end
270   end
271   return text
272 end
273
274 # rewriting a man file for a non-function
275
276 def orewrite(text)
277   buf = []
278   structures = ["\.SH \"Data Structure Documentation\"\n"]
279
280   if ddind = text.index($ddheader)
281      if odind = text.index(text.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/})
282     unless
283      text[ddind+1 .. odind-1].find{|line| line =~ /^[^\.]/}  
284      text = text[0 .. ddind-1] + text[odind .. text.size] 
285   end  end  end
286
287   text.each_with_index{|line,i|
288              line.gsub!(/More.../,"")
289
290      # let the library name appear in the header 
291               if line =~ /^.TH/
292                  line = line.gsub!("\" \"", "\" \"\" \"")
293               end
294
295      # finding structure documentations and merging into "structures"
296              if line =~ /^\.RI\s\"struct\s\\fB(.*)\\fP\"/
297                 structures.push(datastructure($1))
298              end
299
300      #removing indentation
301              if text[i - 1] =~ /^.PP/
302               if line =~ /^\s./
303                line.reverse!.chop!.reverse!
304                end
305              end
306
307      #removing extra "-"
308              if text[i - 1] =~ /^.SH\sNAME/
309                 if line =~ /\\-/
310                   unless line =~ /\\-\s./
311                   line.chop!.chop!.chop!.chop!
312                   end
313                end
314              end
315
316      #removing author section
317              line.gsub!(/^\.SH\s\"AUTHOR\"/,"")
318              line.gsub!("Generated automatically by Doxygen for m17n_test from the source code.","")
319
320              line.gsub!(/\\fP\s+,/,"\\fP,")
321              line.gsub!(/\\fP\s+\./,"\\fP.")
322              line.gsub!(/\\fC\\fB(\w+)\\fP\\fP/){"\\fB" << $1 << "\\fP"}
323
324       buf.push(line)
325   }
326
327   unless structures == ["\.SH \"Data Structure Documentation\"\n"]
328   
329   if dindex = buf.index(buf.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/i})
330      buf = buf[0 .. dindex-1] + structures + buf[dindex .. buf.size]
331      else 
332      buf = buf + structures
333    end
334   end 
335
336  return buf
337 end
338
339 #############################dividing files
340
341 Dir.mkdir $doxywork unless FileTest.directory? $doxywork
342
343 Dir.chdir($srcman3)
344
345 Dir.open(".").each{|filename|
346      if FileTest.directory? filename 
347         next
348      end   
349
350      if filename =~ /\.c\./
351         next
352      end   
353
354      file = open(filename,"r") 
355      text = file.readlines
356      title = text[0]
357
358    if
359       sfunctionstart = text.index(text.find{|i| i == $flheader})
360         if sfunctionend = text.index(text[sfunctionstart+1 .. text.size].find{|i| i =~ /^\.SS|^\.SH/})
361         short_text = text[sfunctionstart .. sfunctionend - 1] 
362        else
363         short_text = text[sfunctionstart .. text.size - 1] 
364        end
365
366    if lfunctionstart = text.index(text.find{|i| i == $fdheader})
367       if lfunctionend = text.index(text[lfunctionstart+1 .. text.size].find{|i| i =~ /^\.SH/})
368          func_text = text[lfunctionstart .. lfunctionend - 1] 
369          group_text = text[0 .. lfunctionstart - 1] + text[lfunctionend ..text.size]
370        else 
371          func_text = text[lfunctionstart .. text.size]
372          group_text = text[0 .. lfunctionstart - 1]
373       end
374       else 
375        func_text = [] 
376        group_text = text  
377    end
378
379    documentfunc(title, func_text, short_text)
380
381   else
382   
383   group_text = text
384   
385   end
386    
387   filetowrite = open($doxywork+filename,"w")
388   filetowrite.puts(group_text)
389   filetowrite.flush
390 }
391
392 #############################rewriting files
393
394 Dir.chdir($dstman3m)
395
396 Dir.open(".").each{|f|  File.delete(f) if FileTest.file?(f)}
397
398 Dir.chdir($doxywork)
399
400 Dir.open(".").each{|filename|
401 unless FileTest.directory? filename
402
403    print "PROCESSING: ", filename, "\n"
404
405     file = open(filename,"r") 
406     text = file.readlines
407
408  if /@function/ =~ text[0]  
409     buf = frewrite(text[1..text.size])
410     else buf = orewrite(text)
411  end
412
413   filetowrite = open($dstman3m+filename,"w")
414   filetowrite.puts(buf)
415   filetowrite.flush
416
417 end
418 }
419
420 Dir.chdir($doxywork)
421
422 Dir.open(".").each{|f|  File.delete(f) if FileTest.file?(f)}