Pygments

  • 683 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
683
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Pygments Grant Chen 13年8月13⽇日星期⼆二
  • 2. •website: http://pygments.org •Install: $ pip install pygments 13年8月13⽇日星期⼆二
  • 3. 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⽇日星期⼆二
  • 4. ARCHITECTURE • lexer • filters • fomatter • style 13年8月13⽇日星期⼆二
  • 5. 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⽇日星期⼆二
  • 6. CSS Result print HtmlFormatter().get_style_defs('.highlight') .highlight .k { color: #AA22FF; font-weight: bold } .highlight .s { color: #BB4444 } ... 13年8月13⽇日星期⼆二
  • 7. Lexer from pygments.lexers import PythonLexer lexer=PythonLexer(stripall=True, encode='chardet',ensurenl=True) 13年8月13⽇日星期⼆二
  • 8. Lexer option • stripnl • stripall • ensurenl (for pygments 1.3) • tabsize • encoding (utf-8, chardet...) 13年8月13⽇日星期⼆二
  • 9. Formatter from pygments.formatters import HtmlFormatter formatter = HtmlFormatter(linenos=1, full=True, style='colorful', encoding='utf-8') 13年8月13⽇日星期⼆二
  • 10. 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⽇日星期⼆二
  • 11. 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⽇日星期⼆二
  • 12. 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⽇日星期⼆二
  • 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⽇日星期⼆二
  • 14. Q & A 13年8月13⽇日星期⼆二