如何获取pygments格式化程序的CSS?

时间:2015-06-25 23:25:47

标签: python html css pygments

我正在以http://pygments.org/docs/quickstart/

的基本方式使用pygments

我的格式化程序是这样创建的:

                formatter = HtmlFormatter(cssclass="codehilite", linenos='table',
                    linenostart = lineno - len(excerpt) + 1,
                    hl_lines = important_lines,
                    style='colorful')           

它工作正常,但我没有从输出中获得任何CSS,只是类。我在哪里可以获得CSS?我想把它放在我的HTML文件的<head>中,所以我不需要单独的.css文件。我从pygments.highlight()获得的只是:

<table class="codehilitetable"><tr><td class="linenos"><div class="linenodiv"><pre>44
45
46
47
48
49
50
51
52
53
54
55
56
57</pre></div></td><td class="code"><div class="codehilite"><pre><span class="cm">/* *********** Interrupt Service Routines *********************************** */</span>

<span class="cm">/**</span>
<span class="cm"> * UART1_RX interrupt service routine.</span>
<span class="cm"> * Clears the UARTRX interrupt flag and disables the interrupt.</span>
<span class="cm"> */</span>
<span class="n">UART1_RXISR_FUNCTION_HEADER</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">UART1_DISABLE_IRQ_RX</span><span class="p">;</span>
    <span class="n">UART1_CLEAR_IRQ_RX_FLAG</span><span class="p">;</span>
<span class="p">}</span>

<span class="o">/**</span> 
 <span class="o">*</span> <span class="n">UART1_TX</span> <span class="n">interrupt</span> <span class="n">service</span> <span class="n">routine</span><span class="p">.</span>
</pre></div>
</td></tr></table>

3 个答案:

答案 0 :(得分:2)

除非您在格式化程序中使用the_css = formatter.get_style_defs() 选项,否则可以像这样获取CSS:

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

String items;
final int READ_EXTERNAL_STORAGE_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, READ_EXTERNAL_STORAGE_REQUEST);
        return;
    }

    ArrayList<File> mySongs = findSongs(Environment.getExternalStorageDirectory());

    for(int i = 0; i < mySongs.size(); i++) {
        toast(mySongs.get(i).getName().toString());
    }
}

public ArrayList<File> findSongs(File root) {
    ArrayList<File> al = new ArrayList<File>();
    File[] files = root.listFiles();
    for(File singleFile : files) {
        if(singleFile.isDirectory() && !singleFile.isHidden()) {
            al.addAll(findSongs(singleFile));
        }

        else {
            if(singleFile.getName().endsWith(".mp3")) {
                  al.add(singleFile);
            }
        }
    }
    return al;
}

public void toast(String text) {
    Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch(requestCode) {
        case READ_EXTERNAL_STORAGE_REQUEST: if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            return;
        }
    }
}
}

然后你可以将它插入你的html标题中。

答案 1 :(得分:1)

您可以从GitHub上的pygments-css存储库获取预生成的CSS。 (提示由https://github.com/trentm/python-markdown2/wiki/fenced-code-blocks建议。)

答案 2 :(得分:0)

Pygments使用样式类来生成CSS。

http://pygments.org/docs/styles/