Using Pygments in ReST documents のコピペ。 myproject/__init__.py に書くのがベストなのかどうかワカランですけど、動いたのでよしと。
myproject/__init__.py
from docutils import nodes
from docutils.parsers.rst import directives
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
PYGMENTS_FORMATTER = HtmlFormatter()
def pygments_directive(name, arguments, options, content, lineno,
content_offset, block_text, state, state_machine):
try:
lexer = get_lexer_by_name(arguments[0])
except ValueError:
# no lexer found
lexer = get_lexer_by_name('text')
parsed = highlight(u'\n'.join(content), lexer, PYGMENTS_FORMATTER)
return [nodes.raw('', parsed, format='html')]
pygments_directive.arguments = (1, 0, 1)
pygments_directive.content = 1
directives.register_directive('sourcecode', pygments_directive)
これもコピペ。
mkdir myproject/static
cd static
curl -O http://pygments.pocoo.org/media/pygments_style.css
※ DownloadしたCSSファイル内の "syntax" をすべて "highlight" に書き換えるっす。
myproject.urls.py
import myproject.settings
urlpatterns = patterns("",
...
)
if myproject.settings.DEBUG:
urlpatterns += patterns("",
(r'^static/(.*)$', 'django.views.static.serve',{'document_root': 'static'}),
)
see: 静的なファイルの提供方法
myproject/templates/base.html
<html>
<head>
...
{% block css %}{% endblock %}
...
</head>
<body>
...
</body>
</html>
myproject/templates/blog/base.html
{% extends "base.html" %}
{% block css %}
<link rel="stylesheet" type="text/css" href="/static/pygments_style.css" />
{% endblock %}
