Skip to content

Commit

Permalink
Merge branch 'pygments_highlighting'
Browse files Browse the repository at this point in the history
Conflicts:
	LICENSE.txt
	publisher/writer.py
  • Loading branch information
stefanv committed Feb 21, 2011
2 parents 7cb4c6a + 6e08d3e commit 92235d0
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 14 deletions.
26 changes: 14 additions & 12 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
Proceedings generator:

Copyright (c) 2010-2011 SciPy Developers.
All rights reserved.

The files code_block.py, rstmath.py and sphinx_highlight.py are
adapted from the Sphinx project and are

Math directive:
Copyright (c) 2007-2010 by the Sphinx team (see http://sphinx.pocoo.org).

Modifications to rstmath.py are

Copyright (c) 2007-2010 by the Sphinx team.
Copyright (c) 2010 Marcin Cieslik <mpc4p at virginia.edu>

All rights reserved.

All above listed source released under the following license:
The following BSD license holds for all the above code:

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

a. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
b. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Expand All @@ -31,4 +34,3 @@ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.

29 changes: 29 additions & 0 deletions papers/00_vanderwalt/00_vanderwalt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,35 @@ nulla. Donec faucibus purus leo. Nullam vel lorem eget enim blandit ultrices.
Ut urna lacus, scelerisque nec pellentesque quis, laoreet eu magna. Quisque ac
justo vitae odio tincidunt tempus at vitae tortor.

Of course, no paper would be complete without some source code. Without
highlighting, it would look like this::

def sum(a, b):
"""Sum two numbers."""

return a + b

With code-highlighting:

.. code-block:: python
def sum(a, b):
"""Sum two numbers."""
return a + b
Maybe also in another language, and with line numbers:

.. code-block:: c
:linenos:
int main() {
for (int i = 0; i < 10; i++) {
/* do something */
}
return 0;
}
Important Part
--------------

Expand Down
20 changes: 20 additions & 0 deletions publisher/build_paper.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,28 @@
\renewenvironment{longtable}{\begin{center}\begin{tabular}}%
{\end{tabular}\end{center}\vspace{2mm}}
}
% Packages required for code highlighting
\usepackage{fancyvrb}
'''

# Add the LaTeX commands required by Pygments to do syntax highlighting

try:
import pygments
except ImportError:
import warnings
warnings.warn(RuntimeWarning('Could not import Pygments. '
'Syntax highlighting will fail.'))
pygments = None

if pygments:
from pygments.formatters import LatexFormatter
from sphinx_highlight import SphinxStyle

preamble += LatexFormatter(style=SphinxStyle).get_style_defs()


settings = {'documentclass': 'IEEEtran',
'use_verbatim_when_possible': True,
'use_latex_citations': True,
Expand Down
29 changes: 29 additions & 0 deletions publisher/code_block.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# --- Code-block directive from Sphinx ---

from docutils import nodes
from docutils.parsers.rst import Directive, directives

class CodeBlock(Directive):
"""
Directive for a code block with special highlighting or line numbering
settings.
"""

has_content = True
required_arguments = 1
optional_arguments = 0
final_argument_whitespace = False
option_spec = {
'linenos': directives.flag,
}

def run(self):
code = u'\n'.join(self.content)
literal = nodes.literal_block(code, code)
literal['language'] = self.arguments[0]
literal['linenos'] = 'linenos' in self.options
return [literal]

directives.register_directive('code-block', CodeBlock)

# --- End code-block directive from Sphinx ---
19 changes: 19 additions & 0 deletions publisher/sphinx_highlight.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from pygments.style import Style
from pygments.styles.friendly import FriendlyStyle
from pygments.token import Generic, Comment, Number

class SphinxStyle(Style):
"""
Like friendly, but a bit darker to enhance contrast on the green
background.
"""

background_color = '#eeffcc'
default_style = ''

styles = FriendlyStyle.styles
styles.update({
Generic.Output: '#333',
Comment: 'italic #408090',
Number: '#208050',
})
25 changes: 23 additions & 2 deletions publisher/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
PreambleCmds)

from rstmath import mathEnv
import code_block

from options import options

Expand Down Expand Up @@ -175,6 +176,28 @@ def visit_thead(self, node):
def depart_thead(self, node):
LaTeXTranslator.depart_thead(self, node)

def visit_literal_block(self, node):
if 'language' in node.attributes:
# do highlighting
from pygments import highlight
from pygments.lexers import PythonLexer, get_lexer_by_name
from pygments.formatters import LatexFormatter

linenos = node.attributes.get('linenos', False)
lexer = get_lexer_by_name(node.attributes['language'])
tex = highlight(node.astext(), lexer,
LatexFormatter(linenos=linenos,
verboptions='fontsize=\\footnotesize'))

self.out.append(tex)
raise nodes.SkipNode
else:
LaTeXTranslator.visit_literal_block(self, node)

def depart_literal_block(self, node):
LaTeXTranslator.depart_literal_block(self, node)


# Math directives from rstex

def visit_InlineMath(self, node):
Expand All @@ -196,7 +219,5 @@ def visit_PartLaTeX(self, node):
raise nodes.SkipNode




writer = Writer()
writer.translator_class = Translator

0 comments on commit 92235d0

Please sign in to comment.