Drkcore

02 10 2009 perl Python Tweet

perlでソースコードのハイライト

Pygmentsをperlで使いたかったので、 Inline::Pythonでやってみた。

use Inline Python => <<'END_OF_PYTHON_CODE';
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter

def py_highlight(code):
   return highlight(code, PythonLexer(), HtmlFormatter())

def get_stylesheet():
    return HtmlFormatter().get_style_defs('.highlight')
END_OF_PYTHON_CODE


$code = <<'PYTHON_CODE';
from reportlab.pdfgen import canvas

c = canvas.Canvas('hello.pdf')
t = c.beginText()
t.setFont(t._fontname, 64)
t.setTextOrigin(0, 0)
t.textOut('Hello, World')
h = 64 / t.getX() * c._pagesize[1]
t.setFont(t._fontname, h)
c.setFontSize(h)
c.rotate(-90)
c.drawString(-c._pagesize[1], t._leading - h, 'Hello, World')
c.save()
PYTHON_CODE

printf qq|<html><head><style type="text/css">%s</style>
</head>\n<body>%s</body>\n</html>|, get_stylesheet(), py_highlight($code);

pygments

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021