diff options
author | milde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2012-05-09 13:42:58 +0000 |
---|---|---|
committer | milde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2012-05-09 13:42:58 +0000 |
commit | 5aa46e8d12bd92b7d541ddd22fa81a8164deb46f (patch) | |
tree | 3f6b49db491a209d70ca0107387ff8da0bacd98b /sandbox/code-block-directive | |
parent | a37d1d928b4a49c118bb17c0d49b09f1fc1e685b (diff) | |
download | docutils-5aa46e8d12bd92b7d541ddd22fa81a8164deb46f.tar.gz |
Clean up code-block-directive sandbox project.
git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@7430 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'sandbox/code-block-directive')
25 files changed, 133 insertions, 2259 deletions
diff --git a/sandbox/code-block-directive/README.txt b/sandbox/code-block-directive/README.txt index 689a387c6..799940171 100644 --- a/sandbox/code-block-directive/README.txt +++ b/sandbox/code-block-directive/README.txt @@ -7,40 +7,18 @@ Proposal for a code-block directive in docutils :Author: Günter Milde :Contact: milde@users.berlios.de :Date: $Date$ -:Copyright: © 2007, 2009 G. Milde, - Released without warranties or conditions of any kind - under the terms of the Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0 -This sandbox project contains experimental code and documentation related to -the proposal for syntax highlight of source code in docutils using a -"code-block" directive. +This sandbox project contained experimental code and documentation related to +the proposal for syntax highlight of source code in Docutils. -See `<docs/syntax-highlight.html>`_ for a full description. +Since version 0.9, Docutils supports this via the `code` directive and role +and the `code` option of the `include` directive. Most of this project is +moved to the attic. -`<rst2html-highlight>`_ - front end for reStructuredText -> HTML conversion supporting the - "code-block" directive. +The documentation in `<docs/syntax-highlight.html>`_ is kept as it +contains ideas for further improvement. -`<rst2latex-highlight>`_ - front end for reStructuredText -> LaTeX conversion supporting the - "code-block" directive. - -`<data>`_ - Style sheets. - -`<docs>`_ - Documentation, concepts, discussion, examples... - -`<pygments_code_block_directive.py>`_ - Working example: defines and registers a - code-block directive using the Pygments_ syntax highlighter. - -`<tools/test_pygments_code_block_directive.py>`_ - Script for interactive testing. - -`<tools/pygments-enhanced-front-ends/>`_ - Legacy front ends, +Sample stylesheets are now available in the `<../stylesheets>`_ repository. .. References diff --git a/sandbox/code-block-directive/data/pygments-default.css b/sandbox/code-block-directive/data/pygments-default.css deleted file mode 100644 index b4dbdae19..000000000 --- a/sandbox/code-block-directive/data/pygments-default.css +++ /dev/null @@ -1,99 +0,0 @@ -/* Stylesheet for pygments enhanced reStructured Text */ -/* ================================================== */ - -/* :Author: Guenter Milde */ -/* :Copyright: 2007 G. Milde */ -/* This stylesheet is released under the GPL v. 2 or later */ - -/* This stylesheet provides syntax highlight for documents generated with a */ -/* pygments_ enhanced reStructured Text -> html converter. */ - -/* Import the default docutils style sheet */ -/* --------------------------------------- */ -/* :: */ - -@import url("/stylesheets/html4css1.css"); - -/* Indent the code block */ -/* --------------------- */ - -/* Content copied from the `html4css1.css` rule for literal blocks. */ -/* Selector adapted to the output of Pygments_. :: */ - -pre.code { - margin-left: 2em ; - margin-right: 2em ; - background-color: #eeeeee - } - -pre.code .ln { /* line numbers */ -/* color: grey; */ - font-size: small; -} - -/* Colour code blocks */ -/* ------------------ */ - -/* Pygments_ has an option to generate stylesheets for html and latex. */ -/* The following code is generated with the command */ -/* `pygmentize -S default -f html > pygments-default.css`:: */ - -.c { color: #008800; font-style: italic } /* Comment */ -.err { border: 1px solid #FF0000 } /* Error */ -.k { color: #AA22FF; font-weight: bold } /* Keyword */ -.o { color: #666666 } /* Operator */ -.cm { color: #008800; font-style: italic } /* Comment.Multiline */ -.cp { color: #008800 } /* Comment.Preproc */ -.c1 { color: #008800; font-style: italic } /* Comment.Single */ -.gd { color: #A00000 } /* Generic.Deleted */ -.ge { font-style: italic } /* Generic.Emph */ -.gr { color: #FF0000 } /* Generic.Error */ -.gh { color: #000080; font-weight: bold } /* Generic.Heading */ -.gi { color: #00A000 } /* Generic.Inserted */ -.go { color: #808080 } /* Generic.Output */ -.gp { color: #000080; font-weight: bold } /* Generic.Prompt */ -.gs { font-weight: bold } /* Generic.Strong */ -.gu { color: #800080; font-weight: bold } /* Generic.Subheading */ -.gt { color: #0040D0 } /* Generic.Traceback */ -.kc { color: #AA22FF; font-weight: bold } /* Keyword.Constant */ -.kd { color: #AA22FF; font-weight: bold } /* Keyword.Declaration */ -.kp { color: #AA22FF } /* Keyword.Pseudo */ -.kr { color: #AA22FF; font-weight: bold } /* Keyword.Reserved */ -.kt { color: #AA22FF; font-weight: bold } /* Keyword.Type */ -.m { color: #666666 } /* Literal.Number */ -.s { color: #BB4444 } /* Literal.String */ -.na { color: #BB4444 } /* Name.Attribute */ -.nb { color: #AA22FF } /* Name.Builtin */ -.nc { color: #0000FF } /* Name.Class */ -.no { color: #880000 } /* Name.Constant */ -.nd { color: #AA22FF } /* Name.Decorator */ -.ni { color: #999999; font-weight: bold } /* Name.Entity */ -.ne { color: #D2413A; font-weight: bold } /* Name.Exception */ -.nf { color: #00A000 } /* Name.Function */ -.nl { color: #A0A000 } /* Name.Label */ -.nn { color: #0000FF; font-weight: bold } /* Name.Namespace */ -.nt { color: #008000; font-weight: bold } /* Name.Tag */ -.nv { color: #B8860B } /* Name.Variable */ -.ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ -.mf { color: #666666 } /* Literal.Number.Float */ -.mh { color: #666666 } /* Literal.Number.Hex */ -.mi { color: #666666 } /* Literal.Number.Integer */ -.mo { color: #666666 } /* Literal.Number.Oct */ -.sb { color: #BB4444 } /* Literal.String.Backtick */ -.sc { color: #BB4444 } /* Literal.String.Char */ -.sd { color: #BB4444; font-style: italic } /* Literal.String.Doc */ -.s2 { color: #BB4444 } /* Literal.String.Double */ -.se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */ -.sh { color: #BB4444 } /* Literal.String.Heredoc */ -.si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */ -.sx { color: #008000 } /* Literal.String.Other */ -.sr { color: #BB6688 } /* Literal.String.Regex */ -.s1 { color: #BB4444 } /* Literal.String.Single */ -.ss { color: #B8860B } /* Literal.String.Symbol */ -.bp { color: #AA22FF } /* Name.Builtin.Pseudo */ -.vc { color: #B8860B } /* Name.Variable.Class */ -.vg { color: #B8860B } /* Name.Variable.Global */ -.vi { color: #B8860B } /* Name.Variable.Instance */ -.il { color: #666666 } /* Literal.Number.Integer.Long */ - -/* .. _pygments: http://pygments.org/ */ diff --git a/sandbox/code-block-directive/data/pygments-default.sty b/sandbox/code-block-directive/data/pygments-default.sty deleted file mode 100644 index 45cc0363e..000000000 --- a/sandbox/code-block-directive/data/pygments-default.sty +++ /dev/null @@ -1,100 +0,0 @@ -% pygments-default.sty -% ******************** -% -% Stylesheet for pygments enhanced reStructured Text -% ================================================== -% -% :Author: Günter Milde -% :Contact: milde@users.berlios.de -% :Revision: $Revision: 5534 $ -% :Date: $Date: 2005-06-28$ -% :Copyright: © 2007, 2009 G. Milde, -% Released without warranties or conditions of any kind -% under the terms of the Apache License, Version 2.0 -% http://www.apache.org/licenses/LICENSE-2.0 -% -% This example style sheet provides syntax highlight for documents generated -% with the legacy front end `rst2latex-pygments`.. -% -% (For the recommended front end `rst2latex-highlight` use -% ``pygments-docutilesroles.sty``.) -% -% Separate paragraphs by vertical space -% ------------------------------------- -% -% This is not needed for syntax highlight, but usually a good idea for -% documents with lots of source code. -% :: - -\usepackage{parskip} - -% Colour code blocks -% ------------------ -% -% Pygments has an option to generate stylesheets for html and latex. -% The following code is based on the output of the command -% `pygmentize -S default -f latex`:: - -\usepackage{fancyvrb} -\usepackage{color} - -\newcommand\at{@} -\newcommand\lb{[} -\newcommand\rb{]} -\newcommand\PYba[1]{\textcolor[rgb]{0.67,0.13,1.00}{\textbf{#1}}} -\newcommand\PYaz[1]{\textcolor[rgb]{0.00,0.25,0.82}{#1}} -\newcommand\PYay[1]{\textcolor[rgb]{0.67,0.13,1.00}{#1}} -\newcommand\PYax[1]{\textcolor[rgb]{0.00,0.63,0.00}{#1}} -\newcommand\PYbc[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} -\newcommand\PYas[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} -\newcommand\PYar[1]{\textcolor[rgb]{0.72,0.53,0.04}{#1}} -\newcommand\PYaq[1]{\textcolor[rgb]{0.73,0.27,0.27}{\textit{#1}}} -\newcommand\PYap[1]{\textcolor[rgb]{0.72,0.53,0.04}{#1}} -\newcommand\PYaw[1]{\textcolor[rgb]{0.67,0.13,1.00}{\textbf{#1}}} -\newcommand\PYav[1]{\textcolor[rgb]{0.60,0.60,0.60}{\textbf{#1}}} -\newcommand\PYau[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} -\newcommand\PYat[1]{\textcolor[rgb]{0.67,0.13,1.00}{\textbf{#1}}} -\newcommand\PYak[1]{\textbf{#1}} -\newcommand\PYaj[1]{\textcolor[rgb]{0.73,0.40,0.53}{#1}} -\newcommand\PYai[1]{\textcolor[rgb]{0.72,0.53,0.04}{#1}} -\newcommand\PYah[1]{\textcolor[rgb]{0.63,0.63,0.00}{#1}} -\newcommand\PYao[1]{\textcolor[rgb]{0.53,0.00,0.00}{#1}} -\newcommand\PYan[1]{\textcolor[rgb]{0.00,0.50,0.00}{#1}} -\newcommand\PYam[1]{\textcolor[rgb]{0.73,0.40,0.13}{\textbf{#1}}} -\newcommand\PYal[1]{\textcolor[rgb]{0.67,0.13,1.00}{\textbf{#1}}} -\newcommand\PYac[1]{\textcolor[rgb]{0.73,0.27,0.27}{#1}} -\newcommand\PYab[1]{\textit{#1}} -\newcommand\PYaa[1]{\textcolor[rgb]{0.50,0.50,0.50}{#1}} -\newcommand\PYag[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} -\newcommand\PYaf[1]{\textcolor[rgb]{0.00,0.53,0.00}{\textit{#1}}} -\newcommand\PYae[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} -\newcommand\PYad[1]{\textcolor[rgb]{0.73,0.27,0.27}{#1}} -\newcommand\PYbb[1]{\textcolor[rgb]{0.73,0.27,0.27}{#1}} -\newcommand\PYaZ[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} -\newcommand\PYaY[1]{\textcolor[rgb]{0.00,0.00,0.50}{\textbf{#1}}} -\newcommand\PYaX[1]{\textcolor[rgb]{0.00,0.50,0.00}{\textbf{#1}}} -\newcommand\PYbd[1]{\textcolor[rgb]{0.73,0.40,0.53}{\textbf{#1}}} -\newcommand\PYbe[1]{\textcolor[rgb]{0.67,0.13,1.00}{\textbf{#1}}} -\newcommand\PYaS[1]{\textcolor[rgb]{0.82,0.25,0.23}{\textbf{#1}}} -\newcommand\PYaR[1]{\textcolor[rgb]{0.50,0.00,0.50}{\textbf{#1}}} -\newcommand\PYaQ[1]{\textcolor[rgb]{0.00,0.53,0.00}{\textit{#1}}} -\newcommand\PYaP[1]{\textcolor[rgb]{0.72,0.53,0.04}{#1}} -\newcommand\PYaW[1]{\textcolor[rgb]{0.73,0.27,0.27}{#1}} -\newcommand\PYaV[1]{\textcolor[rgb]{0.67,0.13,1.00}{#1}} -\newcommand\PYaU[1]{\textcolor[rgb]{0.73,0.27,0.27}{#1}} -\newcommand\PYaT[1]{\textcolor[rgb]{0.00,0.00,1.00}{\textbf{#1}}} -\newcommand\PYaK[1]{\textcolor[rgb]{0.00,0.53,0.00}{#1}} -\newcommand\PYaJ[1]{\textcolor[rgb]{0.67,0.13,1.00}{#1}} -\newcommand\PYaI[1]{\textcolor[rgb]{0.00,0.63,0.00}{#1}} -\newcommand\PYaH[1]{\textcolor[rgb]{0.73,0.27,0.27}{#1}} -\newcommand\PYaO[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} -\newcommand\PYaN[1]{\textcolor[rgb]{0.73,0.27,0.27}{#1}} -\newcommand\PYaM[1]{\textcolor[rgb]{0.00,0.00,0.50}{\textbf{#1}}} -\newcommand\PYaL[1]{\textcolor[rgb]{0.00,0.00,1.00}{#1}} -\newcommand\PYaC[1]{\textcolor[rgb]{0.63,0.00,0.00}{#1}} -\newcommand\PYaB[1]{\textcolor[rgb]{0.00,0.53,0.00}{\textit{#1}}} -\newcommand\PYaA[1]{\textcolor[rgb]{0.67,0.13,1.00}{#1}} -\newcommand\PYaG[1]{\textcolor[rgb]{0.67,0.13,1.00}{\textbf{#1}}} -\newcommand\PYaF[1]{\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{#1}} -\newcommand\PYaE[1]{\textcolor[rgb]{0.72,0.53,0.04}{#1}} -\newcommand\PYaD[1]{\textcolor[rgb]{1.00,0.00,0.00}{#1}} diff --git a/sandbox/code-block-directive/data/pygments-docutilsroles.sty b/sandbox/code-block-directive/data/pygments-docutilsroles.sty deleted file mode 100644 index 55c4f8c11..000000000 --- a/sandbox/code-block-directive/data/pygments-docutilsroles.sty +++ /dev/null @@ -1,146 +0,0 @@ -% Stylesheet for pygments enhanced reStructured Text -% ================================================== -% -% :Author: Günter Milde -% :Contact: milde@users.berlios.de -% :Revision: $Revision: 5534 $ -% :Date: $Date: 2005-06-28$ -% :Copyright: © 2007, 2009 G. Milde, -% Released without warranties or conditions of any kind -% under the terms of the Apache License, Version 2.0 -% http://www.apache.org/licenses/LICENSE-2.0 -% -% This example style sheet provides syntax highlight for documents generated -% with the `rst2latex-highlight` pygments-enhanced Docutils front end. -% -% -% Separate paragraphs by vertical space -% ------------------------------------- -% -% This is not required for syntax highlight, but usually a good idea for -% documents with lots of source code. -% :: - -\usepackage{parskip} - -% Highlight code blocks -% --------------------- -% -% Pygments_ has an option to generate stylesheets for HTML and LaTeX. -% However, the "kryptic" codes used for HTML and LaTeX differ, so that -% the output of the command -% `pygmentize -S default -f latex -O commandprefix=docutilsrole` -% fails to work with rst2latex-highlight. -% :: - -% Colours with LaTeX -\usepackage{color} - -% Standard Postscript fonts -\usepackage[sc]{mathpazo} -\RequirePackage[scaled=.95]{helvet} % scaled to fit Palatino - -% Courier monotype fonts with bold and italic variants -% \usepackage{courier} -% TXfonts monotype -\renewcommand{\ttdefault}{txtt} - -% keyword -\newcommand\DUrolek[1]{\textbf{\textbf{#1}}} -% new function -\newcommand\DUrolenf[1]{\textcolor[rgb]{0.00,0.25,0.82}{#1}} -% punktuation -% \newcommand\DUrolep[1]{\textcolor[rgb]{0.72,0.53,0.04}{#1}} -% string -\newcommand\DUroles[1]{\textcolor[rgb]{0.40,0.40,0.40}{\textit{#1}}} -% number -% \newcommand\DUrolemf[1]{\textcolor[rgb]{0.00,0.53,0.00}{#1}} -% operator -\newcommand\DUrolear[1]{\textcolor[rgb]{0.72,0.53,0.04}{#1}} - -% Incomplete! - -% All STANDARD_TYPES below may appear in the output. (But only the ones -% you like to style need to be defined :-) - -% STANDARD_TYPES = { -% Token: '', -% -% Text: '', -% Whitespace: 'w', -% Error: 'err', -% Other: 'x', -% -% Keyword: 'k', -% Keyword.Constant: 'kc', -% Keyword.Declaration: 'kd', -% Keyword.Pseudo: 'kp', -% Keyword.Reserved: 'kr', -% Keyword.Type: 'kt', -% -% Name: 'n', -% Name.Attribute: 'na', -% Name.Builtin: 'nb', -% Name.Builtin.Pseudo: 'bp', -% Name.Class: 'nc', -% Name.Constant: 'no', -% Name.Decorator: 'nd', -% Name.Entity: 'ni', -% Name.Exception: 'ne', -% Name.Function: 'nf', -% Name.Property: 'py', -% Name.Label: 'nl', -% Name.Namespace: 'nn', -% Name.Other: 'nx', -% Name.Tag: 'nt', -% Name.Variable: 'nv', -% Name.Variable.Class: 'vc', -% Name.Variable.Global: 'vg', -% Name.Variable.Instance: 'vi', -% -% Literal: 'l', -% Literal.Date: 'ld', -% -% String: 's', -% String.Backtick: 'sb', -% String.Char: 'sc', -% String.Doc: 'sd', -% String.Double: 's2', -% String.Escape: 'se', -% String.Heredoc: 'sh', -% String.Interpol: 'si', -% String.Other: 'sx', -% String.Regex: 'sr', -% String.Single: 's1', -% String.Symbol: 'ss', -% -% Number: 'm', -% Number.Float: 'mf', -% Number.Hex: 'mh', -% Number.Integer: 'mi', -% Number.Integer.Long: 'il', -% Number.Oct: 'mo', -% -% Operator: 'o', -% Operator.Word: 'ow', -% -% Punctuation: 'p', -% -% Comment: 'c', -% Comment.Multiline: 'cm', -% Comment.Preproc: 'cp', -% Comment.Single: 'c1', -% Comment.Special: 'cs', -% -% Generic: 'g', -% Generic.Deleted: 'gd', -% Generic.Emph: 'ge', -% Generic.Error: 'gr', -% Generic.Heading: 'gh', -% Generic.Inserted: 'gi', -% Generic.Output: 'go', -% Generic.Prompt: 'gp', -% Generic.Strong: 'gs', -% Generic.Subheading: 'gu', -% Generic.Traceback: 'gt', -% } diff --git a/sandbox/code-block-directive/data/pygments-long.css b/sandbox/code-block-directive/data/pygments-long.css deleted file mode 100644 index f05d26d30..000000000 --- a/sandbox/code-block-directive/data/pygments-long.css +++ /dev/null @@ -1,119 +0,0 @@ -/* Stylesheet for pygments enhanced reStructured Text */ -/* ================================================== */ - -/* :Author: Guenter Milde */ -/* :Copyright: 2007 G. Milde */ -/* This stylesheet is released under the GPL v. 2 or later */ - -/* This stylesheet provides syntax highlight for documents generated with a */ -/* pygments_ enhanced reStructured Text -> html converter. */ - -/* Import the default docutils style sheet */ -/* --------------------------------------- */ -/* :: */ - -@import url("/stylesheets/html4css1.css"); - -/* Indent the code block */ -/* --------------------- */ - -/* Content copied from the `html4css1.css` rule for literal blocks. */ -/* Selector adapted to the output of Pygments_. :: */ - -div.highlight { - margin-left: 2em ; - margin-right: 2em ; - background-color: #eeeeee - } - - -/* Layout for code block tokens */ -/* ---------------------------- */ - -.comment { color: #008800; font-style: italic } -.error { border: 1px solid #FF0000 } -.keyword { color: #AA22FF; font-weight: bold } -.operator { color: #666666 } -.comment.multiline { color: #008800; font-style: italic } -.comment.preproc { color: #008800 } -.comment.single { color: #008800; font-style: italic } -.generic.deleted { color: #A00000 } -.generic.emph { font-style: italic } -.generic.error { color: #FF0000 } -.generic.heading { color: #000080; font-weight: bold } -.generic.inserted { color: #00A000 } -.generic.output { color: #808080 } -.generic.prompt { color: #000080; font-weight: bold } -.generic.strong { font-weight: bold } -.generic.subheading { color: #800080; font-weight: bold } -.generic.traceback { color: #0040D0 } -.keyword.constant { color: #AA22FF; font-weight: bold } -.keyword.declaration { color: #AA22FF; font-weight: bold } -.keyword.pseudo { color: #AA22FF } -.keyword.reserved { color: #AA22FF; font-weight: bold } -.keyword.type { color: #AA22FF; font-weight: bold } -.literal.number { color: #666666 } -.literal.string { color: #BB4444 } -.name.attribute { color: #BB4444 } -.name.builtin { color: #AA22FF } -.name.class { color: #0000FF } -.name.constant { color: #880000 } -.name.decorator { color: #AA22FF } -.name.entity { color: #999999; font-weight: bold } -.name.exception { color: #D2413A; font-weight: bold } -.name.function { color: #00A000 } -.name.label { color: #A0A000 } -.name.namespace { color: #0000FF; font-weight: bold } -.name.tag { color: #008000; font-weight: bold } -.name.variable { color: #B8860B } -.operator.word { color: #AA22FF; font-weight: bold } -.literal.number.float { color: #666666 } -.literal.number.hex { color: #666666 } -.literal.number.integer { color: #666666 } -.literal.number.oct { color: #666666 } -.literal.string.backtick { color: #BB4444 } -.literal.string.char { color: #BB4444 } -.literal.string.doc { color: #BB4444; font-style: italic } -.literal.string.double { color: #BB4444 } -.literal.string.escape { color: #BB6622; font-weight: bold } -.literal.string.heredoc { color: #BB4444 } -.literal.string.interpol { color: #BB6688; font-weight: bold } -.literal.string.other { color: #008000 } -.literal.string.regex { color: #BB6688 } -.literal.string.single { color: #BB4444 } -.literal.string.symbol { color: #B8860B } -.name.builtin.pseudo { color: #AA22FF } -.name.variable.class { color: #B8860B } -.name.variable.global { color: #B8860B } -.name.variable.instance { color: #B8860B } -.literal.number.integer.long { color: #666666 } - -/* .. _pygments: http://pygments.org/ */ - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sandbox/code-block-directive/data/rst2html-pygments b/sandbox/code-block-directive/data/rst2html-pygments deleted file mode 100755 index c227bbf2e..000000000 --- a/sandbox/code-block-directive/data/rst2html-pygments +++ /dev/null @@ -1,60 +0,0 @@ -#!/usr/bin/python - -# :Author: David Goodger, a Pygments author|contributor, Guenter Milde -# :Date: $Date: $ -# :Copyright: This module has been placed in the public domain. - -# This is a merge of the docutils_ `rst2html` front end with an extension -# suggestion taken from the pygments_ documentation. - -""" -A front end to docutils, producing HTML with syntax colouring using pygments -""" - -try: - import locale - locale.setlocale(locale.LC_ALL, '') -except: - pass - -from docutils.core import publish_cmdline, default_description - -description = ('Generates (X)HTML documents from standalone reStructuredText ' - 'sources. Uses `pygments` to colorize the content of' - '"code-block" directives. Needs an adapted stylesheet' - + default_description) - -# Define a new directive `code-block` that uses the `pygments` source -# highlighter to render code in color. -# -# Code from the `pygments`_ documentation for `Using Pygments in ReST -# documents`_. - -from docutils import nodes -from docutils.parsers.rst import directives -from pygments import highlight -from pygments.lexers import get_lexer_by_name -from pygments.formatters import HtmlFormatter - -pygments_formatter = HtmlFormatter() - -def pygments_directive(name, arguments, options, content, lineno, - content_offset, block_text, state, state_machine): - try: - lexer = get_lexer_by_name(arguments[0]) - except ValueError: - # no lexer found - use the text one instead of an exception - lexer = get_lexer_by_name('text') - parsed = highlight(u'\n'.join(content), lexer, pygments_formatter) - return [nodes.raw('', parsed, format='html')] -pygments_directive.arguments = (1, 0, 1) -pygments_directive.content = 1 -directives.register_directive('code-block', pygments_directive) - -# Call the docutils publisher to render the input as html:: - -publish_cmdline(writer_name='html', description=description) - -# .. _doctutile: http://docutils.sf.net/ -# .. _pygments: http://pygments.org/ -# .. _Using Pygments in ReST documents: http://pygments.org/docs/rstdirective/ diff --git a/sandbox/code-block-directive/docs/myfunction.py.pdf b/sandbox/code-block-directive/docs/myfunction.py.pdf Binary files differindex fccaeee8e..6fa42f72a 100644 --- a/sandbox/code-block-directive/docs/myfunction.py.pdf +++ b/sandbox/code-block-directive/docs/myfunction.py.pdf diff --git a/sandbox/code-block-directive/docs/myfunction.py.pseudoxml b/sandbox/code-block-directive/docs/myfunction.py.pseudoxml index 04bd2cb05..cf21d2eab 100644 --- a/sandbox/code-block-directive/docs/myfunction.py.pseudoxml +++ b/sandbox/code-block-directive/docs/myfunction.py.pseudoxml @@ -2,22 +2,91 @@ <paragraph> This is a test of the new "code" directive: <comment xml:space="preserve"> - Translate this document to HTML with a pygments enhanced frontend, e.g. + Translate this document with a pygments enhanced frontend, e.g. - ../rst2html-highlight.py --stylesheet=../data/pygments-default.css + ../rst2html-highlight.py --stylesheet=../data/pygments-default.css + ../rst2latex-highlight.py --stylesheet=../data/pygments-docutilsroles.sty - ../rst2latex-highlight.py --stylesheet=../data/pygments-docutilsroles.sty + or via the test case in - ../rst2pseudoxml-highlight.py + ../pygments_code_block_directive.py --traceback <paragraph> The example from Docutils TODO list: - <literal_block classes="code pythonsi" xml:space="preserve"> - print 'This is Python code.' - for i in range(10): - print i + <literal_block classes="code python" xml:space="preserve"> + <inline classes="k"> + print + + <inline classes="s"> + 'This is Python code.' + + <inline classes="k"> + for + + <inline classes="n"> + i + + <inline classes="ow"> + in + + <inline classes="nb"> + range + <inline classes="p"> + ( + <inline classes="mi"> + 10 + <inline classes="p"> + ): + + + <inline classes="k"> + print + + <inline classes="n"> + i + <paragraph> + Numbered lines: + <literal_block classes="code python" xml:space="preserve"> + <inline classes="ln"> + 1 + <inline classes="c"> + # This is Python code, + + <inline classes="ln"> + 2 + <inline classes="c"> + # that prints the integers from 0 to 9 + + <inline classes="ln"> + 3 + <inline classes="k"> + for + + <inline classes="n"> + i + + <inline classes="ow"> + in + + <inline classes="nb"> + range + <inline classes="p"> + ( + <inline classes="mi"> + 10 + <inline classes="p"> + ): + + <inline classes="ln"> + 4 + + <inline classes="k"> + print + + <inline classes="n"> + i <paragraph> Another example: - <literal_block classes="code python silly" ids="my-function" names="my-function" xml:space="preserve"> + <literal_block classes="code python silly" ids="my-function" names="my_function" xml:space="preserve"> <inline classes="ln"> 7 <inline classes="k"> @@ -36,23 +105,19 @@ <inline classes="ln"> 9 <inline classes="sd"> - + """ + <inline classes="ln"> 10 - <inline classes="sd"> - just a test""" <inline classes="ln"> 11 - - <inline classes="ln"> - 12 <inline classes="c"> # and now for something completely different <inline classes="ln"> - 13 + 12 <inline classes="k"> print diff --git a/sandbox/code-block-directive/docs/myfunction.py.tex b/sandbox/code-block-directive/docs/myfunction.py.tex index 5a7721295..70f96df3a 100644 --- a/sandbox/code-block-directive/docs/myfunction.py.tex +++ b/sandbox/code-block-directive/docs/myfunction.py.tex @@ -14,8 +14,11 @@ \setlength{\DUlineblockindent}{1em} %%% User specified packages and stylesheets -\usepackage{../data/pygments-docutilsroles} +\usepackage{palatino-optima-txtt} +\usepackage{microtype} +\usepackage{bookmark} +\usepackage{../data/pygments-docutilsroles} %%% Fallback definitions for Docutils-specific commands % inline markup (custom roles) @@ -65,7 +68,7 @@ The example from Docutils TODO list: Numbered lines: % \begin{quote}{\ttfamily \raggedright \noindent -\DUrole{ln}{1~}\DUrole{c}{\#~This~is~Python~code,}~\\ +\DUrole{l}{\DUrole{n}{1~}}\DUrole{c}{\#~This~is~Python~code,}~\\ \DUrole{ln}{2~}\DUrole{c}{\#~that~prints~the~integers~from~0~to~9}~\\ \DUrole{ln}{3~}\DUrole{k}{for}~\DUrole{n}{i}~\DUrole{ow}{in}~\DUrole{nb}{range}\DUrole{p}{(}\DUrole{mi}{10}\DUrole{p}{):}~\\ \DUrole{ln}{4~}~~~~\DUrole{k}{print}~\DUrole{n}{i} @@ -75,7 +78,7 @@ Numbered lines: Another example: % \begin{quote}{\ttfamily \raggedright \noindent -\DUrole{ln}{~7~}\DUrole{k}{def}~\DUrole{nf}{my\_function}\DUrole{p}{():}~\\ +\DUrole{l}{\DUrole{n}{~7~}}\DUrole{k}{def}~\DUrole{nf}{my\_function}\DUrole{p}{():}~\\ \DUrole{ln}{~8~}~~~~\DUrole{sd}{"{}"{}"Test~the~lexer.\\ }\DUrole{ln}{~9~}\DUrole{sd}{~~~~"{}"{}"}~\\ \DUrole{ln}{10~}~\\ @@ -84,6 +87,9 @@ Another example: } \end{quote} -The end. +Inline code \texttt{\DUrole{code}{\$\textbackslash{}alpha = +\textbackslash{}int\_0\textasciicircum{}\textbackslash{}infty f(x) dx\$}}. + +Python code \texttt{\DUrole{code}{\DUrole{python}{\DUrole{testclass}{\DUrole{k}{print}\DUrole{p}{(}\DUrole{s}{"The end."}\DUrole{p}{)}}}}} \end{document} diff --git a/sandbox/code-block-directive/docs/myfunction.py.txt b/sandbox/code-block-directive/docs/myfunction.py.txt index cce27ea52..7d632a7d5 100644 --- a/sandbox/code-block-directive/docs/myfunction.py.txt +++ b/sandbox/code-block-directive/docs/myfunction.py.txt @@ -42,4 +42,10 @@ Another example: # and now for something completely different print 8/2 -The end. +Inline code :code:`$\alpha = \int_0^\infty f(x) dx$`. + +.. role:: python(code) + :language: python + :class: testclass + +Python code :python:`print("The end.")` diff --git a/sandbox/code-block-directive/docs/syntax-highlight.txt b/sandbox/code-block-directive/docs/syntax-highlight.txt index d87967995..85c320494 100644 --- a/sandbox/code-block-directive/docs/syntax-highlight.txt +++ b/sandbox/code-block-directive/docs/syntax-highlight.txt @@ -6,169 +6,26 @@ Syntax Highlight :Author: Günter Milde :Contact: milde@users.berlios.de :Date: $Date$ -:Copyright: © 2007, 2009 G. Milde, - Released without warranties or conditions of any kind - under the terms of the Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0 +:Copyright: © 2007, 2009, 2012 G. Milde, +:License: Released under the terms of the `2-Clause BSD license`_, in short: + + Copying and distribution of this file, with or without modification, + are permitted in any medium without royalty provided the copyright + notice and this notice are preserved. + This file is offered as-is, without any warranty. + +.. _2-Clause BSD license: http://www.spdx.org/licenses/BSD-2-Clause + :Abstract: Proposal to add syntax highlight of code blocks to the capabilities of Docutils_. .. sectnum:: .. contents:: -Syntax highlighting significantly enhances the readability of code. However, -in the current version, docutils does not highlight literal blocks. - -This sandbox project aims to add syntax highlight of code blocks to the -capabilities of docutils. To find its way into the docutils core, it should -meet the requirements laid out in a mail on `Questions about writing -programming manuals and scientific documents`__, by docutils main developer -David Goodger: - - I'd be happy to include Python source colouring support, and other - languages would be welcome too. A multi-language solution would be - useful, of course. My issue is providing support for all output formats - -- HTML and LaTeX and XML and anything in the future -- simultaneously. - Just HTML isn't good enough. Until there is a generic-output solution, - this will be something users will have to put together themselves. - -__ http://sourceforge.net/mailarchive/message.php?msg_id=12921194 - -Some older ideas are gathered in Docutils TODO_ document. - -.. _TODO: ../../../docutils/docs/dev/todo.html#colorize-python - -State of the art ----------------- - -There are already docutils extensions providing syntax colouring, e.g: - -`listings`_, - Since Docutils 0.5, the "latex2e" writer supports syntax highlight of - literal blocks via the `listings` package with the - ``--literal-block-env=lstlistings`` option. You need to provide a custom - style sheet. The stylesheets_ repository provides two LaTeX style sheets - for highlighting literal-blocks with "listings". - -Odtwriter_, experimental writer for Docutils OpenOffice export supports syntax - colours using Pygments_. See also the (outdated) section `Odtwriter syntax`_. - -Pygments_ - is a generic syntax highlighter written completely in Python. - - * Usable as a command-line tool and as a Python package. - * Supports about 200 `languages and markup formats`_ (version 1.4). - * Already used by the odtwriter_ and Sphinx. - * Support for new languages, formats, and styles is added easily (modular - structure, Python code, existing documentation). - * Well documented and actively maintained. - * The web site provides a recipe for `using Pygments in ReST documents`_ - (used in the legacy `Pygments enhanced docutils front-ends`_). - -rest2web_, - the "site builder" provides the `colorize`__ macro (using the - `Moin-Moin Python colorizer`_) - -__ http://www.voidspace.org.uk/python/rest2web/macros.html#colorize - -SilverCity_, - a C++ library and Python extension that can provide lexical - analysis for over 20 different programming languages. A recipe__ for a - "code-block" directive provides syntax highlight by SilverCity. - -__ http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252170 - -Sphinx_ - features automatic highlighting using the Pygments_ highlighter. - It introduces the custom directives - - :code-block: similar to the proposal below, - :sourcecode: an alias to "code-block", and - :highlight: configre highlight of "literal blocks". - - (see http://sphinx.pocoo.org/markup/code.html). - -Trac_ - has `reStructuredText support`__ and offers syntax highlighting with - a "code-block" directive using GNU Enscript_, SilverCity_, or Pygments_. - -__ http://trac.edgewall.org/wiki/WikiRestructuredText - - -Summary -""""""" - -On 2009-02-20, David Goodger wrote in docutils-devel - - I'd like to see the extensions implemented in Bruce and Sphinx etc. - folded back into core Docutils eventually. Otherwise we'll end up with - incompatible systems. - -Pygments_ seems to be the most promising Docutils highlighter. - -For printed output and PDFs via LaTeX, the listings_ package is a viable -alternative. - - -Pygments enhanced docutils front-ends -------------------------------------- - -Syntax highlight can be achieved by `front-end scripts`_ combining docutils and -pygments. - - "something users [will have to] put together themselves" - -Advantages: - + Easy implementation with no changes to the stock docutils_. - + Separation of code blocks and ordinary literal blocks. - -Disadvantages: - 1. "code-block" content is formatted by `pygments`_ and inserted in the - document tree as a "raw" node making the approach writer-dependant. - 2. documents are incompatible with the standard docutils because of the - locally defined directive. - 3. more "invasive" markup distracting from content - (no "minimal" code block marker -- three additional lines per code block) - - -Point 1 and 2 lead to the `code-block directive proposal`_. - -Point 3 becomes an issue in software documentation and literate programming -where a code block is the most used block markup. It is addressed in the -proposal for a `configurable literal block directive`_). - - -`code-block` directive proposal -------------------------------- - -Syntax -"""""" - -.. note:: This is the first draft for a reStructuredText definition, - analogue to other directives in ``directives.txt``. - -:Directive Type: "code" -:Doctree Element: literal_block -:Directive Arguments: One (`language`), optional. -:Directive Options: name, class, number-lines. -:Directive Content: Becomes the body of the literal block. - -The "code-block" directive constructs a literal block where the content is -parsed as source code and syntax highlight rules for `language` are applied. -If syntax rules for `language` are not known to Docutils, a warning is -issued and the content is rendered as ordinary literal block with -additional class arguments: "code" and the value of `language`. - - :number-lines: let pygments include line-numbers - - -The following options are recognized: - -``number-lines`` : [start line number] - Precede every code line with a line number. - The optional argument is the number of the first line (defaut 1). - -and the common options `:class:`_ and `:name:`_. +Syntax highlighting significantly enhances the readability of code. +Since version 0.9, docutils supports this with a `code` directive and role +as well as a `code` option to the `include` directive using the Pygments_ +syntax highlighter. Example:: The content of the following directive :: @@ -182,135 +39,26 @@ Example:: is parsed and marked up as Python source code. The actual rendering depends on the style-sheet. - -Remarks -""""""" - -* Without language argument, the parsing step is skipped. Use cases: - - * Mark a literal block as pseudo-code. - - * Suppress warnings about a missing Pygments_ module or unknown languages. - - * Do the parsing in the writer or output processor (e.g. LaTeX with - the listings_ package). - - The language's name can be given as `class` option. - - Alternative: - make the `language` argument compulsory and add a "no-highlight" option. +TODO +==== * TODO: Pygments_ provides filters like VisibleWhitespaceFilter add options to use them? +* Use syntax-highlight=long as default and add basic highlight rules + (keyword, comment, string?) to the default CSS stylesheet to get syntax + highlight out-of-the-box? + + Let the latex2e writer write basic rules in the document preamble if + "code" is used in the document? + +* The latex writer should pass the original content and options to a + ``lstlistings`` environment. with ``--literal-block-env=lstlistings``. -Include directive option -"""""""""""""""""""""""" - -The include directive should get a matching new option: +* Check the `odtwriter`, use common syntax and implementation. -code: language - The entire included text is inserted into the document as if it were the - content of a code-block directive (useful for program listings). - -Code Role -""""""""" - -For inline code snippets, a `code` role should be implemented. Roles for -specific languages might be defined via the `role` directive based on the -generic `code` role. - -Implementation -"""""""""""""" - -Reading -''''''' - -Felix Wiemann provided a `proof of concept`_ script that utilizes the -pygments_ parser to parse a source code string and store the result in -the document tree. - -This concept is used in a `pygments_code_block_directive`_ (Source: -`pygments_code_block_directive.py`_), to define and register a "code-block" -directive. - -* The ``DocutilsInterface`` class uses pygments to parse the content of the - directive and classify the tokens using short CSS class names identical to - pygments HTML output. If pygments is not available, the unparsed code is - returned. TODO: issue a warning. - -* The ``code_block_directive`` function inserts the tokens in a "rich" - <literal_block> element with "classified" <inline> nodes. - -Writing -''''''' - -The writers can use the class information in the <inline> elements to render -the tokens. They should ignore the class information if they are unable to -use it or to pass it on. - -Running the test script `<../tools/test_pygments_code_block_directive.py>`_ -produces example output for a set of writers. - -HTML - The "html" writer works out of the box. - - * The rst2html-highlight_ front end registers the "code-block" directive and - converts an input file to html. - - * Styling is done with the adapted CSS style sheet `pygments-default.css`_ - based on docutils' default stylesheet and the output of - ``pygmentize -S default -f html``. - - The conversion of `<myfunction.py.txt>`_ looks like - `<myfunction.py.htm>`_. - - The "s5" and "pep" writers are not tested yet. - -XML - "xml" and "pseudoxml" work out of the box. - - The conversion of `myfunction.py.txt`_ looks like - `<myfunction.py.xml>`_ respective `<myfunction.py.pseudoxml>`_ - -LaTeX - "latex2e" (SVN version) works out of the box. - - * A style file, e.g. `<pygments-docutilsroles.sty>`_, is required to actually - highlight the code in the output. (As with HTML, the pygments-produced - style file will not work with docutils' output.) - - * Alternatively, the latex writer could reconstruct the original - content and pass it to a ``lstlistings`` environment. - - TODO: This should be the default behaviour with - ``--literal-block-env=lstlistings``. - - The LaTeX output of `myfunction.py.txt`_ looks like `<myfunction.py.tex>`_ - and corresponding PDF like `<myfunction.py.pdf>`_. - -OpenOffice - The `odtwriter` provides syntax highlight with pygments but uses a - different syntax and implementation. - - -TODO -"""" - -1. Minimal implementation: - - * move the code from `pygments_code_block_directive.py`_ to "the right - place". - - * add the CSS rules to the default style-sheet (see pygments-default.css_) - - * provide a LaTeX style. - -2. Write functional test case and sample. - -3. Think about an interface for pygments' options (like "encoding" or - "linenumbers"). +* Provide more sample stylesheets in an official stylesheet library. Configurable literal block directive @@ -385,92 +133,17 @@ Example:: In the same line, a "default-block-quote" setting or directive could be considered to configure the role of a block quote. -Odtwriter syntax ----------------- - -.. attention:: - The content of this section relates to an old version of the - `odtwriter`. Things changed with the inclusion of the `odtwriter` into - standard Docutils. - - This is only kept for historical reasons. - -Dave Kuhlman's odtwriter_ extension can add syntax highlighting -to ordinary literal blocks. - -The ``--add-syntax-highlighting`` command line flag activates syntax -highlighting in literal blocks. By default, the "python" lexer is used. - -You can change this within your reST document with the `sourcecode` -directive:: - - .. sourcecode:: off - - ordinary literal block:: - - content set in teletype - - .. sourcecode:: on - .. sourcecode:: python - - colourful Python code:: - - def hello(): - print "hello world" - - -The "sourcecode" directive defined by the odtwriter is principally -different from the "code-block" directive of ``rst2html-pygments``: - -* The odtwriter directive does not have content. It is a switch. - -* The syntax highlighting state and language/lexer set by this directive - remain in effect until the next sourcecode directive is encountered in the - reST document. - - ``.. sourcecode:: <newstate>`` - make highlighting active or inactive. - <newstate> is either ``on`` or ``off``. - - ``.. sourcecode:: <lexer>`` - change the lexer parsing literal code blocks. - <lexer> should be one of aliases listed at pygment's `languages and - markup formats`_. - -I.e. the odtwriter implements a `configurable literal block directive`_ -(but with a slightly different syntax than the proposal above). .. External links -.. _rest2web: http://www.voidspace.org.uk/python/rest2web/ -.. _Enscript: http://www.gnu.org/software/enscript/enscript.html -.. _SilverCity: http://silvercity.sourceforge.net/ -.. _Trac: http://trac.edgewall.org/ -.. _Moin-Moin Python colorizer: - http://www.standards-schmandards.com/2005/fangs-093/ .. _odtwriter: http://www.rexx.com/~dkuhlman/odtwriter.html .. _Sphinx: http://sphinx.pocoo.org .. _listings: http://www.ctan.org/tex-archive/help/Catalogue/entries/listings.html -.. _PyLit: http://pylit.berlios.de -.. _PyLit Examples: http://pylit.berlios.de/examples/index.html#latex-packages - .. _Pygments: http://pygments.org/ -.. _languages and markup formats: http://pygments.org/languages -.. _Using Pygments in ReST documents: http://pygments.org/docs/rstdirective/ - .. _Docutils: http://docutils.sourceforge.net/ .. _Docutils Document Tree: http://docutils.sf.net/docs/ref/doctree.html#classes -.. _latex-variants: http://docutils.sourceforge.net/sandbox/latex-variants/ -.. _proof of concept: - http://article.gmane.org/gmane.text.docutils.user/3689 .. Internal links -.. _front-end scripts: ../tools/pygments-enhanced-front-ends -.. _pygments-default.css: ../data/pygments-default.css -.. _pygments_code_block_directive.py: ../pygments_code_block_directive.py -.. _pygments_code_block_directive: pygments_code_block_directive-bunt.py.htm -.. _rst2html-highlight: ../rst2html-highlight -.. _pygments-long.css: ../data/pygments-long.css .. _stylesheets: ../../stylesheets/ diff --git a/sandbox/code-block-directive/pygments_code_block_directive.py b/sandbox/code-block-directive/pygments_code_block_directive.py deleted file mode 100755 index 23b8f6a3d..000000000 --- a/sandbox/code-block-directive/pygments_code_block_directive.py +++ /dev/null @@ -1,248 +0,0 @@ -#!/usr/bin/python -# coding: utf-8 - -# :Author: Georg Brandl; Felix Wiemann; Günter Milde -# :Date: $Date$ -# :Copyright: This module has been placed in the public domain. -# -# This is a merge of `Using Pygments in ReST documents`_ from the pygments_ -# documentation, and a `proof of concept`_ by Felix Wiemann. -# -# .. class:: borderless -# -# ========== ============================================================= -# 2007-06-01 Removed redundancy from class values. -# 2007-06-04 Merge of successive tokens of same type -# (code taken from pygments.formatters.others). -# 2007-06-05 Separate docutils formatter script -# Use pygments' CSS class names (like the html formatter) -# allowing the use of pygments-produced style sheets. -# 2007-06-07 Merge in the formatting of the parsed tokens -# (misnamed as docutils_formatter) as class DocutilsInterface -# 2007-06-08 Failsave implementation (fallback to a standard literal block -# if pygments not found) -# 2010-11-27 Rename directive from "code-block" to "code". -# Fix fallback if pygments not found. -# Use class-based interface. -# Add "number-lines" option. -# ========== ============================================================= -# -# :: - -"""Define and register a code directive using pygments""" - -# Requirements -# ------------ -# :: - -from docutils import nodes -from docutils.parsers.rst import directives, Directive -from docutils.parsers.rst.roles import set_classes -try: - import pygments - from pygments.lexers import get_lexer_by_name - from pygments.formatters.html import _get_ttype_class - with_pygments = True -except ImportError: - with_pygments = False - -# Customisation -# ------------- -# -# Do not insert inline nodes for the following tokens. -# (You could add e.g. Token.Punctuation like ``['', 'p']``.) :: - -unstyled_tokens = [''] # Token.Text - -# Lexer -# --------- -# -# This interface class combines code from -# pygments.formatters.html and pygments.formatters.others. - -class Lexer(object): - """Parse `code` lines and yield "classified" tokens. - - Arguments - - code -- list of source code lines to parse - language -- formal language the code is written in. - - Merge subsequent tokens of the same token-type. - - Iterating over an instance yields the tokens as ``(ttype_class, value)`` - tuples, where `ttype_class` is taken from pygments.token.STANDARD_TYPES - and corresponds to the class argument used in pygments html output. - """ - - def __init__(self, code, language): - """ - Set up a lexical analyzer for `code` in `language`. - """ - self.code = code - self.language = language - self.lexer = None - # get lexical analyzer for `language`: - if language in ('', 'text'): - return - if not with_pygments: - raise ApplicationError('Cannot highlight code. ' - 'Pygments package not found.') - try: - self.lexer = get_lexer_by_name(self.language) - except pygments.util.ClassNotFound: - raise ApplicationError('Cannot highlight code. ' - 'No Pygments lexer found for "%s".' % language) - - # Since version 1.2. (released Jan 01, 2010) Pygments has a - # TokenMergeFilter. ``self.merge(tokens)`` in __iter__ can be - # replaced by ``self.lexer.add_filter('tokenmerge')`` in __init__. - - def merge(self, tokens): - """Merge subsequent tokens of same token-type. - - Also strip the final '\n' (added by pygments). - """ - tokens = iter(tokens) - (lasttype, lastval) = tokens.next() - for ttype, value in tokens: - if ttype is lasttype: - lastval += value - else: - yield(lasttype, lastval) - (lasttype, lastval) = (ttype, value) - if lastval != '\n': - yield(lasttype, lastval) - - def __iter__(self): - """Parse self.code and yield "classified" tokens - """ - codestring = u'\n'.join(self.code) - if self.lexer is None: - yield [('', codestring)] - return - tokens = pygments.lex(codestring, self.lexer) - for ttype, value in self.merge(tokens): - # yield (ttype, value) # token type objects - yield (_get_ttype_class(ttype), value) # short name strings - - -class NumberLines(object): - """Insert linenumber-tokens in front of every newline. - - Arguments - - tokens -- iterable of ``(ttype_class, value)`` tuples - startline -- first line number - endline -- last line number - - Iterating over an instance yields the tokens preceded by - a ``('ln', '<line number>')`` token for every line. - Multi-line tokens from pygments are splitted. """ - - def __init__(self, tokens, startline, endline): - self.tokens = tokens - self.startline = startline - # pad linenumbers, e.g. endline == 100 -> fmt_str = '%3d ' - self.fmt_str = '%%%dd ' % len(str(endline)) - - def __iter__(self): - lineno = self.startline - yield ('ln', self.fmt_str % lineno) - for ttype, value in self.tokens: - lines = value.split('\n') - for line in lines[:-1]: - yield (ttype, line + '\n') - lineno += 1 - yield ('ln', self.fmt_str % lineno) - yield (ttype, lines[-1]) - - -# CodeBlock directive -# -------------------- -# :: - -class CodeBlock(Directive): - """Parse and mark up content of a code block. - """ - optional_arguments = 1 - option_spec = {'class': directives.class_option, - 'name': directives.unchanged, - 'number-lines': directives.unchanged # integer or None - } - has_content = True - - def run(self): - self.assert_has_content() - if self.arguments: - language = self.arguments[0] - else: - language = '' - set_classes(self.options) - classes = ['code', language] - if 'classes' in self.options: - classes.extend(self.options['classes']) - - # TODO: config setting to skip lexical analysis: - ## if document.settings.no_highlight: - ## language = '' - - # set up lexical analyzer - tokens = Lexer(self.content, language) - - if 'number-lines' in self.options: - # optional argument `startline`, defaults to 1 - try: - startline = int(self.options['number-lines'] or 1) - except ValueError: - raise self.error(':number-lines: with non-integer start value') - endline = startline + len(self.content) - # add linenumber filter: - tokens = NumberLines(tokens, startline, endline) - - node = nodes.literal_block('\n'.join(self.content), classes=classes) - self.add_name(node) - - # analyze content and add nodes for every token - for cls, value in tokens: - # print (cls, value) - if cls in unstyled_tokens: - # insert as Text to decrease the verbosity of the output. - node += nodes.Text(value, value) - else: - node += nodes.inline(value, value, classes=[cls]) - - return [node] - - -# Register Directive -# ------------------ -# :: - -directives.register_directive('code', CodeBlock) - -# .. _doctutils: http://docutils.sf.net/ -# .. _pygments: http://pygments.org/ -# .. _Using Pygments in ReST documents: http://pygments.org/docs/rstdirective/ -# .. _proof of concept: -# http://article.gmane.org/gmane.text.docutils.user/3689 -# -# Test output -# ----------- -# -# If called from the command line, call the docutils publisher to render the -# input:: - -if __name__ == '__main__': - from docutils.core import publish_cmdline, default_description - description = 'code-block directive test output' + default_description - try: - import locale - locale.setlocale(locale.LC_ALL, '') - except: - pass - # Uncomment the desired output format: - # publish_cmdline(writer_name='pseudoxml', description=description) - # publish_cmdline(writer_name='xml', description=description) - # publish_cmdline(writer_name='html', description=description) - publish_cmdline(writer_name='latex', description=description) diff --git a/sandbox/code-block-directive/rst2html-highlight.py b/sandbox/code-block-directive/rst2html-highlight.py deleted file mode 100755 index 5924da7cf..000000000 --- a/sandbox/code-block-directive/rst2html-highlight.py +++ /dev/null @@ -1,53 +0,0 @@ -#!/usr/bin/python -# coding: utf-8 - -# rst2html-highlight -# ================== -# -# Docutils front-end with syntax highlight. -# -# :Author: David Goodger, Georg Brandl, Günter Milde -# :Date: $Date: 2008-05-22 08:42:52 +0200 (Do, 22. Mai 2008) $ -# :Copyright: This module has been placed in the public domain. -# -# This is a merge of the docutils_ `rst2html` front end with an extension -# suggestion by Felix Wiemann. -# -# :: - -""" -A front end to docutils, producing HTML with syntax colouring using pygments - -Generates (X)HTML documents from standalone reStructuredText sources. Uses -`pygments` to parse and mark up the content of ``.. code::` directives. -Needs an adapted stylesheet -""" - -# Requirements -# ------------ -# -# :: - -try: - import locale - locale.setlocale(locale.LC_ALL, '') -except: - pass - -from docutils.core import publish_cmdline, default_description - -# The `pygments_code_block_directive`_ module defines and registers a new -# directive `code` that uses the `pygments`_ source highlighter to -# render code in color:: - -import pygments_code_block_directive - -# Call the docutils publisher to render the input as html:: - -description = __doc__ + default_description -publish_cmdline(writer_name='html', description=description) - -# .. _docutils: http://docutils.sf.net/ -# .. _pygments_code_block_directive: pygments_code_block_directive.py -# .. _pygments: http://pygments.org/ -# .. _Using Pygments in ReST documents: http://pygments.org/docs/rstdirective/ diff --git a/sandbox/code-block-directive/rst2latex-highlight.py b/sandbox/code-block-directive/rst2latex-highlight.py deleted file mode 100755 index 616ac3ce5..000000000 --- a/sandbox/code-block-directive/rst2latex-highlight.py +++ /dev/null @@ -1,53 +0,0 @@ -#!/usr/bin/python - -# rst2latex-highlight -# =================== -# -# Docutils front-end with syntax highlight. -# -# :Author: David Goodger, a Pygments author|contributor, Guenter Milde -# :Date: $Date: 2008-05-22 08:42:52 +0200 (Do, 22. Mai 2008) $ -# :Copyright: This module has been placed in the public domain. -# -# This is a merge of the docutils_ `rst2latex` front end with an extension -# suggestion taken from the Pygments_ documentation. -# -# :: - -""" -A front end to docutils, producing LaTeX with syntax colouring using pygments - -Generates LaTeX documents from standalone reStructuredText sources. Uses the -`Pygments` syntax highlighter to parse and mark up the content of ``.. -code::` directives. Needs an adapted stylesheet. -""" - -# Requirements -# ------------ -# -# :: - -try: - import locale - locale.setlocale(locale.LC_ALL, '') -except: - pass - -from docutils.core import publish_cmdline, default_description - -# `<pygments_code_block_directive.py>`_ defines and registers a new -# directive `code` that uses the `Pygments`_ syntax highlighter to -# render code in color:: - -import pygments_code_block_directive - -# Call the docutils publisher to render the input as latex:: - -description = __doc__ + default_description -publish_cmdline(writer_name='latex2e', description=description) - - -# .. References: -# .. _docutils: http://docutils.sf.net/ -# .. _pygments: http://pygments.org/ -# .. _Using Pygments in ReST documents: http://pygments.org/docs/rstdirective/ diff --git a/sandbox/code-block-directive/tools/makesty.py b/sandbox/code-block-directive/tools/makesty.py deleted file mode 100644 index 27e17bb09..000000000 --- a/sandbox/code-block-directive/tools/makesty.py +++ /dev/null @@ -1,62 +0,0 @@ -#! /usr/bin/env python -# coding: utf8 -# Copyright: Raphael 'kena' Poss <r.c.poss@uva.nl> -# this file is placed in the public domain. -# -# Convert a CSS stylesheet into one for Docutils' LaTeX output. -# -# Usage example:: -# -# pygmentize -S default -f html | python makesty.py >pygments-default.sty -# -# Versions: -# -# 2012-05-09: Günter Milde <milde@users.sf.net>: -# Bugfix: do not fail at lines without comment. -# Support for digits in role names. -# ``\providecommand`` instead of ``\newcommand``. - -import sys -import re - -print '% Stylesheet for syntax highlight with Docutils' -print '% Generated by makesty.py from a Pygments CSS style' -print '% (output of `pygmentize -S <style> -f html`).' -print -print r'\RequirePackage{color}' - -cnt = 0 -for l in sys.stdin: - - if '/*' in l: - print "% " + l.split('*')[1] - key = l.split(' ', 1)[0][1:] - - s = '#1' - - if 'color:' in l: - col = l.split('#',1)[1][:6] - r = float(int(col[0:2], 16)) / 255. - g = float(int(col[2:4], 16)) / 255. - b = float(int(col[4:6], 16)) / 255. - s = r'\textcolor[rgb]{%.2f,%.2f,%.2f}{%s}' % (r, g, b, s) - - if 'font-style: italic' in l: - s = r'\textit{%s}' % s - if 'font-weight: bold' in l: - s = r'\textbf{%s}' % s - - if 'border:' in l: - col = l.split('#',1)[1][:6] - r = float(int(col[0:2], 16)) / 255. - g = float(int(col[2:4], 16)) / 255. - b = float(int(col[4:6], 16)) / 255. - cname = 'DUcolor%d' % cnt - cnt += 1 - print r'\definecolor{%s}{rgb}{%.2f,%.2f,%.2f}' % (cname, r, g, b) - s = r'\colorbox{%s}{%s}' % (cname, s) - - if re.match(r'.*[0-9]', key) is None: - print r'\providecommand*\DUrole%s[1]{%s}' % (key, s) - else: - print r'\providecommand\csname DUrole%s\endcsname[1]{%s}' % (key, s) diff --git a/sandbox/code-block-directive/tools/pygments-docutilsroles.sty b/sandbox/code-block-directive/tools/pygments-docutilsroles.sty deleted file mode 100644 index a3caea642..000000000 --- a/sandbox/code-block-directive/tools/pygments-docutilsroles.sty +++ /dev/null @@ -1,122 +0,0 @@ -% Stylesheet generated by makesty.py -\usepackage{color} -% Comment -\newcommand\docutilsrolec[1]{\textit{\textcolor[rgb]{0.25,0.50,0.50}{#1}}} -% Error -\definecolor{ducolor0}{rgb}{1.00,0.00,0.00} -\newcommand\docutilsroleerr[1]{\colorbox{ducolor0}{#1}} -% Keyword -\newcommand\docutilsrolek[1]{\textbf{\textcolor[rgb]{0.00,0.50,0.00}{#1}}} -% Operator -\newcommand\docutilsroleo[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} -% Comment.Multiline -\newcommand\docutilsrolecm[1]{\textit{\textcolor[rgb]{0.25,0.50,0.50}{#1}}} -% Comment.Preproc -\newcommand\docutilsrolecp[1]{\textcolor[rgb]{0.74,0.48,0.00}{#1}} -% Comment.Single -% Can't generate style for 'c1' because 'docutilsrolec1' is not a valid macro id. -% Comment.Special -\newcommand\docutilsrolecs[1]{\textit{\textcolor[rgb]{0.25,0.50,0.50}{#1}}} -% Generic.Deleted -\newcommand\docutilsrolegd[1]{\textcolor[rgb]{0.63,0.00,0.00}{#1}} -% Generic.Emph -\newcommand\docutilsrolege[1]{\textit{#1}} -% Generic.Error -\newcommand\docutilsrolegr[1]{\textcolor[rgb]{1.00,0.00,0.00}{#1}} -% Generic.Heading -\newcommand\docutilsrolegh[1]{\textbf{\textcolor[rgb]{0.00,0.00,0.50}{#1}}} -% Generic.Inserted -\newcommand\docutilsrolegi[1]{\textcolor[rgb]{0.00,0.63,0.00}{#1}} -% Generic.Output -\newcommand\docutilsrolego[1]{\textcolor[rgb]{0.50,0.50,0.50}{#1}} -% Generic.Prompt -\newcommand\docutilsrolegp[1]{\textbf{\textcolor[rgb]{0.00,0.00,0.50}{#1}}} -% Generic.Strong -\newcommand\docutilsrolegs[1]{\textbf{#1}} -% Generic.Subheading -\newcommand\docutilsrolegu[1]{\textbf{\textcolor[rgb]{0.50,0.00,0.50}{#1}}} -% Generic.Traceback -\newcommand\docutilsrolegt[1]{\textcolor[rgb]{0.00,0.25,0.82}{#1}} -% Keyword.Constant -\newcommand\docutilsrolekc[1]{\textbf{\textcolor[rgb]{0.00,0.50,0.00}{#1}}} -% Keyword.Declaration -\newcommand\docutilsrolekd[1]{\textbf{\textcolor[rgb]{0.00,0.50,0.00}{#1}}} -% Keyword.Pseudo -\newcommand\docutilsrolekp[1]{\textcolor[rgb]{0.00,0.50,0.00}{#1}} -% Keyword.Reserved -\newcommand\docutilsrolekr[1]{\textbf{\textcolor[rgb]{0.00,0.50,0.00}{#1}}} -% Keyword.Type -\newcommand\docutilsrolekt[1]{\textcolor[rgb]{0.69,0.00,0.25}{#1}} -% Literal.Number -\newcommand\docutilsrolem[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} -% Literal.String -\newcommand\docutilsroles[1]{\textcolor[rgb]{0.73,0.13,0.13}{#1}} -% Name.Attribute -\newcommand\docutilsrolena[1]{\textcolor[rgb]{0.49,0.56,0.16}{#1}} -% Name.Builtin -\newcommand\docutilsrolenb[1]{\textcolor[rgb]{0.00,0.50,0.00}{#1}} -% Name.Class -\newcommand\docutilsrolenc[1]{\textbf{\textcolor[rgb]{0.00,0.00,1.00}{#1}}} -% Name.Constant -\newcommand\docutilsroleno[1]{\textcolor[rgb]{0.53,0.00,0.00}{#1}} -% Name.Decorator -\newcommand\docutilsrolend[1]{\textcolor[rgb]{0.67,0.13,1.00}{#1}} -% Name.Entity -\newcommand\docutilsroleni[1]{\textbf{\textcolor[rgb]{0.60,0.60,0.60}{#1}}} -% Name.Exception -\newcommand\docutilsrolene[1]{\textbf{\textcolor[rgb]{0.82,0.25,0.23}{#1}}} -% Name.Function -\newcommand\docutilsrolenf[1]{\textcolor[rgb]{0.00,0.00,1.00}{#1}} -% Name.Label -\newcommand\docutilsrolenl[1]{\textcolor[rgb]{0.63,0.63,0.00}{#1}} -% Name.Namespace -\newcommand\docutilsrolenn[1]{\textbf{\textcolor[rgb]{0.00,0.00,1.00}{#1}}} -% Name.Tag -\newcommand\docutilsrolent[1]{\textbf{\textcolor[rgb]{0.00,0.50,0.00}{#1}}} -% Name.Variable -\newcommand\docutilsrolenv[1]{\textcolor[rgb]{0.10,0.09,0.49}{#1}} -% Operator.Word -\newcommand\docutilsroleow[1]{\textbf{\textcolor[rgb]{0.67,0.13,1.00}{#1}}} -% Text.Whitespace -\newcommand\docutilsrolew[1]{\textcolor[rgb]{0.73,0.73,0.73}{#1}} -% Literal.Number.Float -\newcommand\docutilsrolemf[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} -% Literal.Number.Hex -\newcommand\docutilsrolemh[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} -% Literal.Number.Integer -\newcommand\docutilsrolemi[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} -% Literal.Number.Oct -\newcommand\docutilsrolemo[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} -% Literal.String.Backtick -\newcommand\docutilsrolesb[1]{\textcolor[rgb]{0.73,0.13,0.13}{#1}} -% Literal.String.Char -\newcommand\docutilsrolesc[1]{\textcolor[rgb]{0.73,0.13,0.13}{#1}} -% Literal.String.Doc -\newcommand\docutilsrolesd[1]{\textit{\textcolor[rgb]{0.73,0.13,0.13}{#1}}} -% Literal.String.Double -% Can't generate style for 's2' because 'docutilsroles2' is not a valid macro id. -% Literal.String.Escape -\newcommand\docutilsrolese[1]{\textbf{\textcolor[rgb]{0.73,0.40,0.13}{#1}}} -% Literal.String.Heredoc -\newcommand\docutilsrolesh[1]{\textcolor[rgb]{0.73,0.13,0.13}{#1}} -% Literal.String.Interpol -\newcommand\docutilsrolesi[1]{\textbf{\textcolor[rgb]{0.73,0.40,0.53}{#1}}} -% Literal.String.Other -\newcommand\docutilsrolesx[1]{\textcolor[rgb]{0.00,0.50,0.00}{#1}} -% Literal.String.Regex -\newcommand\docutilsrolesr[1]{\textcolor[rgb]{0.73,0.40,0.53}{#1}} -% Literal.String.Single -% Can't generate style for 's1' because 'docutilsroles1' is not a valid macro id. -% Literal.String.Symbol -\newcommand\docutilsroless[1]{\textcolor[rgb]{0.10,0.09,0.49}{#1}} -% Name.Builtin.Pseudo -\newcommand\docutilsrolebp[1]{\textcolor[rgb]{0.00,0.50,0.00}{#1}} -% Name.Variable.Class -\newcommand\docutilsrolevc[1]{\textcolor[rgb]{0.10,0.09,0.49}{#1}} -% Name.Variable.Global -\newcommand\docutilsrolevg[1]{\textcolor[rgb]{0.10,0.09,0.49}{#1}} -% Name.Variable.Instance -\newcommand\docutilsrolevi[1]{\textcolor[rgb]{0.10,0.09,0.49}{#1}} -% Literal.Number.Integer.Long -\newcommand\docutilsroleil[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} -\newcommand\docutilsrolep[1]{#1} diff --git a/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/README.txt b/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/README.txt deleted file mode 100644 index aa68c3d11..000000000 --- a/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/README.txt +++ /dev/null @@ -1,69 +0,0 @@ -.. -*- rst-mode -*- - -Pygments enhanced docutils front-ends -------------------------------------- - -The example code in "`Using Pygments in ReST documents`_" defines a new -"sourcecode" directive. The directive takes one argument `language` and uses -the `Pygments`_ source highlighter to parse and render its content as a -colourful source code block. - -Combining the pygments_ example code with the standard docutils_ front-ends, -results in front-end scripts generating output documents with syntax colour. -For consistency with the majority of existing add-ons, the directive is -renamed to "code-block". - -`rst2html-pygments`_ - enhances the standard docutils ``rst2html`` front-end to - generate a HTML rendering with syntax highlight. - -`rst2latex-pygments`_ - enhances docutils' ``rst2latex`` to generate LaTeX with syntax highlight. - -Advantages: - + Easy implementation with no changes to the stock docutils_. - + Separation of code blocks and ordinary literal blocks. - -Disadvantages: - - "code-block" content is formatted by `pygments`_ and inserted in the - document tree as a "raw" node making the approach writer-dependant. - - documents are incompatible with the standard docutils because of the - locally defined directive. - - more "invasive" markup distracting from content - - no "minimal" code block marker -- three additional lines per code block - - -The disadvantages lead to the alternative implementation with the -demonstrator front ends `rst2html-highlight`_ and `rst2latex-highlight`_. - - -Example -""""""" - -Python script: - :text source: `for-else-test.py.txt`_ - :HTML: `for-else-test.py.htm`_ - :LaTeX: `for-else-test.py.tex`_ - :PDF: `for-else-test.py.pdf`_ - -Stylesheets: - :CSS stylesheet: `pygments-default.css`_ - :LaTeX style: `pygments-default.sty`_ - -.. References - -.. _pygments: http://pygments.org/ -.. _docutils: http://docutils.sourceforge.net/ -.. _Using Pygments in ReST documents: http://pygments.org/docs/rstdirective/ - -.. _rst2html-pygments: rst2html-pygments -.. _rst2latex-pygments: rst2latex-pygments -.. _rst2html-highlight: ../../rst2html-highlight -.. _rst2latex-highlight: ../../rst2latex-highlight -.. _for-else-test: -.. _for-else-test.py.htm: for-else-test.py.htm -.. _for-else-test.py.txt: for-else-test.py.txt -.. _for-else-test.py.tex: for-else-test.py.tex -.. _for-else-test.py.pdf: for-else-test.py.pdf -.. _pygments-default.css: ../../data/pygments-default.css -.. _pygments-default.sty: ../../data/pygments-default.sty diff --git a/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/for-else-test.py b/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/for-else-test.py deleted file mode 100644 index 4bb941dd6..000000000 --- a/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/for-else-test.py +++ /dev/null @@ -1,92 +0,0 @@ -# Example for syntax highlight with Pygments -# ========================================== -# -# Translate this document to HTML with a pygments enhanced frontend:: -# -# rst2html-pygments --stylesheet=pygments-default.css -# -# or to LaTeX with:: -# -# rst2latex-pygments --stylesheet=pygments-default.sty -# -# to gain syntax highlight in the output. -# -# Convert between text <-> code source formats with:: -# -# pylit --code-block-marker='.. code-block:: python' -# -# Run the doctests with:: -# -# pylit --doctest for-else-test.py -# -# -# for-else-test -# ------------- -# -# Test the flow in a `for` loop with `else` statement. -# -# First define a simple `for` loop. -# -# .. code-block:: python - -def loop1(iterable): - """simple for loop with `else` statement""" - for i in iterable: - print i - else: - print "iterable empty" - print "Ende" - -# Now test it: -# -# The first test runs as I expect: iterator empty -> else clause applies: -# -# .. code-block:: pycon -# -# >>> execfile('for-else-test.py') -# >>> loop1(range(0)) -# iterable empty -# Ende -# -# However, the else clause even runs if the iterator is not empty in the first -# place but after it is "spent": -# -# .. code-block:: pycon -# -# >>> loop1(range(3)) -# 0 -# 1 -# 2 -# iterable empty -# Ende -# -# It seems like the else clause can only be prevented, if we break out of -# the loop. Let's try -# -# .. code-block:: python - -def loop2(iterable): - """for loop with `break` and `else` statement""" - for i in iterable: - print i - break - else: - print "iterable empty" - print "Ende" - -# And indeed, the else clause is skipped after breaking out of the loop: -# -# .. code-block:: pycon -# -# >>> loop2(range(3)) -# 0 -# Ende -# -# The empty iterator runs as expected: -# -# .. code-block:: pycon -# -# >>> loop2(range(0)) -# iterable empty -# Ende -# diff --git a/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/for-else-test.py.htm b/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/for-else-test.py.htm deleted file mode 100644 index 1e6057be1..000000000 --- a/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/for-else-test.py.htm +++ /dev/null @@ -1,185 +0,0 @@ -<?xml version="1.0" encoding="iso-8859-1" ?> -<!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" xml:lang="en" lang="en"> -<head> -<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> -<meta name="generator" content="Docutils 0.4.1: http://docutils.sourceforge.net/" /> -<title>Example for syntax highlight with Pygments</title> -<style type="text/css"> - -/* Stylesheet for pygments enhanced reStructured Text */ -/* ================================================== */ - -/* :Author: Guenter Milde */ -/* :Copyright: 2007 G. Milde */ -/* This stylesheet is released under the GPL v. 2 or later */ - -/* This stylesheet provides syntax highlight for documents generated with a */ -/* pygments_ enhanced reStructured Text -> html converter. */ - -/* Import the default docutils style sheet */ -/* --------------------------------------- */ -/* :: */ - -@import url("/stylesheets/html4css1.css"); - -/* Indent the code block */ -/* --------------------- */ - -/* Content copied from the `html4css1.css` rule for literal blocks. */ -/* Selector adapted to the output of Pygments_. :: */ - -div.highlight { - margin-left: 2em ; - margin-right: 2em ; - background-color: #eeeeee - } - - -/* Colour code blocks */ -/* ------------------ */ - -/* Pygments_ has an option to generate stylesheets for html and latex. */ -/* The following code is generated with the command */ -/* `pygmentize -S default -f html > pygments-default.css`:: */ - -.c { color: #008800; font-style: italic } /* Comment */ -.err { border: 1px solid #FF0000 } /* Error */ -.k { color: #AA22FF; font-weight: bold } /* Keyword */ -.o { color: #666666 } /* Operator */ -.cm { color: #008800; font-style: italic } /* Comment.Multiline */ -.cp { color: #008800 } /* Comment.Preproc */ -.c1 { color: #008800; font-style: italic } /* Comment.Single */ -.gd { color: #A00000 } /* Generic.Deleted */ -.ge { font-style: italic } /* Generic.Emph */ -.gr { color: #FF0000 } /* Generic.Error */ -.gh { color: #000080; font-weight: bold } /* Generic.Heading */ -.gi { color: #00A000 } /* Generic.Inserted */ -.go { color: #808080 } /* Generic.Output */ -.gp { color: #000080; font-weight: bold } /* Generic.Prompt */ -.gs { font-weight: bold } /* Generic.Strong */ -.gu { color: #800080; font-weight: bold } /* Generic.Subheading */ -.gt { color: #0040D0 } /* Generic.Traceback */ -.kc { color: #AA22FF; font-weight: bold } /* Keyword.Constant */ -.kd { color: #AA22FF; font-weight: bold } /* Keyword.Declaration */ -.kp { color: #AA22FF } /* Keyword.Pseudo */ -.kr { color: #AA22FF; font-weight: bold } /* Keyword.Reserved */ -.kt { color: #AA22FF; font-weight: bold } /* Keyword.Type */ -.m { color: #666666 } /* Literal.Number */ -.s { color: #BB4444 } /* Literal.String */ -.na { color: #BB4444 } /* Name.Attribute */ -.nb { color: #AA22FF } /* Name.Builtin */ -.nc { color: #0000FF } /* Name.Class */ -.no { color: #880000 } /* Name.Constant */ -.nd { color: #AA22FF } /* Name.Decorator */ -.ni { color: #999999; font-weight: bold } /* Name.Entity */ -.ne { color: #D2413A; font-weight: bold } /* Name.Exception */ -.nf { color: #00A000 } /* Name.Function */ -.nl { color: #A0A000 } /* Name.Label */ -.nn { color: #0000FF; font-weight: bold } /* Name.Namespace */ -.nt { color: #008000; font-weight: bold } /* Name.Tag */ -.nv { color: #B8860B } /* Name.Variable */ -.ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ -.mf { color: #666666 } /* Literal.Number.Float */ -.mh { color: #666666 } /* Literal.Number.Hex */ -.mi { color: #666666 } /* Literal.Number.Integer */ -.mo { color: #666666 } /* Literal.Number.Oct */ -.sb { color: #BB4444 } /* Literal.String.Backtick */ -.sc { color: #BB4444 } /* Literal.String.Char */ -.sd { color: #BB4444; font-style: italic } /* Literal.String.Doc */ -.s2 { color: #BB4444 } /* Literal.String.Double */ -.se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */ -.sh { color: #BB4444 } /* Literal.String.Heredoc */ -.si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */ -.sx { color: #008000 } /* Literal.String.Other */ -.sr { color: #BB6688 } /* Literal.String.Regex */ -.s1 { color: #BB4444 } /* Literal.String.Single */ -.ss { color: #B8860B } /* Literal.String.Symbol */ -.bp { color: #AA22FF } /* Name.Builtin.Pseudo */ -.vc { color: #B8860B } /* Name.Variable.Class */ -.vg { color: #B8860B } /* Name.Variable.Global */ -.vi { color: #B8860B } /* Name.Variable.Instance */ -.il { color: #666666 } /* Literal.Number.Integer.Long */ - -/* .. _pygments: http://pygments.org/ */ - -</style> -</head> -<body> -<div class="document" id="example-for-syntax-highlight-with-pygments"> -<h1 class="title">Example for syntax highlight with Pygments</h1> -<p>Translate this document to HTML with a pygments enhanced frontend:</p> -<pre class="literal-block"> -rst2html-pygments --stylesheet=pygments-default.css -</pre> -<p>or to LaTeX with:</p> -<pre class="literal-block"> -rst2latex-pygments --stylesheet=pygments-default.sty -</pre> -<p>to gain syntax highlight in the output.</p> -<p>Convert between text <-> code source formats with:</p> -<pre class="literal-block"> -pylit --code-block-marker='.. code-block:: python' -</pre> -<p>Run the doctests with:</p> -<pre class="literal-block"> -pylit --doctest for-else-test.py -</pre> -<div class="section"> -<h1><a id="for-else-test" name="for-else-test">for-else-test</a></h1> -<p>Test the flow in a <cite>for</cite> loop with <cite>else</cite> statement.</p> -<p>First define a simple <cite>for</cite> loop.</p> -<div class="highlight"><pre><span class="k">def</span> <span class="nf">loop1</span><span class="p">(</span><span class="n">iterable</span><span class="p">):</span> - <span class="sd">"""simple for loop with `else` statement"""</span> - <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span> - <span class="k">print</span> <span class="n">i</span> - <span class="k">else</span><span class="p">:</span> - <span class="k">print</span> <span class="s">"iterable empty"</span> - <span class="k">print</span> <span class="s">"Ende"</span> -</pre></div> -<p>Now test it:</p> -<p>The first test runs as I expect: iterator empty -> else clause applies:</p> -<div class="highlight"><pre><span class="gp">>>> </span><span class="nb">execfile</span><span class="p">(</span><span class="s">'for-else-test.py'</span><span class="p">)</span> -<span class="gp">>>> </span><span class="n">loop1</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">))</span> -<span class="go">iterable empty</span> -<span class="go">Ende</span> -</pre></div> -<p>However, the else clause even runs if the iterator is not empty in the first -place but after it is "spent":</p> -<div class="highlight"><pre><span class="gp">>>> </span><span class="n">loop1</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span> -<span class="go">0</span> -<span class="go">1</span> -<span class="go">2</span> -<span class="go">iterable empty</span> -<span class="go">Ende</span> -</pre></div> -<p>It seems like the else clause can only be prevented, if we break out of -the loop. Let's try</p> -<div class="highlight"><pre><span class="k">def</span> <span class="nf">loop2</span><span class="p">(</span><span class="n">iterable</span><span class="p">):</span> - <span class="sd">"""for loop with `break` and `else` statement"""</span> - <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span> - <span class="k">print</span> <span class="n">i</span> - <span class="k">break</span> - <span class="k">else</span><span class="p">:</span> - <span class="k">print</span> <span class="s">"iterable empty"</span> - <span class="k">print</span> <span class="s">"Ende"</span> -</pre></div> -<p>And indeed, the else clause is skipped after breaking out of the loop:</p> -<div class="highlight"><pre><span class="gp">>>> </span><span class="n">loop2</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span> -<span class="go">0</span> -<span class="go">Ende</span> -</pre></div> -<p>The empty iterator runs as expected:</p> -<div class="highlight"><pre><span class="gp">>>> </span><span class="n">loop2</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">))</span> -<span class="go">iterable empty</span> -<span class="go">Ende</span> -</pre></div> -</div> -</div> -<div class="footer"> -<hr class="footer" /> -Generated on: 2007-06-21. - -</div> -</body> -</html> diff --git a/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/for-else-test.py.pdf b/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/for-else-test.py.pdf Binary files differdeleted file mode 100644 index fa39a5967..000000000 --- a/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/for-else-test.py.pdf +++ /dev/null diff --git a/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/for-else-test.py.tex b/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/for-else-test.py.tex deleted file mode 100644 index e16a5dd71..000000000 --- a/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/for-else-test.py.tex +++ /dev/null @@ -1,170 +0,0 @@ -\documentclass[10pt,a4paper,english]{scrartcl} -\usepackage{babel} -\usepackage[T1]{fontenc} -\usepackage{shortvrb} -\usepackage{ucs} -\usepackage[utf8x]{inputenc} -\usepackage{tabularx} -\usepackage{longtable} -\usepackage{booktabs} -\setlength{\extrarowheight}{2pt} -\usepackage{amsmath} -\usepackage{graphicx} -\usepackage{color} -\usepackage{multirow} -\usepackage{ifthen} -\typearea{12} -% generated by Docutils <http://docutils.sourceforge.net/> -\newlength{\admonitionwidth} -\setlength{\admonitionwidth}{0.9\textwidth} -\newlength{\docinfowidth} -\setlength{\docinfowidth}{0.9\textwidth} -\newlength{\locallinewidth} -\newcommand{\optionlistlabel}[1]{\bf #1 \hfill} -\newenvironment{optionlist}[1] -{\begin{list}{} - {\setlength{\labelwidth}{#1} - \setlength{\rightmargin}{1cm} - \setlength{\leftmargin}{\rightmargin} - \addtolength{\leftmargin}{\labelwidth} - \addtolength{\leftmargin}{\labelsep} - \renewcommand{\makelabel}{\optionlistlabel}} -}{\end{list}} -\newlength{\lineblockindentation} -\setlength{\lineblockindentation}{2.5em} -\newenvironment{lineblock}[1] -{\begin{list}{} - {\setlength{\partopsep}{\parskip} - \addtolength{\partopsep}{\baselineskip} - \topsep0pt\itemsep0.15\baselineskip\parsep0pt - \leftmargin#1} - \raggedright} -{\end{list}} -% begin: floats for footnotes tweaking. -\setlength{\floatsep}{0.5em} -\setlength{\textfloatsep}{\fill} -\addtolength{\textfloatsep}{3em} -\renewcommand{\textfraction}{0.5} -\renewcommand{\topfraction}{0.5} -\renewcommand{\bottomfraction}{0.5} -\setcounter{totalnumber}{50} -\setcounter{topnumber}{50} -\setcounter{bottomnumber}{50} -% end floats for footnotes -% some commands, that could be overwritten in the style file. -\newcommand{\rubric}[1]{\subsection*{~\hfill {\it #1} \hfill ~}} -\newcommand{\titlereference}[1]{\textsl{#1}} -% end of "some commands" -% user specified packages and stylesheets: -\usepackage{../../data/pygments-default} -\ifthenelse{\isundefined{\hypersetup}}{ -\usepackage[colorlinks=true,linkcolor=blue,urlcolor=blue]{hyperref} -}{} -\title{Example for syntax highlight with Pygments} -\author{} -\date{} -\hypersetup{ -pdftitle={Example for syntax highlight with Pygments} -} -\raggedbottom -\begin{document} -\maketitle - -\setlength{\locallinewidth}{\linewidth} - -Translate this document to HTML with a pygments enhanced frontend: -\begin{quote}\begin{verbatim} -rst2html-pygments --stylesheet=pygments-default.css -\end{verbatim} -\end{quote} - -or to LaTeX with: -\begin{quote}\begin{verbatim} -rst2latex-pygments --stylesheet=pygments-default.sty -\end{verbatim} -\end{quote} - -to gain syntax highlight in the output. - -Convert between text {\textless}-{\textgreater} code source formats with: -\begin{quote}\begin{verbatim} -pylit --code-block-marker='.. code-block:: python' -\end{verbatim} -\end{quote} - -Run the doctests with: -\begin{quote}\begin{verbatim} -pylit --doctest for-else-test.py -\end{verbatim} -\end{quote} - - -%___________________________________________________________________________ - -\hypertarget{for-else-test}{} -\pdfbookmark[0]{for-else-test}{for-else-test} -\section*{for-else-test} -\label{for-else-test} - -Test the flow in a \titlereference{for} loop with \titlereference{else} statement. - -First define a simple \titlereference{for} loop. -\begin{Verbatim}[commandchars=@\[\]] -@PYay[def] @PYaK[loop1](iterable): - @PYas["""simple for loop with `else` statement"""] - @PYay[for] i @PYan[in] iterable: - @PYay[print] i - @PYay[else]: - @PYay[print] @PYad["]@PYad[iterable empty]@PYad["] - @PYay[print] @PYad["]@PYad[Ende]@PYad["] -\end{Verbatim} - -Now test it: - -The first test runs as I expect: iterator empty -{\textgreater} else clause applies: -\begin{Verbatim}[commandchars=@\[\]] -@PYaO[>>> ]@PYaX[execfile](@PYad[']@PYad[for-else-test.py]@PYad[']) -@PYaO[>>> ]loop1(@PYaX[range](@PYaw[0])) -@PYaa[iterable empty] -@PYaa[Ende] -\end{Verbatim} - -However, the else clause even runs if the iterator is not empty in the first -place but after it is ``spent'': -\begin{Verbatim}[commandchars=@\[\]] -@PYaO[>>> ]loop1(@PYaX[range](@PYaw[3])) -@PYaa[0] -@PYaa[1] -@PYaa[2] -@PYaa[iterable empty] -@PYaa[Ende] -\end{Verbatim} - -It seems like the else clause can only be prevented, if we break out of -the loop. Let's try -\begin{Verbatim}[commandchars=@\[\]] -@PYay[def] @PYaK[loop2](iterable): - @PYas["""for loop with `break` and `else` statement"""] - @PYay[for] i @PYan[in] iterable: - @PYay[print] i - @PYay[break] - @PYay[else]: - @PYay[print] @PYad["]@PYad[iterable empty]@PYad["] - @PYay[print] @PYad["]@PYad[Ende]@PYad["] -\end{Verbatim} - -And indeed, the else clause is skipped after breaking out of the loop: -\begin{Verbatim}[commandchars=@\[\]] -@PYaO[>>> ]loop2(@PYaX[range](@PYaw[3])) -@PYaa[0] -@PYaa[Ende] -\end{Verbatim} - -The empty iterator runs as expected: -\begin{Verbatim}[commandchars=@\[\]] -@PYaO[>>> ]loop2(@PYaX[range](@PYaw[0])) -@PYaa[iterable empty] -@PYaa[Ende] -\end{Verbatim} - -\end{document} diff --git a/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/for-else-test.py.txt b/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/for-else-test.py.txt deleted file mode 100644 index d9687ecd5..000000000 --- a/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/for-else-test.py.txt +++ /dev/null @@ -1,92 +0,0 @@ -Example for syntax highlight with Pygments -========================================== - -Translate this document to HTML with a pygments enhanced frontend:: - - rst2html-pygments --stylesheet=pygments-default.css - -or to LaTeX with:: - - rst2latex-pygments --stylesheet=pygments-default.sty - -to gain syntax highlight in the output. - -Convert between text <-> code source formats with:: - - pylit --code-block-marker='.. code-block:: python' - -Run the doctests with:: - - pylit --doctest for-else-test.py - - -for-else-test -------------- - -Test the flow in a `for` loop with `else` statement. - -First define a simple `for` loop. - -.. code-block:: python - - def loop1(iterable): - """simple for loop with `else` statement""" - for i in iterable: - print i - else: - print "iterable empty" - print "Ende" - -Now test it: - -The first test runs as I expect: iterator empty -> else clause applies: - -.. code-block:: pycon - - >>> execfile('for-else-test.py') - >>> loop1(range(0)) - iterable empty - Ende - -However, the else clause even runs if the iterator is not empty in the first -place but after it is "spent": - -.. code-block:: pycon - - >>> loop1(range(3)) - 0 - 1 - 2 - iterable empty - Ende - -It seems like the else clause can only be prevented, if we break out of -the loop. Let's try - -.. code-block:: python - - def loop2(iterable): - """for loop with `break` and `else` statement""" - for i in iterable: - print i - break - else: - print "iterable empty" - print "Ende" - -And indeed, the else clause is skipped after breaking out of the loop: - -.. code-block:: pycon - - >>> loop2(range(3)) - 0 - Ende - -The empty iterator runs as expected: - -.. code-block:: pycon - - >>> loop2(range(0)) - iterable empty - Ende - diff --git a/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/rst2html-pygments b/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/rst2html-pygments deleted file mode 100755 index 706b25980..000000000 --- a/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/rst2html-pygments +++ /dev/null @@ -1,60 +0,0 @@ -#!/usr/bin/python - -# :Author: David Goodger, the Pygments team, Guenter Milde -# :Date: $Date: $ -# :Copyright: This module has been placed in the public domain. - -# This is a merge of the docutils_ `rst2html` front end with an extension -# suggestion taken from the pygments_ documentation. - -""" -A front end to docutils, producing HTML with syntax colouring using pygments -""" - -try: - import locale - locale.setlocale(locale.LC_ALL, '') -except: - pass - -from docutils.core import publish_cmdline, default_description - -description = ('Generates (X)HTML documents from standalone reStructuredText ' - 'sources. Uses `pygments` to colorize the content of' - '"code-block" directives. Needs an adapted stylesheet' - + default_description) - -# Define a new directive `code-block` that uses the `pygments` source -# highlighter to render code in color. -# -# Code from the `pygments`_ documentation for `Using Pygments in ReST -# documents`_. - -from docutils import nodes -from docutils.parsers.rst import directives -from pygments import highlight -from pygments.lexers import get_lexer_by_name -from pygments.formatters import HtmlFormatter - -pygments_formatter = HtmlFormatter() - -def pygments_directive(name, arguments, options, content, lineno, - content_offset, block_text, state, state_machine): - try: - lexer = get_lexer_by_name(arguments[0]) - except ValueError: - # no lexer found - use the text one instead of an exception - lexer = get_lexer_by_name('text') - parsed = highlight(u'\n'.join(content), lexer, pygments_formatter) - return [nodes.raw('', parsed, format='html')] -pygments_directive.arguments = (1, 0, 1) -pygments_directive.content = 1 -directives.register_directive('code-block', pygments_directive) - -# Call the docutils publisher to render the input as html:: - -publish_cmdline(writer_name='html', description=description) - -# .. _doctutile: http://docutils.sf.net/ -# .. _pygments: http://pygments.org/ -# .. _Using Pygments in ReST documents: http://pygments.org/docs/rstdirective/ diff --git a/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/rst2latex-pygments b/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/rst2latex-pygments deleted file mode 100755 index c53c2aa74..000000000 --- a/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/rst2latex-pygments +++ /dev/null @@ -1,60 +0,0 @@ -#!/usr/bin/python - -# Author: David Goodger, the Pygments team, Günter Milde -# Date: $Date: $ -# Copyright: This module has been placed in the public domain. - -# This is a merge of the docutils_ `rst2latex` front end with an extension -# suggestion taken from the pygments_ documentation. - -""" -A front end to docutils, producing LaTeX with syntax colouring using pygments -""" - -try: - import locale - locale.setlocale(locale.LC_ALL, '') -except: - pass - -from docutils.core import publish_cmdline, default_description - -description = ('Generates LaTeX documents from standalone reStructuredText ' - 'sources. Uses `pygments` to colorize the content of' - '"code-block" directives. Needs an adapted stylesheet' - + default_description) - -# Define a new directive `code-block` that uses the `pygments` source -# highlighter to render code in color. -# -# Code from the `pygments`_ documentation for `Using Pygments in ReST -# documents`_. - -from docutils import nodes -from docutils.parsers.rst import directives -from pygments import highlight -from pygments.lexers import get_lexer_by_name -from pygments.formatters import LatexFormatter - -pygments_formatter = LatexFormatter() - -def pygments_directive(name, arguments, options, content, lineno, - content_offset, block_text, state, state_machine): - try: - lexer = get_lexer_by_name(arguments[0]) - except ValueError: - # no lexer found - use the text one instead of an exception - lexer = get_lexer_by_name('text') - parsed = highlight(u'\n'.join(content), lexer, pygments_formatter) - return [nodes.raw('', parsed, format='latex')] -pygments_directive.arguments = (1, 0, 1) -pygments_directive.content = 1 -directives.register_directive('code-block', pygments_directive) - -# Call the docutils publisher to render the input as latex:: - -publish_cmdline(writer_name='latex', description=description) - -# .. _doctutile: http://docutils.sf.net/ -# .. _pygments: http://pygments.org/ -# .. _Using Pygments in ReST documents: http://pygments.org/docs/rstdirective/ diff --git a/sandbox/code-block-directive/tools/test_pygments_code_block_directive.py b/sandbox/code-block-directive/tools/test_pygments_code_block_directive.py deleted file mode 100755 index def706889..000000000 --- a/sandbox/code-block-directive/tools/test_pygments_code_block_directive.py +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env python -# -*- coding: iso-8859-1 -*- - -# Test the parsing and formatting by pygments: - -# :Author: Felix Wiemann; Günter Milde -# :Date: $Date$ -# :Copyright: This module has been placed in the public domain. - -# Requirements -# ------------ - -from docutils import nodes, utils, core - -# Prepend parent dir to the PYTHONPATH (This is a hack to get this test -# working without installing the pygments_code_block_directive module. -# Not needed if the module is installed in the PYTHONPATH) -import sys -sys.path.insert(0, '..') - -from pygments_code_block_directive import DocutilsInterface - -# Test data -# --------- - -code_sample = """\ -def my_function(): - "just a test" - print 8/2 -""" - -language = "python" - -# Do not insert inline nodes for the following tokens. -# (You could add e.g. Token.Punctuation like ``['', 'p']``.) :: -unstyled_tokens = [''] - -# Set up a document tree -# ---------------------- - -document = utils.new_document('generated') -literal_block = nodes.literal_block(classes=["code", language]) -document += literal_block - - -# Parse code and fill the <literal_block> -# ---------------------------------------- - -for cls, value in DocutilsInterface(code_sample, language): - if cls in unstyled_tokens: - # insert as Text to decrease the verbosity of the output. - node = nodes.Text(value, value) - else: - node = nodes.inline(value, value, classes=[cls]) - literal_block += node - -# Write -# ----- - -writer_names = ('html', 'pseudoxml', 'xml', 'latex', 'newlatex2e', 's5') -for name in writer_names[:]: - print "\nusing writer %r\n" % name - print core.publish_from_doctree(document, writer_name=name) - |