diff options
author | Andrew Manning <tamanning@zoho.com> | 2016-06-16 19:45:02 -0400 |
---|---|---|
committer | Andrew Manning <tamanning@zoho.com> | 2016-06-16 19:45:02 -0400 |
commit | e5f4d80bbe0c085a55f676aa1b05fb9bab1e4dc7 (patch) | |
tree | 3b46494707be3560c855e1b1a4cdcd329f7fa570 /library/Text_Highlighter/generate | |
parent | f2dda646ecf1d11bf88c085d1174d3c147f799b1 (diff) | |
parent | aa77b04860a68370d829f1081418e69777d0e1f9 (diff) | |
download | volse-hubzilla-e5f4d80bbe0c085a55f676aa1b05fb9bab1e4dc7.tar.gz volse-hubzilla-e5f4d80bbe0c085a55f676aa1b05fb9bab1e4dc7.tar.bz2 volse-hubzilla-e5f4d80bbe0c085a55f676aa1b05fb9bab1e4dc7.zip |
Merge remote-tracking branch 'upstream/dev' into dev
Diffstat (limited to 'library/Text_Highlighter/generate')
-rw-r--r-- | library/Text_Highlighter/generate | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/library/Text_Highlighter/generate b/library/Text_Highlighter/generate new file mode 100644 index 000000000..4e22e82fd --- /dev/null +++ b/library/Text_Highlighter/generate @@ -0,0 +1,171 @@ +#!@php_bin@ +<?php +/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ +/** + * Console highlighter class generator + * + * PHP versions 4 and 5 + * + * LICENSE: This source file is subject to version 3.0 of the PHP license + * that is available through the world-wide-web at the following URI: + * http://www.php.net/license/3_0.txt. If you did not receive a copy of + * the PHP License and are unable to obtain it through the web, please + * send a note to license@php.net so we can mail you a copy immediately. + * + * @category Text + * @package Text_Highlighter + * @author Andrey Demenev <demenev@gmail.com> + * @copyright 2004 Andrey Demenev + * @license http://www.php.net/license/3_0.txt PHP License + * @version CVS: $Id$ + * @link http://pear.php.net/package/Text_Highlighter + */ + +require_once 'Text/Highlighter/Generator.php'; +require_once 'Console/Getopt.php'; + +$options = Console_Getopt::getopt($argv, 'x:p:d:h', array('xml=', 'php=','dir=', 'help')); + +if (PEAR::isError($options)) { + $message = str_replace('Console_Getopt: ','',$options->message); + usage($message); +} + +$source = array(); +$dest = array(); +$dir = ''; + +$expectp = false; +$expectx = false; +$unexpectedx = false; +$unexpectedp = false; +$si = $di = 0; + +foreach ($options[0] as $option) { + switch ($option[0]) { + case 'x': + case '--xml': + $source[$si] = $option[1]; + if ($si) { + $di++; + } + $si++; + if ($expectp) { + $unexpectedx = true; + } + $expectp = true; + $expectx = false; + break; + + case 'p': + case '--php': + if ($expectx) { + $unexpectedp = true; + } + $dest[$di] = $option[1]; + $expectp = false; + $expectx = true; + break; + + case 'd': + case '--dir': + $dir = $option[1]; + break; + + case 'h': + case '--help': + usage(); + break; + } +} + + +if ($unexpectedx && !$dir) { + usage('Unexpected -x or --xml', STDERR); +} + +if ($unexpectedp) { + usage('Unexpected -p or --php', STDERR); +} + +$nsource = count($source); +$ndest = count($dest); + +if (!$nsource && !$ndest) { + $source[]='php://stdin'; + if (!$dir) { + $dest[]='php://stdout'; + } else { + $dest[] = null; + } +} elseif ($expectp && !$dir && $nsource > 1) { + usage('-x or --xml without following -p or --php', STDERR); +} elseif ($nsource == 1 && !$ndest && !$dir) { + $dest[]='php://stdout'; +} + +if ($dir && substr($dir,-1)!='/' && substr($dir,-1)!=='\\' ) { + $dir .= DIRECTORY_SEPARATOR; +} + + +foreach ($source as $i => $xmlfile) +{ + $gen = new Text_Highlighter_Generator; + $gen->setInputFile($xmlfile); + if ($gen->hasErrors()) { + break; + } + $gen->generate(); + if ($gen->hasErrors()) { + break; + } + if (isset($dest[$i])) { + $phpfile = $dest[$i]; + } else { + $phpfile = $dir . $gen->language . '.php'; + } + $gen->saveCode($phpfile); + if ($gen->hasErrors()) { + break; + } +} +if ($gen->hasErrors()) { + $errors = $gen->getErrors(); + foreach ($errors as $error) { + fwrite (STDERR, $error . "\n"); + } + exit(1); +} + +function usage($message='', $file=STDOUT) +{ + $code = 0; + if ($message) { + $message .= "\n\n"; + $code = 1; + } + $message .= <<<MSG +Generates a highlighter class from XML source +Usage: +generate options + +Options: + -x filename, --xml=filename + source XML file. Multiple input files can be specified, in which + case each -x option must be followed by -p unless -d is specified + Defaults to stdin + -p filename, --php=filename + destination PHP file. Defaults to stdout. If specied multiple times, + each -p must follow -x + -d dirname, --dir=dirname + Default destination directory. File names will be taken from XML input + ("lang" attribute of <highlight> tag) + -h, --help + This help +MSG; + fwrite ($file, $message); + exit($code); +} +?> + |