mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Add ReSTRenderer
This commit is contained in:
parent
662389b8b0
commit
d960f2265d
@ -15,7 +15,7 @@ from jinja2.sandbox import SandboxedEnvironment
|
|||||||
from sphinx import package_dir
|
from sphinx import package_dir
|
||||||
from sphinx.jinja2glue import SphinxFileSystemLoader
|
from sphinx.jinja2glue import SphinxFileSystemLoader
|
||||||
from sphinx.locale import get_translator
|
from sphinx.locale import get_translator
|
||||||
from sphinx.util import texescape
|
from sphinx.util import rst, texescape
|
||||||
|
|
||||||
if False:
|
if False:
|
||||||
# For type annotation
|
# For type annotation
|
||||||
@ -84,3 +84,17 @@ class LaTeXRenderer(SphinxRenderer):
|
|||||||
self.env.variable_end_string = '%>'
|
self.env.variable_end_string = '%>'
|
||||||
self.env.block_start_string = '<%'
|
self.env.block_start_string = '<%'
|
||||||
self.env.block_end_string = '%>'
|
self.env.block_end_string = '%>'
|
||||||
|
|
||||||
|
|
||||||
|
class ReSTRenderer(SphinxRenderer):
|
||||||
|
def __init__(self, template_path=None, language=None):
|
||||||
|
# type: (str, str) -> None
|
||||||
|
super().__init__(template_path)
|
||||||
|
|
||||||
|
# add language to environment
|
||||||
|
self.env.extend(language=language)
|
||||||
|
|
||||||
|
# use texescape as escape filter
|
||||||
|
self.env.filters['e'] = rst.escape
|
||||||
|
self.env.filters['escape'] = rst.escape
|
||||||
|
self.env.filters['heading'] = rst.heading
|
||||||
|
37
tests/test_util_template.py
Normal file
37
tests/test_util_template.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
"""
|
||||||
|
test_util_template
|
||||||
|
~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Tests sphinx.util.template functions.
|
||||||
|
|
||||||
|
:copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
|
||||||
|
:license: BSD, see LICENSE for details.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from sphinx.util.template import ReSTRenderer
|
||||||
|
|
||||||
|
|
||||||
|
def test_ReSTRenderer_escape():
|
||||||
|
r = ReSTRenderer()
|
||||||
|
template = '{{ "*hello*" | e }}'
|
||||||
|
assert r.render_string(template, {}) == r'\*hello\*'
|
||||||
|
|
||||||
|
|
||||||
|
def test_ReSTRenderer_heading():
|
||||||
|
r = ReSTRenderer()
|
||||||
|
|
||||||
|
template = '{{ "hello" | heading }}'
|
||||||
|
assert r.render_string(template, {}) == 'hello\n====='
|
||||||
|
|
||||||
|
template = '{{ "hello" | heading(1) }}'
|
||||||
|
assert r.render_string(template, {}) == 'hello\n====='
|
||||||
|
|
||||||
|
template = '{{ "русский язык" | heading(2) }}'
|
||||||
|
assert r.render_string(template, {}) == ('русский язык\n'
|
||||||
|
'------------')
|
||||||
|
|
||||||
|
# language: ja
|
||||||
|
r.env.language = 'ja'
|
||||||
|
template = '{{ "русский язык" | heading }}'
|
||||||
|
assert r.render_string(template, {}) == ('русский язык\n'
|
||||||
|
'=======================')
|
Loading…
Reference in New Issue
Block a user