Class: Rygments::Wrapper
- Inherits:
-
Object
- Object
- Rygments::Wrapper
- Defined in:
- ext/wrapper.c
Class Method Summary collapse
-
.new(helper_path) ⇒ Wrapper instance
Creates a new instance of the Python wrapper.
Instance Method Summary collapse
-
#highlight_file(filename, lexer, formatter) ⇒ String
This method performs syntax highlighting on a file.
-
#highlight_string(string, lexer, formatter) ⇒ String
This method performs syntax highlighting on a string.
Class Method Details
.new(helper_path) ⇒ Wrapper instance
Creates a new instance of the Python wrapper. Do not use this function directly. Instead, use the Rygments.highlight functions which will take care of handling the wrapper.
Note: The Wrapper should be treated as a singleton. It is not designed to handle multiple instances. Furthermore, on Darwin initializing, finalizing and again initializing the Python interpreter results in segmentation faults.
249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'ext/wrapper.c', line 249
static VALUE wrapper_new(VALUE klass, VALUE path) {
/* Patch the Python search path to include the rygments module path */
setenv("PYTHONPATH", RSTRING_PTR(path), 1);
/* Initialize the Python interpreter and load the helper module */
struct wrapper_struct *ptr = ALLOC(struct wrapper_struct);
initialize(ptr);
/* Wrap the state structure */
VALUE tdata = Data_Wrap_Struct(klass, 0, wrapper_free, ptr);
return tdata;
}
|
Instance Method Details
#highlight_file(filename, lexer, formatter) ⇒ String
This method performs syntax highlighting on a file. Do not call this function directly. Instead, use the Rygments.highlight functions.
The return value is always a string. An exception is raised whenever an error occurs.
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'ext/wrapper.c', line 175
static VALUE wrapper_highlight_file(VALUE self, VALUE filename, VALUE lexer, VALUE formatter) {
FILE *f;
/* Make sure the filename is a string */
Check_Type(filename, T_STRING);
/* Open the file */
if ((f=fopen(RSTRING_PTR(filename),"r"))==NULL) {
rb_sys_fail("could not open file");
}
/* Determine the file size */
if (fseek(f,0,SEEK_END) == -1) {
rb_sys_fail("could not seek to end of file");
}
long fsize=ftell(f);
if (fsize == -1) {
rb_sys_fail("could not determine file length");
}
rewind(f);
/* Read the contents of the file into a buffer */
char *buf=ALLOC_N(char, fsize);
if ((fread(buf,1,fsize,f))!=(size_t)fsize) {
/* Close file handle with cached errno value */
int err = errno;
fclose(f);
errno = err;
rb_sys_fail("could not read from file");
}
fclose(f);
/* Convert the buffer to a Ruby string */
VALUE code = rb_str_new(buf, fsize);
free(buf);
/* Call the string highlighting method */
return wrapper_highlight_string(self, code, lexer, formatter);
}
|
#highlight_string(string, lexer, formatter) ⇒ String
This method performs syntax highlighting on a string. Do not call this function directly. Instead, use the Rygments.highlight functions.
The return value is always a string. An exception is raised whenever an error occurs.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'ext/wrapper.c', line 121
static VALUE wrapper_highlight_string(VALUE self, VALUE code, VALUE lexer, VALUE formatter) {
struct wrapper_struct *s;
PyObject *pArgs, *pValue;
int res;
/* Make sure we have three strings as input */
Check_Type(code, T_STRING);
Check_Type(lexer, T_STRING);
Check_Type(formatter, T_STRING);
/* Get the wrapper structure */
Data_Get_Struct(self, struct wrapper_struct, s);
/* Collect arguments in a tuple */
pArgs = PyTuple_New(3);
res = 0;
res += put_in_tuple(pArgs, 0, RSTRING_PTR(code));
res += put_in_tuple(pArgs, 1, RSTRING_PTR(lexer));
res += put_in_tuple(pArgs, 2, RSTRING_PTR(formatter));
/* Check if we have bad arguments */
if (res) {
Py_DECREF(pArgs);
rb_raise(rb_eRuntimeError, "bad input given");
}
/* Call the function */
pValue = PyObject_CallObject(s->rygmentize, pArgs);
Py_DECREF(pArgs);
/* Check if we have a valid result */
if (pValue == NULL) {
reraise();
rb_raise(rb_eRuntimeError, "Call failed");
}
/* Convert the pygmentized result to a Ruby string */
VALUE result = rb_str_new2(PyString_AsString(pValue));
Py_DECREF(pValue);
return result;
}
|