Skip to content

Commit

Permalink
Update literalinclude to work with Sphinx 1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
nyergler committed Apr 30, 2017
1 parent 34e7a34 commit 3144043
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 18 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ python:
- "3.6"

env:
- SPHINX_SPEC=Sphinx~=1.5.0
- SPHINX_SPEC=git+https://github.com/sphinx-doc/sphinx.git#egg=Sphinx-dev

install:
- pip install coveralls
- pip install --allow-external Sphinx --allow-unverified Sphinx $SPHINX_SPEC
- pip install Sphinx $SPHINX_SPEC
- pip install git+https://github.com/nyergler/sphinx-testing.git
- python setup.py install

Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'pyyaml',
'sh',
'Sphinx',
'sphinxcontrib-websupport',
]


Expand All @@ -38,8 +39,8 @@
],
},
tests_require=[
'munch',
'sphinx-testing',
'munch',
'sphinx-testing',
],
test_suite='tut.tests',
)
87 changes: 76 additions & 11 deletions src/tut/sphinx/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@
from sphinx.directives.code import (
dedent_lines,
LiteralInclude,
LiteralIncludeReader as SphinxLiteralIncludeReader,
)

from tut import diff
from .manager import TutManager


class TutLiteralInclude(LiteralInclude):
class LiteralIncludeReader(SphinxLiteralIncludeReader):

def read_with_encoding(self, filename, document, codec_info, encoding):
# get the current Tut
manager = TutManager.get(self.state.document.settings.env)
def __init__(self, filename, options, config, tut, gitref):
self._tut = tut
self._gitref = gitref

rel_path, tut_path = self.state.document.settings.env.relfn2path(
manager.resolve_option(self, 'path'),
)
super().__init__(filename, options, config)

rel_fn, _ = self.state.document.settings.env.relfn2path(self.arguments[0])
def read_file(self, filename, location=None):
# type: (unicode, Any) -> List[unicode]

tut = manager.tut(tut_path)
try:
text = tut.file(self.state.document.git_ref, rel_fn[len(rel_path) + 1:]).decode('utf-8')
text = self._tut.file(self._gitref, self.filename[len(self._tut.path) + 1:]).decode(self.encoding)

if 'tab-width' in self.options:
text = text.expandtabs(self.options['tab-width'])

lines = text.splitlines(True)
if 'dedent' in self.options:
return dedent_lines(lines, self.options.get('dedent'))
return dedent_lines(lines, self.options.get('dedent'), location=location)
else:
return lines
except (IOError, OSError):
Expand All @@ -40,6 +40,71 @@ def read_with_encoding(self, filename, document, codec_info, encoding):
'be wrong, try giving an :encoding: option') %
(self.encoding, filename))

class TutLiteralInclude(LiteralInclude):

def run(self):
# type: () -> List[nodes.Node]
document = self.state.document
if not document.settings.file_insertion_enabled:
return [document.reporter.warning('File insertion disabled',
line=self.lineno)]
env = document.settings.env

# get the current Tut
manager = TutManager.get(env)
rel_path, tut_path = self.state.document.settings.env.relfn2path(
manager.resolve_option(self, 'path'),
)

# convert options['diff'] to absolute path
if 'diff' in self.options:
_, path = env.relfn2path(self.options['diff'])
self.options['diff'] = path

try:
location = self.state_machine.get_source_and_line(self.lineno)
rel_filename, filename = env.relfn2path(self.arguments[0])
env.note_dependency(rel_filename)

reader = LiteralIncludeReader(
filename, self.options, env.config,
tut=manager.tut(tut_path),
gitref=self.state.document.git_ref,
)
text, lines = reader.read(location=location)

retnode = nodes.literal_block(text, text, source=filename)
set_source_info(self, retnode)
if self.options.get('diff'): # if diff is set, set udiff
retnode['language'] = 'udiff'
elif 'language' in self.options:
retnode['language'] = self.options['language']
retnode['linenos'] = ('linenos' in self.options or
'lineno-start' in self.options or
'lineno-match' in self.options)
retnode['classes'] += self.options.get('class', [])
extra_args = retnode['highlight_args'] = {}
if 'emphasize-lines' in self.options:
hl_lines = parselinenos(self.options['emphasize-lines'], lines)
if any(i >= lines for i in hl_lines):
logger.warning('line number spec is out of range(1-%d): %r' %
(lines, self.options['emphasize_lines']),
location=location)
extra_args['hl_lines'] = [x + 1 for x in hl_lines if x < lines]
extra_args['linenostart'] = reader.lineno_start

if 'caption' in self.options:
caption = self.options['caption'] or self.arguments[0]
retnode = container_wrapper(self, retnode, caption)

# retnode will be note_implicit_target that is linked from caption and numref.
# when options['name'] is provided, it should be primary ID.
self.add_name(retnode)

return [retnode]
except Exception as exc:
return [document.reporter.warning(str(exc), line=self.lineno)]


class TutCodeDiff(Directive):
has_content = False
Expand Down
1 change: 1 addition & 0 deletions src/tut/tests/test_sphinx_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def test_literalinclude_fetches_from_git(self, git_mock, sphinx_app, status, war
})
git_mock().tut().configure_mock(**{
'file.return_value': b'foobar',
'path': test_root/'literalinclude'/'testing',
})
sphinx_app.builder.build_all()

Expand Down
5 changes: 2 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
[tox]
envlist = {py36}-sphinx-{15,dev}
envlist = {py36}-sphinx-{dev}

[testenv]
pip_pre = True
install_command = pip install --allow-external Sphinx --allow-unverified Sphinx -U {opts} {packages}
basepython =
py36: python3.6
deps =
sphinx-15: Sphinx~=1.5.0
sphinx-dev: git+https://github.com/sphinx-doc/sphinx.git#egg=Sphinx-dev
git+https://github.com/nyergler/sphinx-testing.git#egg=sphinx-testing-dev

commands=python setup.py test

0 comments on commit 3144043

Please sign in to comment.