*** empty log message ***
[m17n/m17n-docs.git] / utils / mkman2.rb
1 #! /usr/bin/ruby
2 #¼Â¹Ô¤¹¤ë¤È../work¤ò¤Ä¤«¤Ã¤Æ¥Õ¥¡¥¤¥ë¤òʬ¤±¡¢
3 #¤â¤È¤Îdirectory¤Ë¤«¤­¤Ê¤ª¤¹¡£
4 # see also ¤ÎÃæ¿È¤ò woman ¸þ¤±¤Ë½ñ¤­´¹¤¨¤ë¡£
5
6 ####  to find data structure documentation files and rewriting them 
7
8 def writeuntilsectionheader(buf, text, index)
9       for line in text[index .. text.size] 
10           if line =~ /^.S(H|S)/ 
11              return
12           end 
13           buf.push(line)
14      end
15  end
16
17 def  writedocumentation(buf, text, index)
18       for line in text[index .. text.size] 
19           if line =~ /^.SH/ 
20              return
21           end 
22            if 
23                line =~ /^.SS\s"(.+)"/
24                buf.push(".PP\n\\fB".concat($1).concat("\\fP\n"))
25                else
26                buf.push(line)
27             end
28       end
29  end
30
31
32 def datastructure(struct)
33
34    text = open(struct.concat(".3"),"r").readlines
35
36    buf = []
37
38 #  title
39     if tline = text.index("\.SH NAME\n")
40      if text[tline+1] =~ /^([\w\s]+)\s\\-\s(.+)$/ 
41         buf.push(".SS ".concat($1).concat("\n"))
42         buf.push(".PP\n")
43         buf.push($2.concat("\n"))
44         nl = tline + 2
45         writeuntilsectionheader(buf, text, nl)
46      else
47         if 
48         text[tline+1] =~ /^([\w\s]+)\s\\-/ 
49         buf.push(".SS ".concat($1).concat("\n"))
50         buf.push(".PP\n")
51         else
52         buf.push(text[tline+1])
53         buf.push(".PP\n")   
54         end
55      end
56     end
57
58     if ddline = text.index("\.SH \"DETAILED DESCRIPTION\"\n")
59        nl = ddline + 1 
60        writeuntilsectionheader(buf, text, nl)
61    end
62
63 # public types
64     if ptline = text.index("\.SS \"Public Types\"\n")
65        buf.push("\\fBPublic Types:\\fP\n")
66        nl = ptline + 1 
67        writeuntilsectionheader(buf, text, nl)
68        buf.push(".PP\n")
69    end
70
71 # member enumeration documentation
72    if medline = text.index("\.SH \"MEMBER ENUMERATION DOCUMENTATION\"\n")
73       buf.push("\.PP\n\\fBMEMBER ENUMERATION DOCUMENTATION:\\fP\n")
74        nl = medline + 1 
75        writedocumentation(buf, text, nl)
76    end
77
78 # data fileds
79     if dfline = text.index("\.SS \"Data Fields\"\n")
80        buf.push("\\fBData Fields:\\fP\n")
81        nl = dfline + 1 
82        writeuntilsectionheader(buf, text, nl)
83        buf.push(".PP\n")
84    end
85
86 # field documentation
87    if fdline = text.index("\.SH \"FIELD DOCUMENTATION\"\n")
88       buf.push("\.PP\n\\fBFIELD DOCUMENTATION:\\fP\n")
89        nl = fdline + 1 
90        writedocumentation(buf, text, nl)
91    end
92
93 return buf
94 end
95
96 ###############################
97 ### ¥Õ¥¡¥¤¥ëʬ¤±
98
99 def documentfunc(title, func_text, short_text)
100   func_text.grep(/^\.SS/){|i| 
101  #¼«Ê¬¤è¤ê¸å¤í¤À¤±
102       func_rest =  func_text[func_text.index(i)..func_text.size] 
103       documentfunc2(i, title, func_rest, short_text)}
104 end
105
106 def documentfunc2 (dstart, title, func_text, short_text)
107  #func_text¤ÎºÇ½é¤Î¹ÔÃæ¤Î´Ø¿ô̾
108    dstart =~ /\s([a-z_]+)\s\(/
109    return if $1 == nil
110    fname = $1
111    ffname = "\\fB".concat($1.concat("\\fP"))
112  #short_text¤Î´Ø¿ô̾¤Î£²¹Ô¸å¤¬brief¡£
113    brief =  short_text[short_text.index(short_text.find{|i| i.index(ffname)}) + 2]
114  #´Ø¿ô¤´¤È¤Î¥Õ¥¡¥¤¥ë¤òºî¤ë¡£
115    file = open("../work/".concat(fname.concat(".3")), "w")
116    file.puts("@function")
117  #¥Ø¥Ã¥À
118    /^\.TH \"([^"]*)\"\s/ =~ title
119    oname = $1
120    newtitle = title.gsub(oname, fname.chop.chop)
121    file.puts(newtitle)
122  #@brief
123    file.print("@brief ") 
124    if /^.RI/ =~ brief 
125       file.puts(brief) 
126      else file.print("\n") 
127    end
128  #¥í¥ó¥°
129    file.puts(func_text[0])
130    for line in func_text[1 .. func_text.size]
131      break if /.SS/ =~ line
132      file.puts(line)
133    end  
134    file.flush
135    end
136
137 ####rewriting each man file
138 ### rewriting a man file for a function
139
140 def frewrite(text)
141   buf = [text[0]]
142   title =  text[0].split(" ")[1].chop!.reverse.chop!.reverse
143
144   if text.index("\\fBReturns: \\fP\n")  == nil
145    print  title, ": Returns not described\n" 
146    end  
147   if text.index("\\fBErrors: \\fP\n")  == nil
148    print  title, ": Errors not described\n" 
149    end
150
151   buf.push(".ad l\n.nh\n.SH NAME\n")
152 #  if @brief is given  
153   if text[1] =~ /\"\\fI(.+)\\fP\"/
154      buf.push(title.concat(" \- ").concat($1))
155      else buf.push(title)
156   end
157
158   synopsys = text.find{|line| line =~ /\.SS/} 
159   buf.push("\n\n.SH SYNOPSIS").push(formatsynopsys(synopsys)).push("\n")
160
161   description = text[text.index(synopsys)+2..text.size]
162   if 
163     description == []
164     print title, ": No description found\n"
165     else
166     buf.push("\n.SH DESCRIPTION\n").push(womanrewrite(desrewrite(description)))
167   end
168  return buf
169 end
170
171 ####synopsys section of a function
172
173 def formatsynopsys(line)
174   line.chop!.chop!.reverse!.chop!.chop!.chop!.chop!.chop!.reverse!
175
176 #  line.gsub!(/\\fP\s*/,"\n.ft\n")
177 #  line.gsub!(/\\fB/,"\n.ft B\n")
178
179   line.gsub!(/\s(\w*)\)/){"\n\\fI" << $1 << "\\fP)"}
180   line.gsub!(/\s(\w*),/){"\n\\fI" << $1 << "\\fP,"}
181   line.gsub!(/\s(\w*)\s\(/){"\n\\fB" << $1 << "\\fP ("}
182 end
183
184 ####non-synopsys section of a function
185
186 def desrewrite(text)
187
188 #removing identation
189   text.grep(/^\\fB.+\\fP/){|line| 
190                      ind = text.index(line)
191                      text.delete_at(ind+1)  if text[ind+1] == (".in +1c\n")}
192
193
194   text.each_with_index{|line,i|
195
196 #removing "More..."  hyperlink
197   line.gsub!(/More.../,"")
198 # ? ad hoc 
199   line.gsub!(/^\.TP/,"")
200
201 #headers
202   line.gsub!(/^\\fBReturns: \\fP/,"\n.SH RETURNS\n.PP")
203   line.gsub!(/^\\fBSee Also:\\fP/,"\n.SH \"SEE ALSO\"\n.PP")
204   line.gsub!(/^\\fBErrors:\\fP/,"\n.SH ERRORS\n.PP")
205
206   line.gsub!(/^\\fB(.+)\\fP/){"\n.SS " << $1}
207
208 #removing indentation
209  if text[i - 1] =~ /^.PP/
210     if line =~ /^\s./
211    line.reverse!.chop!.reverse!
212   end
213  end
214
215 # removing the results of doxygen bug 
216 # \fP required for . , ; and <> 
217    line.gsub!(/\s*(\\fP)+\s*(,|\.|;)\s+/){"\\fP" << $2 << "\n.ft R\n"}
218
219    line.gsub!(/(\\fP)+\s*>/,"\\fP>")
220    line.gsub!(/<\s+\\f(P|I|B|C|)/){"<\\f" << $1}
221
222
223    line.gsub!(/\s*(\\fP)+\s+/,"\n.ft R\n")
224
225    line.gsub!(/\s+\\f(I|C)\\fB\s*/,"\n.ft B\n")
226    line.gsub!(/\s+\\f(B|I)\\fC\s*/,"\n.ft C\n")
227    line.gsub!(/\s+\\f(B|C)\\fI\s*/,"\n.ft I\n")
228    line.gsub!(/\s+\\fB\s*/,"\n.ft B\n")
229    line.gsub!(/\s+\\fC\s*/,"\n.ft C\n")
230    line.gsub!(/\s+\\fI\s*/,"\n.ft I\n")
231
232   }
233
234 return text
235 end
236
237 def womanrewrite(text)
238
239   if sasectionstart  = text.index(text.find{|line| line =~ /^\.SH\s"SEE ALSO"/})
240      aftersasection = text[sasectionstart+1 .. text.size]
241     if sasectionend = aftersasection.index(aftersasection.find{|line| line =~ /^\.SH/})
242         for line in text[sasectionstart+1 .. sasectionend + sasectionstart]
243             line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
244         end
245       else
246         for line in text[sasectionstart+1 .. text.size]
247             line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
248        end
249      end
250   end
251   return text
252 end
253
254 # rewriting a man file for a non-function
255
256 def orewrite(text)
257   buf = []
258   structures = ["\.SH \"Data Structure Documentation\"\n"]
259
260   if ddind = text.index("\.SH \"DETAILED DESCRIPTION\"\n")
261      if odind = text.index(text.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/})
262     unless
263      text[ddind+1 .. odind-1].find{|line| line =~ /^[^\.]/}  
264      text = text[0 .. ddind-1] + text[odind .. text.size] 
265   end  end  end
266
267   text.each_with_index{|line,i|
268              line.gsub!(/More.../,"")
269
270      # finding structure documentations and merging into "structures"
271              if line =~ /^\.RI\s\"struct\s\\fB(.*)\\fP\"/
272                 structures.push(datastructure($1))
273              end
274
275      #removing indentation
276              if text[i - 1] =~ /^.PP/
277               if line =~ /^\s./
278                line.reverse!.chop!.reverse!
279                end
280              end
281
282      #removing extra "-"
283              if text[i - 1] =~ /^.SH\sNAME/
284                 if line =~ /\\-/
285                   unless line =~ /\\-\s./
286                   line.chop!.chop!.chop!.chop!
287                   end
288                end
289              end
290
291      #removing author section
292              line.gsub!(/^\.SH\s\"AUTHOR\"/,"")
293              line.gsub!("Generated automatically by Doxygen for m17n_test from the source code.","")
294
295              line.gsub!(/\\fP\s+,/,"\\fP,")
296              line.gsub!(/\\fP\s+\./,"\\fP.")
297              line.gsub!(/\\fC\\fB(\w+)\\fP\\fP/){"\\fB" << $1 << "\\fP"}
298
299       buf.push(line)
300   }
301
302   unless structures == ["\.SH \"Data Structure Documentation\"\n"]
303   
304   if dindex = buf.index(buf.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/})
305      buf = buf[0 .. dindex-1] + structures + buf[dindex .. buf.size]
306      else 
307      buf = buf + structures
308    end
309   end 
310
311  return buf
312 end
313
314 #############################dividing files
315
316 Dir.mkdir "../work" unless FileTest.directory? "../work"
317
318 Dir.open(".").each{|filename|
319      if FileTest.directory? filename 
320         next
321      end   
322
323      if filename =~ /\.c\./
324 #       File.delete(filename)
325         next
326      end   
327
328      file = open(filename,"r") 
329      text = file.readlines
330      title = text[0]
331
332    if
333       sfunctionstart = text.index(text.find{|i| i =~ /^\.SS "Function/})
334         if sfunctionend = text.index(text[sfunctionstart+1 .. text.size].find{|i| i =~ /^\.SS|^\.SH/})
335         short_text = text[sfunctionstart .. sfunctionend - 1] 
336        else
337         short_text = text[sfunctionstart .. text.size - 1] 
338        end
339
340    if lfunctionstart = text.index(text.find{|i| i =~ /^\.SH "FUNCTION DOCUMENTATION"/})
341       if lfunctionend = text.index(text[lfunctionstart+1 .. text.size].find{|i| i =~ /^\.SH/})
342          func_text = text[lfunctionstart .. lfunctionend - 1] 
343          group_text = text[0 .. lfunctionstart - 1] + text[lfunctionend ..text.size]
344        else 
345          func_text = text[lfunctionstart .. text.size]
346          group_text = text[0 .. lfunctionstart - 1]
347       end
348       else 
349        func_text = [] 
350        group_text = text  
351    end
352
353    documentfunc(title, func_text, short_text)
354
355   else
356   
357   group_text = text
358   
359   end
360    
361   filetowrite = open("../work/".concat(filename),"w")
362   filetowrite.puts(group_text)
363   filetowrite.flush
364 }
365
366
367 #############################rewriting files
368 Dir.mkdir "../../man" unless FileTest.directory? "../../man"
369 Dir.mkdir "../../man/man3" unless FileTest.directory? "../../man/man3"
370
371
372 Dir.open("../work").each{|filename|
373 unless FileTest.directory? filename
374
375    print "PROCESSING: ", filename, "\n"
376
377     file = open("../work/".concat(filename),"r") 
378     text = file.readlines
379
380  if /@function/ =~ text[0]  
381     buf = frewrite(text[1..text.size])
382     else buf = orewrite(text)
383  end
384
385   filetowrite = open("../../man/man3/".concat(filename),"w")
386   filetowrite.puts(buf)
387   filetowrite.flush
388
389 end
390 }
391
392
393 Dir.chdir("../work")
394
395 Dir.open(".").each{|f|  File.delete(f) if FileTest.file?(f)}