SWIG is a tool to generate interfaces for more than 10 programming languages, inluding Python, Perl, Java and C#.
The highlight package includes a SWIG interface file highlight.i which contains all information to generate wrapper code for the highlight::CodeGenerator class. The output module gives you access to the highlight parser from within your favorite (scripting) language.
These are the basic steps to create a python module:
swig -c++ -python -o highlight_wrap.cpp highlight.i
g++ -c highlight_wrap.cpp -I/usr/include/python2.3/ -I../../highlight
g++ -shared highlight_wrap.o ../../highlight/*.o -o _highlight.so
python testmod.py testmod.py testmod.py.html -l --style emacs
%module highlight
%include stl.i
%include std_string.i
%apply const std::string& { const string& };
%apply std::string { string };
%{
#include "../../src/include/codegenerator.h"
#include "../../src/include/datadir.h"
%}
%include "../../src/include/enums.h"
%include "../../src/include/syntaxreader.h"
%include "../../src/include/codegenerator.h"
%include "../../src/include/datadir.h"
# -*- coding: utf-8 -*-
# More advanced SWIG module test script
#
# Import highlight.py, which is the interface for the _highlight.so module.
# See highlight.py for all available attributes and class members.
#
# Example: swig_cli.py testmod.py testmod.py.html -l --style emacs
import highlight
import sys
from optparse import OptionParser
formatList = { "html": highlight.HTML,
"xhtml": highlight.XHTML,
"latex": highlight.LATEX,
"rtf": highlight.RTF,
"tex": highlight.TEX,
"ansi": highlight.ANSI,
"xterm256": highlight.XTERM256,
"svg": highlight.SVG
}
HL_DIR="/usr/share/highlight"
def highlightFile():
parser = OptionParser("usage: %prog [options] input-file output-file")
parser.add_option("-O", "--format", default="html",
choices=("html","xhtml","latex","tex","rtf","ansi","xterm256","svg"),
help="Output format (html, xhtml, latex, tex, rtf, ansi, xterm256, svg)")
parser.add_option("-d", "--doc-title", default="Source file",
help="document title")
parser.add_option("-f", "--fragment", action="store_true",
help="omit file header and footer")
parser.add_option("-F", "--reformat",
choices=('allman','gnu','java','kr','linux', 'banner','stroustrup','whitesmith'),
help="reformats and indents output in given style")
parser.add_option("-l", "--linenumbers", action="store_true",
help="print line numbers in output file")
parser.add_option("-S", "--syntax",
help="specify type of source code")
parser.add_option("-s", "--theme", default="seashell",
help="defines colour theme")
parser.add_option("-u", "--encoding", default="ISO-8859-1",
help="define output encoding which matches input file encoding")
(options, args) = parser.parse_args(sys.argv[1:])
if len(args)!=2:
parser.print_help()
return
formatName=options.format.lower()
outFormat = formatName in formatList and formatList[formatName] or highlight.HTML
(infile, outfile) = args
#get a generator instance (for HTML output)
gen=highlight.CodeGenerator.getInstance(outFormat)
datadir=highlight.DataDir()
datadir.searchDataDir("")
#initialize the generator with a colour theme and the language definition
gen.initTheme(datadir.getThemePath("%s.theme" % options.theme))
if options.reformat:
gen.initIndentationScheme(options.reformat)
if (options.syntax):
syntax = options.syntax
else:
syntax = infile[infile.rindex(".")+1:]
gen.loadLanguage(datadir.getLangPath("%s.lang" % syntax))
gen.setIncludeStyle(1)
gen.setTitle(options.doc_title)
gen.setFragmentCode(options.fragment)
gen.setPrintLineNumbers(options.linenumbers)
gen.setEncoding(options.encoding)
gen.generateFile(infile, outfile)
# clear the instance
highlight.CodeGenerator.deleteInstance(gen)
###############################################################################
if __name__ == "__main__":
highlightFile()
The makefile also includes a rule to create a perl module:
# Perl SWIG module test script
#
# Import highlight.pm, which is the interface for the highlight.so module.
# See highlight.pm for all available attributes and class members.
use highlight;
#get a generator instance (for HTML output)
my $gen = highlight::CodeGenerator::getInstance($highlight::CodeGenerator::HTML);
my $dir = new highlight::DataDir();
$dir->searchDataDir("");
my $themepath=$dir->getThemePath("seashell.theme");
my $langpath=$dir->getLangPath("c.lang");
#initialize the generator with a colour theme and the language definition
$gen->initTheme($themepath);
$gen->loadLanguage($langpath);
#set some parameters
$gen->setIncludeStyle(1);
$gen->setEncoding("ISO-8859-1");
#get output string
my $output=$gen->generateString("int main(int argc, char **argv) {\n".
" HighlightApp app;\n".
" return app.run(argc, argv);\n".
"}\n");
print $output;
# clear the instance
highlight::CodeGenerator::deleteInstance($gen);