Merge pull request #6158 from tk0miya/refactor_citation_ref

refactor CitationReferences transform
This commit is contained in:
Takeshi KOMIYA 2019-03-10 17:45:06 +09:00 committed by GitHub
commit 1aba35f445
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 21 deletions

View File

@ -20,7 +20,7 @@ from docutils.writers import UnfilteredWriter
from sphinx.deprecation import RemovedInSphinx30Warning from sphinx.deprecation import RemovedInSphinx30Warning
from sphinx.transforms import ( from sphinx.transforms import (
ApplySourceWorkaround, ExtraTranslatableNodes, CitationReferences, ApplySourceWorkaround, ExtraTranslatableNodes, SmartQuotesSkipper, CitationReferences,
DefaultSubstitutions, MoveModuleTargets, HandleCodeBlocks, SortIds, FigureAligner, DefaultSubstitutions, MoveModuleTargets, HandleCodeBlocks, SortIds, FigureAligner,
AutoNumbering, AutoIndexUpgrader, FilterSystemMessages, AutoNumbering, AutoIndexUpgrader, FilterSystemMessages,
UnreferencedFootnotesDetector, SphinxSmartQuotes, DoctreeReadEvent, ManpageLink UnreferencedFootnotesDetector, SphinxSmartQuotes, DoctreeReadEvent, ManpageLink
@ -99,7 +99,7 @@ class SphinxStandaloneReader(SphinxBaseReader):
RemoveTranslatableInline, FilterSystemMessages, RefOnlyBulletListTransform, RemoveTranslatableInline, FilterSystemMessages, RefOnlyBulletListTransform,
UnreferencedFootnotesDetector, SphinxSmartQuotes, ManpageLink, UnreferencedFootnotesDetector, SphinxSmartQuotes, ManpageLink,
SphinxDomains, SubstitutionDefinitionsRemover, DoctreeReadEvent, SphinxDomains, SubstitutionDefinitionsRemover, DoctreeReadEvent,
UIDTransform] UIDTransform, SmartQuotesSkipper]
def __init__(self, app, *args, **kwargs): def __init__(self, app, *args, **kwargs):
# type: (Sphinx, Any, Any) -> None # type: (Sphinx, Any, Any) -> None
@ -141,7 +141,7 @@ class SphinxI18nReader(SphinxBaseReader):
AutoNumbering, SortIds, RemoveTranslatableInline, AutoNumbering, SortIds, RemoveTranslatableInline,
FilterSystemMessages, RefOnlyBulletListTransform, FilterSystemMessages, RefOnlyBulletListTransform,
UnreferencedFootnotesDetector, SphinxSmartQuotes, ManpageLink, UnreferencedFootnotesDetector, SphinxSmartQuotes, ManpageLink,
SubstitutionDefinitionsRemover] SubstitutionDefinitionsRemover, SmartQuotesSkipper]
def set_lineno_for_reporter(self, lineno): def set_lineno_for_reporter(self, lineno):
# type: (int) -> None # type: (int) -> None

View File

@ -23,7 +23,9 @@ from sphinx.locale import _, __
from sphinx.util import logging from sphinx.util import logging
from sphinx.util.docutils import new_document from sphinx.util.docutils import new_document
from sphinx.util.i18n import format_date from sphinx.util.i18n import format_date
from sphinx.util.nodes import NodeMatcher, apply_source_workaround, is_smartquotable from sphinx.util.nodes import (
NodeMatcher, apply_source_workaround, copy_source_info, is_smartquotable
)
if False: if False:
# For type annotation # For type annotation
@ -198,6 +200,18 @@ class SortIds(SphinxTransform):
node['ids'] = node['ids'][1:] + [node['ids'][0]] node['ids'] = node['ids'][1:] + [node['ids'][0]]
class SmartQuotesSkipper(SphinxTransform):
"""Mark specific nodes as not smartquoted."""
default_priority = 619
def apply(self, **kwargs):
# type: (Any) -> None
# citation labels
for node in self.document.traverse(nodes.citation):
label = cast(nodes.label, node[0])
label['support_smartquotes'] = False
class CitationReferences(SphinxTransform): class CitationReferences(SphinxTransform):
""" """
Replace citation references by pending_xref nodes before the default Replace citation references by pending_xref nodes before the default
@ -207,23 +221,16 @@ class CitationReferences(SphinxTransform):
def apply(self, **kwargs): def apply(self, **kwargs):
# type: (Any) -> None # type: (Any) -> None
# mark citation labels as not smartquoted for node in self.document.traverse(nodes.citation_reference):
for citation in self.document.traverse(nodes.citation): target = node.astext()
label = cast(nodes.label, citation[0]) ref = addnodes.pending_xref(target, refdomain='std', reftype='citation',
label['support_smartquotes'] = False reftarget=target, refwarn=True,
for citation_ref in self.document.traverse(nodes.citation_reference):
cittext = citation_ref.astext()
refnode = addnodes.pending_xref(cittext, refdomain='std', reftype='citation',
reftarget=cittext, refwarn=True,
support_smartquotes=False, support_smartquotes=False,
ids=citation_ref["ids"]) ids=node["ids"],
refnode.source = citation_ref.source or citation_ref.parent.source classes=node.get('classes', []))
refnode.line = citation_ref.line or citation_ref.parent.line ref += nodes.inline(target, '[%s]' % target)
refnode += nodes.inline(cittext, '[%s]' % cittext) copy_source_info(node, ref)
for class_name in citation_ref.attributes.get('classes', []): node.replace_self(ref)
refnode['classes'].append(class_name)
citation_ref.parent.replace(citation_ref, refnode)
TRANSLATABLE_NODES = { TRANSLATABLE_NODES = {