Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45000 > unrolled thread

Anybody familiar with pygments ?

Started by"dabaichi" <valbendan@outlook.com>
First post2013-05-09 12:10 +0800
Last post2013-05-10 17:24 +0200
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Anybody familiar with pygments ? "dabaichi" <valbendan@outlook.com> - 2013-05-09 12:10 +0800
    Re: Anybody familiar with pygments ? Fábio Santos <fabiosantosart@gmail.com> - 2013-05-09 09:40 +0100
    Re: Anybody familiar with pygments ? Chris “Kwpolska” Warrick <kwpolska@gmail.com> - 2013-05-10 17:24 +0200

#45000 — Anybody familiar with pygments ?

From"dabaichi" <valbendan@outlook.com>
Date2013-05-09 12:10 +0800
SubjectAnybody familiar with pygments ?
Message-ID<kmf7cf$2e26$1@adenine.netfront.net>
Hello guys:
pygments version: 1.6
python version :3.3.1(64bits, use msi installer install)
os:windows 7 (64bits)
Hereis my python source code:

#!/usr/bin/env python
#-*- coding=utf-8 -*-

import os
import sys
from pygments import highlight as html_highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from pygments.styles import get_style_by_name

def highlight(filename):
    formatter = HtmlFormatter(linenos=True, style="vim")
    filename_split = filename.split('.')

    if len(filename_split) == 1:
        return None

    lexer = get_lexer_by_name(filename_split[len(filename_split) - 1])

    try:
        with open(filename, 'r') as fp:
            content = fp.read()
    except Exception:
        return None

    html_content = html_highlight(content, lexer, formatter)
    return html_content

def main():
    filename = r'test.c'
    print(highlight(filename))

if __name__ == "__main__":
    main()


Here is test.c source code:
#include <stdio.h>

int main(int argc, char * argv[])
{
    printf("This is a test!\n");
    return 0;
}

And hereis the output file:
<table class="highlighttable"><tr><td class="linenos"><div 
class="linenodiv"><pre>1
2
3
4
5
6
7</pre></div></td><td class="code"><div class="highlight"><pre><span 
class="cp">#include &lt;stdio.h&gt;</span>

<span class="kt">int</span> <span class="nf">main</span><span 
class="p">(</span><span class="kt">int</span> <span 
class="n">argc</span><span class="p">,</span> <span class="kt">char</span> 
<span class="o">*</span> <span class="n">argv</span><span 
class="p">[])</span>
<span class="p">{</span>
    <span class="n">printf</span><span class="p">(</span><span 
class="s">&quot;This is a test!</span><span class="se">\n</span><span 
class="s">&quot;</span><span class="p">);</span>
    <span class="k">return</span> <span class="mi">0</span><span 
class="p">;</span>
<span class="p">}</span>
</pre></div>
</td></tr></table>

It looks like:
1 #include <stdio.h>
2
3 int main(int argc, char * argv[])
4 {
5     printf("This is a test!\n");
6     return 0;
7 }
8
with no color in firefox (20.0.1)

I want to know why output html file with no color ?
Is my code error ?

Best regard!
--peipei 


--- news://freenews.netfront.net/ - complaints: news@netfront.net ---

[toc] | [next] | [standalone]


#45020

FromFábio Santos <fabiosantosart@gmail.com>
Date2013-05-09 09:40 +0100
Message-ID<mailman.1489.1368088819.3114.python-list@python.org>
In reply to#45000

[Multipart message — attachments visible in raw view] — view raw

On 9 May 2013 05:19, "dabaichi" <valbendan@outlook.com> wrote:
>
> And hereis the output file:

That's not the output file. That is just an HTML fragment to put on your
page. A full HTML file will need more things, which is the reason why you
don't see color output.

> I want to know why output html file with no color ?

Because there is no CSS. The output  has a lot of <span> tags with classes.
You are supposed to use a CSS file along with it.

So, first put that output into a complete HTML document (with a head, a
body...) And in that document add or link to your CSS file with the color
information. I never used pygments but there may be some readily available.

[toc] | [prev] | [next] | [standalone]


#45107

FromChris “Kwpolska” Warrick <kwpolska@gmail.com>
Date2013-05-10 17:24 +0200
Message-ID<mailman.1534.1368199473.3114.python-list@python.org>
In reply to#45000
On Thu, May 9, 2013 at 10:40 AM, Fábio Santos <fabiosantosart@gmail.com> wrote:
>
> On 9 May 2013 05:19, "dabaichi" <valbendan@outlook.com> wrote:
>>
>> And hereis the output file:
>
> That's not the output file. That is just an HTML fragment to put on your
> page. A full HTML file will need more things, which is the reason why you
> don't see color output.
>
>> I want to know why output html file with no color ?
>
> Because there is no CSS. The output  has a lot of <span> tags with classes.
> You are supposed to use a CSS file along with it.
>
> So, first put that output into a complete HTML document (with a head, a
> body...) And in that document add or link to your CSS file with the color
> information. I never used pygments but there may be some readily available.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

First off, the Python code was unnecessary in this case, because it
would be cheaper to use the pygmentize app included.

Second off, CSS is available at
https://github.com/richleland/pygments-css or by running `pygmentize
-f html -O full FILE`.

--
Kwpolska <http://kwpolska.tk> | GPG KEY: 5EAAEA16
stop html mail                | always bottom-post
http://asciiribbon.org        | http://caliburn.nl/topposting.html

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web