summaryrefslogtreecommitdiffstats
path: root/paste/include/geshi/scripts/get-keywords/get-keywords.php
blob: c5d4534219b85f776b0d7c1f486e7dde0b1d793f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
 * GeSHi - Generic Syntax Highlighter
 * 
 * For information on how to use GeSHi, please consult the documentation
 * found in the docs/ directory, or online at http://geshi.org/docs/
 * 
 *  This file is part of GeSHi.
 *
 *  GeSHi is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  GeSHi is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GeSHi; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * You can view a copy of the GNU GPL in the COPYING file that comes
 * with GeSHi, in the docs/ directory.
 *
 * @package   scripts
 * @author    Nigel McNie <nigel@geshi.org>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright (C) 2005 Nigel McNie
 * @version   1.1.0
 * 
 */

define('GESHI_GET_KEYWORDS_VERSION', '0.1.2');

/**
 * Script to get keywords for languages from katepart kde package
 * 
 * Usage:
 * get-keywords.php css properties
 * get-keywords.php css attributes
 * get-keywords.php php statements
 * get-keywords.php php keywords
 * get-keywords.php --list-langs
 * get-keywords.php --list-groups css
 * ...
 * 
 * @todo [blocking 1.1.1] customise output format options (one per line, formatted for pasting into lang file, HTML)
 */
  
// As always...
error_reporting(E_ALL);

/** Get standard functions */
require_once 'lib/functions.get-keywords.php';
/** Get the KeywordGetter class */
require_once 'lib/class.keywordgetter.php';
/** Get the console options reader class */
require_once 'lib/pear/Console/Getopt.php';

// Parse command line options
$opt_parser =& new Console_Getopt;
$args = $opt_parser->getopt($argv, 'hv', array('help', 'version', 'list-groups=', 'list-langs'));

// Print error if there was an argument not recognised
if (PEAR::isError($args)) {
    echo str_replace('Console_Getopt', $argv[0], $args->getMessage()) . '
Try `' . $argv[0] . " --help' for more information.\n";
    exit(1);
}


//
// Do the easy options first
//

// Check for help
if (get_option(array('h', 'help'), $args)) {
    show_help();
    exit;
}

// Check for version
if (get_option(array('v', 'version'), $args)) {
    show_version();
    exit;
}

// Check for --list-langs
if (get_option('list-langs', $args)) {
    $languages = KeywordGetter::getSupportedLanguages();
    print_r($languages);
    exit;
}

// Check for --list-groups
if (false !== ($language = get_option('list-groups', $args))) {
    $kwgetter =& KeywordGetter::factory($language);
    if (KeywordGetter::isError($kwgetter)) {
        die($kwgetter->lastError());
    }
    print_r($kwgetter->getValidKeywordGroups());
    exit;
}


//
// Simple options are not being used - time to actually
// get keywords if we can
//

// If we don't have a language and a keyword type, show the help and exit
if (!isset($argv[1]) || !isset($argv[2])) {
    show_help();
    exit;
}

// Create a new keyword getter. If this language is not supported, exit
$kwgetter =& KeywordGetter::factory($argv[1]);
if (KeywordGetter::isError($kwgetter)) {
    die($kwgetter->lastError());
}

// Get the keywords based on the required keyword group. If there
// is an error getting the keywords, print it and exit
$keywords = $kwgetter->getKeywords($argv[2]);
if (KeywordGetter::isError($keywords)) {
    die($keywords->lastError());
}

// Simply echo to standard out, although a todo would be to make this customisable
// @todo [blocking 1.1.1] Customise the output of keywords (to a file perhaps?)
$result = '';
$spaces = '            ';
foreach ($keywords as $keyword) {
    $result .= "'".$keyword . "', ";
}
$result = wordwrap($result, 75);
$result = $spaces . str_replace("\n", "\n$spaces", $result);
echo substr($result, 0, -2) . "\n";

?>