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