def write_content(content_type, content, formatter, fh=sys.stdout):
#
nlines = len(content)
content = dedent(''.join(content))
# ' ' to keep pygments from removing empty lines
# split, merge by \n can introduce one additional line
content = [' \n' if x == '' else x + '\n' for x in content.split('\n')][:nlines]
#
if content_type == 'COMMENT':
fh.write(highlight(''.join(content), SoS_Lexer(), formatter))
elif content_type in ('REPORT', 'report'):
fh.write(highlight(''.join(content), TextLexer(), formatter))
elif content_type == 'SECTION':
fh.write(highlight(''.join(content), SoS_Lexer(), formatter))
elif content_type == 'DIRECTIVE':
fh.write(highlight(''.join(content), SoS_Lexer(), formatter))
elif content_type == 'STATEMENT':
fh.write(highlight(''.join(content), SoS_Lexer(), formatter))
elif content_type == 'ERROR':
fh.write(highlight(''.join(content), SoS_Lexer(), formatter))
else:
if content_type == 'run':
content_type = 'bash'
elif content_type == 'node':
content_type = 'JavaScript'
elif content_type == 'report':
content_type = 'text'
try:
lexer = get_lexer_by_name(content_type)
except Exception:
try:
lexer = guess_lexer(''.join(content))
except Exception:
lexer = TextLexer()
fh.write(highlight((''.join(content)), lexer, formatter))
评论列表
文章目录