File manager - Edit - /usr/share/doc/python3-pygments/html/docs/quickstart.html
Back
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Introduction and Quickstart — Pygments 2.2.0 documentation</title> <link rel="stylesheet" href="../_static/pygments14.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <link rel="shortcut icon" href="../_static/favicon.ico"/> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="next" title="Command Line Interface" href="cmdline.html" /> <link rel="prev" title="Download and installation" href="../download.html" /> <link href='http://fonts.googleapis.com/css?family=PT+Sans:300,400,700' rel='stylesheet' type='text/css'> <style type="text/css"> table.right { float: right; margin-left: 20px; } table.right td { border: 1px solid #ccc; } </style> <script type="text/javascript"> // intelligent scrolling of the sidebar content $(window).scroll(function() { var sb = $('.sphinxsidebarwrapper'); var win = $(window); var sbh = sb.height(); var offset = $('.sphinxsidebar').position()['top']; var wintop = win.scrollTop(); var winbot = wintop + win.innerHeight(); var curtop = sb.position()['top']; var curbot = curtop + sbh; // does sidebar fit in window? if (sbh < win.innerHeight()) { // yes: easy case -- always keep at the top sb.css('top', $u.min([$u.max([0, wintop - offset - 10]), $(document).height() - sbh - 200])); } else { // no: only scroll if top/bottom edge of sidebar is at // top/bottom edge of window if (curtop > wintop && curbot > winbot) { sb.css('top', $u.max([wintop - offset - 10, 0])); } else if (curtop < wintop && curbot < winbot) { sb.css('top', $u.min([winbot - sbh - offset - 20, $(document).height() - sbh - 200])); } } }); </script> </head><body> <div class="outerwrapper"> <div class="pageheader"> <ul> <li><a href="../index.html">Home</a></li> <li><a href="../languages.html">Languages</a></li> <li><a href="../faq.html">FAQ</a></li> <li><a href="../download.html">Get it</a></li> <li><a href="index.html">Docs</a></li> </ul> <div> <a href="../index.html"> <img src="../_static/logo.png" alt="Pygments logo" /> </a> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="../index.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">Introduction and Quickstart</a><ul> <li><a class="reference internal" href="#architecture">Architecture</a></li> <li><a class="reference internal" href="#example">Example</a></li> <li><a class="reference internal" href="#options">Options</a></li> <li><a class="reference internal" href="#lexer-and-formatter-lookup">Lexer and formatter lookup</a></li> <li><a class="reference internal" href="#guessing-lexers">Guessing lexers</a></li> <li><a class="reference internal" href="#command-line-usage">Command line usage</a></li> </ul> </li> </ul> <strong>« <a href="index.html">Back to docs index</a></strong> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <div class="searchformwrapper"> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="introduction-and-quickstart"> <h1>Introduction and Quickstart<a class="headerlink" href="#introduction-and-quickstart" title="Permalink to this headline">¶</a></h1> <p>Welcome to Pygments! This document explains the basic concepts and terms and gives a few examples of how to use the library.</p> <div class="section" id="architecture"> <h2>Architecture<a class="headerlink" href="#architecture" title="Permalink to this headline">¶</a></h2> <p>There are four types of components that work together highlighting a piece of code:</p> <ul class="simple"> <li>A <strong>lexer</strong> splits the source into tokens, fragments of the source that have a token type that determines what the text represents semantically (e.g., keyword, string, or comment). There is a lexer for every language or markup format that Pygments supports.</li> <li>The token stream can be piped through <strong>filters</strong>, which usually modify the token types or text fragments, e.g. uppercasing all keywords.</li> <li>A <strong>formatter</strong> then takes the token stream and writes it to an output file, in a format such as HTML, LaTeX or RTF.</li> <li>While writing the output, a <strong>style</strong> determines how to highlight all the different token types. It maps them to attributes like “red and bold”.</li> </ul> </div> <div class="section" id="example"> <h2>Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h2> <p>Here is a small example for highlighting Python code:</p> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pygments</span> <span class="kn">import</span> <span class="n">highlight</span> <span class="kn">from</span> <span class="nn">pygments.lexers</span> <span class="kn">import</span> <span class="n">PythonLexer</span> <span class="kn">from</span> <span class="nn">pygments.formatters</span> <span class="kn">import</span> <span class="n">HtmlFormatter</span> <span class="n">code</span> <span class="o">=</span> <span class="s1">'print "Hello World"'</span> <span class="k">print</span> <span class="n">highlight</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">PythonLexer</span><span class="p">(),</span> <span class="n">HtmlFormatter</span><span class="p">())</span> </pre></div> </div> <p>which prints something like this:</p> <div class="highlight-html notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">div</span> <span class="na">class</span><span class="o">=</span><span class="s">"highlight"</span><span class="p">></span> <span class="p"><</span><span class="nt">pre</span><span class="p">><</span><span class="nt">span</span> <span class="na">class</span><span class="o">=</span><span class="s">"k"</span><span class="p">></span>print<span class="p"></</span><span class="nt">span</span><span class="p">></span> <span class="p"><</span><span class="nt">span</span> <span class="na">class</span><span class="o">=</span><span class="s">"s"</span><span class="p">></span><span class="ni">&quot;</span>Hello World<span class="ni">&quot;</span><span class="p"></</span><span class="nt">span</span><span class="p">></</span><span class="nt">pre</span><span class="p">></span> <span class="p"></</span><span class="nt">div</span><span class="p">></span> </pre></div> </div> <p>As you can see, Pygments uses CSS classes (by default, but you can change that) instead of inline styles in order to avoid outputting redundant style information over and over. A CSS stylesheet that contains all CSS classes possibly used in the output can be produced by:</p> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">print</span> <span class="n">HtmlFormatter</span><span class="p">()</span><span class="o">.</span><span class="n">get_style_defs</span><span class="p">(</span><span class="s1">'.highlight'</span><span class="p">)</span> </pre></div> </div> <p>The argument to <code class="xref py py-func docutils literal notranslate"><span class="pre">get_style_defs()</span></code> is used as an additional CSS selector: the output may look like this:</p> <div class="highlight-css notranslate"><div class="highlight"><pre><span></span><span class="p">.</span><span class="nc">highlight</span> <span class="p">.</span><span class="nc">k</span> <span class="p">{</span> <span class="k">color</span><span class="p">:</span> <span class="mh">#AA22FF</span><span class="p">;</span> <span class="k">font-weight</span><span class="p">:</span> <span class="kc">bold</span> <span class="p">}</span> <span class="p">.</span><span class="nc">highlight</span> <span class="p">.</span><span class="nc">s</span> <span class="p">{</span> <span class="k">color</span><span class="p">:</span> <span class="mh">#BB4444</span> <span class="p">}</span> <span class="o">...</span> </pre></div> </div> </div> <div class="section" id="options"> <h2>Options<a class="headerlink" href="#options" title="Permalink to this headline">¶</a></h2> <p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">highlight()</span></code> function supports a fourth argument called <em>outfile</em>, it must be a file object if given. The formatted output will then be written to this file instead of being returned as a string.</p> <p>Lexers and formatters both support options. They are given to them as keyword arguments either to the class or to the lookup method:</p> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pygments</span> <span class="kn">import</span> <span class="n">highlight</span> <span class="kn">from</span> <span class="nn">pygments.lexers</span> <span class="kn">import</span> <span class="n">get_lexer_by_name</span> <span class="kn">from</span> <span class="nn">pygments.formatters</span> <span class="kn">import</span> <span class="n">HtmlFormatter</span> <span class="n">lexer</span> <span class="o">=</span> <span class="n">get_lexer_by_name</span><span class="p">(</span><span class="s2">"python"</span><span class="p">,</span> <span class="n">stripall</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> <span class="n">formatter</span> <span class="o">=</span> <span class="n">HtmlFormatter</span><span class="p">(</span><span class="n">linenos</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">cssclass</span><span class="o">=</span><span class="s2">"source"</span><span class="p">)</span> <span class="n">result</span> <span class="o">=</span> <span class="n">highlight</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">lexer</span><span class="p">,</span> <span class="n">formatter</span><span class="p">)</span> </pre></div> </div> <p>This makes the lexer strip all leading and trailing whitespace from the input (<cite>stripall</cite> option), lets the formatter output line numbers (<cite>linenos</cite> option), and sets the wrapping <code class="docutils literal notranslate"><span class="pre"><div></span></code>’s class to <code class="docutils literal notranslate"><span class="pre">source</span></code> (instead of <code class="docutils literal notranslate"><span class="pre">highlight</span></code>).</p> <p>Important options include:</p> <dl class="docutils"> <dt><cite>encoding</cite> <span class="classifier-delimiter">:</span> <span class="classifier">for lexers and formatters</span></dt> <dd>Since Pygments uses Unicode strings internally, this determines which encoding will be used to convert to or from byte strings.</dd> <dt><cite>style</cite> <span class="classifier-delimiter">:</span> <span class="classifier">for formatters</span></dt> <dd>The name of the style to use when writing the output.</dd> </dl> <p>For an overview of builtin lexers and formatters and their options, visit the <a class="reference internal" href="lexers.html"><span class="doc">lexer</span></a> and <a class="reference internal" href="formatters.html"><span class="doc">formatters</span></a> lists.</p> <p>For a documentation on filters, see <a class="reference internal" href="filters.html"><span class="doc">this page</span></a>.</p> </div> <div class="section" id="lexer-and-formatter-lookup"> <h2>Lexer and formatter lookup<a class="headerlink" href="#lexer-and-formatter-lookup" title="Permalink to this headline">¶</a></h2> <p>If you want to lookup a built-in lexer by its alias or a filename, you can use one of the following methods:</p> <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pygments.lexers</span> <span class="kn">import</span> <span class="p">(</span><span class="n">get_lexer_by_name</span><span class="p">,</span> <span class="gp">... </span> <span class="n">get_lexer_for_filename</span><span class="p">,</span> <span class="n">get_lexer_for_mimetype</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">get_lexer_by_name</span><span class="p">(</span><span class="s1">'python'</span><span class="p">)</span> <span class="go"><pygments.lexers.PythonLexer></span> <span class="gp">>>> </span><span class="n">get_lexer_for_filename</span><span class="p">(</span><span class="s1">'spam.rb'</span><span class="p">)</span> <span class="go"><pygments.lexers.RubyLexer></span> <span class="gp">>>> </span><span class="n">get_lexer_for_mimetype</span><span class="p">(</span><span class="s1">'text/x-perl'</span><span class="p">)</span> <span class="go"><pygments.lexers.PerlLexer></span> </pre></div> </div> <p>All these functions accept keyword arguments; they will be passed to the lexer as options.</p> <p>A similar API is available for formatters: use <a class="reference internal" href="api.html#pygments.formatters.get_formatter_by_name" title="pygments.formatters.get_formatter_by_name"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_formatter_by_name()</span></code></a> and <a class="reference internal" href="api.html#pygments.formatters.get_formatter_for_filename" title="pygments.formatters.get_formatter_for_filename"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_formatter_for_filename()</span></code></a> from the <a class="reference internal" href="api.html#module-pygments.formatters" title="pygments.formatters"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pygments.formatters</span></code></a> module for this purpose.</p> </div> <div class="section" id="guessing-lexers"> <h2>Guessing lexers<a class="headerlink" href="#guessing-lexers" title="Permalink to this headline">¶</a></h2> <p>If you don’t know the content of the file, or you want to highlight a file whose extension is ambiguous, such as <code class="docutils literal notranslate"><span class="pre">.html</span></code> (which could contain plain HTML or some template tags), use these functions:</p> <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pygments.lexers</span> <span class="kn">import</span> <span class="n">guess_lexer</span><span class="p">,</span> <span class="n">guess_lexer_for_filename</span> <span class="gp">>>> </span><span class="n">guess_lexer</span><span class="p">(</span><span class="s1">'#!/usr/bin/python</span><span class="se">\n</span><span class="s1">print "Hello World!"'</span><span class="p">)</span> <span class="go"><pygments.lexers.PythonLexer></span> <span class="gp">>>> </span><span class="n">guess_lexer_for_filename</span><span class="p">(</span><span class="s1">'test.py'</span><span class="p">,</span> <span class="s1">'print "Hello World!"'</span><span class="p">)</span> <span class="go"><pygments.lexers.PythonLexer></span> </pre></div> </div> <p><a class="reference internal" href="api.html#pygments.lexers.guess_lexer" title="pygments.lexers.guess_lexer"><code class="xref py py-func docutils literal notranslate"><span class="pre">guess_lexer()</span></code></a> passes the given content to the lexer classes’ <code class="xref py py-meth docutils literal notranslate"><span class="pre">analyse_text()</span></code> method and returns the one for which it returns the highest number.</p> <p>All lexers have two different filename pattern lists: the primary and the secondary one. The <a class="reference internal" href="api.html#pygments.lexers.get_lexer_for_filename" title="pygments.lexers.get_lexer_for_filename"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_lexer_for_filename()</span></code></a> function only uses the primary list, whose entries are supposed to be unique among all lexers. <a class="reference internal" href="api.html#pygments.lexers.guess_lexer_for_filename" title="pygments.lexers.guess_lexer_for_filename"><code class="xref py py-func docutils literal notranslate"><span class="pre">guess_lexer_for_filename()</span></code></a>, however, will first loop through all lexers and look at the primary and secondary filename patterns if the filename matches. If only one lexer matches, it is returned, else the guessing mechanism of <a class="reference internal" href="api.html#pygments.lexers.guess_lexer" title="pygments.lexers.guess_lexer"><code class="xref py py-func docutils literal notranslate"><span class="pre">guess_lexer()</span></code></a> is used with the matching lexers.</p> <p>As usual, keyword arguments to these functions are given to the created lexer as options.</p> </div> <div class="section" id="command-line-usage"> <h2>Command line usage<a class="headerlink" href="#command-line-usage" title="Permalink to this headline">¶</a></h2> <p>You can use Pygments from the command line, using the <strong class="program">pygmentize</strong> script:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ pygmentize test.py </pre></div> </div> <p>will highlight the Python file test.py using ANSI escape sequences (a.k.a. terminal colors) and print the result to standard output.</p> <p>To output HTML, use the <code class="docutils literal notranslate"><span class="pre">-f</span></code> option:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ pygmentize -f html -o test.html test.py </pre></div> </div> <p>to write an HTML-highlighted version of test.py to the file test.html. Note that it will only be a snippet of HTML, if you want a full HTML document, use the “full” option:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ pygmentize -f html -O full -o test.html test.py </pre></div> </div> <p>This will produce a full HTML document with included stylesheet.</p> <p>A style can be selected with <code class="docutils literal notranslate"><span class="pre">-O</span> <span class="pre">style=<name></span></code>.</p> <p>If you need a stylesheet for an existing HTML file using Pygments CSS classes, it can be created with:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ pygmentize -S default -f html > style.css </pre></div> </div> <p>where <code class="docutils literal notranslate"><span class="pre">default</span></code> is the style name.</p> <p>More options and tricks and be found in the <a class="reference internal" href="cmdline.html"><span class="doc">command line reference</span></a>.</p> </div> </div> </div> </div> </div> <div class="clearer"></div> </div> <div class="footer" role="contentinfo"> © Copyright 2006-2017, Georg Brandl and Pygments contributors. Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.7.6. <br/> Pygments logo created by <a href="http://joelunger.com">Joel Unger</a>. Backgrounds from <a href="http://subtlepatterns.com">subtlepatterns.com</a>. </div> </div> </body> </html>
| ver. 1.4 |
Github
|
.
| PHP 8.2.30 | Generation time: 0 |
proxy
|
phpinfo
|
Settings