mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Refactor util.docutils.new_document()
cache (#10805)
This commit is contained in:
parent
4cd950e1ba
commit
592da865d3
@ -30,6 +30,8 @@ logger = logging.getLogger(__name__)
|
|||||||
report_re = re.compile('^(.+?:(?:\\d+)?): \\((DEBUG|INFO|WARNING|ERROR|SEVERE)/(\\d+)?\\) ')
|
report_re = re.compile('^(.+?:(?:\\d+)?): \\((DEBUG|INFO|WARNING|ERROR|SEVERE)/(\\d+)?\\) ')
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
|
from docutils.frontend import Values
|
||||||
|
|
||||||
from sphinx.builders import Builder
|
from sphinx.builders import Builder
|
||||||
from sphinx.config import Config
|
from sphinx.config import Config
|
||||||
from sphinx.environment import BuildEnvironment
|
from sphinx.environment import BuildEnvironment
|
||||||
@ -598,7 +600,7 @@ if docutils.__version_info__ <= (0, 18):
|
|||||||
|
|
||||||
# cache a vanilla instance of nodes.document
|
# cache a vanilla instance of nodes.document
|
||||||
# Used in new_document() function
|
# Used in new_document() function
|
||||||
__document_cache__: Optional[nodes.document] = None
|
__document_cache__: Tuple["Values", Reporter]
|
||||||
|
|
||||||
|
|
||||||
def new_document(source_path: str, settings: Any = None) -> nodes.document:
|
def new_document(source_path: str, settings: Any = None) -> nodes.document:
|
||||||
@ -609,15 +611,18 @@ def new_document(source_path: str, settings: Any = None) -> nodes.document:
|
|||||||
This makes an instantiation of document nodes much faster.
|
This makes an instantiation of document nodes much faster.
|
||||||
"""
|
"""
|
||||||
global __document_cache__
|
global __document_cache__
|
||||||
if __document_cache__ is None:
|
try:
|
||||||
__document_cache__ = docutils.utils.new_document(source_path)
|
cached_settings, reporter = __document_cache__
|
||||||
|
except NameError:
|
||||||
|
doc = docutils.utils.new_document(source_path)
|
||||||
|
__document_cache__ = cached_settings, reporter = doc.settings, doc.reporter
|
||||||
|
|
||||||
if settings is None:
|
if settings is None:
|
||||||
# Make a copy of ``settings`` from cache to accelerate instantiation
|
# Make a copy of the cached settings to accelerate instantiation
|
||||||
settings = copy(__document_cache__.settings)
|
settings = copy(cached_settings)
|
||||||
|
|
||||||
# Create a new instance of nodes.document using cached reporter
|
# Create a new instance of nodes.document using cached reporter
|
||||||
from sphinx import addnodes
|
from sphinx import addnodes
|
||||||
document = addnodes.document(settings, __document_cache__.reporter, source=source_path)
|
document = addnodes.document(settings, reporter, source=source_path)
|
||||||
document.note_source(source_path, -1)
|
document.note_source(source_path, -1)
|
||||||
return document
|
return document
|
||||||
|
Loading…
Reference in New Issue
Block a user