@@ -1,3 +1,9 @@ | |||||
Version 0.8.4c20 | |||||
============================================================ | |||||
* Added test for codehilite markdown extension (Issue #82) | |||||
* Added rst_directive.py from the pygments repository (Issue #82) | |||||
Version 0.8.4c19 | Version 0.8.4c19 | ||||
============================================================ | ============================================================ | ||||
@@ -9,7 +15,6 @@ Version 0.8.4c18 | |||||
* Added support for default publisher (Issue #83) | * Added support for default publisher (Issue #83) | ||||
Version 0.8.4c17 | Version 0.8.4c17 | ||||
============================================================ | ============================================================ | ||||
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||
THE SOFTWARE. | |||||
THE SOFTWARE. |
@@ -1,4 +1,4 @@ | |||||
Version 0.8.4c19 | |||||
Version 0.8.4c20 | |||||
A brand new **hyde** | A brand new **hyde** | ||||
==================== | ==================== | ||||
@@ -125,7 +125,7 @@ def markdown(env, value): | |||||
import markdown as md | import markdown as md | ||||
except ImportError: | except ImportError: | ||||
logger.error(u"Cannot load the markdown library.") | logger.error(u"Cannot load the markdown library.") | ||||
raise TemplateError("Cannot load the markdown library") | |||||
raise TemplateError(u"Cannot load the markdown library") | |||||
output = value | output = value | ||||
d = {} | d = {} | ||||
if hasattr(env.config, 'markdown'): | if hasattr(env.config, 'markdown'): | ||||
@@ -147,8 +147,16 @@ def restructuredtext(env, value): | |||||
try: | try: | ||||
from docutils.core import publish_parts | from docutils.core import publish_parts | ||||
except ImportError: | except ImportError: | ||||
print u"Requires docutils library to use restructuredtext tag." | |||||
raise | |||||
logger.error(u"Cannot load the docutils library.") | |||||
raise TemplateError(u"Cannot load the docutils library.") | |||||
highlight_source = False | |||||
if hasattr(env.config, 'restructuredtext'): | |||||
highlight_source = getattr(env.config.restructuredtext, 'highlight_source', False) | |||||
if highlight_source: | |||||
import hyde.lib.pygments.rst_directive | |||||
parts = publish_parts(source=value, writer_name="html") | parts = publish_parts(source=value, writer_name="html") | ||||
return parts['html_body'] | return parts['html_body'] | ||||
@@ -0,0 +1,82 @@ | |||||
# -*- coding: utf-8 -*- | |||||
""" | |||||
The Pygments reStructuredText directive | |||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |||||
This fragment is a Docutils_ 0.5 directive that renders source code | |||||
(to HTML only, currently) via Pygments. | |||||
To use it, adjust the options below and copy the code into a module | |||||
that you import on initialization. The code then automatically | |||||
registers a ``sourcecode`` directive that you can use instead of | |||||
normal code blocks like this:: | |||||
.. sourcecode:: python | |||||
My code goes here. | |||||
If you want to have different code styles, e.g. one with line numbers | |||||
and one without, add formatters with their names in the VARIANTS dict | |||||
below. You can invoke them instead of the DEFAULT one by using a | |||||
directive option:: | |||||
.. sourcecode:: python | |||||
:linenos: | |||||
My code goes here. | |||||
Look at the `directive documentation`_ to get all the gory details. | |||||
.. _Docutils: http://docutils.sf.net/ | |||||
.. _directive documentation: | |||||
http://docutils.sourceforge.net/docs/howto/rst-directives.html | |||||
:copyright: Copyright 2006-2011 by the Pygments team, see AUTHORS. | |||||
:license: BSD, see LICENSE for details. | |||||
""" | |||||
# Options | |||||
# ~~~~~~~ | |||||
# Set to True if you want inline CSS styles instead of classes | |||||
INLINESTYLES = False | |||||
from pygments.formatters import HtmlFormatter | |||||
# The default formatter | |||||
DEFAULT = HtmlFormatter(noclasses=INLINESTYLES) | |||||
# Add name -> formatter pairs for every variant you want to use | |||||
VARIANTS = { | |||||
'linenos': HtmlFormatter(noclasses=INLINESTYLES, linenos=True), | |||||
} | |||||
from docutils import nodes | |||||
from docutils.parsers.rst import directives, Directive | |||||
from pygments import highlight | |||||
from pygments.lexers import get_lexer_by_name, TextLexer | |||||
class Pygments(Directive): | |||||
""" Source code syntax hightlighting. | |||||
""" | |||||
required_arguments = 1 | |||||
optional_arguments = 0 | |||||
final_argument_whitespace = True | |||||
option_spec = dict([(key, directives.flag) for key in VARIANTS]) | |||||
has_content = True | |||||
def run(self): | |||||
self.assert_has_content() | |||||
try: | |||||
lexer = get_lexer_by_name(self.arguments[0]) | |||||
except ValueError: | |||||
# no lexer found - use the text one instead of an exception | |||||
lexer = TextLexer() | |||||
# take an arbitrary option if more than one is given | |||||
formatter = self.options and VARIANTS[self.options.keys()[0]] or DEFAULT | |||||
parsed = highlight(u'\n'.join(self.content), lexer, formatter) | |||||
return [nodes.raw('', parsed, format='html')] | |||||
directives.register_directive('sourcecode', Pygments) |
@@ -193,6 +193,41 @@ Hello | |||||
<h1 class="title">Hello</h1> | <h1 class="title">Hello</h1> | ||||
</div>""", html | </div>""", html | ||||
def test_restructuredtext_with_sourcecode(): | |||||
source = """ | |||||
{% restructuredtext %} | |||||
Code | |||||
==== | |||||
.. sourcecode:: python | |||||
def add(a, b): | |||||
return a + b | |||||
See `Example`_ | |||||
.. _Example: example.html | |||||
{% endrestructuredtext %} | |||||
""" | |||||
expected = """ | |||||
<div class="document" id="code"> | |||||
<h1 class="title">Code</h1> | |||||
<div class="highlight"><pre><span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> | |||||
<span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> | |||||
</pre></div> | |||||
<p>See <a class="reference external" href="example.html">Example</a></p> | |||||
</div> | |||||
""" | |||||
t = Jinja2Template(JINJA2.path) | |||||
s = Site(JINJA2.path) | |||||
c = Config(JINJA2.path, config_dict=dict( | |||||
restructuredtext=dict(highlight_source=True))) | |||||
s.config = c | |||||
t.configure(s) | |||||
html = t.render(source, {}).strip() | |||||
assert html.strip() == expected.strip() | |||||
def test_markdown_with_extensions(): | def test_markdown_with_extensions(): | ||||
source = """ | source = """ | ||||
{%markdown%} | {%markdown%} | ||||
@@ -3,4 +3,4 @@ | |||||
Handles hyde version | Handles hyde version | ||||
TODO: Use fabric like versioning scheme | TODO: Use fabric like versioning scheme | ||||
""" | """ | ||||
__version__ = '0.8.4c19' | |||||
__version__ = '0.8.4c20' |