Pygments
Upcoming SlideShare
Loading in...5
×
 

Pygments

on

  • 736 views

 

Statistics

Views

Total Views
736
Views on SlideShare
736
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Pygments Pygments Presentation Transcript

    • Pygments Grant Chen 13年8月13⽇日星期⼆二
    • •website: http://pygments.org •Install: $ pip install pygments 13年8月13⽇日星期⼆二
    • Command line 產⽣生沒有css的html。 $ pygmentize -f html -l python -o test.html test.py 產⽣生有css的html $ pygmentize -O full,style=emacs -o test.html test.py 產⽣生有⾏行數的html $ pygmentize -f html -O full,style=colorful,linenos=1 -l python test.py 產⽣生css $ pygmentize -f html -S colorful -a .highlight 13年8月13⽇日星期⼆二
    • ARCHITECTURE • lexer • filters • fomatter • style 13年8月13⽇日星期⼆二
    • Example from pygments import highlight from pygments.lexers import PythonLexer from pygments.formatters import HtmlFormatter code = 'print "Hello World"' print highlight(code, PythonLexer(), HtmlFormatter(), outfile=None) Result <div class="highlight"> <pre><span class="k">print</span> <span class="s">&quot;Hello World&quot;</span></pre> </div> 13年8月13⽇日星期⼆二
    • CSS Result print HtmlFormatter().get_style_defs('.highlight') .highlight .k { color: #AA22FF; font-weight: bold } .highlight .s { color: #BB4444 } ... 13年8月13⽇日星期⼆二
    • Lexer from pygments.lexers import PythonLexer lexer=PythonLexer(stripall=True, encode='chardet',ensurenl=True) 13年8月13⽇日星期⼆二
    • Lexer option • stripnl • stripall • ensurenl (for pygments 1.3) • tabsize • encoding (utf-8, chardet...) 13年8月13⽇日星期⼆二
    • Formatter from pygments.formatters import HtmlFormatter formatter = HtmlFormatter(linenos=1, full=True, style='colorful', encoding='utf-8') 13年8月13⽇日星期⼆二
    • Filters >>> from pygments.lexers import PythonLexer >>> l = PythonLexer() >>> # add a filter given by a string and options >>> l.add_filter('codetagify', case='lower') >>> l.filters [<pygments.filters.CodeTagFilter object at 0xb785decc>] >>> from pygments.filters import KeywordCaseFilter >>> # or give an instance >>> l.add_filter(KeywordCaseFilter(case='lower')) 13年8月13⽇日星期⼆二
    • Style >>> from pygments.styles import get_style_by_name >>> get_style_by_name('colorful') <class 'pygments.styles.colorful.ColorfulStyle'> >>> from pygments.styles import get_style_by_name >>> HtmlFormatter(style='colorful').style <class 'pygments.styles.colorful.ColorfulStyle'> >>> from yourapp.yourmodule import YourStyle >>> HtmlFormatter(style=YourStyle).style <class 'yourapp.yourmodule.YourStyle'> 13年8月13⽇日星期⼆二
    • Create Own Styles from pygments.style import Style from pygments.token import Keyword, Name, Comment, String, Error, Number, Operator, Generic class YourStyle(Style): default_style = "" styles = { Comment: 'italic #888', Keyword: 'bold #005', Name: '#f00', Name.Function: '#0f0', Name.Class: 'bold #0f0', String: 'bg:#eee #111' } 13年8月13⽇日星期⼆二
    • Getting a list of available styles >>> from pygments.styles import get_all_styles >>> styles = list(get_all_styles()) >>> print styles print styles ['monokai', 'manni', 'rrt', 'perldoc', 'borland', 'colorful', 'default', 'murphy', 'vs', 'trac', 'tango', 'fruity', 'autumn', 'bw', 'emacs', 'vim', 'pastie', 'friendly', 'native'] 13年8月13⽇日星期⼆二
    • Q & A 13年8月13⽇日星期⼆二