2 # Usage: mkman.rb SRCDIR DSTDIR -*- coding: euc-jp; -*-
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 # Extract documentation for each program and write out manuals.
217 def documentprog (text)
222 text.each_with_index{|line,i|
223 if line =~ /^\.SH\s*\"(\S+) -- ([^\"]+)\"/
224 # Remember the title and short description. It may or may not
228 elsif line =~ /^\.SS\s*"SYNOPSIS"/
229 # Only a program has "SYNOPSIS".
230 progtext.push(header.gsub("m17nExProg",title))
231 progtext.push(".ad l\n.nh\n.SH NAME\n")
232 progtext.push(title + " - " + brief)
233 progtext.push(".SH SYNOPSIS")
234 elsif line =~ /^\.PD 0/
235 # Don't write this "inter-paragraph vertical distance" command
236 elsif line =~ /Print this message./
237 # This is always the end of description for a program.
238 if progtext.length > 0
239 file = open($doxywork+title+$manext,"w")
245 elsif progtext.length > 0
246 if line =~ /^\.SS\s*(.*)/
247 # Convert ".SS ..." to ".SH ..."
248 progtext.push(".SH " + $1)
256 ####rewriting each man file
257 ### rewriting a man file for a function
261 # let the library name appear in the header (for old buggy doxygen)
262 # buf = [text[0].gsub!(/\" \"/, "\" \"\" \"")]
265 title = text[0].split(" ")[1].chop!.reverse.chop!.reverse
267 buf.push(".ad l\n.nh\n.SH NAME\n")
269 if text[1] =~ /\"\\fI(.+)\\fP\"/
270 buf.push(title.concat(" \\- ").concat($1))
271 else buf.push(title.concat(" \\- "))
274 synopsys = text.find{|line| line =~ /\.SS/}
275 buf.push("\n\n.SH SYNOPSIS").push(formatsynopsys(synopsys)).push("\n")
277 description = text[text.index(synopsys)+2..text.size]
280 print title, ": No description found\n"
282 descriptiontext = womanrewrite(desrewrite(description))
283 buf.push("\n.SH DESCRIPTION\n").push(descriptiontext)
288 ####synopsys section of a function
290 def formatsynopsys(line)
291 line.chop!.chop!.reverse!.chop!.chop!.chop!.chop!.chop!.reverse!
293 # line.gsub!(/\\fP\s*/,"\n.ft\n")
294 # line.gsub!(/\\fB/,"\n.ft B\n")
296 line.gsub!(/\s(\w*)\)/){"\n\\fI" << $1 << "\\fP)"}
297 line.gsub!(/\s(\w*),/){"\n\\fI" << $1 << "\\fP,"}
298 line.gsub!(/\s(\w*)\s\(/){"\n\\fB" << $1 << "\\fP ("}
301 ####non-synopsys section of a function
304 returndescribed = false
305 errordescribed = false
308 text.grep(/^\\fB.+\\fP/){|line|
309 ind = text.index(line)
310 text.delete_at(ind+1) if text[ind+1] == (".in +1c\n")}
312 ### letting verbatim end in place Part1
315 text.each_with_index{|line,i|
318 line.gsub!(/\.RS 4/,"\n.RS 4")
319 # line.gsub!(/^\.RE/,"")
321 ### letting verbatim end in place Part2
328 line.gsub!(/^\.PP/,".fi")
333 #removing "More..." hyperlink
334 line.gsub!(/More.../,"")
336 line.gsub!(/^\.TP/,"")
340 if line =~ /Return\svalue:/
341 returndescribed = true
343 line.gsub!(/^\\fBReturn value:\\fP/,"\n.SH RETURN VALUE\n.PP")
344 line.gsub!(/^\\fBReturn value:\s\\fP/,"\n.SH RETURN VALUE\n.PP")
346 errordescribed = true
348 line.gsub!(/^\\fBErrors:\\fP/,"\n.SH ERRORS\n.PP")
349 line.gsub!(/^\\fBSee Also:\\fP/,"\n.SH \"SEE ALSO\"\n.PP")
350 # line.gsub!(/^\\fBSee Also:/,"\n.SH \"SEE ALSO\"\n.PP\n\\fB")
351 line.gsub!(/^\\fBExample:\\fP/,"\n.SH Example:\n.PP\n")
353 #test 2008/3/4 is there other headers? line.gsub!(/^\\fB(.+)[^\)]\\fP/){"\n.SS " << $1}
354 # [^\)] in the pattern is added to avoid the first function in see also section.
356 #removing indentation
357 if text[i - 1] =~ /^.PP/
359 line.reverse!.chop!.reverse!
363 # removing the results of doxygen bug
364 # \fP required for . , ; and <>
365 line.gsub!(/\s*(\\fP)+\s*(,|\.|;)\s+/){"\\fP" << $2 << "\n.ft R\n"}
367 line.gsub!(/(\\fP)+\s*>/,"\\fP>")
368 line.gsub!(/<\s+\\f(P|I|B|C|)/){"<\\f" << $1}
371 line.gsub!(/\s*(\\fP)+\s+/,"\n.ft R\n")
373 line.gsub!(/\s+\\f(I|C)\\fB\s*/,"\n.ft B\n")
374 line.gsub!(/\s+\\f(B|I)\\fC\s*/,"\n.ft C\n")
375 line.gsub!(/\s+\\f(B|C)\\fI\s*/,"\n.ft I\n")
376 line.gsub!(/\s+\\fB\s*/,"\n.ft B\n")
377 line.gsub!(/\s+\\fC\s*/,"\n.ft C\n")
378 line.gsub!(/\s+\\fI\s*/,"\n.ft I\n")
382 unless returndescribed == true
383 print (" return not described \n")
385 unless errordescribed == true
386 print (" errors not described \n")
392 def womanrewrite(text)
394 if sasectionstart = text.index(text.find{|line| line =~ /^\.SH\s"SEE ALSO"/})
395 aftersasection = text[sasectionstart+1 .. text.size]
396 if sasectionend = aftersasection.index(aftersasection.find{|line| line =~ /^\.SH/})
397 for line in text[sasectionstart+1 .. sasectionend + sasectionstart]
398 line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
401 for line in text[sasectionstart+1 .. text.size]
402 line.gsub!(/(\w*)\\fP\(\)/){ $1 << "(3)\\fP"}
409 # rewriting a man file for a non-function
413 structures = ["\.SH \"Data Structure Documentation\"\n"]
415 if ddind = text.index($ddheader)
416 if odind = text.index(text.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/})
418 text[ddind+1 .. odind-1].find{|line| line =~ /^[^\.]/}
419 text = text[0 .. ddind-1] + text[odind .. text.size]
422 text.each_with_index{|line,i|
424 ### changes the type of list, and indentation
434 # let the library name appear in the header (for old buggy doxygen)
436 # line = line.gsub!(/\" \"/, "\" \"\" \"")
439 # finding structure documentations and merging into "structures"
440 if line =~ /^\.RI\s\"struct\s\\fB(.*)\\fP\"/
441 structures.push(datastructure($1))
444 #removing indentation
445 if text[i - 1] =~ /^.PP/
447 line.reverse!.chop!.reverse!
452 # if text[i - 1] =~ /^.SH\sNAME/
454 # unless line =~ /\\-\s./
455 # line.chop!.chop!.chop!.chop!
460 if text[i-2] =~ /^.SH\sNAME/ && text[i] =~ /^.PP/ && text[i+1] =~ /./
461 # Convert this line sequence:
465 # M-text objects and API for them.
468 # M-text \- M-text objects and API for them.
469 prevline = buf.pop.chop!.chop!.chop!.chop!.chop!.gsub(" ","_").concat(" \\- ")
470 text[i+1] = prevline.concat(text[i+1])
472 #removing author section
473 line.gsub!(/^\.SH\s\"AUTHOR\"/,"")
474 line.gsub!(/Generated automatically by Doxygen for m17n_test from the source code\./,"")
476 line.gsub!(/^\\fBSee Also:\\fP/,"\n\\fBSEE ALSO\\fp\n")
477 line.gsub!(/^\\fBReturn value:\\fP/,"\n\\fBRETURN VALUE\\fp\n")
478 line.gsub!(/More.../,"")
480 line.gsub!(/\\fP\s+,/,"\\fP,")
481 line.gsub!(/\\fP\s+\./,"\\fP.")
482 line.gsub!(/\\fC\\fB(\w+)\\fP\\fP/){"\\fB" << $1 << "\\fP"}
487 unless structures == ["\.SH \"Data Structure Documentation\"\n"]
489 if dindex = buf.index(buf.find{|line| line =~ /\.SH\s+.+\s+DOCUMENTATION/i})
490 buf = buf[0 .. dindex-1] + structures + buf[dindex .. buf.size]
492 buf = buf + structures
499 #############################dividing files
501 Dir.mkdir $doxywork unless FileTest.directory? $doxywork
505 Dir.open(".").each{|filename|
507 if FileTest.directory? filename
511 if filename =~ /\.[ch]\./
515 if filename =~ /\.txt\./
519 print "RUBY DIVIDING: ", filename, "\n"
521 file = open(filename,"r")
522 text = file.readlines
525 if filename =~ /m17nExProg.1/
529 if sfunctionstart = text.index(text.find{|i| i == $flheader})
530 if sfunctionend = text.index(text[sfunctionstart+1 .. text.size].find{|i| i =~ /^\.SS|^\.SH/})
531 short_text = text[sfunctionstart .. sfunctionend - 1]
533 short_text = text[sfunctionstart .. text.size - 1]
536 if lfunctionstart = text.index(text.find{|i| i == $fdheader})
537 if lfunctionend = text.index(text[lfunctionstart+1 .. text.size].find{|i| i =~ /^\.SH/})
538 func_text = text[lfunctionstart .. lfunctionend - 1]
539 group_text = text[0 .. lfunctionstart - 1] + text[lfunctionend ..text.size]
541 func_text = text[lfunctionstart .. text.size]
542 group_text = text[0 .. lfunctionstart - 1]
549 residue = documentfunc(title, func_text, short_text)
553 group_text = group_text + [".SH \"Function Documentation\"\n.PP\n"] + residue
562 filetowrite = open($doxywork+filename,"w")
563 filetowrite.puts(group_text)
570 #############################rewriting files
574 Dir.open(".").each{|f| File.delete(f) if FileTest.file?(f)}
578 Dir.open(".").each{|filename|
579 unless FileTest.directory? filename
581 file = open(filename,"r")
582 text = file.readlines
584 if text.include?($fielddheader)
588 print "RUBY REWRINTING: ", filename, "\n"
590 if /@function/ =~ text[0]
591 buf = frewrite(text[1..text.size])
592 else buf = orewrite(text)
595 filetowrite = open($dstdir+filename,"w")
596 filetowrite.puts(headbuf)
597 filetowrite.puts(buf)
598 filetowrite.puts(footbuf)