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