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