4

この投稿で尋ねられ、回答されているように、きれいなコード リストにSyntaxHighlighterを使用できます。

ReStructuredText では、次のように raw ディレクティブを使用できます。

.. 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>

ただし、使用できるコード ディレクティブが必要です。

.. code:: 

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

コード ディレクティブを次の HTML コードに変換するにはどうすればよいですか?

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

2 に答える 2

3

私が使用した方法があります:

  • をインストールrst2pdfpygmentsます。

  • 次に、のコピーを作成しrst2html、それを呼び出すか、myrst2html必要なものを何でも呼び出します。

  • コピーでは、インポートの後にこれを追加します。

    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) 
    

以上で、コード ブロック ディレクティブが作成されました。

于 2011-02-21T01:58:34.630 に答える
0

次のように動作させることができます:

  1. を生成する<pre>..</pre>には、ParsedLiteral を変更する必要があったため、次のように ParsedLiteral クラスを Code にコピーしました。ライン5を変更することself.options['class'] = ['brush: js;'] # <--が主なアイデアです。

    クラス コード (ディレクティブ):

    option_spec = {'class': directives.class_option}
    has_content = True
    
    def run(self):
        self.options['class'] = ['brush: js;'] # <--
        set_classes(self.options)
        self.assert_has_content()
        text = '\n'.join(self.content)
        text_nodes, messages = self.state.inline_text(text, self.lineno)
        node = nodes.literal_block(text, '', *text_nodes, **self.options)
        node.line = self.content_offset + 1
        return [node] + messages
    
  2. 次のようにinit .pyに 1 行追加します。

    _directive_registry = { 'コード' : ('本体', 'コード'),

これで、次のコードを使用できます

.. code::

   print "Hello world!"  # *tricky* code

この HTML コードを取得するには

<pre class="brush: js; literal-block">
print &quot;Hello world!&quot;  # <em>tricky</em> code
</pre>

考えられる簡単な解決策?

「bruch: js;」のパラメーターを渡す方法が見つかれば、ParsedLiteral を使用できます。しかし、私がコードを試したとき

.. parsed-literal::
   :class: "brunch: js;"

   print "Hello world!"  # *tricky* code

タグは になり<pre class="brunch ja">ます。

于 2011-01-18T00:19:08.183 に答える