1 Star 1 Fork 3

xuejingkai/scientific-visualization-book

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
rst2latex.py 1.53 KB
一键复制 编辑 原始数据 按行查看 历史
#!/usr/bin/env python3
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
"""
A customized front end to the Docutils Publisher, producing LaTeX with valid codeblocks using the listings package.
"""
try:
import locale
locale.setlocale(locale.LC_ALL, '')
except:
pass
from docutils.core import publish_cmdline
from docutils.parsers.rst import directives, Directive
from docutils import nodes
class CodeBlock(Directive):
required_arguments = 1
optional_arguments = 0
final_argument_whitespace = False
option_spec = {}
has_content = True
rstlang_to_listingslang = {
'text': '{}'
}
def run(self):
language = self.rstlang_to_listingslang.get(self.arguments[0], self.arguments[0])
content = '\n'.join(self.content)
latex = '\\begin{{lstlisting}}[language={}]\n{}\n\\end{{lstlisting}}'.format(language, content)
return [nodes.raw('', latex, format='latex')]
description = ('Generates LaTeX documents from standalone reStructuredText '
'sources. '
'Reads from <source> (default is stdin) and writes to '
'<destination> (default is stdout). See '
'<http://docutils.sourceforge.net/docs/user/latex.html> for '
'the full reference.')
for directive_name in ('code', 'code-block'):
directives.register_directive(directive_name, CodeBlock)
publish_cmdline(writer_name='latex', description=description)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/xuejingkai/scientific-visualization-book.git
git@gitee.com:xuejingkai/scientific-visualization-book.git
xuejingkai
scientific-visualization-book
scientific-visualization-book
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385