際際滷

際際滷Share a Scribd company logo
Pygments
Grant Chen
13定8埖13?晩佛豚?屈
?website: http://pygments.org
?Install: $ pip install pygments
13定8埖13?晩佛豚?屈
Command line
a?伏]嗤css議html。
$ pygmentize -f html -l python -o test.html test.py
a?伏嗤css議html
$ pygmentize -O full,style=emacs -o test.html test.py
a?伏嗤?佩亀html
$ pygmentize -f html -O full,style=colorful,linenos=1
-l python test.py
a?伏css
$ pygmentize -f html -S colorful -a .highlight
13定8埖13?晩佛豚?屈
ARCHITECTURE
? lexer
? ?lters
? 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?晩佛豚?屈

More Related Content

Pygments

  • 2. ?website: http://pygments.org ?Install: $ pip install pygments 13定8埖13?晩佛豚?屈
  • 3. Command line a?伏]嗤css議html。 $ pygmentize -f html -l python -o test.html test.py a?伏嗤css議html $ pygmentize -O full,style=emacs -o test.html test.py a?伏嗤?佩亀html $ pygmentize -f html -O full,style=colorful,linenos=1 -l python test.py a?伏css $ pygmentize -f html -S colorful -a .highlight 13定8埖13?晩佛豚?屈
  • 4. ARCHITECTURE ? lexer ? ?lters ? 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?晩佛豚?屈