generate 4.1 KB

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