2 # Usage: mkman.rb SRCDIR DSTDIR -*- coding: euc-japan; -*-
4 #¼Â¹Ô¤¹¤ë¤È/tmp/doxyman¤ò¤Ä¤«¤Ã¤Æ¥Õ¥¡¥¤¥ë¤òʬ¤±¡¢
5 #¤â¤È¤Îdirectory¤Ë¤«¤¤Ê¤ª¤¹¡£
6 # see also ¤ÎÃæ¿È¤ò woman ¸þ¤±¤Ë½ñ¤´¹¤¨¤ë¡£
11 # Setting up directory names.
14 $currentdir = Dir.pwd+"/"
15 $srcdir=$currentdir+$*[1]+"/"
16 $dstdir=$currentdir+$*[2]+"/"
17 $doxywork = $currentdir+"doxywork/"
18 $sampledir="sample/man3/"
19 $headfile = $currentdir+"manhead"
21 headbuf = open($headfile, "r").readlines
24 # Extra information about header strings Doxygen generates in a man file.
27 def nextheader(text,index)
28 header = text[index+1..text.size].find{|i| i =~ /\.S(S|H)/}
32 grouptext = open($sampledir+"SampleGroup.3","r").readlines.reverse
34 fd = grouptext.index(grouptext.find{|i| i =~ /SampleFunction/})
35 $fdheader = nextheader(grouptext,fd)
37 restofthetext = grouptext[fd+1..grouptext.size]
39 fl = restofthetext.index(restofthetext.find{|i| i =~ /SampleFunction/})
40 $flheader = nextheader(restofthetext,fl)
42 dd = grouptext.index(grouptext.find{|i| i =~ /long group document/})
43 $ddheader = nextheader(grouptext,dd)
46 structext = open($sampledir+"SampleStructure.3","r").readlines.reverse
48 fieldd = structext.index(structext.find{|i| i =~ /SampleField/})
49 $fielddheader = nextheader(structext,fieldd)
51 restofstructext = structext[fieldd+1..structext.size]
53 fieldl = restofstructext.index(restofstructext.find{|i| i =~ /SampleField/})
54 $fieldlheader = nextheader(restofstructext, fieldl)
60 #### to find data structure documentation files and rewriting them
62 def writeuntilsectionheader(buf, text, index)
63 for line in text[index .. text.size]
71 def writedocumentation(buf, text, index)
72 for line in text[index .. text.size]
77 line =~ /^.SS\s"(.+)"/
78 buf.push(".PP\n\\fB".concat($1).concat("\\fP\n"))
86 def datastructure(struct)
88 text = open(struct.concat($manext),"r").readlines
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"))
97 buf.push($2.concat("\n"))
99 writeuntilsectionheader(buf, text, nl)
102 text[tline+1] =~ /^([\w\s]+)\s\\-/
103 buf.push(".SS ".concat($1).concat("\n"))
106 buf.push(text[tline+1])
112 if ddline = text.index(text.find{|i| i = $ddheader})
114 writeuntilsectionheader(buf, text, nl)
118 if ptline = text.index("\.SS \"Public Types\"\n")
119 buf.push("\\fBPublic Types:\\fP\n")
121 writeuntilsectionheader(buf, text, nl)
125 # member enumeration documentation
126 if medline = text.index("\.SH \"MEMBER ENUMERATION DOCUMENTATION\"\n")
127 buf.push("\.PP\n\\fBMEMBER ENUMERATION DOCUMENTATION:\\fP\n")
129 writedocumentation(buf, text, nl)
133 # if dfline = text.index($fieldlheader)
134 # buf.push("\\fBData Fields:\\fP\n")
136 # writeuntilsectionheader(buf, text, nl)
140 # field documentation
141 if fdline = text.index($fielddheader)
142 buf.push("\.PP\n\\fBFIELD DOCUMENTATION:\\fP\n")
144 writedocumentation(buf, text, nl)
147 # File.delete(struct) if FileTest.file?(struct)
152 ###############################
155 def documentfunc(title, func_text, short_text)
157 func_text.grep(/^\.SS/){|i|
159 func_rest = func_text[func_text.index(i) ..func_text.size]
160 if funclast = func_rest.index(func_rest[1 .. func_rest.size].find{|m| m =~ /^\.SS/})
161 func_desc = func_rest[0 .. funclast - 1]
163 func_desc = func_rest
166 #fname: func_text¤ÎºÇ½é¤Î¹ÔÃæ¤Î´Ø¿ô̾
167 i =~ /\s([a-z0-9_]+)\s\(/
171 ffname = "\\fB".concat($1.concat("\\fP"))
172 #short_text¤Î´Ø¿ô̾¤Î£²¹Ô¸å¤¬brief¡£
173 if short_text.find{|i| i.index(ffname)}
174 brief = short_text[short_text.index(short_text.find{|i| i.index(ffname)}) + 2]
175 documentfunc2(fname, title, func_desc, brief)
176 else # obsolete function ¤Î¾ì¹ç
177 residue.push(func_desc)
184 def documentfunc2 (fname, title, func_desc, brief)
186 #´Ø¿ô¤´¤È¤Î¥Õ¥¡¥¤¥ë¤òºî¤ë¡£
187 file = open($doxywork+fname+$manext, "w")
188 file.puts("@function")
190 /^\.TH \"([^"]*)\"\s/ =~ title
192 ##????? newtitle = title.gsub(oname, fname.chop.chop)
193 newtitle = title.gsub(oname, fname)
196 file.print("@brief ")
199 else file.print("\n")
203 # for line in func_desc[1 .. func_desc.size]
204 # break if /.SS/ =~ line
211 ####rewriting each man file
212 ### rewriting a man file for a function
216 # let the library name appear in the header
217 buf = [text[0].gsub!(/\" \"/, "\" \"\" \"")]
219 title = text[0].split(" ")[1].chop!.reverse.chop!.reverse
221 buf.push(".ad l\n.nh\n.SH NAME\n")
223 if text[1] =~ /\"\\fI(.+)\\fP\"/
224 buf.push(title.concat(" \- ").concat($1))
228 synopsys = text.find{|line| line =~ /\.SS/}
229 buf.push("\n\n.SH SYNOPSIS").push(formatsynopsys(synopsys)).push("\n")
231 description = text[text.index(synopsys)+2..text.size]
234 print title, ": No description found\n"
236 descriptiontext = womanrewrite(desrewrite(description))
237 buf.push("\n.SH DESCRIPTION\n").push(descriptiontext)
242 ####synopsys section of a function
244 def formatsynopsys(line)
245 line.chop!.chop!.reverse!.chop!.chop!.chop!.chop!.chop!.reverse!
247 # line.gsub!(/\\fP\s*/,"\n.ft\n")
248 # line.gsub!(/\\fB/,"\n.ft B\n")
250 line.gsub!(/\s(\w*)\)/){"\n\\fI" << $1 << "\\fP)"}
251 line.gsub!(/\s(\w*),/){"\n\\fI" << $1 << "\\fP,"}
252 line.gsub!(/\s(\w*)\s\(/){"\n\\fB" << $1 << "\\fP ("}
255 ####non-synopsys section of a function
258 returndescribed = false
259 errordescribed = false
262 text.grep(/^\\fB.+\\fP/){|line|
263 ind = text.index(line)
264 text.delete_at(ind+1) if text[ind+1] == (".in +1c\n")}
266 ### letting verbatim end in place Part1
269 text.each_with_index{|line,i|
272 line.gsub!(/^\.RS 4/,"")
274 ### letting verbatim end in place Part2
281 line.gsub!(/^\.PP/,".fi")
286 #removing "More..." hyperlink
287 line.gsub!(/More.../,"")
289 line.gsub!(/^\.TP/,"")
293 if line =~ /Return\svalue:/
294 returndescribed = true
296 line.gsub!(/^\\fBReturn value:\\fP/,"\n.SH RETURN VALUE\n.PP")
298 errordescribed = true
300 line.gsub!(/^\\fBErrors:\\fP/,"\n.SH ERRORS\n.PP")
301 line.gsub!(/^\\fBSee Also:\\fP/,"\n.SH \"SEE ALSO\"\n.PP")
303 line.gsub!(/^\\fB(.+)[^\)]\\fP/){"\n.SS " << $1}
304 # [^\)] in the pattern is added to avoid the first function in see also section.
306 #removing indentation
307 if text[i - 1] =~ /^.PP/
309 line.reverse!.chop!.reverse!
313 # removing the results of doxygen bug
314 # \fP required for . , ; and <>
315 line.gsub!(/\s*(\\fP)+\s*(,|\.|;)\s+/){"\\fP" << $2 << "\n.ft R\n"}
317 line.gsub!(/(\\fP)+\s*>/,"\\fP>")
318 line.gsub!(/<\s+\\f(P|I|B|C|)/){"<\\f" << $1}
321 line.gsub!(/\s*(\\fP)+\s+/,"\n.ft R\n")
323 line.gsub!(/\s+\\f(I|C)\\fB\s*/,"\n.ft B\n")
324 line.gsub!(/\s+\\f(B|I)\\fC\s*/,"\n.ft C\n")
325 line.gsub!(/\s+\\f(B|C)\\fI\s*/,"\n.ft I\n")
326 line.gsub!(/\s+\\fB\s*/,"\n.ft B\n")
327 line.gsub!(/\s+\\fC\s*/,"\n.ft C\n")
328 line.gsub!(/\s+\\fI\s*/,"\n.ft I\n")
332 unless returndescribed == true
333 print (" return not described \n")
335 unless errordescribed == true
336 print (" errors not described \n")
342 def womanrewrite(text)
344 if sasectionstart = text.index(text.find{|line| line =~ /^\.SH\s"SEE ALSO"/})
345 aftersasection = text[sasectionstart+1 .. text.size]
346 if sasectionend = aftersasection.index(aftersasection.find{|line| line =~ /^\.SH/})
347 for line in text[sasectionstart+1 .. sasectionend + sasectionstart]
348 line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
351 for line in text[sasectionstart+1 .. text.size]
352 line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
359 # rewriting a man file for a non-function
363 structures = ["\.SH \"Data Structure Documentation\"\n"]
365 if ddind = text.index($ddheader)
366 if odind = text.index(text.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/})
368 text[ddind+1 .. odind-1].find{|line| line =~ /^[^\.]/}
369 text = text[0 .. ddind-1] + text[odind .. text.size]
372 text.each_with_index{|line,i|
373 line.gsub!(/More.../,"")
375 ### let verbatim end in place
376 line.gsub!(/^.nf/,".NF")
378 ### test1/16/2004 changes the type of list, and indentation
387 ### end of test1/20/2004
389 # let the library name appear in the header
391 line = line.gsub!(/\" \"/, "\" \"\" \"")
394 # finding structure documentations and merging into "structures"
395 if line =~ /^\.RI\s\"struct\s\\fB(.*)\\fP\"/
396 structures.push(datastructure($1))
399 #removing indentation
400 if text[i - 1] =~ /^.PP/
402 line.reverse!.chop!.reverse!
407 if text[i - 1] =~ /^.SH\sNAME/
409 unless line =~ /\\-\s./
410 line.chop!.chop!.chop!.chop!
415 #removing author section
416 line.gsub!(/^\.SH\s\"AUTHOR\"/,"")
417 line.gsub!(/Generated automatically by Doxygen for m17n_test from the source code\./,"")
419 line.gsub!(/\\fP\s+,/,"\\fP,")
420 line.gsub!(/\\fP\s+\./,"\\fP.")
421 line.gsub!(/\\fC\\fB(\w+)\\fP\\fP/){"\\fB" << $1 << "\\fP"}
426 unless structures == ["\.SH \"Data Structure Documentation\"\n"]
428 if dindex = buf.index(buf.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/i})
429 buf = buf[0 .. dindex-1] + structures + buf[dindex .. buf.size]
431 buf = buf + structures
438 #############################dividing files
440 Dir.mkdir $doxywork unless FileTest.directory? $doxywork
444 Dir.open(".").each{|filename|
446 if FileTest.directory? filename
450 if filename =~ /\.[ch]\./
454 if filename =~ /\.txt\./
458 print "RUBY DIVIDING: ", filename, "\n"
460 file = open(filename,"r")
461 text = file.readlines
465 sfunctionstart = text.index(text.find{|i| i == $flheader})
466 if sfunctionend = text.index(text[sfunctionstart+1 .. text.size].find{|i| i =~ /^\.SS|^\.SH/})
467 short_text = text[sfunctionstart .. sfunctionend - 1]
469 short_text = text[sfunctionstart .. text.size - 1]
472 if lfunctionstart = text.index(text.find{|i| i == $fdheader})
473 if lfunctionend = text.index(text[lfunctionstart+1 .. text.size].find{|i| i =~ /^\.SH/})
474 func_text = text[lfunctionstart .. lfunctionend - 1]
475 group_text = text[0 .. lfunctionstart - 1] + text[lfunctionend ..text.size]
477 func_text = text[lfunctionstart .. text.size]
478 group_text = text[0 .. lfunctionstart - 1]
485 residue = documentfunc(title, func_text, short_text)
489 group_text = group_text + [".SH \"Function Documentation\"\n.PP\n"] + residue
498 filetowrite = open($doxywork+filename,"w")
499 filetowrite.puts(group_text)
503 #############################rewriting files
507 Dir.open(".").each{|f| File.delete(f) if FileTest.file?(f)}
511 Dir.open(".").each{|filename|
512 unless FileTest.directory? filename
514 file = open(filename,"r")
515 text = file.readlines
517 if text.include?($fielddheader)
521 print "RUBY REWRINTING: ", filename, "\n"
523 if /@function/ =~ text[0]
524 buf = frewrite(text[1..text.size])
525 else buf = orewrite(text)
528 filetowrite = open($dstdir+filename,"w")
529 filetowrite.puts(headbuf)
530 filetowrite.puts(buf)