mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Move env.warn_and_replace() to SphinxInput class
This commit is contained in:
parent
f7dd8e94e2
commit
e2be2ca77f
@ -14,7 +14,6 @@ import os
|
||||
import sys
|
||||
import time
|
||||
import types
|
||||
import codecs
|
||||
import fnmatch
|
||||
import warnings
|
||||
from os import path
|
||||
@ -642,21 +641,6 @@ class BuildEnvironment(object):
|
||||
|
||||
# --------- SINGLE FILE READING --------------------------------------------
|
||||
|
||||
def warn_and_replace(self, error):
|
||||
# type: (Any) -> Tuple
|
||||
"""Custom decoding error handler that warns and replaces."""
|
||||
linestart = error.object.rfind(b'\n', 0, error.start)
|
||||
lineend = error.object.find(b'\n', error.start)
|
||||
if lineend == -1:
|
||||
lineend = len(error.object)
|
||||
lineno = error.object.count(b'\n', 0, error.start) + 1
|
||||
logger.warning('undecodable source characters, replacing with "?": %r',
|
||||
(error.object[linestart + 1:error.start] + b'>>>' +
|
||||
error.object[error.start:error.end] + b'<<<' +
|
||||
error.object[error.end:lineend]),
|
||||
location=(self.docname, lineno))
|
||||
return (u'?', error.end)
|
||||
|
||||
def prepare_settings(self, docname):
|
||||
"""Prepare to set up environment for reading."""
|
||||
# type: (unicode) -> None
|
||||
@ -693,7 +677,6 @@ class BuildEnvironment(object):
|
||||
self.note_dependency(docutilsconf)
|
||||
|
||||
with sphinx_domains(self), rst.default_role(docname, self.config.default_role):
|
||||
codecs.register_error('sphinx', self.warn_and_replace) # type: ignore
|
||||
doctree = read_doc(self.app, self, self.doc2path(docname))
|
||||
|
||||
# post-processing
|
||||
|
25
sphinx/io.py
25
sphinx/io.py
@ -8,6 +8,8 @@
|
||||
:copyright: Copyright 2007-2017 by the Sphinx team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
import codecs
|
||||
|
||||
from docutils.io import FileInput, NullOutput
|
||||
from docutils.core import Publisher
|
||||
from docutils.readers import standalone
|
||||
@ -25,6 +27,7 @@ from sphinx.transforms.compact_bullet_list import RefOnlyBulletListTransform
|
||||
from sphinx.transforms.i18n import (
|
||||
PreserveTranslatableMessages, Locale, RemoveTranslatableInline,
|
||||
)
|
||||
from sphinx.util import logging
|
||||
from sphinx.util import import_object, split_docinfo
|
||||
from sphinx.util.docutils import LoggingReporter
|
||||
|
||||
@ -40,6 +43,9 @@ if False:
|
||||
from sphinx.environment import BuildEnvironment # NOQA
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class SphinxBaseReader(standalone.Reader):
|
||||
"""
|
||||
Add our source parsers
|
||||
@ -154,6 +160,10 @@ class SphinxFileInput(FileInput):
|
||||
# type: (Sphinx, BuildEnvironment, Any, Any) -> None
|
||||
self.app = app
|
||||
self.env = env
|
||||
|
||||
# set up error handler
|
||||
codecs.register_error('sphinx', self.warn_and_replace) # type: ignore
|
||||
|
||||
kwds['error_handler'] = 'sphinx' # py3: handle error on open.
|
||||
FileInput.__init__(self, *args, **kwds)
|
||||
|
||||
@ -187,6 +197,21 @@ class SphinxFileInput(FileInput):
|
||||
data = self.env.config.rst_prolog + '\n' + data
|
||||
return docinfo + data
|
||||
|
||||
def warn_and_replace(self, error):
|
||||
# type: (Any) -> Tuple
|
||||
"""Custom decoding error handler that warns and replaces."""
|
||||
linestart = error.object.rfind(b'\n', 0, error.start)
|
||||
lineend = error.object.find(b'\n', error.start)
|
||||
if lineend == -1:
|
||||
lineend = len(error.object)
|
||||
lineno = error.object.count(b'\n', 0, error.start) + 1
|
||||
logger.warning('undecodable source characters, replacing with "?": %r',
|
||||
(error.object[linestart + 1:error.start] + b'>>>' +
|
||||
error.object[error.start:error.end] + b'<<<' +
|
||||
error.object[error.end:lineend]),
|
||||
location=(self.env.docname, lineno))
|
||||
return (u'?', error.end)
|
||||
|
||||
|
||||
def read_doc(app, env, filename):
|
||||
"""Parse a document and convert to doctree."""
|
||||
|
Loading…
Reference in New Issue
Block a user