Skip to content

Commit

Permalink
latex: code highlighting debugged
Browse files Browse the repository at this point in the history
Highlighted Sourcecode from Pygments requires the fancyvrb and color packages.
To get it working with the Sphinx LaTeX Parser some commands where necessary
which were commented out. And last but not least creating a lstlisting
environment is not a reasonable choice, considering that this package has the
same job as Pygments. Therefore the overwritten method depart_literal_block was
removed, that Sphinx' default will be used. The result is for some reasons
rather ugly, but at least it works now.

I also changed the code example to a Python function to show that highlighting
really works now.

FYI: For some reason I also needed to uncomment the `notice` environment
defintion. Building from master without the changes introduced here didn't work
at all. Might be something I did, though.
But maybe this is a hint that a lot more of the sphinx.sty needs to be
uncommented, but nobody noticed because it wasn't used until now.

Fixes jterrace#8

Signed-off-by: Erik Bernoth <[email protected]>
  • Loading branch information
erikbgithub committed Sep 30, 2013
1 parent 5d21b2d commit 99165e3
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 132 deletions.
4 changes: 3 additions & 1 deletion ch-typography/text.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ You can make block quotes:

You can make code blocks::

this is a code block
def hello(what="world"):
""" an example Python function """
return "hello {}".format(what)

Some convenience substitutions are defined in the epilog:

Expand Down
36 changes: 0 additions & 36 deletions extensions/latex_mods.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,42 +107,6 @@ def depart_row(self, node):
self.body.append('\\\\\n')
self.table.rowcount += 1

def depart_literal_block(self, node):
code = self.verbatim.rstrip('\n')
lang = self.hlsettingstack[-1][0]
linenos = code.count('\n') >= self.hlsettingstack[-1][1] - 1
highlight_args = node.get('highlight_args', {})
if 'language' in node:
# code-block directives
lang = node['language']
highlight_args['force'] = True
if 'linenos' in node:
linenos = node['linenos']
def warner(msg):
self.builder.warn(msg, (self.curfilestack[-1], node.line))
hlcode = self.highlighter.highlight_block(code, lang, warn=warner,
linenos=linenos, **highlight_args)
hlcode = hlcode.replace('\$', '$')
hlcode = hlcode.replace('\%', '%')
# workaround for Unicode issue
hlcode = hlcode.replace(u'€', u'@texteuro[]')
# must use original Verbatim environment and "tabular" environment
if self.table:
hlcode = hlcode.replace('\\begin{Verbatim}',
'\\begin{OriginalVerbatim}')
self.table.has_problematic = True
self.table.has_verbatim = True
# get consistent trailer
hlcode = hlcode.rstrip()[:-14] # strip \end{Verbatim}
hlcode = hlcode.rstrip() + '\n'
hlcode = '\n' + hlcode + '\\end{%sVerbatim}\n' % (self.table and 'Original' or '')
hlcode = hlcode.replace('Verbatim', 'lstlisting')
begin_bracket = hlcode.find('[')
end_bracket = hlcode.find(']')
hlcode = hlcode[:begin_bracket] + '[]' + hlcode[end_bracket+1:]
self.body.append(hlcode)
self.verbatim = None

def visit_figure(self, node):
ids = ''
for id in self.next_figure_ids:
Expand Down
190 changes: 95 additions & 95 deletions tex/sphinx.sty
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
%\RequirePackage{makeidx}
\RequirePackage{framed}
\RequirePackage{ifthen}
%\RequirePackage{color}
\RequirePackage{color}
% For highlighted code.
%\RequirePackage{fancyvrb}
\RequirePackage{fancyvrb}
% For table captions.
\RequirePackage{threeparttable}
% Handle footnotes in tables.
Expand All @@ -43,8 +43,8 @@
% \definecolor{OuterLinkColor}{rgb}{0.216,0.439,0.388}
% % Redefine these colors to something not white if you want to have colored
% % background and border for code examples.
% \definecolor{VerbatimColor}{rgb}{1,1,1}
% \definecolor{VerbatimBorderColor}{rgb}{1,1,1}
\definecolor{VerbatimColor}{rgb}{1,1,1}
\definecolor{VerbatimBorderColor}{rgb}{1,1,1}

