跳转到主要内容

一组templatetags和过滤器,可以在django模板中使用restructured text。

项目描述

django-rstify - django模板中的restructured text过滤器

django-rstify是一组模板过滤器,提供从restructured text到HTML的简单转换。除了这个基本功能外,此应用程序还允许您使用pygments库在您的文本中突出显示源代码片段。

安装

安装此应用程序有几种方法

  1. 从我的git仓库检出最新版本

    git clone git://github.com/bartTC/django-rstify.git

然后,要么将目录 rstify 放入您的pythonpath中,要么切换到 django-rstify 并执行 python setup.py install

  1. 使用easy_install安装此应用程序

    easy_install django-rstify

最后,将 rstify 放入您的django项目中的 INSTALLED_APPS 设置。

如何在模板中使用它

要将对象中的restructured text转换为HTML,只需对其应用 rstify 过滤器

{% load rstify_tags %}

{{ entry.content|rstify }}

如果您想转换内联内容,请使用 filter templatetag将其包围

{% load rstify_tags %}

{% filter rstify %}
This is some *restructured text*.
{% endfilter %}

如何在源代码中使用它

在您的代码中应用此过滤器非常简单

>>> from rstify import rstify
>>>
>>> print rstify('This is *restructured text*.')
<p>This is <em>restructured text</em>.</p>

初始标题级别

默认情况下,您的重构文本中的初始标题在HTML中变为<h1>

>>> header = '''
... ================
... This is a Header
... ================
... '''
>>> print rstify(header)
<div class="section">
<h1><a id="this-is-a-header" name="this-is-a-header">This is a Header</a></h1>
</div>

您可以通过将initial_header_level设置为1到6的整数来覆盖此设置

>>> print rstify(header, initial_header_level=3)
<div class="section">
<h3><a id="this-is-a-header" name="this-is-a-header">This is a Header</a></h3>
</div>

在您的模板中,只需将其设置为rstify过滤器的第一个选项

{{ entry.content|rstify:"3" }}

语法高亮显示

django-rstify使用pygments库提供语法高亮显示。要突出显示重构文本的部分,只需将其放入sourcecode指令中

Here is some text. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Integer porttitor nulla sed dui. Aenean lorem mi, tincidunt et, porttitor
nec, condimentum venenatis, felis. Maecenas ornare blandit leo.

.. sourcecode:: python

    def foo(bar):
        return bar*2

Continue with text. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Integer porttitor nulla sed dui. Aenean lorem mi, tincidunt et, porttitor nec,
condimentum venenatis, felis. Maecenas ornare blandit leo.

Pygments提供了一组高亮显示器(也称为lexer),只需将python替换为您选择的lexer即可。以下是完整的高亮显示器集合可用lexer。阅读pygments样式,了解如何使用css着色输出。

许可证

此应用程序根据New BSD License授权。有关详细信息,请参阅LICENSE。django-rstify附带一个pygments指令,该指令根据BSD License发布。有关详细信息,请参阅rstify/pygments_directive.py

项目详情


下载文件

下载适合您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

django-rstify-0.2.tar.gz (5.2 kB 查看哈希)

上传时间

支持者