Skip to content

Commit

Permalink
Add default language retrieval for code fences (#35)
Browse files Browse the repository at this point in the history
fixes #23
  • Loading branch information
chrisjsewell committed Feb 14, 2020
1 parent 30ebd98 commit 935222b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
1 change: 0 additions & 1 deletion myst_parser/block_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ def __init__(self, lines):
front_matter = FrontMatter(lines)
self.children.append(front_matter)
start_line = front_matter.range[1]
print(start_line)
lines = lines[start_line:]
self.children.extend(tokenize(lines, start_line))

Expand Down
21 changes: 16 additions & 5 deletions myst_parser/docutils_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,24 @@ def render_math(self, token):
def render_code_fence(self, token):
if token.language.startswith("{") and token.language.endswith("}"):
return self.render_directive(token)
text = token.children[0].content
node = nodes.literal_block(text, text, language=token.language)
self.current_node.append(node)
self.render_block_code(token, default_language=True)

def render_block_code(self, token):
def render_block_code(self, token, default_language=False):
# indented code blocks will always have no language,
# but for code fences, if not set, a default_language will be retrieved
text = token.children[0].content
node = nodes.literal_block(text, text, language=token.language)
language = token.language
if not language and default_language:
try:
sphinx_env = self.document.settings.env
language = sphinx_env.temp_data.get(
"highlight_language", sphinx_env.config.highlight_language
)
except AttributeError:
pass
if not language and default_language:
language = self.config.get("highlight_language", "")
node = nodes.literal_block(text, text, language=language)
self.current_node.append(node)

def render_inline_code(self, token):
Expand Down
5 changes: 5 additions & 0 deletions tests/sphinx/sourcedirs/basic/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,8 @@ this is a second paragraph
[name][key]

[key]: https://www.google.com "a title"

```
def func(a, b=1):
print(a)
```
7 changes: 7 additions & 0 deletions tests/sphinx/test_sphinx_builds/test_basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ <h1>
name
</a>
</p>
<div class="highlight-default notranslate">
<div class="highlight">
<pre><span></span><span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
</pre>
</div>
</div>
</div>
</div>
</div>
Expand Down

0 comments on commit 935222b

Please sign in to comment.