diff options
author | milde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2007-06-08 16:05:01 +0000 |
---|---|---|
committer | milde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2007-06-08 16:05:01 +0000 |
commit | 0784ae4285103bb3098e482fc4f915b977fdf6db (patch) | |
tree | c44f93238162a571b54c12a352c53b1850ec1281 /sandbox/code-block-directive/tools | |
parent | 698b0e9f6dd70ef89d4e47783efbaff903e3155a (diff) | |
download | docutils-0784ae4285103bb3098e482fc4f915b977fdf6db.tar.gz |
New sandbox project for development of a code-block directive
git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@5229 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'sandbox/code-block-directive/tools')
7 files changed, 513 insertions, 0 deletions
diff --git a/sandbox/code-block-directive/tools/pygments-default.css b/sandbox/code-block-directive/tools/pygments-default.css new file mode 100644 index 000000000..5d933198c --- /dev/null +++ b/sandbox/code-block-directive/tools/pygments-default.css @@ -0,0 +1,95 @@ +/* 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/ */ diff --git a/sandbox/code-block-directive/tools/pygments-default.sty b/sandbox/code-block-directive/tools/pygments-default.sty new file mode 100644 index 000000000..d1567557a --- /dev/null +++ b/sandbox/code-block-directive/tools/pygments-default.sty @@ -0,0 +1,86 @@ +% 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. + +% Separate paragraphs by vertical space +% ------------------------------------- + +\setlength{\parindent}{0em} +\setlength{\parskip}{1ex} + +% 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 latex`:: + +\usepackage{fancyvrb} +\usepackage{color} + +\newcommand\at{@} +\newcommand\lb{[} +\newcommand\rb{]} +\newcommand\Cba[1]{\textcolor[rgb]{0.67,0.13,1.00}{\textbf{#1}}} +\newcommand\Caz[1]{\textcolor[rgb]{0.00,0.25,0.82}{#1}} +\newcommand\Cay[1]{\textcolor[rgb]{0.67,0.13,1.00}{#1}} +\newcommand\Cax[1]{\textcolor[rgb]{0.00,0.63,0.00}{#1}} +\newcommand\Cbc[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} +\newcommand\Cas[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} +\newcommand\Car[1]{\textcolor[rgb]{0.72,0.53,0.04}{#1}} +\newcommand\Caq[1]{\textcolor[rgb]{0.73,0.27,0.27}{\textit{#1}}} +\newcommand\Cap[1]{\textcolor[rgb]{0.72,0.53,0.04}{#1}} +\newcommand\Caw[1]{\textcolor[rgb]{0.67,0.13,1.00}{\textbf{#1}}} +\newcommand\Cav[1]{\textcolor[rgb]{0.60,0.60,0.60}{\textbf{#1}}} +\newcommand\Cau[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} +\newcommand\Cat[1]{\textcolor[rgb]{0.67,0.13,1.00}{\textbf{#1}}} +\newcommand\Cak[1]{\textbf{#1}} +\newcommand\Caj[1]{\textcolor[rgb]{0.73,0.40,0.53}{#1}} +\newcommand\Cai[1]{\textcolor[rgb]{0.72,0.53,0.04}{#1}} +\newcommand\Cah[1]{\textcolor[rgb]{0.63,0.63,0.00}{#1}} +\newcommand\Cao[1]{\textcolor[rgb]{0.53,0.00,0.00}{#1}} +\newcommand\Can[1]{\textcolor[rgb]{0.00,0.50,0.00}{#1}} +\newcommand\Cam[1]{\textcolor[rgb]{0.73,0.40,0.13}{\textbf{#1}}} +\newcommand\Cal[1]{\textcolor[rgb]{0.67,0.13,1.00}{\textbf{#1}}} +\newcommand\Cac[1]{\textcolor[rgb]{0.73,0.27,0.27}{#1}} +\newcommand\Cab[1]{\textit{#1}} +\newcommand\Caa[1]{\textcolor[rgb]{0.50,0.50,0.50}{#1}} +\newcommand\Cag[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} +\newcommand\Caf[1]{\textcolor[rgb]{0.00,0.53,0.00}{\textit{#1}}} +\newcommand\Cae[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} +\newcommand\Cad[1]{\textcolor[rgb]{0.73,0.27,0.27}{#1}} +\newcommand\Cbb[1]{\textcolor[rgb]{0.73,0.27,0.27}{#1}} +\newcommand\CaZ[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} +\newcommand\CaY[1]{\textcolor[rgb]{0.00,0.00,0.50}{\textbf{#1}}} +\newcommand\CaX[1]{\textcolor[rgb]{0.00,0.50,0.00}{\textbf{#1}}} +\newcommand\Cbd[1]{\textcolor[rgb]{0.73,0.40,0.53}{\textbf{#1}}} +\newcommand\Cbe[1]{\textcolor[rgb]{0.67,0.13,1.00}{\textbf{#1}}} +\newcommand\CaS[1]{\textcolor[rgb]{0.82,0.25,0.23}{\textbf{#1}}} +\newcommand\CaR[1]{\textcolor[rgb]{0.50,0.00,0.50}{\textbf{#1}}} +\newcommand\CaQ[1]{\textcolor[rgb]{0.00,0.53,0.00}{\textit{#1}}} +\newcommand\CaP[1]{\textcolor[rgb]{0.72,0.53,0.04}{#1}} +\newcommand\CaW[1]{\textcolor[rgb]{0.73,0.27,0.27}{#1}} +\newcommand\CaV[1]{\textcolor[rgb]{0.67,0.13,1.00}{#1}} +\newcommand\CaU[1]{\textcolor[rgb]{0.73,0.27,0.27}{#1}} +\newcommand\CaT[1]{\textcolor[rgb]{0.00,0.00,1.00}{\textbf{#1}}} +\newcommand\CaK[1]{\textcolor[rgb]{0.00,0.53,0.00}{#1}} +\newcommand\CaJ[1]{\textcolor[rgb]{0.67,0.13,1.00}{#1}} +\newcommand\CaI[1]{\textcolor[rgb]{0.00,0.63,0.00}{#1}} +\newcommand\CaH[1]{\textcolor[rgb]{0.73,0.27,0.27}{#1}} +\newcommand\CaO[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} +\newcommand\CaN[1]{\textcolor[rgb]{0.73,0.27,0.27}{#1}} +\newcommand\CaM[1]{\textcolor[rgb]{0.00,0.00,0.50}{\textbf{#1}}} +\newcommand\CaL[1]{\textcolor[rgb]{0.00,0.00,1.00}{#1}} +\newcommand\CaC[1]{\textcolor[rgb]{0.63,0.00,0.00}{#1}} +\newcommand\CaB[1]{\textcolor[rgb]{0.00,0.53,0.00}{\textit{#1}}} +\newcommand\CaA[1]{\textcolor[rgb]{0.67,0.13,1.00}{#1}} +\newcommand\CaG[1]{\textcolor[rgb]{0.67,0.13,1.00}{\textbf{#1}}} +\newcommand\CaF[1]{\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{#1}} +\newcommand\CaE[1]{\textcolor[rgb]{0.72,0.53,0.04}{#1}} +\newcommand\CaD[1]{\textcolor[rgb]{1.00,0.00,0.00}{#1}} diff --git a/sandbox/code-block-directive/tools/pygments-long.css b/sandbox/code-block-directive/tools/pygments-long.css new file mode 100644 index 000000000..f05d26d30 --- /dev/null +++ b/sandbox/code-block-directive/tools/pygments-long.css @@ -0,0 +1,119 @@ +/* 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/tools/rst2html-highlight b/sandbox/code-block-directive/tools/rst2html-highlight new file mode 100755 index 000000000..737a8c802 --- /dev/null +++ b/sandbox/code-block-directive/tools/rst2html-highlight @@ -0,0 +1,36 @@ +#!/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 + +Generates (X)HTML documents from standalone reStructuredText sources. Uses +`pygments` to parse and mark up the content of ``.. code-block::` directives. +Needs an adapted stylesheet +""" + +try: + import locale + locale.setlocale(locale.LC_ALL, '') +except: + pass + +from docutils.core import publish_cmdline, default_description + +# Define and register a new directive `code-block` 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) + +# .. _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/rst2html-pygments b/sandbox/code-block-directive/tools/rst2html-pygments new file mode 100755 index 000000000..c227bbf2e --- /dev/null +++ b/sandbox/code-block-directive/tools/rst2html-pygments @@ -0,0 +1,60 @@ +#!/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/tools/rst2latex-pygments b/sandbox/code-block-directive/tools/rst2latex-pygments new file mode 100755 index 000000000..f52fc167f --- /dev/null +++ b/sandbox/code-block-directive/tools/rst2latex-pygments @@ -0,0 +1,60 @@ +#!/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_ `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 new file mode 100755 index 000000000..b130a7b2f --- /dev/null +++ b/sandbox/code-block-directive/tools/test_pygments_code_block_directive.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python +# -*- coding: iso-8859-1 -*- + +# Test the parsing and formatting by pygments: + +# :Author: Felix Wiemann; Guenter Milde +# :Date: $Date$ +# :Copyright: This module has been placed in the public domain. + +# Requirements +# ------------ + +from docutils import nodes, utils, core +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-block", 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[2:3]: + print "\nusing writer %r\n" % name + print core.publish_from_doctree(document, writer_name=name) + |