home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ruby164.zip / rbemx164.zip / ruby / share / doc / rdtool-0.6.10 / utils / rdswap.rb < prev   
Text File  |  1999-10-22  |  6KB  |  208 lines

  1. #!/usr/local/bin/ruby -s
  2. #######
  3. # rdswap.rb (c) C.Hintze <c.hintze@gmx.net> 30.08.1999
  4. #######
  5.  
  6.  
  7. require "ostruct";
  8.  
  9. if ARGV.size < 2 and not ($h or $help)
  10.     print "Wrong # of paramter! Use `-h' for help.\n";
  11.     exit 1;
  12. elsif $h or $help
  13.     print eval('"'+DATA.read+'"');
  14.     exit 0;
  15. end   
  16.  
  17. srcfile = ARGV.select{|fn| fn =~ /\.rb$/o};
  18. case srcfile.size
  19. when 0
  20.     $stderr.print "Warning: No `.rb' file given! Take first file as source.\n";
  21.     srcfile = ARGV[0];
  22. when 1
  23.     srcfile = srcfile[0];
  24. else
  25.     print "Sorry! Only one source file (`.rb') allowed!\n";
  26.     exit(1);
  27. end
  28.  
  29. docs = {};
  30. srcs = {};
  31.  
  32. rddoc, rddocs = nil, [];
  33. source, sources = [], [[]];
  34.  
  35. while gets
  36.     lang = $1 if File::basename(String($<)) =~ /^.*?([^.]+)$/o;
  37.     if /^=begin/o .. /^=end/o
  38.         title = $2 if /^=begin(\s+(.*))?$/o;
  39.         unless rddoc.nil?
  40.             unless /^=end/o
  41.                 rddoc.lines << $_;
  42.             else
  43.                 rddocs << rddoc;
  44.                 sources << [];
  45.                 rddoc = nil;
  46.                 title = nil;
  47.             end
  48.         else               # New RD block found! Instantiate data container.
  49.             rddoc = OpenStruct.new
  50.             rddoc.kind, rddoc.lines = title, [];
  51.         end
  52.     else                   # It is not a RD block means, it is a source line!
  53.         sources[-1] << $_;
  54.     end
  55.     if $<.eof?             # One file finished. Remember data and proceed.
  56.         docs[lang] = rddocs;
  57.         srcs[lang] = sources;
  58.         rddoc, rddocs = nil, [];
  59.         source, sources = [], [[]];
  60.     end
  61. end
  62.  
  63. langs = docs.keys;
  64. langs.delete("rb");        # `rb' is not a language but the script!
  65. source = srcs["rb"];       # Assign it for preventing later look-ups
  66. srcdoc = docs["rb"];
  67. sourcesize = source.size;  # Do not recalculate size again and again.
  68. srcdocsize = srcdoc.size;
  69.  
  70. for lang in langs
  71.     docblk = docs[lang];
  72.     max = [sourcesize, srcdocsize, docblk.size].max;
  73.     filename = File.join(srcfile+"."+lang);
  74.     open(filename, "w+") do |fd|
  75.         j = 0;
  76.         for i in 0...max   # Goto every block; be it source or RD.
  77.             fd.print source[i].join unless source[i].nil? || source[i].empty?;
  78.             sblk, dblk = srcdoc[i], docblk[j];
  79.             blk = (dblk and (dblk.kind == sblk.kind)) ? dblk : sblk;
  80.             next unless blk;
  81.             j += 1 if blk == dblk;
  82.             fd.print "=begin #{blk && blk.kind}\n", blk.lines.join, "=end\n";
  83.         end
  84.     end
  85.     print "File `#{filename}' created.\n" if $v;
  86. end
  87.  
  88. exit(0);
  89.  
  90. __END__
  91.  
  92. Purpose:
  93.    This tool is written to support you to write multi-language documents
  94.    using the Ruby-Document-Format (RD).
  95.  
  96.    The idea for such a tool was originated by
  97.    
  98.            Minero Aoki <aamine@dp.u-netsurf.ne.jp>,
  99.         
  100.    how has thought about, how to make life easier for developers who have to
  101.    write and maintain scripts in more than one language.
  102.  
  103.    You have to specify at least two filenames on the command line. One
  104.    containing the Ruby script, the second containing a translated RD. If the
  105.    script does *not* end with `.rb', it has to be the first filename mentioned
  106.    on the command line! In opposition, all files containing translations *must
  107.    not* ending with `.rb'! They should use a extension that describes the
  108.    language. So that would give us the following picture:
  109.  
  110.       - sample.rb : Script contains the original documentation.
  111.       - sample.jp : Documentation written in Japanese.
  112.       - sample.de : Translation to German.
  113.  
  114.    The tool doesn't care about the language extensions. You can name them as
  115.    you like! So the file containing the Japanese translation above, could also
  116.    be names e.g. `sample.japan' or even `japantranslation.japan'.
  117.  
  118.    For every translation file, a new file will be created. The name is build
  119.    from the script filename plus the language extension. So regarding the
  120.    example above, following files would be created:
  121.  
  122.       - sample.rb.jp
  123.       - sample.rb.de
  124.  
  125.    or, given the alternative translation filename as mentioned above...
  126.  
  127.       - sample.rb.japan
  128.  
  129. How does it work?
  130.    The contents of all files will be split into source and RD blocks. The
  131.    source of the translation files, will be discarded! Every RD block may
  132.    be of a certain type. The type will be taken from the contents directly
  133.    following the `=begin' on the same line. If there is only a lonely `=begin'
  134.    on a line by itself, the type of the block is `nil'. That means in
  135.  
  136.         :
  137.         =begin
  138.          bla bla
  139.         =end
  140.         :
  141.         =begin whatever or not
  142.          blub blub
  143.         =end
  144.         :
  145.    
  146.    the first block would be of type `nil' and the second one of type `whatever
  147.    or not'.
  148.    
  149.    Block types are important for the translation. If a source will be
  150.    generated from a script and a translation file, only these blocks are taken
  151.    from the translation files, that comes in the right sequence *and* contains
  152.    the same type as the block in the script! For example:
  153.  
  154.         # File sample.rb
  155.         :
  156.         =begin gnark
  157.          Some comment
  158.         =end
  159.         :
  160.         =begin
  161.          block 2
  162.         =end
  163.         :
  164.         =begin
  165.          block 3
  166.         =end
  167.         :
  168.  
  169.         # File sample.de
  170.         :
  171.         =begin
  172.          Block zwei
  173.         =end
  174.         :
  175.         =begin
  176.          Block drei
  177.         =end
  178.         :
  179.  
  180.    Here, the first block of `sample.rb' will *not* be translated, as there is
  181.    no translation block with that type in sample.de! So the first block would
  182.    be inserted as-it-is into the translated script. The blocks afterwards,
  183.    however, are translated as the block type does match (it is `nil' there).
  184.  
  185.    Attention: In a translation file, a second block will only be used, if
  186.               a first one was already used (matched). A third block will
  187.               only be used, if a second one was used already!
  188.  
  189.    That means, if the first block of `sample.de' would be of type e.g. `Never
  190.    match', then no block would ever be taken to replace anyone of `sample.rb'.
  191.    
  192. Syntax:
  193.    #{File::basename $0} [-h|-v] <filename>...
  194.  
  195. Whereby:
  196.    -h  shows this help text.
  197.    -v  shows some more text during processing.
  198.    <filename>  Means a file, that contains RD and/or Ruby code.
  199.  
  200. Examples:
  201.    #{File::basename $0} -v sample.rb sample.ja sample.de
  202.    #{File::basename $0} -v sample.ja sample.rb sample.de
  203.    #{File::basename $0} -v sample.ja sample.de sample.rb
  204.    #{File::basename $0} -v sample.??
  205.  
  206. Author:
  207.    Clemens Hintze <c.hintze@gmx.net>.
  208.