使用ReStructuredText进行相当的源代码列表 [英] Use ReStructuredText for pretty source code listing

查看:135
本文介绍了使用ReStructuredText进行相当的源代码列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如这篇文章,可以使用 SyntaxHighlighter 获得漂亮的代码清单。



使用ReStructuredText,我可以像下面这样使用raw指令。

  .. raw :: html 

< script type =text / javascriptsrc =http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js><< ; /脚本>
< script type =text / javascriptsrc =http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js>< / script>
< link type =text / css =stylesheethref =http://alexgorbatchev.com/pub/sh/current/styles/shCoreDefault.css/>
< script type =text / javascript> SyntaxHighlighter.all();< / script>

我可以使用`SyntaxHighlighter< http://alexgorbatchev.com/SyntaxHighlighter/>`_来突出显示源代码。

.. raw :: html

< pre class =brush:js;>
函数helloSyntaxHighlighter()
{
returnhi!;
}
< / pre>

然而,我需要使用代码指令。

  .. code :: 

函数helloSyntaxHighlighter()
{
returnhi!;
}

如何将代码指令转换为以下HTML代码?

 < pre class =brush:js;> 
函数helloSyntaxHighlighter()
{
returnhi!;
}
< / pre>


解决方案

我有一种方法:




  • 安装 rst2pdf pygments rst2html 的副本,称之为 myrst2html。


  • 在副本中,在导入之后添加:

     从docutils.parsers.rst导入指令
    进口rst2pdf.pygments_code_block_directive
    directives.register_directive( '码块',
    rst2pdf。 pygments_code_block_directive.code_block_directive)




这就是它,你现在有代码块指令。


As is asked and answered in this post, one can use SyntaxHighlighter for pretty code listing.

With ReStructuredText, I can use raw directive as follows.

.. raw:: html

    <script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js"></script>
    <script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js"></script>
    <link type="text/css" rel="stylesheet" href="http://alexgorbatchev.com/pub/sh/current/styles/shCoreDefault.css"/>
    <script type="text/javascript">SyntaxHighlighter.all();</script>

I could use `SyntaxHighlighter <http://alexgorbatchev.com/SyntaxHighlighter/>`_ for highlighting source code. 

.. raw:: html

    <pre class="brush: js;">
    function helloSyntaxHighlighter()
    {
        return "hi!";
    }
    </pre>

However, I need to have code directive that I can use.

.. code:: 

    function helloSyntaxHighlighter()
    {
        return "hi!";
    }

How can I translate code directive into the following HTML code?

<pre class="brush: js;">
function helloSyntaxHighlighter()
{
    return "hi!";
}
</pre>

解决方案

There is a way I have used:

  • Install rst2pdf and pygments.

  • Then make a copy of rst2html, call it myrst2html or whatever you want.

  • In the copy, add this after the imports:

    from docutils.parsers.rst import directives 
    import rst2pdf.pygments_code_block_directive 
    directives.register_directive('code-block',
        rst2pdf.pygments_code_block_directive.code_block_directive) 
    

And that's it, you now have the code-block directives.

这篇关于使用ReStructuredText进行相当的源代码列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