*** 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   if text.index("\\fBReturns: \\fP\n")  == nil
205    print  title, ": Returns not described\n" 
206    end  
207   if text.index("\\fBErrors: \\fP\n")  == nil
208    print  title, ": Errors not described\n" 
209    end
210
211   buf.push(".ad l\n.nh\n.SH NAME\n")
212 #  if @brief is given  
213   if text[1] =~ /\"\\fI(.+)\\fP\"/
214      buf.push(title.concat(" \- ").concat($1))
215      else buf.push(title)
216   end
217
218   synopsys = text.find{|line| line =~ /\.SS/} 
219   buf.push("\n\n.SH SYNOPSIS").push(formatsynopsys(synopsys)).push("\n")
220
221   description = text[text.index(synopsys)+2..text.size]
222   if 
223     description == []
224     print title, ": No description found\n"
225     else
226     buf.push("\n.SH DESCRIPTION\n").push(womanrewrite(desrewrite(description)))
227   end
228  return buf
229 end
230
231 ####synopsys section of a function
232
233 def formatsynopsys(line)
234   line.chop!.chop!.reverse!.chop!.chop!.chop!.chop!.chop!.reverse!
235
236 #  line.gsub!(/\\fP\s*/,"\n.ft\n")
237 #  line.gsub!(/\\fB/,"\n.ft B\n")
238
239   line.gsub!(/\s(\w*)\)/){"\n\\fI" << $1 << "\\fP)"}
240   line.gsub!(/\s(\w*),/){"\n\\fI" << $1 << "\\fP,"}
241   line.gsub!(/\s(\w*)\s\(/){"\n\\fB" << $1 << "\\fP ("}
242 end
243
244 ####non-synopsys section of a function
245
246 def desrewrite(text)
247
248 #removing identation
249   text.grep(/^\\fB.+\\fP/){|line| 
250                      ind = text.index(line)
251                      text.delete_at(ind+1)  if text[ind+1] == (".in +1c\n")}
252
253
254   text.each_with_index{|line,i|
255
256 #removing "More..."  hyperlink
257   line.gsub!(/More.../,"")
258 # ? ad hoc 
259   line.gsub!(/^\.TP/,"")
260
261 #headers
262   line.gsub!(/^\\fBReturn value:\\fP/,"\n.SH RETURNS\n.PP")
263   line.gsub!(/^\\fBSee Also:\\fP/,"\n.SH \"SEE ALSO\"\n.PP")
264   line.gsub!(/^\\fBErrors:\\fP/,"\n.SH ERRORS\n.PP")
265
266   line.gsub!(/^\\fB(.+)\\fP/){"\n.SS " << $1}
267
268 #removing indentation
269  if text[i - 1] =~ /^.PP/
270     if line =~ /^\s./
271    line.reverse!.chop!.reverse!
272   end
273  end
274
275 # removing the results of doxygen bug 
276 # \fP required for . , ; and <> 
277    line.gsub!(/\s*(\\fP)+\s*(,|\.|;)\s+/){"\\fP" << $2 << "\n.ft R\n"}
278
279    line.gsub!(/(\\fP)+\s*>/,"\\fP>")
280    line.gsub!(/<\s+\\f(P|I|B|C|)/){"<\\f" << $1}
281
282
283    line.gsub!(/\s*(\\fP)+\s+/,"\n.ft R\n")
284
285    line.gsub!(/\s+\\f(I|C)\\fB\s*/,"\n.ft B\n")
286    line.gsub!(/\s+\\f(B|I)\\fC\s*/,"\n.ft C\n")
287    line.gsub!(/\s+\\f(B|C)\\fI\s*/,"\n.ft I\n")
288    line.gsub!(/\s+\\fB\s*/,"\n.ft B\n")
289    line.gsub!(/\s+\\fC\s*/,"\n.ft C\n")
290    line.gsub!(/\s+\\fI\s*/,"\n.ft I\n")
291
292   }
293
294 return text
295 end
296
297 def womanrewrite(text)
298
299   if sasectionstart  = text.index(text.find{|line| line =~ /^\.SH\s"SEE ALSO"/})
300      aftersasection = text[sasectionstart+1 .. text.size]
301     if sasectionend = aftersasection.index(aftersasection.find{|line| line =~ /^\.SH/})
302         for line in text[sasectionstart+1 .. sasectionend + sasectionstart]
303             line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
304         end
305       else
306         for line in text[sasectionstart+1 .. text.size]
307             line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
308        end
309      end
310   end
311   return text
312 end
313
314 # rewriting a man file for a non-function
315
316 def orewrite(text)
317   buf = []
318   structures = ["\.SH \"Data Structure Documentation\"\n"]
319
320   if ddind = text.index($ddheader)
321      if odind = text.index(text.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/})
322     unless
323      text[ddind+1 .. odind-1].find{|line| line =~ /^[^\.]/}  
324      text = text[0 .. ddind-1] + text[odind .. text.size] 
325   end  end  end
326
327   text.each_with_index{|line,i|
328              line.gsub!(/More.../,"")
329
330      # let the library name appear in the header 
331               if line =~ /^.TH/
332                  line = line.gsub!("\" \"", "\" \"\" \"")
333               end
334
335      # finding structure documentations and merging into "structures"
336              if line =~ /^\.RI\s\"struct\s\\fB(.*)\\fP\"/
337                 structures.push(datastructure($1))
338              end
339
340      #removing indentation
341              if text[i - 1] =~ /^.PP/
342               if line =~ /^\s./
343                line.reverse!.chop!.reverse!
344                end
345              end
346
347      #removing extra "-"
348              if text[i - 1] =~ /^.SH\sNAME/
349                 if line =~ /\\-/
350                   unless line =~ /\\-\s./
351                   line.chop!.chop!.chop!.chop!
352                   end
353                end
354              end
355
356      #removing author section
357              line.gsub!(/^\.SH\s\"AUTHOR\"/,"")
358              line.gsub!("Generated automatically by Doxygen for m17n_test from the source code.","")
359
360              line.gsub!(/\\fP\s+,/,"\\fP,")
361              line.gsub!(/\\fP\s+\./,"\\fP.")
362              line.gsub!(/\\fC\\fB(\w+)\\fP\\fP/){"\\fB" << $1 << "\\fP"}
363
364       buf.push(line)
365   }
366
367   unless structures == ["\.SH \"Data Structure Documentation\"\n"]
368   
369   if dindex = buf.index(buf.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/i})
370      buf = buf[0 .. dindex-1] + structures + buf[dindex .. buf.size]
371      else 
372      buf = buf + structures
373    end
374   end 
375
376  return buf
377 end
378
379 #############################dividing files
380
381 Dir.mkdir $doxywork unless FileTest.directory? $doxywork
382
383 Dir.chdir($srcman3)
384
385 Dir.open(".").each{|filename|
386      if FileTest.directory? filename 
387         next
388      end   
389
390      #if filename =~ /\.c\./
391      #   next
392      #end   
393
394      file = open(filename,"r") 
395      text = file.readlines
396      title = text[0]
397
398    if
399       sfunctionstart = text.index(text.find{|i| i == $flheader})
400         if sfunctionend = text.index(text[sfunctionstart+1 .. text.size].find{|i| i =~ /^\.SS|^\.SH/})
401         short_text = text[sfunctionstart .. sfunctionend - 1] 
402        else
403         short_text = text[sfunctionstart .. text.size - 1] 
404        end
405
406    if lfunctionstart = text.index(text.find{|i| i == $fdheader})
407       if lfunctionend = text.index(text[lfunctionstart+1 .. text.size].find{|i| i =~ /^\.SH/})
408          func_text = text[lfunctionstart .. lfunctionend - 1] 
409          group_text = text[0 .. lfunctionstart - 1] + text[lfunctionend ..text.size]
410        else 
411          func_text = text[lfunctionstart .. text.size]
412          group_text = text[0 .. lfunctionstart - 1]
413       end
414       else 
415        func_text = [] 
416        group_text = text  
417    end
418
419    documentfunc(title, func_text, short_text)
420
421   else
422   
423   group_text = text
424   
425   end
426    
427   filetowrite = open($doxywork+filename,"w")
428   filetowrite.puts(group_text)
429   filetowrite.flush
430 }
431
432 #############################rewriting files
433
434 Dir.chdir($dstman3m)
435
436 Dir.open(".").each{|f|  File.delete(f) if FileTest.file?(f)}
437
438 Dir.chdir($doxywork)
439
440 Dir.open(".").each{|filename|
441 unless FileTest.directory? filename
442
443      file = open(filename,"r") 
444         text = file.readlines
445
446      if text.include?($fielddheader)
447         next 
448         end
449
450      print "PROCESSING: ", filename, "\n"
451
452      if /@function/ =~ text[0]  
453         buf = frewrite(text[1..text.size])
454         else buf = orewrite(text)
455       end
456
457      filetowrite = open($dstman3m+filename,"w")
458      filetowrite.puts(buf)
459      filetowrite.flush
460 end
461 }