From 99165e30994e32973bd6628c9cb515ae83dbba36 Mon Sep 17 00:00:00 2001 From: Erik Bernoth Date: Mon, 30 Sep 2013 14:41:28 +0200 Subject: [PATCH] latex: code highlighting debugged 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 #8 Signed-off-by: Erik Bernoth --- ch-typography/text.rst | 4 +- extensions/latex_mods.py | 36 -------- tex/sphinx.sty | 190 +++++++++++++++++++-------------------- 3 files changed, 98 insertions(+), 132 deletions(-) diff --git a/ch-typography/text.rst b/ch-typography/text.rst index c3020e1..7611d16 100644 --- a/ch-typography/text.rst +++ b/ch-typography/text.rst @@ -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: diff --git a/extensions/latex_mods.py b/extensions/latex_mods.py index c8e3081..94aa3af 100644 --- a/extensions/latex_mods.py +++ b/extensions/latex_mods.py @@ -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: diff --git a/tex/sphinx.sty b/tex/sphinx.sty index 46f9f88..70aa422 100644 --- a/tex/sphinx.sty +++ b/tex/sphinx.sty @@ -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. @@ -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 @@ -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} @@ -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