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