Skip to content

Commit

Permalink
Preserve code blocks.
Browse files Browse the repository at this point in the history
Preserve language in code blocks, instead of turning them in plain
literal-blocks.
  • Loading branch information
bfabio authored and davidfritzsche committed Jan 18, 2020
1 parent b0fe4c0 commit 24c637c
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/sphinx_rst_builder/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,22 @@ def depart_versionmodified(self, node):
self.end_state()

def visit_literal_block(self, node):
self.add_text("::")
self.new_state(self.indent)
if node.rawsource != node.astext():
# most probably a parsed-literal block -- don't highlight
return super().visit_literal_block(node)

lang = node.get('language', 'default')
linenos = node.get('linenos', False)

if lang == 'default':
self.add_text("::")
else:
self.add_text(".. code-block:: {}\n".format(lang))
if node.get('linenos', False):
self.add_text(" :linenos:")

self.new_state(self.indent)

def depart_literal_block(self, node):
self.end_state(wrap=False)

Expand Down

0 comments on commit 24c637c

Please sign in to comment.