summaryrefslogtreecommitdiff
path: root/sandbox/code-block-directive
diff options
context:
space:
mode:
authormilde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2009-05-06 12:10:07 +0000
committermilde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2009-05-06 12:10:07 +0000
commitf67ba51b158c34c46dd275f265c8ef43fa8a8eee (patch)
tree1456ed517cf214acf07007a4b03c8346b5d4a4b6 /sandbox/code-block-directive
parent400f306b29686f8cfdf8cc64275af5f14c20c5b2 (diff)
downloaddocutils-f67ba51b158c34c46dd275f265c8ef43fa8a8eee.tar.gz
Minor sandbox updates
git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@5936 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'sandbox/code-block-directive')
-rw-r--r--sandbox/code-block-directive/data/pygments-docutilsroles.sty101
-rw-r--r--sandbox/code-block-directive/docs/myfunction.py.pdfbin38686 -> 25434 bytes
-rw-r--r--sandbox/code-block-directive/docs/syntax-highlight.txt6
-rwxr-xr-xsandbox/code-block-directive/pygments_code_block_directive.py47
4 files changed, 125 insertions, 29 deletions
diff --git a/sandbox/code-block-directive/data/pygments-docutilsroles.sty b/sandbox/code-block-directive/data/pygments-docutilsroles.sty
index 7abdf4931..d22319772 100644
--- a/sandbox/code-block-directive/data/pygments-docutilsroles.sty
+++ b/sandbox/code-block-directive/data/pygments-docutilsroles.sty
@@ -36,20 +36,111 @@
% 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}
+% \usepackage{courier}
+% TXfonts monotype
+\renewcommand{\ttdefault}{txtt}
% keyword
-\newcommand\docutilsrolek[1]{\textcolor[rgb]{0.67,0.13,1.00}{\textbf{#1}}}
+\newcommand\docutilsrolek[1]{\textbf{\textbf{#1}}}
% new function
\newcommand\docutilsrolenf[1]{\textcolor[rgb]{0.00,0.25,0.82}{#1}}
% punktuation
-\newcommand\docutilsrolep[1]{\textcolor[rgb]{0.72,0.53,0.04}{#1}}
+% \newcommand\docutilsrolep[1]{\textcolor[rgb]{0.72,0.53,0.04}{#1}}
% string
\newcommand\docutilsroles[1]{\textcolor[rgb]{0.40,0.40,0.40}{\textit{#1}}}
% number
-\newcommand\docutilsrolemf[1]{\textcolor[rgb]{0.00,0.53,0.00}{#1}}
+% \newcommand\docutilsrolemf[1]{\textcolor[rgb]{0.00,0.53,0.00}{#1}}
% operator
\newcommand\docutilsrolear[1]{\textcolor[rgb]{0.72,0.53,0.04}{#1}}
-% Incomplete!
+% 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/docs/myfunction.py.pdf b/sandbox/code-block-directive/docs/myfunction.py.pdf
index 5b847ab6f..05fba74ea 100644
--- a/sandbox/code-block-directive/docs/myfunction.py.pdf
+++ b/sandbox/code-block-directive/docs/myfunction.py.pdf
Binary files differ
diff --git a/sandbox/code-block-directive/docs/syntax-highlight.txt b/sandbox/code-block-directive/docs/syntax-highlight.txt
index fd14712b6..a464e5747 100644
--- a/sandbox/code-block-directive/docs/syntax-highlight.txt
+++ b/sandbox/code-block-directive/docs/syntax-highlight.txt
@@ -101,6 +101,12 @@ Odtwriter_, experimental writer for Docutils OpenOffice export supports syntax
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
diff --git a/sandbox/code-block-directive/pygments_code_block_directive.py b/sandbox/code-block-directive/pygments_code_block_directive.py
index 7535acad1..0a115d905 100755
--- a/sandbox/code-block-directive/pygments_code_block_directive.py
+++ b/sandbox/code-block-directive/pygments_code_block_directive.py
@@ -3,10 +3,12 @@
# :Author: the Pygments team; Felix Wiemann; Guenter 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
@@ -16,10 +18,10 @@
# 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
+# 2007-06-08 Failsave implementation (fallback to a standard literal block
# if pygments not found)
# ========== ===========================================================
-#
+#
# ::
"""Define and register a code-block directive using pygments
@@ -42,7 +44,7 @@ except ImportError:
# Customisation
# -------------
-#
+#
# Do not insert inline nodes for the following tokens.
# (You could add e.g. Token.Punctuation like ``['', 'p']``.) ::
@@ -50,25 +52,25 @@ unstyled_tokens = ['']
# DocutilsInterface
# -----------------
-#
+#
# This interface class combines code from
# pygments.formatters.html and pygments.formatters.others.
-#
+#
# It does not require anything of docutils and could also become a part of
# pygments::
class DocutilsInterface(object):
"""Parse `code` string and yield "classified" tokens.
-
+
Arguments
-
+
code -- string of source code to parse
language -- formal language the code is written in.
-
- Merge subsequent tokens of the same token-type.
-
- Yields the tokens as ``(ttype_class, value)`` tuples,
- where ttype_class is taken from pygments.token.STANDARD_TYPES and
+
+ Merge subsequent tokens of the same token-type.
+
+ 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.
"""
@@ -76,7 +78,7 @@ class DocutilsInterface(object):
def __init__(self, code, language):
self.code = code
self.language = language
-
+
def lex(self):
# Get lexer for language (use text as fallback)
try:
@@ -85,8 +87,8 @@ class DocutilsInterface(object):
# info: "no pygments lexer for %s, using 'text'"%self.language
lexer = get_lexer_by_name('text')
return pygments.lex(self.code, lexer)
-
-
+
+
def join(self, tokens):
"""join subsequent tokens of same token-type
"""
@@ -107,7 +109,7 @@ class DocutilsInterface(object):
tokens = self.lex()
except IOError:
print "INFO: Pygments lexer not found, using fallback"
- # TODO: write message to INFO
+ # TODO: write message to INFO
yield ('', self.code)
return
@@ -127,7 +129,7 @@ def code_block_directive(name, arguments, options, content, lineno,
language = arguments[0]
# create a literal block element and set class argument
code_block = nodes.literal_block(classes=["code-block", language])
-
+
# parse content with pygments and add to code_block element
for cls, value in DocutilsInterface(u'\n'.join(content), language):
if cls in unstyled_tokens:
@@ -152,10 +154,10 @@ directives.register_directive('code-block', code_block_directive)
# .. _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::
@@ -173,6 +175,3 @@ if __name__ == '__main__':
# publish_cmdline(writer_name='html', description=description)
# publish_cmdline(writer_name='latex', description=description)
# publish_cmdline(writer_name='newlatex2e', description=description)
-
-
-