generate.bat 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. @echo off
  2. rem vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:
  3. rem Console highlighter class generator
  4. rem PHP versions 4 and 5
  5. rem LICENSE: This source file is subject to version 3.0 of the PHP license
  6. rem that is available through the world-wide-web at the following URI:
  7. rem http://www.php.net/license/3_0.txt. If you did not receive a copy of
  8. rem the PHP License and are unable to obtain it through the web, please
  9. rem send a note to license@php.net so we can mail you a copy immediately.
  10. rem @category Text
  11. rem @package Text_Highlighter
  12. rem @author Andrey Demenev <demenev@gmail.com>
  13. rem @copyright 2004 Andrey Demenev
  14. rem @license http://www.php.net/license/3_0.txt PHP License
  15. rem @version CVS: $Id: generate.bat,v 1.1 2007/06/03 02:35:28 ssttoo Exp $
  16. rem @link http://pear.php.net/package/Text_Highlighter
  17. set "MHL_PARAMS="
  18. :doshift
  19. set "MHL_PARAMS=%MHL_PARAMS% %1"
  20. shift
  21. if -%1- == -- GOTO noshift
  22. GOTO doshift
  23. :noshift
  24. @php_bin@ -q -d output_buffering=1 -d include_path="@php_dir@" @bin_dir@/Text/Highlighter/generate.bat %MHL_PARAMS%
  25. GOTO finish
  26. <?php
  27. ob_end_clean();
  28. if (!defined('STDOUT')) {
  29. define('STDOUT', fopen('php://stdout', 'wb'));
  30. define('STDERR', fopen('php://stderr', 'wb'));
  31. }
  32. require_once 'Text/Highlighter/Generator.php';
  33. require_once 'Console/Getopt.php';
  34. $options = Console_Getopt::getopt($argv, 'x:p:d:h', array('xml=', 'php=','dir=', 'help'));
  35. if (PEAR::isError($options)) {
  36. $message = str_replace('Console_Getopt: ','',$options->message);
  37. usage($message);
  38. }
  39. $source = array();
  40. $dest = array();
  41. $dir = '';
  42. $expectp = false;
  43. $expectx = false;
  44. $unexpectedx = false;
  45. $unexpectedp = false;
  46. $si = $di = 0;
  47. foreach ($options[0] as $option) {
  48. switch ($option[0]) {
  49. case 'x':
  50. case '--xml':
  51. $source[$si] = $option[1];
  52. if ($si) {
  53. $di++;
  54. }
  55. $si++;
  56. if ($expectp) {
  57. $unexpectedx = true;
  58. }
  59. $expectp = true;
  60. $expectx = false;
  61. break;
  62. case 'p':
  63. case '--php':
  64. if ($expectx) {
  65. $unexpectedp = true;
  66. }
  67. $dest[$di] = $option[1];
  68. $expectp = false;
  69. $expectx = true;
  70. break;
  71. case 'd':
  72. case '--dir':
  73. $dir = $option[1];
  74. break;
  75. case 'h':
  76. case '--help':
  77. usage();
  78. break;
  79. }
  80. }
  81. if ($unexpectedx && !$dir) {
  82. usage('Unexpected -x or --xml', STDERR);
  83. }
  84. if ($unexpectedp) {
  85. usage('Unexpected -p or --php', STDERR);
  86. }
  87. $nsource = count($source);
  88. $ndest = count($dest);
  89. if (!$nsource && !$ndest) {
  90. $source[]='php://stdin';
  91. if (!$dir) {
  92. $dest[]='php://stdout';
  93. } else {
  94. $dest[] = null;
  95. }
  96. } elseif ($expectp && !$dir && $nsource > 1) {
  97. usage('-x or --xml without following -p or --php', STDERR);
  98. } elseif ($nsource == 1 && !$ndest && !$dir) {
  99. $dest[]='php://stdout';
  100. }
  101. if ($dir && substr($dir,-1)!='/' && substr($dir,-1)!=='\\' ) {
  102. $dir .= DIRECTORY_SEPARATOR;
  103. }
  104. foreach ($source as $i => $xmlfile)
  105. {
  106. $gen =& new Text_Highlighter_Generator;
  107. $gen->setInputFile($xmlfile);
  108. if ($gen->hasErrors()) {
  109. break;
  110. }
  111. $gen->generate();
  112. if ($gen->hasErrors()) {
  113. break;
  114. }
  115. if (isset($dest[$i])) {
  116. $phpfile = $dest[$i];
  117. } else {
  118. $phpfile = $dir . $gen->language . '.php';
  119. }
  120. $gen->saveCode($phpfile);
  121. if ($gen->hasErrors()) {
  122. break;
  123. }
  124. }
  125. if ($gen->hasErrors()) {
  126. $errors = $gen->getErrors();
  127. foreach ($errors as $error) {
  128. fwrite (STDERR, $error . "\n");
  129. }
  130. exit(1);
  131. }
  132. exit(0);
  133. function usage($message='', $file=STDOUT)
  134. {
  135. $code = 0;
  136. if ($message) {
  137. $message .= "\n\n";
  138. $code = 1;
  139. }
  140. $message .= <<<MSG
  141. Generates a highlighter class from XML source
  142. Usage:
  143. generate options
  144. Options:
  145. -x filename, --xml=filename
  146. source XML file. Multiple input files can be specified, in which
  147. case each -x option must be followed by -p unless -d is specified
  148. Defaults to stdin
  149. -p filename, --php=filename
  150. destination PHP file. Defaults to stdout. If specied multiple times,
  151. each -p must follow -x
  152. -d dirname, --dir=dirname
  153. Default destination directory. File names will be taken from XML input
  154. ("lang" attribute of <highlight> tag)
  155. -h, --help
  156. This help
  157. MSG;
  158. fwrite ($file, $message);
  159. exit($code);
  160. }
  161. ?>
  162. :finish