pygments.rb 0.2.13 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.md +45 -19
- data/Rakefile +21 -11
- data/bench.rb +15 -48
- data/cache-lexers.rb +8 -0
- data/lexers +0 -0
- data/lib/pygments.rb +3 -6
- data/lib/pygments/mentos.py +343 -0
- data/lib/pygments/popen.rb +383 -0
- data/lib/pygments/version.rb +1 -1
- data/pygments.rb.gemspec +5 -4
- data/test/test_data.c +2581 -0
- data/test/test_data.py +514 -0
- data/test/test_data_generated +2582 -0
- data/test/test_pygments.rb +208 -84
- data/vendor/pygments-main/pygments/lexers/_mapping.py +1 -1
- data/vendor/pygments-main/pygments/lexers/shell.py +1 -1
- data/vendor/simplejson/.gitignore +10 -0
- data/vendor/simplejson/.travis.yml +5 -0
- data/vendor/simplejson/CHANGES.txt +291 -0
- data/vendor/simplejson/LICENSE.txt +19 -0
- data/vendor/simplejson/MANIFEST.in +5 -0
- data/vendor/simplejson/README.rst +19 -0
- data/vendor/simplejson/conf.py +179 -0
- data/vendor/simplejson/index.rst +628 -0
- data/vendor/simplejson/scripts/make_docs.py +18 -0
- data/vendor/simplejson/setup.py +104 -0
- data/vendor/simplejson/simplejson/__init__.py +510 -0
- data/vendor/simplejson/simplejson/_speedups.c +2745 -0
- data/vendor/simplejson/simplejson/decoder.py +425 -0
- data/vendor/simplejson/simplejson/encoder.py +567 -0
- data/vendor/simplejson/simplejson/ordered_dict.py +119 -0
- data/vendor/simplejson/simplejson/scanner.py +77 -0
- data/vendor/simplejson/simplejson/tests/__init__.py +67 -0
- data/vendor/simplejson/simplejson/tests/test_bigint_as_string.py +55 -0
- data/vendor/simplejson/simplejson/tests/test_check_circular.py +30 -0
- data/vendor/simplejson/simplejson/tests/test_decimal.py +66 -0
- data/vendor/simplejson/simplejson/tests/test_decode.py +83 -0
- data/vendor/simplejson/simplejson/tests/test_default.py +9 -0
- data/vendor/simplejson/simplejson/tests/test_dump.py +67 -0
- data/vendor/simplejson/simplejson/tests/test_encode_basestring_ascii.py +46 -0
- data/vendor/simplejson/simplejson/tests/test_encode_for_html.py +32 -0
- data/vendor/simplejson/simplejson/tests/test_errors.py +34 -0
- data/vendor/simplejson/simplejson/tests/test_fail.py +91 -0
- data/vendor/simplejson/simplejson/tests/test_float.py +19 -0
- data/vendor/simplejson/simplejson/tests/test_indent.py +86 -0
- data/vendor/simplejson/simplejson/tests/test_item_sort_key.py +20 -0
- data/vendor/simplejson/simplejson/tests/test_namedtuple.py +121 -0
- data/vendor/simplejson/simplejson/tests/test_pass1.py +76 -0
- data/vendor/simplejson/simplejson/tests/test_pass2.py +14 -0
- data/vendor/simplejson/simplejson/tests/test_pass3.py +20 -0
- data/vendor/simplejson/simplejson/tests/test_recursion.py +67 -0
- data/vendor/simplejson/simplejson/tests/test_scanstring.py +117 -0
- data/vendor/simplejson/simplejson/tests/test_separators.py +42 -0
- data/vendor/simplejson/simplejson/tests/test_speedups.py +20 -0
- data/vendor/simplejson/simplejson/tests/test_tuple.py +49 -0
- data/vendor/simplejson/simplejson/tests/test_unicode.py +109 -0
- data/vendor/simplejson/simplejson/tool.py +39 -0
- metadata +80 -22
- data/ext/extconf.rb +0 -14
- data/ext/pygments.c +0 -466
- data/lib/pygments/c.rb +0 -54
- data/lib/pygments/ffi.rb +0 -155
- data/vendor/.gitignore +0 -1
@@ -0,0 +1,39 @@
|
|
1
|
+
r"""Command-line tool to validate and pretty-print JSON
|
2
|
+
|
3
|
+
Usage::
|
4
|
+
|
5
|
+
$ echo '{"json":"obj"}' | python -m simplejson.tool
|
6
|
+
{
|
7
|
+
"json": "obj"
|
8
|
+
}
|
9
|
+
$ echo '{ 1.2:3.4}' | python -m simplejson.tool
|
10
|
+
Expecting property name: line 1 column 2 (char 2)
|
11
|
+
|
12
|
+
"""
|
13
|
+
import sys
|
14
|
+
import simplejson as json
|
15
|
+
|
16
|
+
def main():
|
17
|
+
if len(sys.argv) == 1:
|
18
|
+
infile = sys.stdin
|
19
|
+
outfile = sys.stdout
|
20
|
+
elif len(sys.argv) == 2:
|
21
|
+
infile = open(sys.argv[1], 'rb')
|
22
|
+
outfile = sys.stdout
|
23
|
+
elif len(sys.argv) == 3:
|
24
|
+
infile = open(sys.argv[1], 'rb')
|
25
|
+
outfile = open(sys.argv[2], 'wb')
|
26
|
+
else:
|
27
|
+
raise SystemExit(sys.argv[0] + " [infile [outfile]]")
|
28
|
+
try:
|
29
|
+
obj = json.load(infile,
|
30
|
+
object_pairs_hook=json.OrderedDict,
|
31
|
+
use_decimal=True)
|
32
|
+
except ValueError, e:
|
33
|
+
raise SystemExit(e)
|
34
|
+
json.dump(obj, outfile, sort_keys=True, indent=' ', use_decimal=True)
|
35
|
+
outfile.write('\n')
|
36
|
+
|
37
|
+
|
38
|
+
if __name__ == '__main__':
|
39
|
+
main()
|
metadata
CHANGED
@@ -1,46 +1,62 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pygments.rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Aman Gupta
|
14
|
+
- Ted Nyman
|
14
15
|
autorequire:
|
15
16
|
bindir: bin
|
16
17
|
cert_chain: []
|
17
18
|
|
18
|
-
date: 2012-
|
19
|
-
default_executable:
|
19
|
+
date: 2012-09-26 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
-
name:
|
22
|
+
name: yajl-ruby
|
23
23
|
prerelease: false
|
24
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
26
26
|
requirements:
|
27
27
|
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
hash:
|
29
|
+
hash: 19
|
30
30
|
segments:
|
31
|
+
- 1
|
32
|
+
- 1
|
31
33
|
- 0
|
32
|
-
|
33
|
-
- 3
|
34
|
-
version: 0.5.3
|
34
|
+
version: 1.1.0
|
35
35
|
type: :runtime
|
36
36
|
version_requirements: *id001
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
|
-
name:
|
38
|
+
name: posix-spawn
|
39
39
|
prerelease: false
|
40
40
|
requirement: &id002 !ruby/object:Gem::Requirement
|
41
41
|
none: false
|
42
42
|
requirements:
|
43
|
-
- -
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 31
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
- 3
|
49
|
+
- 6
|
50
|
+
version: 0.3.6
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: rake-compiler
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
44
60
|
- !ruby/object:Gem::Version
|
45
61
|
hash: 15
|
46
62
|
segments:
|
@@ -49,8 +65,8 @@ dependencies:
|
|
49
65
|
- 6
|
50
66
|
version: 0.7.6
|
51
67
|
type: :development
|
52
|
-
version_requirements: *
|
53
|
-
description: pygments.rb exposes the pygments syntax highlighter
|
68
|
+
version_requirements: *id003
|
69
|
+
description: pygments.rb exposes the pygments syntax highlighter to Ruby
|
54
70
|
email:
|
55
71
|
- aman@tmm1.net
|
56
72
|
executables: []
|
@@ -65,16 +81,18 @@ files:
|
|
65
81
|
- README.md
|
66
82
|
- Rakefile
|
67
83
|
- bench.rb
|
68
|
-
-
|
69
|
-
-
|
84
|
+
- cache-lexers.rb
|
85
|
+
- lexers
|
70
86
|
- lib/pygments.rb
|
71
|
-
- lib/pygments/c.rb
|
72
|
-
- lib/pygments/ffi.rb
|
73
87
|
- lib/pygments/lexer.rb
|
88
|
+
- lib/pygments/mentos.py
|
89
|
+
- lib/pygments/popen.rb
|
74
90
|
- lib/pygments/version.rb
|
75
91
|
- pygments.rb.gemspec
|
92
|
+
- test/test_data.c
|
93
|
+
- test/test_data.py
|
94
|
+
- test/test_data_generated
|
76
95
|
- test/test_pygments.rb
|
77
|
-
- vendor/.gitignore
|
78
96
|
- vendor/custom_lexers/github.py
|
79
97
|
- vendor/pygments-main/AUTHORS
|
80
98
|
- vendor/pygments-main/CHANGES
|
@@ -413,7 +431,47 @@ files:
|
|
413
431
|
- vendor/pygments-main/tests/test_token.py
|
414
432
|
- vendor/pygments-main/tests/test_using_api.py
|
415
433
|
- vendor/pygments-main/tests/test_util.py
|
416
|
-
|
434
|
+
- vendor/simplejson/.gitignore
|
435
|
+
- vendor/simplejson/.travis.yml
|
436
|
+
- vendor/simplejson/CHANGES.txt
|
437
|
+
- vendor/simplejson/LICENSE.txt
|
438
|
+
- vendor/simplejson/MANIFEST.in
|
439
|
+
- vendor/simplejson/README.rst
|
440
|
+
- vendor/simplejson/conf.py
|
441
|
+
- vendor/simplejson/index.rst
|
442
|
+
- vendor/simplejson/scripts/make_docs.py
|
443
|
+
- vendor/simplejson/setup.py
|
444
|
+
- vendor/simplejson/simplejson/__init__.py
|
445
|
+
- vendor/simplejson/simplejson/_speedups.c
|
446
|
+
- vendor/simplejson/simplejson/decoder.py
|
447
|
+
- vendor/simplejson/simplejson/encoder.py
|
448
|
+
- vendor/simplejson/simplejson/ordered_dict.py
|
449
|
+
- vendor/simplejson/simplejson/scanner.py
|
450
|
+
- vendor/simplejson/simplejson/tests/__init__.py
|
451
|
+
- vendor/simplejson/simplejson/tests/test_bigint_as_string.py
|
452
|
+
- vendor/simplejson/simplejson/tests/test_check_circular.py
|
453
|
+
- vendor/simplejson/simplejson/tests/test_decimal.py
|
454
|
+
- vendor/simplejson/simplejson/tests/test_decode.py
|
455
|
+
- vendor/simplejson/simplejson/tests/test_default.py
|
456
|
+
- vendor/simplejson/simplejson/tests/test_dump.py
|
457
|
+
- vendor/simplejson/simplejson/tests/test_encode_basestring_ascii.py
|
458
|
+
- vendor/simplejson/simplejson/tests/test_encode_for_html.py
|
459
|
+
- vendor/simplejson/simplejson/tests/test_errors.py
|
460
|
+
- vendor/simplejson/simplejson/tests/test_fail.py
|
461
|
+
- vendor/simplejson/simplejson/tests/test_float.py
|
462
|
+
- vendor/simplejson/simplejson/tests/test_indent.py
|
463
|
+
- vendor/simplejson/simplejson/tests/test_item_sort_key.py
|
464
|
+
- vendor/simplejson/simplejson/tests/test_namedtuple.py
|
465
|
+
- vendor/simplejson/simplejson/tests/test_pass1.py
|
466
|
+
- vendor/simplejson/simplejson/tests/test_pass2.py
|
467
|
+
- vendor/simplejson/simplejson/tests/test_pass3.py
|
468
|
+
- vendor/simplejson/simplejson/tests/test_recursion.py
|
469
|
+
- vendor/simplejson/simplejson/tests/test_scanstring.py
|
470
|
+
- vendor/simplejson/simplejson/tests/test_separators.py
|
471
|
+
- vendor/simplejson/simplejson/tests/test_speedups.py
|
472
|
+
- vendor/simplejson/simplejson/tests/test_tuple.py
|
473
|
+
- vendor/simplejson/simplejson/tests/test_unicode.py
|
474
|
+
- vendor/simplejson/simplejson/tool.py
|
417
475
|
homepage: http://github.com/tmm1/pygments.rb
|
418
476
|
licenses: []
|
419
477
|
|
@@ -443,7 +501,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
443
501
|
requirements: []
|
444
502
|
|
445
503
|
rubyforge_project:
|
446
|
-
rubygems_version: 1.
|
504
|
+
rubygems_version: 1.8.24
|
447
505
|
signing_key:
|
448
506
|
specification_version: 3
|
449
507
|
summary: pygments wrapper for ruby
|
data/ext/extconf.rb
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
require 'mkmf'
|
2
|
-
|
3
|
-
python = %w[ 2.7 2.6 2.5 2.4 ].find do |version|
|
4
|
-
have_library("python#{version}", 'Py_Initialize', "python#{version}/Python.h")
|
5
|
-
end
|
6
|
-
|
7
|
-
$CFLAGS << " -Wall "
|
8
|
-
|
9
|
-
unless python
|
10
|
-
$stderr.puts '*** could not find libpython or Python.h'
|
11
|
-
else
|
12
|
-
$defs << "-DPYGMENTS_PYTHON_VERSION=#{python.gsub('.','')}"
|
13
|
-
create_makefile('pygments_ext')
|
14
|
-
end
|
data/ext/pygments.c
DELETED
@@ -1,466 +0,0 @@
|
|
1
|
-
#include <stdio.h>
|
2
|
-
#include <stdlib.h>
|
3
|
-
|
4
|
-
#include <ruby.h>
|
5
|
-
|
6
|
-
#if PYGMENTS_PYTHON_VERSION == 24
|
7
|
-
#include <python2.4/Python.h>
|
8
|
-
#elif PYGMENTS_PYTHON_VERSION == 25
|
9
|
-
#include <python2.5/Python.h>
|
10
|
-
#elif PYGMENTS_PYTHON_VERSION == 26
|
11
|
-
#include <python2.6/Python.h>
|
12
|
-
#elif PYGMENTS_PYTHON_VERSION == 27
|
13
|
-
#include <python2.7/Python.h>
|
14
|
-
#else
|
15
|
-
#error Unknown python version
|
16
|
-
#endif
|
17
|
-
|
18
|
-
#ifdef RUBY_VM
|
19
|
-
#include <ruby/st.h>
|
20
|
-
#include <ruby/encoding.h>
|
21
|
-
#else
|
22
|
-
#include <st.h>
|
23
|
-
#endif
|
24
|
-
|
25
|
-
#ifndef RSTRING_PTR
|
26
|
-
#define RSTRING_PTR(str) RSTRING(str)->ptr
|
27
|
-
#endif
|
28
|
-
#ifndef RSTRING_LEN
|
29
|
-
#define RSTRING_LEN(str) RSTRING(str)->len
|
30
|
-
#endif
|
31
|
-
|
32
|
-
static VALUE mPygments, mPygmentsC;
|
33
|
-
static PyObject
|
34
|
-
/* modules */
|
35
|
-
*pygments,
|
36
|
-
*pygments_lexers,
|
37
|
-
*pygments_formatters,
|
38
|
-
*pygments_styles,
|
39
|
-
*pygments_filters,
|
40
|
-
|
41
|
-
/* lexer methods */
|
42
|
-
*guess_lexer,
|
43
|
-
*guess_lexer_for_filename,
|
44
|
-
*get_lexer_for_mimetype,
|
45
|
-
*get_lexer_for_filename,
|
46
|
-
*get_lexer_by_name,
|
47
|
-
*get_all_lexers,
|
48
|
-
|
49
|
-
/* formatter methods */
|
50
|
-
*get_formatter_by_name,
|
51
|
-
*get_all_formatters,
|
52
|
-
|
53
|
-
/* highlighter methods */
|
54
|
-
*highlight,
|
55
|
-
|
56
|
-
/* style methods */
|
57
|
-
*get_all_styles,
|
58
|
-
|
59
|
-
/* filter methods */
|
60
|
-
*get_all_filters;
|
61
|
-
|
62
|
-
static int
|
63
|
-
each_hash_i(VALUE key, VALUE val, VALUE arg)
|
64
|
-
{
|
65
|
-
Check_Type(key, T_SYMBOL);
|
66
|
-
|
67
|
-
PyObject *py = (PyObject*) arg, *py_val = NULL;
|
68
|
-
switch (TYPE(val)) {
|
69
|
-
case T_NIL:
|
70
|
-
return ST_CONTINUE;
|
71
|
-
break;
|
72
|
-
|
73
|
-
case T_FALSE:
|
74
|
-
py_val = Py_False;
|
75
|
-
break;
|
76
|
-
|
77
|
-
case T_TRUE:
|
78
|
-
py_val = Py_True;
|
79
|
-
break;
|
80
|
-
|
81
|
-
case T_STRING:
|
82
|
-
py_val = PyString_FromString(RSTRING_PTR(val));
|
83
|
-
break;
|
84
|
-
|
85
|
-
default:
|
86
|
-
Check_Type(val, T_STRING);
|
87
|
-
}
|
88
|
-
|
89
|
-
PyDict_SetItemString(py, rb_id2name(SYM2ID(key)), py_val);
|
90
|
-
|
91
|
-
return ST_CONTINUE;
|
92
|
-
}
|
93
|
-
|
94
|
-
static PyObject*
|
95
|
-
rb_to_py(VALUE rb)
|
96
|
-
{
|
97
|
-
PyObject *py = NULL;
|
98
|
-
|
99
|
-
switch (TYPE(rb)) {
|
100
|
-
case T_HASH:
|
101
|
-
py = PyDict_New();
|
102
|
-
rb_hash_foreach(rb, each_hash_i, (VALUE)py);
|
103
|
-
break;
|
104
|
-
}
|
105
|
-
|
106
|
-
return py;
|
107
|
-
}
|
108
|
-
|
109
|
-
static VALUE
|
110
|
-
py_to_rb(PyObject *py)
|
111
|
-
{
|
112
|
-
VALUE rb = Qnil;
|
113
|
-
Py_ssize_t len, i;
|
114
|
-
|
115
|
-
if (py) {
|
116
|
-
if (PyIter_Check(py)) {
|
117
|
-
PyObject *item;
|
118
|
-
rb = rb_ary_new();
|
119
|
-
|
120
|
-
while ((item = PyIter_Next(py))) {
|
121
|
-
rb_ary_push(rb, py_to_rb(item));
|
122
|
-
Py_DECREF(item);
|
123
|
-
}
|
124
|
-
|
125
|
-
} else if (PyString_Check(py)) {
|
126
|
-
char *data;
|
127
|
-
|
128
|
-
if (PyString_AsStringAndSize(py, &data, &len) == 0) {
|
129
|
-
rb = rb_str_new(data, len);
|
130
|
-
}
|
131
|
-
|
132
|
-
} else if (PyTuple_Check(py)) {
|
133
|
-
len = PyTuple_Size(py);
|
134
|
-
|
135
|
-
PyObject *item;
|
136
|
-
rb = rb_ary_new();
|
137
|
-
|
138
|
-
for (i=0; i<len; i++) {
|
139
|
-
item = PyTuple_GetItem(py, i);
|
140
|
-
rb_ary_push(rb, py_to_rb(item));
|
141
|
-
}
|
142
|
-
|
143
|
-
} else if (PyList_Check(py)) {
|
144
|
-
len = PyList_Size(py);
|
145
|
-
|
146
|
-
PyObject *item;
|
147
|
-
rb = rb_ary_new();
|
148
|
-
|
149
|
-
for (i=0; i<len; i++) {
|
150
|
-
item = PyList_GetItem(py, i);
|
151
|
-
rb_ary_push(rb, py_to_rb(item));
|
152
|
-
}
|
153
|
-
|
154
|
-
} else if (PyDict_Check(py)) {
|
155
|
-
PyObject *key, *val;
|
156
|
-
Py_ssize_t pos = 0;
|
157
|
-
|
158
|
-
rb = rb_hash_new();
|
159
|
-
|
160
|
-
while (PyDict_Next(py, &pos, &key, &val)) {
|
161
|
-
rb_hash_aset(rb, py_to_rb(key), py_to_rb(val));
|
162
|
-
}
|
163
|
-
}
|
164
|
-
}
|
165
|
-
|
166
|
-
return rb;
|
167
|
-
}
|
168
|
-
|
169
|
-
static PyObject*
|
170
|
-
pygments__lexer_for(VALUE code, VALUE options)
|
171
|
-
{
|
172
|
-
VALUE filename, mimetype, lexer;
|
173
|
-
PyObject *ret = NULL, *args = NULL, *kwargs = NULL;
|
174
|
-
|
175
|
-
if (RTEST(code))
|
176
|
-
Check_Type(code, T_STRING);
|
177
|
-
|
178
|
-
if (RTEST(options)) {
|
179
|
-
Check_Type(options, T_HASH);
|
180
|
-
|
181
|
-
VALUE kw = rb_hash_aref(options, ID2SYM(rb_intern("options")));
|
182
|
-
if (RTEST(kw)) {
|
183
|
-
Check_Type(kw, T_HASH);
|
184
|
-
kwargs = rb_to_py(kw);
|
185
|
-
}
|
186
|
-
|
187
|
-
lexer = rb_hash_aref(options, ID2SYM(rb_intern("lexer")));
|
188
|
-
filename = rb_hash_aref(options, ID2SYM(rb_intern("filename")));
|
189
|
-
mimetype = rb_hash_aref(options, ID2SYM(rb_intern("mimetype")));
|
190
|
-
|
191
|
-
if (RTEST(lexer)) {
|
192
|
-
Check_Type(lexer, T_STRING);
|
193
|
-
args = Py_BuildValue("(s)", RSTRING_PTR(lexer));
|
194
|
-
ret = PyObject_Call(get_lexer_by_name, args, kwargs);
|
195
|
-
|
196
|
-
} else if (RTEST(mimetype)) {
|
197
|
-
Check_Type(mimetype, T_STRING);
|
198
|
-
args = Py_BuildValue("(s)", RSTRING_PTR(mimetype));
|
199
|
-
ret = PyObject_Call(get_lexer_for_mimetype, args, kwargs);
|
200
|
-
|
201
|
-
} else if (RTEST(filename)) {
|
202
|
-
Check_Type(filename, T_STRING);
|
203
|
-
|
204
|
-
if (RTEST(code)) {
|
205
|
-
args = Py_BuildValue("(ss)", RSTRING_PTR(filename), RSTRING_PTR(code));
|
206
|
-
ret = PyObject_Call(guess_lexer_for_filename, args, kwargs);
|
207
|
-
} else {
|
208
|
-
args = Py_BuildValue("(s)", RSTRING_PTR(filename));
|
209
|
-
ret = PyObject_Call(get_lexer_for_filename, args, kwargs);
|
210
|
-
}
|
211
|
-
}
|
212
|
-
}
|
213
|
-
|
214
|
-
if (ret == NULL && RTEST(code)) {
|
215
|
-
args = Py_BuildValue("(s)", RSTRING_PTR(code));
|
216
|
-
ret = PyObject_Call(guess_lexer, args, kwargs);
|
217
|
-
}
|
218
|
-
|
219
|
-
Py_XDECREF(args);
|
220
|
-
Py_XDECREF(kwargs);
|
221
|
-
PyErr_Clear();
|
222
|
-
return ret;
|
223
|
-
}
|
224
|
-
|
225
|
-
static VALUE
|
226
|
-
rb_pygments_lexer_name_for(int argc, VALUE *argv, VALUE self)
|
227
|
-
{
|
228
|
-
VALUE code = Qnil, options = Qnil;
|
229
|
-
VALUE name = Qnil;
|
230
|
-
PyObject *lexer = NULL, *aliases = NULL;
|
231
|
-
|
232
|
-
int found = rb_scan_args(argc, argv, "11", &code, &options);
|
233
|
-
|
234
|
-
if (found > 0) {
|
235
|
-
if (found == 1 && TYPE(code) == T_HASH) {
|
236
|
-
options = code;
|
237
|
-
code = Qnil;
|
238
|
-
}
|
239
|
-
|
240
|
-
lexer = pygments__lexer_for(code, options);
|
241
|
-
|
242
|
-
if (lexer) {
|
243
|
-
aliases = PyObject_GetAttrString(lexer, "aliases");
|
244
|
-
if (aliases && PyList_Size(aliases) > 0) {
|
245
|
-
PyObject *alias = PyList_GetItem(aliases, 0);
|
246
|
-
name = rb_str_new2(PyString_AsString(alias));
|
247
|
-
}
|
248
|
-
}
|
249
|
-
}
|
250
|
-
|
251
|
-
Py_XDECREF(aliases);
|
252
|
-
Py_XDECREF(lexer);
|
253
|
-
PyErr_Clear();
|
254
|
-
return name;
|
255
|
-
}
|
256
|
-
|
257
|
-
static VALUE
|
258
|
-
rb_pygments_css(int argc, VALUE *argv, VALUE self)
|
259
|
-
{
|
260
|
-
VALUE css = Qnil, prefix = Qnil, options = Qnil;
|
261
|
-
PyObject *args = NULL, *kwargs = NULL, *formatter = NULL;
|
262
|
-
int found = rb_scan_args(argc, argv, "02", &prefix, &options);
|
263
|
-
|
264
|
-
if (found == 1 && TYPE(prefix) == T_HASH) {
|
265
|
-
options = prefix;
|
266
|
-
prefix = Qnil;
|
267
|
-
}
|
268
|
-
|
269
|
-
if (RTEST(prefix))
|
270
|
-
Check_Type(prefix, T_STRING);
|
271
|
-
|
272
|
-
if (RTEST(options)) {
|
273
|
-
Check_Type(options, T_HASH);
|
274
|
-
kwargs = rb_to_py(options);
|
275
|
-
}
|
276
|
-
|
277
|
-
args = Py_BuildValue("(s)", "html");
|
278
|
-
formatter = PyObject_Call(get_formatter_by_name, args, kwargs);
|
279
|
-
if (formatter) {
|
280
|
-
PyObject *styles = PyObject_CallMethod(formatter, "get_style_defs", "(s)", RTEST(prefix) ? RSTRING_PTR(prefix) : "");
|
281
|
-
if (styles) {
|
282
|
-
css = rb_str_new2(PyString_AsString(styles));
|
283
|
-
}
|
284
|
-
Py_XDECREF(styles);
|
285
|
-
}
|
286
|
-
|
287
|
-
Py_XDECREF(args);
|
288
|
-
Py_XDECREF(kwargs);
|
289
|
-
Py_XDECREF(formatter);
|
290
|
-
PyErr_Clear();
|
291
|
-
|
292
|
-
return css;
|
293
|
-
}
|
294
|
-
|
295
|
-
static VALUE
|
296
|
-
rb_pygments_highlight(int argc, VALUE *argv, VALUE self)
|
297
|
-
{
|
298
|
-
PyObject *args = NULL, *kwargs = NULL, *lexer = NULL, *formatter = NULL;
|
299
|
-
VALUE code = Qnil, options = Qnil, ret = Qnil, format = Qnil;
|
300
|
-
rb_scan_args(argc, argv, "11", &code, &options);
|
301
|
-
|
302
|
-
if (RTEST(options)) {
|
303
|
-
format = rb_hash_aref(options, ID2SYM(rb_intern("formatter")));
|
304
|
-
if (RTEST(format))
|
305
|
-
Check_Type(format, T_STRING);
|
306
|
-
|
307
|
-
VALUE kw = rb_hash_aref(options, ID2SYM(rb_intern("options")));
|
308
|
-
if (RTEST(kw)) {
|
309
|
-
Check_Type(kw, T_HASH);
|
310
|
-
kwargs = rb_to_py(kw);
|
311
|
-
}
|
312
|
-
}
|
313
|
-
|
314
|
-
lexer = pygments__lexer_for(code, options);
|
315
|
-
|
316
|
-
if (lexer) {
|
317
|
-
args = Py_BuildValue("(s)", RTEST(format) ? RSTRING_PTR(format) : "html");
|
318
|
-
formatter = PyObject_Call(get_formatter_by_name, args, kwargs);
|
319
|
-
Py_XDECREF(args);
|
320
|
-
args = NULL;
|
321
|
-
|
322
|
-
if (formatter) {
|
323
|
-
PyObject *input = NULL, *output = NULL;
|
324
|
-
|
325
|
-
input = PyUnicode_FromStringAndSize(RSTRING_PTR(code), RSTRING_LEN(code));
|
326
|
-
if (input) {
|
327
|
-
output = PyObject_CallFunction(highlight, "(OOO)", input, lexer, formatter);
|
328
|
-
|
329
|
-
if (output) {
|
330
|
-
PyObject *string = PyUnicode_AsEncodedString(output, "utf-8", "strict");
|
331
|
-
if (string) {
|
332
|
-
Py_ssize_t len;
|
333
|
-
char *data;
|
334
|
-
|
335
|
-
if (PyString_AsStringAndSize(string, &data, &len) == 0) {
|
336
|
-
ret = rb_str_new(data, len);
|
337
|
-
}
|
338
|
-
}
|
339
|
-
Py_XDECREF(string);
|
340
|
-
}
|
341
|
-
}
|
342
|
-
|
343
|
-
Py_XDECREF(output);
|
344
|
-
Py_XDECREF(input);
|
345
|
-
}
|
346
|
-
}
|
347
|
-
|
348
|
-
Py_XDECREF(args);
|
349
|
-
Py_XDECREF(kwargs);
|
350
|
-
Py_XDECREF(lexer);
|
351
|
-
PyErr_Clear();
|
352
|
-
|
353
|
-
#ifdef RUBY_VM
|
354
|
-
if (RTEST(ret))
|
355
|
-
rb_funcall(ret, rb_intern("force_encoding"), 1, rb_str_new2("utf-8"));
|
356
|
-
#endif
|
357
|
-
|
358
|
-
return ret;
|
359
|
-
}
|
360
|
-
|
361
|
-
static VALUE
|
362
|
-
rb_pygments_styles(int argc, VALUE *argv, VALUE self)
|
363
|
-
{
|
364
|
-
PyObject *styles = PyObject_CallFunction(get_all_styles, "");
|
365
|
-
VALUE ret = py_to_rb(styles);
|
366
|
-
Py_XDECREF(styles);
|
367
|
-
PyErr_Clear();
|
368
|
-
return ret;
|
369
|
-
}
|
370
|
-
|
371
|
-
static VALUE
|
372
|
-
rb_pygments_filters(int argc, VALUE *argv, VALUE self)
|
373
|
-
{
|
374
|
-
PyObject *filters = PyObject_CallFunction(get_all_filters, "");
|
375
|
-
VALUE ret = py_to_rb(filters);
|
376
|
-
Py_XDECREF(filters);
|
377
|
-
PyErr_Clear();
|
378
|
-
return ret;
|
379
|
-
}
|
380
|
-
|
381
|
-
static VALUE
|
382
|
-
rb_pygments_lexers(int argc, VALUE *argv, VALUE self)
|
383
|
-
{
|
384
|
-
PyObject *lexers = PyObject_CallFunction(get_all_lexers, "");
|
385
|
-
VALUE ret = py_to_rb(lexers);
|
386
|
-
Py_XDECREF(lexers);
|
387
|
-
PyErr_Clear();
|
388
|
-
return ret;
|
389
|
-
}
|
390
|
-
|
391
|
-
static VALUE
|
392
|
-
rb_pygments_formatters(int argc, VALUE *argv, VALUE self)
|
393
|
-
{
|
394
|
-
PyObject *formatters = PyObject_CallFunction(get_all_formatters, "");
|
395
|
-
PyObject *item;
|
396
|
-
VALUE ret = rb_ary_new();
|
397
|
-
|
398
|
-
while ((item = PyIter_Next(formatters))) {
|
399
|
-
VALUE curr = rb_ary_new();
|
400
|
-
rb_ary_push(ret, curr);
|
401
|
-
|
402
|
-
rb_ary_push(curr, py_to_rb(PyObject_GetAttrString(item, "__name__")));
|
403
|
-
rb_ary_push(curr, py_to_rb(PyObject_GetAttrString(item, "name")));
|
404
|
-
rb_ary_push(curr, py_to_rb(PyObject_GetAttrString(item, "aliases")));
|
405
|
-
|
406
|
-
Py_DECREF(item);
|
407
|
-
}
|
408
|
-
|
409
|
-
Py_XDECREF(formatters);
|
410
|
-
PyErr_Clear();
|
411
|
-
return ret;
|
412
|
-
}
|
413
|
-
|
414
|
-
#define ENSURE(var, expr) do{ \
|
415
|
-
if ((var = (expr)) == NULL) { \
|
416
|
-
rb_raise(rb_eRuntimeError, "unable to lookup " # var); \
|
417
|
-
} \
|
418
|
-
} while(0)
|
419
|
-
|
420
|
-
void
|
421
|
-
Init_pygments_ext()
|
422
|
-
{
|
423
|
-
{ /* python stuff */
|
424
|
-
Py_Initialize();
|
425
|
-
|
426
|
-
/* modules */
|
427
|
-
ENSURE(pygments, PyImport_ImportModule("pygments"));
|
428
|
-
ENSURE(pygments_lexers, PyImport_ImportModule("pygments.lexers"));
|
429
|
-
ENSURE(pygments_formatters, PyImport_ImportModule("pygments.formatters"));
|
430
|
-
ENSURE(pygments_styles, PyImport_ImportModule("pygments.styles"));
|
431
|
-
ENSURE(pygments_filters, PyImport_ImportModule("pygments.filters"));
|
432
|
-
|
433
|
-
/* lexer methods */
|
434
|
-
ENSURE(guess_lexer, PyObject_GetAttrString(pygments_lexers, "guess_lexer"));
|
435
|
-
ENSURE(guess_lexer_for_filename, PyObject_GetAttrString(pygments_lexers, "guess_lexer_for_filename"));
|
436
|
-
ENSURE(get_lexer_for_filename, PyObject_GetAttrString(pygments_lexers, "get_lexer_for_filename"));
|
437
|
-
ENSURE(get_lexer_for_mimetype, PyObject_GetAttrString(pygments_lexers, "get_lexer_for_mimetype"));
|
438
|
-
ENSURE(get_lexer_by_name, PyObject_GetAttrString(pygments_lexers, "get_lexer_by_name"));
|
439
|
-
ENSURE(get_all_lexers, PyObject_GetAttrString(pygments_lexers, "get_all_lexers"));
|
440
|
-
|
441
|
-
/* formatter methods */
|
442
|
-
ENSURE(get_formatter_by_name, PyObject_GetAttrString(pygments_formatters, "get_formatter_by_name"));
|
443
|
-
ENSURE(get_all_formatters, PyObject_GetAttrString(pygments_formatters, "get_all_formatters"));
|
444
|
-
|
445
|
-
/* highlighter methods */
|
446
|
-
ENSURE(highlight, PyObject_GetAttrString(pygments, "highlight"));
|
447
|
-
|
448
|
-
/* style methods */
|
449
|
-
ENSURE(get_all_styles, PyObject_GetAttrString(pygments_styles, "get_all_styles"));
|
450
|
-
|
451
|
-
/* filter methods */
|
452
|
-
ENSURE(get_all_filters, PyObject_GetAttrString(pygments_filters, "get_all_filters"));
|
453
|
-
}
|
454
|
-
|
455
|
-
{ /* ruby stuff */
|
456
|
-
mPygments = rb_define_module("Pygments");
|
457
|
-
mPygmentsC = rb_define_module_under(mPygments, "C");
|
458
|
-
rb_define_method(mPygmentsC, "lexer_name_for", rb_pygments_lexer_name_for, -1);
|
459
|
-
rb_define_method(mPygmentsC, "css", rb_pygments_css, -1);
|
460
|
-
rb_define_method(mPygmentsC, "_highlight", rb_pygments_highlight, -1);
|
461
|
-
rb_define_method(mPygmentsC, "styles", rb_pygments_styles, 0);
|
462
|
-
rb_define_method(mPygmentsC, "filters", rb_pygments_filters, 0);
|
463
|
-
rb_define_method(mPygmentsC, "_lexers", rb_pygments_lexers, 0);
|
464
|
-
rb_define_method(mPygmentsC, "_formatters", rb_pygments_formatters, 0);
|
465
|
-
}
|
466
|
-
}
|