]> git.mxchange.org Git - friendica.git/blob - library/Text_Highlighter/generate
This merge brings back dbm::is_result() where I could find it.
[friendica.git] / library / Text_Highlighter / generate
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$
21  * @link       http://pear.php.net/package/Text_Highlighter
22  */
23
24 require_once 'Text/Highlighter/Generator.php';
25 require_once 'Console/Getopt.php';
26
27 $options = Console_Getopt::getopt($argv, 'x:p:d:h', array('xml=', 'php=','dir=', 'help'));
28
29 if (PEAR::isError($options)) {
30     $message = str_replace('Console_Getopt: ','',$options->message);
31     usage($message);
32 }
33
34 $source = array();
35 $dest   = array();
36 $dir    = '';
37
38 $expectp = false;
39 $expectx = false;
40 $unexpectedx = false;
41 $unexpectedp = false;
42 $si = $di = 0;
43
44 foreach ($options[0] as $option) {
45     switch ($option[0]) {
46         case 'x':
47         case '--xml':
48             $source[$si] = $option[1];
49             if ($si) {
50                 $di++;
51             }
52             $si++;
53             if ($expectp) {
54                 $unexpectedx = true;
55             }
56             $expectp = true;
57             $expectx = false;
58             break;
59
60         case 'p':
61         case '--php':
62             if ($expectx) {
63                 $unexpectedp = true;
64             }
65             $dest[$di] = $option[1];
66             $expectp = false;
67             $expectx = true;
68             break;
69
70         case 'd':
71         case '--dir':
72             $dir = $option[1];
73             break;
74
75         case 'h':
76         case '--help':
77             usage();
78             break;
79     }
80 }
81
82
83 if ($unexpectedx && !$dir) {
84     usage('Unexpected -x or --xml', STDERR);
85 }
86
87 if ($unexpectedp) {
88     usage('Unexpected -p or --php', STDERR);
89 }
90
91 $nsource = count($source);
92 $ndest = count($dest);
93
94 if (!$nsource && !$ndest) {
95     $source[]='php://stdin';
96     if (!$dir) {
97         $dest[]='php://stdout';
98     } else {
99       $dest[] = null;
100     }
101 } elseif ($expectp && !$dir && $nsource > 1) {
102     usage('-x or --xml without following -p or --php', STDERR);
103 } elseif ($nsource == 1 && !$ndest && !$dir) {
104     $dest[]='php://stdout';
105 }
106
107 if ($dir && substr($dir,-1)!='/' && substr($dir,-1)!=='\\' ) {
108     $dir .= DIRECTORY_SEPARATOR;
109 }
110
111
112 foreach ($source as $i => $xmlfile)
113 {
114     $gen = new Text_Highlighter_Generator;
115     $gen->setInputFile($xmlfile);
116     if ($gen->hasErrors()) {
117         break;
118     }
119     $gen->generate();
120     if ($gen->hasErrors()) {
121         break;
122     }
123     if (isset($dest[$i])) {
124         $phpfile = $dest[$i];
125     } else {
126         $phpfile = $dir . $gen->language . '.php';
127     }
128     $gen->saveCode($phpfile);
129     if ($gen->hasErrors()) {
130         break;
131     }
132 }
133 if ($gen->hasErrors()) {
134     $errors = $gen->getErrors();
135     foreach ($errors as $error) {
136         fwrite (STDERR, $error . "\n");
137     }
138     exit(1);
139 }
140
141 function usage($message='', $file=STDOUT)
142 {
143     $code = 0;
144     if ($message) {
145         $message .= "\n\n";
146         $code = 1;
147     }
148     $message .= <<<MSG
149 Generates a highlighter class from XML source
150 Usage:
151 generate options
152
153 Options:
154   -x filename, --xml=filename
155         source XML file. Multiple input files can be specified, in which
156         case each -x option must be followed by -p unless -d is specified
157         Defaults to stdin
158   -p filename, --php=filename
159         destination PHP file. Defaults to stdout. If specied multiple times,
160         each -p must follow -x
161   -d dirname, --dir=dirname
162         Default destination directory. File names will be taken from XML input
163         ("lang" attribute of <highlight> tag)
164   -h, --help
165         This help
166 MSG;
167     fwrite ($file, $message);
168     exit($code);
169 }
170 ?>
171