summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormilde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2012-09-14 14:27:12 +0000
committermilde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2012-09-14 14:27:12 +0000
commit1f84f50d0ac04b64d8f7b061199b6e521fef3b3a (patch)
tree2a92b846819b392682e86690a25351bc65617d21
parentfa7f004a62410c9118bc5925b5a95e73a136cf8b (diff)
downloaddocutils-1f84f50d0ac04b64d8f7b061199b6e521fef3b3a.tar.gz
Change default of "syntax highlight" option to "long",
Add basic syntax highlight styles for LaTeX and HTML. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@7514 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
-rw-r--r--HISTORY.txt2
-rw-r--r--docs/ref/rst/directives.txt8
-rw-r--r--docs/user/config.txt2
-rw-r--r--docutils/parsers/rst/__init__.py2
-rw-r--r--docutils/parsers/rst/roles.py5
-rw-r--r--docutils/writers/html4css1/html4css1.css17
-rw-r--r--docutils/writers/latex2e/__init__.py9
-rw-r--r--test/functional/expected/standalone_rst_html4css1.html13
-rw-r--r--test/functional/expected/standalone_rst_latex.tex18
-rw-r--r--test/functional/expected/standalone_rst_pseudoxml.txt25
-rw-r--r--test/functional/expected/standalone_rst_xetex.tex18
-rw-r--r--test/functional/input/data/standard.txt16
-rw-r--r--test/test_parsers/test_rst/test_directives/test_code.py34
-rwxr-xr-xtest/test_parsers/test_rst/test_directives/test_include.py12
-rwxr-xr-xtest/test_parsers/test_rst/test_interpreted.py28
15 files changed, 108 insertions, 101 deletions
diff --git a/HISTORY.txt b/HISTORY.txt
index dec8f1ee1..e5399db43 100644
--- a/HISTORY.txt
+++ b/HISTORY.txt
@@ -25,6 +25,8 @@ Changes Since 0.9.1
- Fix [3559988] and [3560841] __import__ local writer, reader, languages
and parsers for Python 2.7 up.
- Fix import of PIL.Image.
+ - Change default of "syntax highlight" option to "long",
+ basic syntax highlight styles for LaTeX and HTML.
* docutils/io.py
diff --git a/docs/ref/rst/directives.txt b/docs/ref/rst/directives.txt
index 0f042bd1d..71cfe6d09 100644
--- a/docs/ref/rst/directives.txt
+++ b/docs/ref/rst/directives.txt
@@ -479,13 +479,13 @@ Code
The "code" directive constructs a literal block. If the code language is
specified, the content is parsed by the Pygments_ syntax highlighter and
tokens are stored in nested `inline elements`_ with class arguments
-according to their syntactic category. The actual highlighting depends on a
-style-sheet (e.g. one `generated by Pygments`__).
+according to their syntactic category. The actual highlighting can be
+customized with a style-sheet (e.g. one `generated by Pygments`__).
The parsing can be turned off with the syntax_highlight_ configuration
setting and command line option or by specifying the language as `:class:`_
-option (leaveing the directive argument empty). This also avoids warnings
-when Pygments_ is not installed or the language is not amongst the
+option instead of directive argument. This also avoids warnings
+when Pygments_ is not installed or the language is not in the
`supported languages and markup formats`_.
__ http://pygments.org/docs/cmdline/#generating-styles
diff --git a/docs/user/config.txt b/docs/user/config.txt
index 0a8fdbfb7..170fca0dd 100644
--- a/docs/user/config.txt
+++ b/docs/user/config.txt
@@ -600,7 +600,7 @@ _`syntax_highlight`
No code parsing. Use this to avoid the "Pygments not
found" warning when Pygments is not installed.
- Default: "short". Option: ``--syntax-highlight``.
+ Default: "long". Option: ``--syntax-highlight``.
.. _Pygments: http://pygments.org/
.. _code: ../ref/rst/directives.html#code
diff --git a/docutils/parsers/rst/__init__.py b/docutils/parsers/rst/__init__.py
index 4caf75f76..1e52609f6 100644
--- a/docutils/parsers/rst/__init__.py
+++ b/docutils/parsers/rst/__init__.py
@@ -136,7 +136,7 @@ class Parser(docutils.parsers.Parser):
'"long", "short", or "none (no parsing)". Default is "short".',
['--syntax-highlight'],
{'choices': ['long', 'short', 'none'],
- 'default': 'short', 'metavar': '<format>'}),))
+ 'default': 'long', 'metavar': '<format>'}),))
config_section = 'restructuredtext parser'
config_section_dependencies = ('parsers',)
diff --git a/docutils/parsers/rst/roles.py b/docutils/parsers/rst/roles.py
index 73ec479e6..750fe33cc 100644
--- a/docutils/parsers/rst/roles.py
+++ b/docutils/parsers/rst/roles.py
@@ -320,11 +320,10 @@ def code_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
set_classes(options)
language = options.get('language', '')
classes = ['code']
- if language:
- classes.append(language)
if 'classes' in options:
classes.extend(options['classes'])
-
+ if language and language not in classes:
+ classes.append(language)
try:
tokens = Lexer(utils.unescape(text, 1), language,
inliner.document.settings.syntax_highlight)
diff --git a/docutils/writers/html4css1/html4css1.css b/docutils/writers/html4css1/html4css1.css
index bb82b3b38..cf522b9d2 100644
--- a/docutils/writers/html4css1/html4css1.css
+++ b/docutils/writers/html4css1/html4css1.css
@@ -68,7 +68,7 @@ div.tip p.admonition-title {
div.attention p.admonition-title, div.caution p.admonition-title,
div.danger p.admonition-title, div.error p.admonition-title,
-div.warning p.admonition-title {
+div.warning p.admonition-title, .code .error {
color: red ;
font-weight: bold ;
font-family: sans-serif }
@@ -244,13 +244,14 @@ pre.literal-block, pre.doctest-block, pre.math, pre.code {
margin-left: 2em ;
margin-right: 2em }
-pre.code .ln { /* line numbers */
- color: grey;
-}
-
-.code {
- background-color: #eeeeee
-}
+pre.code .ln { color: grey; } /* line numbers */
+pre.code, code { background-color: #eeeeee }
+pre.code .comment, code .comment { color: #5C6576 }
+pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
+pre.code .literal.string, code .literal.string { color: #0C5404 }
+pre.code .name.builtin, code .name.builtin { color: #352B84 }
+pre.code .deleted, code .deleted { background-color: #DEB0A1}
+pre.code .inserted, code .inserted { background-color: #A3D289}
span.classifier {
font-family: sans-serif ;
diff --git a/docutils/writers/latex2e/__init__.py b/docutils/writers/latex2e/__init__.py
index 3e793e783..4c9af1325 100644
--- a/docutils/writers/latex2e/__init__.py
+++ b/docutils/writers/latex2e/__init__.py
@@ -545,6 +545,12 @@ PreambleCmds.graphicx_auto = r"""% Check output format
\usepackage[pdftex]{graphicx}
\fi'))"""
+PreambleCmds.highlight_rules = r"""% basic code highlight:
+\providecommand*\DUrolecomment[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}}
+\providecommand*\DUroledeleted[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}}
+\providecommand*\DUrolekeyword[1]{\textbf{#1}}
+\providecommand*\DUrolestring[1]{\textit{#1}}"""
+
PreambleCmds.inline = r"""
% inline markup (custom roles)
% \DUrole{#1}{#2} tries \DUrole#1{#2}
@@ -2370,6 +2376,9 @@ class LaTeXTranslator(nodes.NodeVisitor):
def visit_literal(self, node):
self.literal = True
+ if 'code' in node.get('classes', []):
+ self.requirements['color'] = PreambleCmds.color
+ self.requirements['code'] = PreambleCmds.highlight_rules
self.out.append('\\texttt{')
if node['classes']:
self.visit_inline(node)
diff --git a/test/functional/expected/standalone_rst_html4css1.html b/test/functional/expected/standalone_rst_html4css1.html
index 3c4c8233d..9e08674cc 100644
--- a/test/functional/expected/standalone_rst_html4css1.html
+++ b/test/functional/expected/standalone_rst_html4css1.html
@@ -858,14 +858,13 @@ numbers:</p>
<span class="ln">10 </span> print i
</pre>
<p>For inline code snippets, there is the <cite>code</cite> role, which can be used
-directly (the code will not be parsed/tagged, as the language is not
-known) or as base for special code roles, e.g.,</p>
-<!-- -->
-<blockquote>
-Docutils uses LaTeX syntax for math directives and roles:
-<code class="latex tex">\alpha = f(x)</code> prints <span class="math">
+directly (the code will not be parsed/tagged, as the language is not known)
+or as base for special code roles, e.g. the LaTeX code in the next
+paragraph.</p>
+<p>Docutils uses LaTeX syntax for math directives and roles:
+<code class="tex">\alpha = f(x)</code> prints <span class="math">
\(\alpha = f(x)\)</span>
-.</blockquote>
+.</p>
<p>The <tt class="docutils literal">:code:</tt> option of the <cite>include</cite> directive sets the included content
as a code block, here the rst file <tt class="docutils literal">header_footer.txt</tt> with line numbers:</p>
<pre class="code rst literal-block">
diff --git a/test/functional/expected/standalone_rst_latex.tex b/test/functional/expected/standalone_rst_latex.tex
index 4a217eec5..015aebb2a 100644
--- a/test/functional/expected/standalone_rst_latex.tex
+++ b/test/functional/expected/standalone_rst_latex.tex
@@ -10,6 +10,11 @@
% Prevent side-effects if French hyphenation patterns are not loaded:
\frenchbsetup{StandardLayout}
\AtBeginDocument{\selectlanguage{english}\noextrasfrench}
+% basic code highlight:
+\providecommand*\DUrolecomment[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}}
+\providecommand*\DUroledeleted[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}}
+\providecommand*\DUrolekeyword[1]{\textbf{#1}}
+\providecommand*\DUrolestring[1]{\textit{#1}}
\usepackage{color}
\usepackage{float} % float configuration
\floatplacement{figure}{H} % place figures here definitely
@@ -1276,17 +1281,12 @@ numbers:
\end{quote}
For inline code snippets, there is the \DUroletitlereference{code} role, which can be used
-directly (the code will not be parsed/tagged, as the language is not
-known) or as base for special code roles, e.g.,
-
-%
-%
-\begin{quote}
+directly (the code will not be parsed/tagged, as the language is not known)
+or as base for special code roles, e.g. the LaTeX code in the next
+paragraph.
Docutils uses LaTeX syntax for math directives and roles:
-\texttt{\DUrole{code}{\DUrole{latex}{\DUrole{tex}{\textbackslash{}alpha = f(x)}}}} prints $\alpha = f(x)$.
-
-\end{quote}
+\texttt{\DUrole{code}{\DUrole{tex}{\textbackslash{}alpha = f(x)}}} prints $\alpha = f(x)$.
The \texttt{:code:} option of the \DUroletitlereference{include} directive sets the included content
as a code block, here the rst file \texttt{header\_footer.txt} with line numbers:
diff --git a/test/functional/expected/standalone_rst_pseudoxml.txt b/test/functional/expected/standalone_rst_pseudoxml.txt
index f571c34e0..5cbe5e404 100644
--- a/test/functional/expected/standalone_rst_pseudoxml.txt
+++ b/test/functional/expected/standalone_rst_pseudoxml.txt
@@ -1665,19 +1665,17 @@
<title_reference>
code
role, which can be used
- directly (the code will not be parsed/tagged, as the language is not
- known) or as base for special code roles, e.g.,
- <comment xml:space="preserve">
- <block_quote>
- <paragraph>
- Docutils uses LaTeX syntax for math directives and roles:
- <literal classes="code latex tex">
- \alpha = f(x)
- prints
- <math>
- \alpha = f(x)
- .
- <target ids="pygments" names="pygments" refuri="http://pygments.org/">
+ directly (the code will not be parsed/tagged, as the language is not known)
+ or as base for special code roles, e.g. the LaTeX code in the next
+ paragraph.
+ <paragraph>
+ Docutils uses LaTeX syntax for math directives and roles:
+ <literal classes="code tex">
+ \alpha = f(x)
+ prints
+ <math>
+ \alpha = f(x)
+ .
<paragraph>
The
<literal>
@@ -1697,6 +1695,7 @@
<inline classes="ln">
2
.. footer:: Document footer
+ <target ids="pygments" names="pygments" refuri="http://pygments.org/">
<section ids="substitution-definitions" names="substitution\ definitions">
<title auto="1" refid="id65">
<generated classes="sectnum">
diff --git a/test/functional/expected/standalone_rst_xetex.tex b/test/functional/expected/standalone_rst_xetex.tex
index 8adc77146..d3ab640f0 100644
--- a/test/functional/expected/standalone_rst_xetex.tex
+++ b/test/functional/expected/standalone_rst_xetex.tex
@@ -8,6 +8,11 @@
\usepackage{polyglossia}
\setdefaultlanguage{english}
\setotherlanguages{british,french,german}
+% basic code highlight:
+\providecommand*\DUrolecomment[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}}
+\providecommand*\DUroledeleted[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}}
+\providecommand*\DUrolekeyword[1]{\textbf{#1}}
+\providecommand*\DUrolestring[1]{\textit{#1}}
\usepackage{color}
\usepackage{float} % float configuration
\floatplacement{figure}{H} % place figures here definitely
@@ -1275,17 +1280,12 @@ numbers:
\end{quote}
For inline code snippets, there is the \DUroletitlereference{code} role, which can be used
-directly (the code will not be parsed/tagged, as the language is not
-known) or as base for special code roles, e.g.,
-
-%
-%
-\begin{quote}
+directly (the code will not be parsed/tagged, as the language is not known)
+or as base for special code roles, e.g. the LaTeX code in the next
+paragraph.
Docutils uses LaTeX syntax for math directives and roles:
-\texttt{\DUrole{code}{\DUrole{latex}{\DUrole{tex}{\textbackslash{}alpha = f(x)}}}} prints $\alpha = f(x)$.
-
-\end{quote}
+\texttt{\DUrole{code}{\DUrole{tex}{\textbackslash{}alpha = f(x)}}} prints $\alpha = f(x)$.
The \texttt{:code:} option of the \DUroletitlereference{include} directive sets the included content
as a code block, here the rst file \texttt{header\_footer.txt} with line numbers:
diff --git a/test/functional/input/data/standard.txt b/test/functional/input/data/standard.txt
index 162b70a19..ac8ce47b3 100644
--- a/test/functional/input/data/standard.txt
+++ b/test/functional/input/data/standard.txt
@@ -780,18 +780,15 @@ numbers:
print i
For inline code snippets, there is the `code` role, which can be used
-directly (the code will not be parsed/tagged, as the language is not
-known) or as base for special code roles, e.g.,
+directly (the code will not be parsed/tagged, as the language is not known)
+or as base for special code roles, e.g. the LaTeX code in the next
+paragraph.
.. role:: tex(code)
- :language: latex
+ :language: tex
-..
-
- Docutils uses LaTeX syntax for math directives and roles:
- :tex:`\alpha = f(x)` prints :math:`\alpha = f(x)`.
-
-.. _Pygments: http://pygments.org/
+Docutils uses LaTeX syntax for math directives and roles:
+:tex:`\alpha = f(x)` prints :math:`\alpha = f(x)`.
The ``:code:`` option of the `include` directive sets the included content
as a code block, here the rst file ``header_footer.txt`` with line numbers:
@@ -800,6 +797,7 @@ as a code block, here the rst file ``header_footer.txt`` with line numbers:
:code: rst
:number-lines:
+.. _Pygments: http://pygments.org/
Substitution Definitions
------------------------
diff --git a/test/test_parsers/test_rst/test_directives/test_code.py b/test/test_parsers/test_rst/test_directives/test_code.py
index 873d653ef..5e013d485 100644
--- a/test/test_parsers/test_rst/test_directives/test_code.py
+++ b/test/test_parsers/test_rst/test_directives/test_code.py
@@ -104,13 +104,13 @@ totest['code-parsing'] = [
<document source="test data">
<literal_block classes="code python testclass" xml:space="preserve">
\n\
- <inline classes="k">
+ <inline classes="keyword">
print
\n\
- <inline classes="s">
+ <inline classes="literal string">
'hello world'
\n\
- <inline classes="c">
+ <inline classes="comment">
# to stdout
"""],
["""\
@@ -131,22 +131,22 @@ totest['code-parsing'] = [
<literal_block classes="code python testclass" ids="my-function" names="my_function" xml:space="preserve">
<inline classes="ln">
7 \n\
- <inline classes="k">
+ <inline classes="keyword">
def
\n\
- <inline classes="nf">
+ <inline classes="name function">
my_function
- <inline classes="p">
+ <inline classes="punctuation">
():
\n\
<inline classes="ln">
8 \n\
\n\
- <inline classes="sd">
+ <inline classes="literal string doc">
\'\'\'Test the lexer.
<inline classes="ln">
9 \n\
- <inline classes="sd">
+ <inline classes="literal string doc">
\'\'\'
\n\
<inline classes="ln">
@@ -155,20 +155,20 @@ totest['code-parsing'] = [
<inline classes="ln">
11 \n\
\n\
- <inline classes="c">
+ <inline classes="comment">
# and now for something completely different
\n\
<inline classes="ln">
12 \n\
\n\
- <inline classes="k">
+ <inline classes="keyword">
print
\n\
- <inline classes="mi">
+ <inline classes="literal number integer">
8
- <inline classes="o">
+ <inline classes="operator">
/
- <inline classes="mi">
+ <inline classes="literal number integer">
2
"""],
["""\
@@ -181,15 +181,15 @@ totest['code-parsing'] = [
<document source="test data">
<literal_block classes="code latex testclass" xml:space="preserve">
hello \n\
- <inline classes="k">
+ <inline classes="keyword">
\\emph
- <inline classes="nb">
+ <inline classes="name builtin">
{
world
- <inline classes="nb">
+ <inline classes="name builtin">
}
\n\
- <inline classes="c">
+ <inline classes="comment">
% emphasize"""],
["""\
.. code:: rst
diff --git a/test/test_parsers/test_rst/test_directives/test_include.py b/test/test_parsers/test_rst/test_directives/test_include.py
index 5fce520d6..92f1ce307 100755
--- a/test/test_parsers/test_rst/test_directives/test_include.py
+++ b/test/test_parsers/test_rst/test_directives/test_include.py
@@ -913,15 +913,15 @@ Included code
<paragraph>
Included code
<literal_block classes="code rst" source="%s" xml:space="preserve">
- <inline classes="gh">
+ <inline classes="generic heading">
Inclusion 1
\n\
- <inline classes="gh">
+ <inline classes="generic heading">
-----------
\n\
\n\
This file is used by \n\
- <inline classes="s">
+ <inline classes="literal string">
``test_include.py``
.
""" % reldir(include1)],
@@ -939,12 +939,12 @@ Included code
<literal_block classes="code rst" source="%s" xml:space="preserve">
<inline classes="ln">
1 \n\
- <inline classes="gh">
+ <inline classes="generic heading">
Inclusion 1
\n\
<inline classes="ln">
2 \n\
- <inline classes="gh">
+ <inline classes="generic heading">
-----------
\n\
<inline classes="ln">
@@ -953,7 +953,7 @@ Included code
<inline classes="ln">
4 \n\
This file is used by \n\
- <inline classes="s">
+ <inline classes="literal string">
``test_include.py``
.
""" % reldir(include1)],
diff --git a/test/test_parsers/test_rst/test_interpreted.py b/test/test_parsers/test_rst/test_interpreted.py
index 7bdce8b94..cf7b9bbef 100755
--- a/test/test_parsers/test_rst/test_interpreted.py
+++ b/test/test_parsers/test_rst/test_interpreted.py
@@ -244,22 +244,22 @@ Custom role based on code role:
<document source="test data">
<paragraph>
Custom role based on code role:
- <literal classes="code latex tex">
- <inline classes="s">
+ <literal classes="code tex latex">
+ <inline classes="literal string">
$
- <inline classes="nb">
+ <inline classes="name builtin">
\x07lpha \n\
- <inline classes="o">
+ <inline classes="operator">
=
- <inline classes="nb">
+ <inline classes="name builtin">
f
- <inline classes="o">
+ <inline classes="operator">
(
- <inline classes="nb">
+ <inline classes="name builtin">
x
- <inline classes="o">
+ <inline classes="operator">
)
- <inline classes="s">
+ <inline classes="literal string">
$
.
"""],
@@ -278,14 +278,14 @@ Python code :python:`print("The end")`.
Custom role based on code role:
<paragraph>
Python code \n\
- <literal classes="code python testclass">
- <inline classes="k">
+ <literal classes="code testclass python">
+ <inline classes="keyword">
print
- <inline classes="p">
+ <inline classes="punctuation">
(
- <inline classes="s">
+ <inline classes="literal string">
"The end"
- <inline classes="p">
+ <inline classes="punctuation">
)
.
"""],