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