Skip to content

Commit

Permalink
🐛 FIX: directive/role name translations
Browse files Browse the repository at this point in the history
The language_module was not being correctly loaded and used for directives/roles.
  • Loading branch information
chrisjsewell committed Jan 20, 2021
1 parent b82373a commit ceeac0b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
17 changes: 9 additions & 8 deletions myst_parser/docutils_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
from contextlib import contextmanager
from copy import deepcopy
from datetime import date, datetime
from types import ModuleType
from typing import Any, Dict, List, Optional, Union, cast

import jinja2
import yaml
from docutils import nodes
from docutils.frontend import OptionParser
from docutils.languages import get_language
from docutils.parsers.rst import Directive, DirectiveError
from docutils.parsers.rst import Parser as RSTParser
from docutils.parsers.rst import directives, roles
from docutils.parsers.rst.directives.misc import Include
from docutils.parsers.rst.languages import get_language as get_language_rst
from docutils.statemachine import StringList
from docutils.transforms.components import Filter
from docutils.utils import Reporter, new_document
Expand Down Expand Up @@ -105,8 +106,11 @@ def setup_render(self, options: Dict[str, Any], env: Dict[str, Any]):
"current_node", self.document
)
self.reporter: Reporter = self.document.reporter
self.language_module: str = self.document.settings.language_code
get_language(self.language_module)
# note there are actually two possible language modules:
# one from docutils.languages, and one from docutils.parsers.rst.languages
self.language_module_rst: ModuleType = get_language_rst(
self.document.settings.language_code
)
self._level_to_elem: Dict[int, nodes.Element] = {0: self.document}

def render(self, tokens: List[Token], options, env: AttrDict):
Expand Down Expand Up @@ -720,7 +724,7 @@ def render_myst_role(self, token: Token):
rawsource = f":{name}:`{token.content}`"
lineno = token_line(token) if token.map else 0
role_func, messages = roles.role(
name, self.language_module, lineno, self.reporter
name, self.language_module_rst, lineno, self.reporter
)
inliner = MockInliner(self, lineno)
if role_func:
Expand Down Expand Up @@ -847,7 +851,7 @@ def run_directive(

# get directive class
directive_class, messages = directives.directive(
name, self.language_module, self.document
name, self.language_module_rst, self.document
) # type: (Directive, list)
if not directive_class:
error = self.reporter.error(
Expand Down Expand Up @@ -1016,9 +1020,6 @@ def render_substitution(self, token, inline: bool):
# we record used references before nested parsing, then remove them after
self.document.sub_references.update(references)

if inline:
state_machine = MockStateMachine(self, position)

try:
if inline and not REGEX_DIRECTIVE_START.match(rendered):
sub_nodes, _ = state.inline_text(rendered, position)
Expand Down
7 changes: 3 additions & 4 deletions myst_parser/mocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from docutils.parsers.rst import Directive, DirectiveError
from docutils.parsers.rst import Parser as RSTParser
from docutils.parsers.rst.directives.misc import Include
from docutils.parsers.rst.languages import get_language
from docutils.parsers.rst.states import Body, Inliner, RSTStateMachine
from docutils.statemachine import StringList
from docutils.utils import unescape
Expand Down Expand Up @@ -41,7 +40,7 @@ def __init__(self, renderer: "DocutilsRenderer", lineno: int):
# but I can't see how that would work in RST?
self.reporter.get_source_and_line = lambda l: (self.document["source"], l)
self.parent = renderer.current_node
self.language = renderer.language_module
self.language = renderer.language_module_rst
self.rfc_url = "rfc%d.html"

def problematic(
Expand Down Expand Up @@ -92,7 +91,7 @@ def __init__(
class Struct:
document = self.document
reporter = self.document.reporter
language = self.document.settings.language_code
language = renderer.language_module_rst
title_styles: List[str] = []
section_level = max(renderer._level_to_elem)
section_bubble_up_kludge = False
Expand Down Expand Up @@ -278,7 +277,7 @@ def __init__(self, renderer: "DocutilsRenderer", lineno: int):
self._renderer = renderer
self._lineno = lineno
self.document = renderer.document
self.language = get_language(renderer.language_module)
self.language = renderer.language_module_rst
self.reporter = self.document.reporter
self.node: nodes.Element = renderer.current_node
self.match_titles: bool = True
Expand Down
4 changes: 2 additions & 2 deletions tests/test_renderers/fixtures/directive_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,6 @@ Unknown Directive:
Unknown directive type "unknown".
<system_message level="1" line="1" source="notset" type="INFO">
<paragraph>
Problem retrieving directive entry from language module 'en': 'str' object has no attribute 'directives'.
No directive entry for "unknown" in module "docutils.parsers.rst.languages.en".
Trying "unknown" as canonical directive name.
.
.

0 comments on commit ceeac0b

Please sign in to comment.