% for PDF output, use colors and maximal compression
%\newif\ifsphinxpdfoutput\sphinxpdfoutputfalse
Expand Down Expand Up @@ -126,46 +126,46 @@

% % Redefine the Verbatim environment to allow border and background colors.
% % The original environment is still used for verbatims within tables.
% \let\OriginalVerbatim=\Verbatim
% \let\endOriginalVerbatim=\endVerbatim
\let\OriginalVerbatim=\Verbatim
\let\endOriginalVerbatim=\endVerbatim

% Play with vspace to be able to keep the indentation.
% \newlength\distancetoright
% \def\mycolorbox#1{%
% \setlength\distancetoright{\linewidth}%
% \advance\distancetoright -\@totalleftmargin %
% \fcolorbox{VerbatimBorderColor}{VerbatimColor}{%
% \begin{minipage}{\distancetoright}%
% #1
% \end{minipage}%
% }%
% }
% \def\FrameCommand{\mycolorbox}
%
% \renewcommand{\Verbatim}[1][1]{%
% % list starts new par, but we don't want it to be set apart vertically
% \bgroup\parskip=0pt%
% \smallskip%
% % The list environement is needed to control perfectly the vertical
% % space.
% \list{}{%
% \setlength\parskip{0pt}%
% \setlength\itemsep{0ex}%
% \setlength\topsep{0ex}%
% \setlength\partopsep{0pt}%
% \setlength\leftmargin{0pt}%
% }%
% \item\MakeFramed {\FrameRestore}%
% \small%
% \OriginalVerbatim[#1]%
% }
% \renewcommand{\endVerbatim}{%
% \endOriginalVerbatim%
% \endMakeFramed%
% \endlist%
% % close group to restore \parskip
% \egroup%
% }
\newlength\distancetoright
\def\mycolorbox#1{%
\setlength\distancetoright{\linewidth}%
\advance\distancetoright -\@totalleftmargin %
\fcolorbox{VerbatimBorderColor}{VerbatimColor}{%
\begin{minipage}{\distancetoright}%
#1
\end{minipage}%
}%
}
\def\FrameCommand{\mycolorbox}

\renewcommand{\Verbatim}[1][1]{%
% list starts new par, but we don't want it to be set apart vertically
\bgroup\parskip=0pt%
\smallskip%
% The list environement is needed to control perfectly the vertical
% space.
\list{}{%
\setlength\parskip{0pt}%
\setlength\itemsep{0ex}%
\setlength\topsep{0ex}%
\setlength\partopsep{0pt}%
\setlength\leftmargin{0pt}%
}%
\item\MakeFramed {\FrameRestore}%
\small%
\OriginalVerbatim[#1]%
}
\renewcommand{\endVerbatim}{%
\endOriginalVerbatim%
\endMakeFramed%
\endlist%
% close group to restore \parskip
\egroup%
}


% \moduleauthor{name}{email}
Expand Down Expand Up @@ -227,63 +227,63 @@
% }

% Notices / Admonitions
%
% \newlength{\py@noticelength}
%
% \newcommand{\py@heavybox}{
% \setlength{\fboxrule}{1pt}
% \setlength{\fboxsep}{6pt}
% \setlength{\py@noticelength}{\linewidth}
% \addtolength{\py@noticelength}{-2\fboxsep}
% \addtolength{\py@noticelength}{-2\fboxrule}
% %\setlength{\shadowsize}{3pt}
% \Sbox
% \minipage{\py@noticelength}
% }
% \newcommand{\py@endheavybox}{
% \endminipage
% \endSbox
% \fbox{\TheSbox}
% }
%
% \newcommand{\py@lightbox}{{%
% \setlength\parskip{0pt}\par
% \rule[0ex]{\linewidth}{0.5pt}%
% \par\vspace{-0.5ex}%
% }}
% \newcommand{\py@endlightbox}{{%
% \setlength{\parskip}{0pt}%
% \par\rule[0.5ex]{\linewidth}{0.5pt}%
% \par\vspace{-0.5ex}%
% }}

