Created
October 1, 2012 23:23
-
-
Save cespare/3815154 to your computer and use it in GitHub Desktop.
A first hello-world attempt at pygments.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// #cgo CFLAGS: -I/usr/include/python2.7 | |
// #cgo LDFLAGS: -lpython2.7 | |
// #include <Python.h> | |
// int pyRunString(const char *s) { return PyRun_SimpleString(s); } | |
import "C" | |
import "unsafe" | |
var pythonCode = ` | |
from pygments import highlight | |
from pygments.lexers import RubyLexer | |
from pygments.formatters import HtmlFormatter | |
code = 'puts "Hello World"' | |
print highlight(code, RubyLexer(), HtmlFormatter()) | |
` | |
func main() { | |
C.Py_Initialize() | |
cs := C.CString(pythonCode) | |
C.pyRunString(cs) | |
C.free(unsafe.Pointer(cs)) | |
C.Py_Finalize() | |
} | |
// $ go run test.go | |
// <div class="highlight"><pre><span class="nb">puts</span> <span class="s2">"Hello World"</span> | |
// </pre></div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// Unfortunately go-python doesn't seem to quite install correctly. If I use it I'll probably fork and fix the CGO library path stuff. | |
// Installation: | |
// $ CGO_CFLAGS="-I/usr/include/python2.7" CGO_LDFLAGS="-lpython2.7 -L/usr/lib" go get -a -v bitbucket.org/binet/go-python/pkg/python | |
import "bitbucket.org/binet/go-python/pkg/python" | |
var pythonCode = ` | |
from pygments import highlight | |
from pygments.lexers import RubyLexer | |
from pygments.formatters import HtmlFormatter | |
code = 'puts "Hello World"' | |
print highlight(code, RubyLexer(), HtmlFormatter()) | |
` | |
func main() { | |
python.PyRun_SimpleString(pythonCode) | |
} | |
// Same output as first program |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is working for me if you are still looking for pygments in go you should give it a try https://github.com/sevki/sandman