From db2d4326a850130f850ae40dafa69eb4a52f5e5b Mon Sep 17 00:00:00 2001 From: milde Date: Wed, 16 Dec 2020 10:41:03 +0000 Subject: [PATCH] Don't drop "code" directive content if Warnings are silenced. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Output the content without syntax highlight to prevent data loss. It is still recommended to solve the issue instead of just silencing the warning. (Languages that are unknown to the Pygments Lexer should be specified as :class: option value instead of directive argument.) Thanks to Jérôme Carretero for the report and suggestion. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@8596 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/docutils/parsers/rst/directives/body.py | 6 +++++- docutils/docutils/utils/code_analyzer.py | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/docutils/docutils/parsers/rst/directives/body.py b/docutils/docutils/parsers/rst/directives/body.py index 9f7dcd243..6d218d07b 100644 --- a/docutils/docutils/parsers/rst/directives/body.py +++ b/docutils/docutils/parsers/rst/directives/body.py @@ -158,7 +158,11 @@ class CodeBlock(Directive): tokens = Lexer(u'\n'.join(self.content), language, self.state.document.settings.syntax_highlight) except LexerError as error: - raise self.warning(error) + if self.state.document.settings.report_level > 2: + # don't report warnings -> insert without syntax highligt + tokens = Lexer(u'\n'.join(self.content), language, 'none') + else: + raise self.warning(error) if 'number-lines' in self.options: # optional argument `startline`, defaults to 1 diff --git a/docutils/docutils/utils/code_analyzer.py b/docutils/docutils/utils/code_analyzer.py index db818b734..65220c8fa 100644 --- a/docutils/docutils/utils/code_analyzer.py +++ b/docutils/docutils/utils/code_analyzer.py @@ -37,7 +37,7 @@ class Lexer(object): code -- string of source code to parse, language -- formal language the code is written in, - tokennames -- either 'long', 'short', or '' (see below). + tokennames -- either 'long', 'short', or 'none' (see below). Merge subsequent tokens of the same token-type. @@ -47,7 +47,7 @@ class Lexer(object): 'long': downcased full token type name, 'short': short name defined by pygments.token.STANDARD_TYPES (= class argument used in pygments html output), - 'none': skip lexical analysis. + 'none': skip lexical analysis. """ def __init__(self, code, language, tokennames='short'): -- 2.11.4.GIT