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