\newlength{\py@noticelength}

\newcommand{\py@heavybox}{
\setlength{\fboxrule}{1pt}
\setlength{\fboxsep}{6pt}
\setlength{\py@noticelength}{\linewidth}
\addtolength{\py@noticelength}{-2\fboxsep}
\addtolength{\py@noticelength}{-2\fboxrule}
%\setlength{\shadowsize}{3pt}
\Sbox
\minipage{\py@noticelength}
}
\newcommand{\py@endheavybox}{
\endminipage
\endSbox
\fbox{\TheSbox}
}

\newcommand{\py@lightbox}{{%
\setlength\parskip{0pt}\par
\rule[0ex]{\linewidth}{0.5pt}%
\par\vspace{-0.5ex}%
}}
\newcommand{\py@endlightbox}{{%
\setlength{\parskip}{0pt}%
\par\rule[0.5ex]{\linewidth}{0.5pt}%
\par\vspace{-0.5ex}%
}}

% Some are quite plain:
% \newcommand{\py@noticestart@note}{\py@lightbox}
% \newcommand{\py@noticeend@note}{\py@endlightbox}
% \newcommand{\py@noticestart@hint}{\py@lightbox}
% \newcommand{\py@noticeend@hint}{\py@endlightbox}
% \newcommand{\py@noticestart@important}{\py@lightbox}
% \newcommand{\py@noticeend@important}{\py@endlightbox}
% \newcommand{\py@noticestart@tip}{\py@lightbox}
% \newcommand{\py@noticeend@tip}{\py@endlightbox}
\newcommand{\py@noticestart@note}{\py@lightbox}
\newcommand{\py@noticeend@note}{\py@endlightbox}
\newcommand{\py@noticestart@hint}{\py@lightbox}
\newcommand{\py@noticeend@hint}{\py@endlightbox}
\newcommand{\py@noticestart@important}{\py@lightbox}
\newcommand{\py@noticeend@important}{\py@endlightbox}
\newcommand{\py@noticestart@tip}{\py@lightbox}
\newcommand{\py@noticeend@tip}{\py@endlightbox}

% Others gets more visible distinction:
% \newcommand{\py@noticestart@warning}{\py@heavybox}
% \newcommand{\py@noticeend@warning}{\py@endheavybox}
% \newcommand{\py@noticestart@caution}{\py@heavybox}
% \newcommand{\py@noticeend@caution}{\py@endheavybox}
% \newcommand{\py@noticestart@attention}{\py@heavybox}
% \newcommand{\py@noticeend@attention}{\py@endheavybox}
% \newcommand{\py@noticestart@danger}{\py@heavybox}
% \newcommand{\py@noticeend@danger}{\py@endheavybox}
% \newcommand{\py@noticestart@error}{\py@heavybox}
% \newcommand{\py@noticeend@error}{\py@endheavybox}

% \newenvironment{notice}[2]{
% \def\py@noticetype{#1}
% \csname py@noticestart@#1\endcsname
% \strong{#2}
% }{\csname py@noticeend@\py@noticetype\endcsname}
\newcommand{\py@noticestart@warning}{\py@heavybox}
\newcommand{\py@noticeend@warning}{\py@endheavybox}
\newcommand{\py@noticestart@caution}{\py@heavybox}
\newcommand{\py@noticeend@caution}{\py@endheavybox}
\newcommand{\py@noticestart@attention}{\py@heavybox}
\newcommand{\py@noticeend@attention}{\py@endheavybox}
\newcommand{\py@noticestart@danger}{\py@heavybox}
\newcommand{\py@noticeend@danger}{\py@endheavybox}
\newcommand{\py@noticestart@error}{\py@heavybox}
\newcommand{\py@noticeend@error}{\py@endheavybox}

\newenvironment{notice}[2]{
\def\py@noticetype{#1}
\csname py@noticestart@#1\endcsname
\strong{#2}
}{\csname py@noticeend@\py@noticetype\endcsname}

% Allow the release number to be specified independently of the
% \date{}. This allows the date to reflect the document's date and
Expand Down

0 comments on commit 99165e3

Please sign in to comment.