*** 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+"/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      returndescribed = true
263   end 
264   line.gsub!(/^\\fBReturn value:\\fP/,"\n.SH RETURNS\n.PP")
265   if line =~ /Errors:/  
266      errordescribed = true
267   end
268   line.gsub!(/^\\fBErrors:\\fP/,"\n.SH ERRORS\n.PP")
269   line.gsub!(/^\\fBSee Also:\\fP/,"\n.SH \"SEE ALSO\"\n.PP")
270
271   line.gsub!(/^\\fB(.+)\\fP/){"\n.SS " << $1}
272
273 #removing indentation
274  if text[i - 1] =~ /^.PP/
275     if line =~ /^\s./
276    line.reverse!.chop!.reverse!
277   end
278  end
279
280 # removing the results of doxygen bug 
281 # \fP required for . , ; and <> 
282    line.gsub!(/\s*(\\fP)+\s*(,|\.|;)\s+/){"\\fP" << $2 << "\n.ft R\n"}
283
284    line.gsub!(/(\\fP)+\s*>/,"\\fP>")
285    line.gsub!(/<\s+\\f(P|I|B|C|)/){"<\\f" << $1}
286
287
288    line.gsub!(/\s*(\\fP)+\s+/,"\n.ft R\n")
289
290    line.gsub!(/\s+\\f(I|C)\\fB\s*/,"\n.ft B\n")
291    line.gsub!(/\s+\\f(B|I)\\fC\s*/,"\n.ft C\n")
292    line.gsub!(/\s+\\f(B|C)\\fI\s*/,"\n.ft I\n")
293    line.gsub!(/\s+\\fB\s*/,"\n.ft B\n")
294    line.gsub!(/\s+\\fC\s*/,"\n.ft C\n")
295    line.gsub!(/\s+\\fI\s*/,"\n.ft I\n")
296
297   }
298
299 unless returndescribed == true 
300        print (" return not described \n")
301 end 
302 unless errordescribed == true 
303        print (" errors not described \n")
304 end 
305
306 return text
307 end
308
309 def womanrewrite(text)
310
311   if sasectionstart  = text.index(text.find{|line| line =~ /^\.SH\s"SEE ALSO"/})
312      aftersasection = text[sasectionstart+1 .. text.size]
313     if sasectionend = aftersasection.index(aftersasection.find{|line| line =~ /^\.SH/})
314         for line in text[sasectionstart+1 .. sasectionend + sasectionstart]
315             line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
316         end
317       else
318         for line in text[sasectionstart+1 .. text.size]
319             line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
320        end
321      end
322   end
323   return text
324 end
325
326 # rewriting a man file for a non-function
327
328 def orewrite(text)
329   buf = []
330   structures = ["\.SH \"Data Structure Documentation\"\n"]
331
332   if ddind = text.index($ddheader)
333      if odind = text.index(text.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/})
334     unless
335      text[ddind+1 .. odind-1].find{|line| line =~ /^[^\.]/}  
336      text = text[0 .. ddind-1] + text[odind .. text.size] 
337   end  end  end
338
339   text.each_with_index{|line,i|
340              line.gsub!(/More.../,"")
341
342      # let the library name appear in the header 
343               if line =~ /^.TH/
344                  line = line.gsub!("\" \"", "\" \"\" \"")
345               end
346
347      # finding structure documentations and merging into "structures"
348              if line =~ /^\.RI\s\"struct\s\\fB(.*)\\fP\"/
349                 structures.push(datastructure($1))
350              end
351
352      #removing indentation
353              if text[i - 1] =~ /^.PP/
354               if line =~ /^\s./
355                line.reverse!.chop!.reverse!
356                end
357              end
358
359      #removing extra "-"
360              if text[i - 1] =~ /^.SH\sNAME/
361                 if line =~ /\\-/
362                   unless line =~ /\\-\s./
363                   line.chop!.chop!.chop!.chop!
364                   end
365                end
366              end
367
368      #removing author section
369              line.gsub!(/^\.SH\s\"AUTHOR\"/,"")
370              line.gsub!("Generated automatically by Doxygen for m17n_test from the source code.","")
371
372              line.gsub!(/\\fP\s+,/,"\\fP,")
373              line.gsub!(/\\fP\s+\./,"\\fP.")
374              line.gsub!(/\\fC\\fB(\w+)\\fP\\fP/){"\\fB" << $1 << "\\fP"}
375
376       buf.push(line)
377   }
378
379   unless structures == ["\.SH \"Data Structure Documentation\"\n"]
380   
381   if dindex = buf.index(buf.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/i})
382      buf = buf[0 .. dindex-1] + structures + buf[dindex .. buf.size]
383      else 
384      buf = buf + structures
385    end
386   end 
387
388  return buf
389 end
390
391 #############################dividing files
392
393 Dir.mkdir $doxywork unless FileTest.directory? $doxywork
394
395 Dir.chdir($srcman3)
396
397 Dir.open(".").each{|filename|
398
399      if FileTest.directory? filename 
400         next
401      end   
402
403      #if filename =~ /\.c\./
404      #   next
405      #end   
406
407      file = open(filename,"r") 
408      text = file.readlines
409      title = text[0]
410
411    if
412       sfunctionstart = text.index(text.find{|i| i == $flheader})
413         if sfunctionend = text.index(text[sfunctionstart+1 .. text.size].find{|i| i =~ /^\.SS|^\.SH/})
414         short_text = text[sfunctionstart .. sfunctionend - 1] 
415        else
416         short_text = text[sfunctionstart .. text.size - 1] 
417        end
418
419    if lfunctionstart = text.index(text.find{|i| i == $fdheader})
420       if lfunctionend = text.index(text[lfunctionstart+1 .. text.size].find{|i| i =~ /^\.SH/})
421          func_text = text[lfunctionstart .. lfunctionend - 1] 
422          group_text = text[0 .. lfunctionstart - 1] + text[lfunctionend ..text.size]
423        else 
424          func_text = text[lfunctionstart .. text.size]
425          group_text = text[0 .. lfunctionstart - 1]
426       end
427       else 
428        func_text = [] 
429        group_text = text  
430    end
431
432    documentfunc(title, func_text, short_text)
433
434   else
435   
436   group_text = text
437   
438   end
439    
440   filetowrite = open($doxywork+filename,"w")
441   filetowrite.puts(group_text)
442   filetowrite.flush
443 }
444
445 #############################rewriting files
446
447 Dir.chdir($dstman3m)
448
449 Dir.open(".").each{|f|  File.delete(f) if FileTest.file?(f)}
450
451 Dir.chdir($doxywork)
452
453 Dir.open(".").each{|filename|
454 unless FileTest.directory? filename
455
456      file = open(filename,"r") 
457         text = file.readlines
458
459      if text.include?($fielddheader)
460         next 
461         end
462
463      print "PROCESSING: ", filename, "\n"
464
465      if /@function/ =~ text[0]  
466         buf = frewrite(text[1..text.size])
467         else buf = orewrite(text)
468       end
469
470      filetowrite = open($dstman3m+filename,"w")
471      filetowrite.puts(buf)
472      filetowrite.flush
473 end
474 }