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/"
22 $headfile = $currentdir+"manhead"
23 $footfile = $currentdir+"manfoot"
25 headbuf = open($headfile, "r").readlines
26 footbuf = open($footfile, "r").readlines
29 # Extra information about header strings Doxygen generates in a man file.
32 def nextheader(text,index)
33 header = text[index+1..text.size].find{|i| i =~ /\.S(S|H)/}
37 grouptext = open($sampledir+"SampleGroup.3","r").readlines.reverse
39 fd = grouptext.index(grouptext.find{|i| i =~ /SampleFunction/})
40 $fdheader = nextheader(grouptext,fd)
42 restofthetext = grouptext[fd+1..grouptext.size]
44 fl = restofthetext.index(restofthetext.find{|i| i =~ /SampleFunction/})
45 $flheader = nextheader(restofthetext,fl)
47 dd = grouptext.index(grouptext.find{|i| i =~ /long group document/})
48 $ddheader = nextheader(grouptext,dd)
51 structext = open($sampledir+"SampleStructure.3","r").readlines.reverse
53 fieldd = structext.index(structext.find{|i| i =~ /SampleField/})
54 $fielddheader = nextheader(structext,fieldd)
56 restofstructext = structext[fieldd+1..structext.size]
58 fieldl = restofstructext.index(restofstructext.find{|i| i =~ /SampleField/})
59 $fieldlheader = nextheader(restofstructext, fieldl)
65 #### to find data structure documentation files and rewriting them
67 def writeuntilsectionheader(buf, text, index)
68 for line in text[index .. text.size]
76 def writedocumentation(buf, text, index)
77 for line in text[index .. text.size]
82 line =~ /^.SS\s"(.+)"/
83 buf.push(".PP\n\\fB".concat($1).concat("\\fP\n"))
91 def datastructure(struct)
93 text = open(struct.concat($manext),"r").readlines
98 if tline = text.index("\.SH NAME\n")
99 if text[tline+1] =~ /^([\w\s]+)\s\\-\s(.+)$/
100 buf.push(".SS ".concat($1).concat("\n"))
102 buf.push($2.concat("\n"))
104 writeuntilsectionheader(buf, text, nl)
107 text[tline+1] =~ /^([\w\s]+)\s\\-/
108 buf.push(".SS ".concat($1).concat("\n"))
111 buf.push(text[tline+1])
117 if ddline = text.index(text.find{|i| i = $ddheader})
119 writeuntilsectionheader(buf, text, nl)
123 if ptline = text.index("\.SS \"Public Types\"\n")
124 buf.push("\\fBPublic Types:\\fP\n")
126 writeuntilsectionheader(buf, text, nl)
130 # member enumeration documentation
131 if medline = text.index("\.SH \"MEMBER ENUMERATION DOCUMENTATION\"\n")
132 buf.push("\.PP\n\\fBMEMBER ENUMERATION DOCUMENTATION:\\fP\n")
134 writedocumentation(buf, text, nl)
138 # if dfline = text.index($fieldlheader)
139 # buf.push("\\fBData Fields:\\fP\n")
141 # writeuntilsectionheader(buf, text, nl)
145 # field documentation
146 if fdline = text.index($fielddheader)
147 buf.push("\.PP\n\\fBFIELD DOCUMENTATION:\\fP\n")
149 writedocumentation(buf, text, nl)
152 # File.delete(struct) if FileTest.file?(struct)
157 ###############################
160 def documentfunc(title, func_text, short_text)
162 func_text.grep(/^\.SS/){|i|
164 func_rest = func_text[func_text.index(i) ..func_text.size]
165 if funclast = func_rest.index(func_rest[1 .. func_rest.size].find{|m| m =~ /^\.SS/})
166 func_desc = func_rest[0 .. funclast - 1]
168 func_desc = func_rest
171 #fname: func_text¤ÎºÇ½é¤Î¹ÔÃæ¤Î´Ø¿ô̾
172 i =~ /\s([a-z0-9_]+)\s\(/
176 ffname = "\\fB".concat($1.concat("\\fP"))
177 #short_text¤Î´Ø¿ô̾¤Î£²¹Ô¸å¤¬brief¡£
178 if short_text.find{|i| i.index(ffname)}
179 brief = short_text[short_text.index(short_text.find{|i| i.index(ffname)}) + 2]
180 documentfunc2(fname, title, func_desc, brief)
181 else # obsolete function ¤Î¾ì¹ç
182 residue.push(func_desc)
189 def documentfunc2 (fname, title, func_desc, brief)
191 #´Ø¿ô¤´¤È¤Î¥Õ¥¡¥¤¥ë¤òºî¤ë¡£
192 file = open($doxywork+fname+$manext, "w")
193 file.puts("@function")
195 /^\.TH \"([^"]*)\"\s/ =~ title
197 ##????? newtitle = title.gsub(oname, fname.chop.chop)
198 newtitle = title.gsub(oname, fname)
201 file.print("@brief ")
204 else file.print("\n")
208 # for line in func_desc[1 .. func_desc.size]
209 # break if /.SS/ =~ line
216 ####rewriting each man file
217 ### rewriting a man file for a function
221 # let the library name appear in the header
222 buf = [text[0].gsub!(/\" \"/, "\" \"\" \"")]
224 title = text[0].split(" ")[1].chop!.reverse.chop!.reverse
226 buf.push(".ad l\n.nh\n.SH NAME\n")
228 if text[1] =~ /\"\\fI(.+)\\fP\"/
229 buf.push(title.concat(" \\- ").concat($1))
230 else buf.push(title.concat(" \\- "))
233 synopsys = text.find{|line| line =~ /\.SS/}
234 buf.push("\n\n.SH SYNOPSIS").push(formatsynopsys(synopsys)).push("\n")
236 description = text[text.index(synopsys)+2..text.size]
239 print title, ": No description found\n"
241 descriptiontext = womanrewrite(desrewrite(description))
242 buf.push("\n.SH DESCRIPTION\n").push(descriptiontext)
247 ####synopsys section of a function
249 def formatsynopsys(line)
250 line.chop!.chop!.reverse!.chop!.chop!.chop!.chop!.chop!.reverse!
252 # line.gsub!(/\\fP\s*/,"\n.ft\n")
253 # line.gsub!(/\\fB/,"\n.ft B\n")
255 line.gsub!(/\s(\w*)\)/){"\n\\fI" << $1 << "\\fP)"}
256 line.gsub!(/\s(\w*),/){"\n\\fI" << $1 << "\\fP,"}
257 line.gsub!(/\s(\w*)\s\(/){"\n\\fB" << $1 << "\\fP ("}
260 ####non-synopsys section of a function
263 returndescribed = false
264 errordescribed = false
267 text.grep(/^\\fB.+\\fP/){|line|
268 ind = text.index(line)
269 text.delete_at(ind+1) if text[ind+1] == (".in +1c\n")}
271 ### letting verbatim end in place Part1
274 text.each_with_index{|line,i|
277 line.gsub!(/\.RS 4/,"\n.RS 4")
278 # line.gsub!(/^\.RE/,"")
280 ### letting verbatim end in place Part2
287 line.gsub!(/^\.PP/,".fi")
292 #removing "More..." hyperlink
293 line.gsub!(/More.../,"")
295 line.gsub!(/^\.TP/,"")
299 if line =~ /Return\svalue:/
300 returndescribed = true
302 line.gsub!(/^\\fBReturn value:\\fP/,"\n.SH RETURN VALUE\n.PP")
303 line.gsub!(/^\\fBReturn value:\s\\fP/,"\n.SH RETURN VALUE\n.PP")
305 errordescribed = true
307 line.gsub!(/^\\fBErrors:\\fP/,"\n.SH ERRORS\n.PP")
308 line.gsub!(/^\\fBSee Also:\\fP/,"\n.SH \"SEE ALSO\"\n.PP")
309 # line.gsub!(/^\\fBSee Also:/,"\n.SH \"SEE ALSO\"\n.PP\n\\fB")
310 line.gsub!(/^\\fBExample:\\fP/,"\n.SH Example:\n.PP\n")
312 #test 2008/3/4 is there other headers? line.gsub!(/^\\fB(.+)[^\)]\\fP/){"\n.SS " << $1}
313 # [^\)] in the pattern is added to avoid the first function in see also section.
315 #removing indentation
316 if text[i - 1] =~ /^.PP/
318 line.reverse!.chop!.reverse!
322 # removing the results of doxygen bug
323 # \fP required for . , ; and <>
324 line.gsub!(/\s*(\\fP)+\s*(,|\.|;)\s+/){"\\fP" << $2 << "\n.ft R\n"}
326 line.gsub!(/(\\fP)+\s*>/,"\\fP>")
327 line.gsub!(/<\s+\\f(P|I|B|C|)/){"<\\f" << $1}
330 line.gsub!(/\s*(\\fP)+\s+/,"\n.ft R\n")
332 line.gsub!(/\s+\\f(I|C)\\fB\s*/,"\n.ft B\n")
333 line.gsub!(/\s+\\f(B|I)\\fC\s*/,"\n.ft C\n")
334 line.gsub!(/\s+\\f(B|C)\\fI\s*/,"\n.ft I\n")
335 line.gsub!(/\s+\\fB\s*/,"\n.ft B\n")
336 line.gsub!(/\s+\\fC\s*/,"\n.ft C\n")
337 line.gsub!(/\s+\\fI\s*/,"\n.ft I\n")
341 unless returndescribed == true
342 print (" return not described \n")
344 unless errordescribed == true
345 print (" errors not described \n")
351 def womanrewrite(text)
353 if sasectionstart = text.index(text.find{|line| line =~ /^\.SH\s"SEE ALSO"/})
354 aftersasection = text[sasectionstart+1 .. text.size]
355 if sasectionend = aftersasection.index(aftersasection.find{|line| line =~ /^\.SH/})
356 for line in text[sasectionstart+1 .. sasectionend + sasectionstart]
357 line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
360 for line in text[sasectionstart+1 .. text.size]
361 line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
368 # rewriting a man file for a non-function
372 structures = ["\.SH \"Data Structure Documentation\"\n"]
374 if ddind = text.index($ddheader)
375 if odind = text.index(text.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/})
377 text[ddind+1 .. odind-1].find{|line| line =~ /^[^\.]/}
378 text = text[0 .. ddind-1] + text[odind .. text.size]
381 text.each_with_index{|line,i|
383 ### changes the type of list, and indentation
393 # let the library name appear in the header
395 line = line.gsub!(/\" \"/, "\" \"\" \"")
398 # finding structure documentations and merging into "structures"
399 if line =~ /^\.RI\s\"struct\s\\fB(.*)\\fP\"/
400 structures.push(datastructure($1))
403 #removing indentation
404 if text[i - 1] =~ /^.PP/
406 line.reverse!.chop!.reverse!
411 # if text[i - 1] =~ /^.SH\sNAME/
413 # unless line =~ /\\-\s./
414 # line.chop!.chop!.chop!.chop!
419 #removing author section
420 line.gsub!(/^\.SH\s\"AUTHOR\"/,"")
421 line.gsub!(/Generated automatically by Doxygen for m17n_test from the source code\./,"")
423 line.gsub!(/^\\fBSee Also:\\fP/,"\n\\fBSEE ALSO\\fp\n")
424 line.gsub!(/^\\fBReturn value:\\fP/,"\n\\fBRETURN VALUE\\fp\n")
425 line.gsub!(/More.../,"")
427 line.gsub!(/\\fP\s+,/,"\\fP,")
428 line.gsub!(/\\fP\s+\./,"\\fP.")
429 line.gsub!(/\\fC\\fB(\w+)\\fP\\fP/){"\\fB" << $1 << "\\fP"}
434 unless structures == ["\.SH \"Data Structure Documentation\"\n"]
436 if dindex = buf.index(buf.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/i})
437 buf = buf[0 .. dindex-1] + structures + buf[dindex .. buf.size]
439 buf = buf + structures
446 #############################dividing files
448 Dir.mkdir $doxywork unless FileTest.directory? $doxywork
452 Dir.open(".").each{|filename|
454 if FileTest.directory? filename
458 if filename =~ /\.[ch]\./
462 if filename =~ /\.txt\./
466 print "RUBY DIVIDING: ", filename, "\n"
468 file = open(filename,"r")
469 text = file.readlines
473 sfunctionstart = text.index(text.find{|i| i == $flheader})
474 if sfunctionend = text.index(text[sfunctionstart+1 .. text.size].find{|i| i =~ /^\.SS|^\.SH/})
475 short_text = text[sfunctionstart .. sfunctionend - 1]
477 short_text = text[sfunctionstart .. text.size - 1]
480 if lfunctionstart = text.index(text.find{|i| i == $fdheader})
481 if lfunctionend = text.index(text[lfunctionstart+1 .. text.size].find{|i| i =~ /^\.SH/})
482 func_text = text[lfunctionstart .. lfunctionend - 1]
483 group_text = text[0 .. lfunctionstart - 1] + text[lfunctionend ..text.size]
485 func_text = text[lfunctionstart .. text.size]
486 group_text = text[0 .. lfunctionstart - 1]
493 residue = documentfunc(title, func_text, short_text)
497 group_text = group_text + [".SH \"Function Documentation\"\n.PP\n"] + residue
506 filetowrite = open($doxywork+filename,"w")
507 filetowrite.puts(group_text)
511 #############################rewriting files
515 Dir.open(".").each{|f| File.delete(f) if FileTest.file?(f)}
519 Dir.open(".").each{|filename|
520 unless FileTest.directory? filename
522 file = open(filename,"r")
523 text = file.readlines
525 if text.include?($fielddheader)
529 print "RUBY REWRINTING: ", filename, "\n"
531 if /@function/ =~ text[0]
532 buf = frewrite(text[1..text.size])
533 else buf = orewrite(text)
536 filetowrite = open($dstdir+filename,"w")
537 filetowrite.puts(headbuf)
538 filetowrite.puts(buf)
539 filetowrite.puts(footbuf)