Skip to content

Instantly share code, notes, and snippets.

@jneen
Last active June 14, 2019 02:40
Show Gist options
  • Save jneen/b938c1b34c2ec57bd7477cae8c8fdac5 to your computer and use it in GitHub Desktop.
Save jneen/b938c1b34c2ec57bd7477cae8c8fdac5 to your computer and use it in GitHub Desktop.
\makeatletter
\newwrite\shellout@out
\def\shellout@writeout@start#1{%
\begingroup
\immediate\openout\shellout@out#1%
\let\do\@makeother\dospecials%
\catcode`\^^M\active%
\def\verbatim@processline{%
\immediate\write\shellout@out{\the\verbatim@line}}%
}%
\def\shellout@writeout@end{%
\immediate\closeout\shellout@out\endgroup%
}%
\newcounter{shellout@shell@id}
\counterwithin{shellout@shell@id}{paragraph}
\def\shellout@setup#1{%
\stepcounter{shellout@shell@id}%
\gdef\shellout@shell@cmd{#1}%
\gdef\shellout@shell@infile{\theshelloutcacheprefix.\theshellout@shell@id.shellin.tmp}%
\gdef\shellout@shell@outfile{\theshelloutcacheprefix.\theshellout@shell@id.shellout.tex}%
}
\def\shellout@shell@start#1{%
\shellout@setup{#1}%
\shellout@writeout@start{\shellout@shell@infile}%
}%
\def\shellout@shell@perform{%
\ifx\shellout@cachemode 0%
\immediate\write18{./bin/shellout \shellout@shell@infile'' \shellout@shell@outfile'' \shellout@shell@cmd''}%
\fi%
\immediate\input{\shellout@shell@outfile}%
}
\def\shellout@shell@end{%
\shellout@writeout@end%
\noindent\obeyspaces\shellout@shell@perform%
}
\def\shelloutone#1{%
\stepcounter{shellout@shell@id}%
\gdef\shellout@shell@cmd{#1}%
\gdef\shellout@shell@infile{/dev/null}%
\gdef\shellout@shell@outfile{\theshelloutcacheprefix.\theshellout@shell@id.shellout.tex}%
\shellout@shell@perform%
}
\def\shelloutcacheprefix#1{
\gdef\theshelloutcacheprefix{#1}
}
\shelloutcacheprefix{\jobname}
\def\shelloutcachemode{\let\shellout@cachemode1}%
\def\shelloutnocachemode{\let\shellout@cachemode0}%
\shelloutnocachemode%
\newenvironment{shellout}[1]{
\begingroup
\@bsphack%
\shellout@shell@start{#1}%
\verbatim
}{
\endverbatim%
\@esphack
\shellout@shell@end%
\endgroup
}
\gdef\rouge@bin{rougify}
\def\hilitestyle#1{
\shelloutone{\rouge@bin'' style --tex #1''}%
}
\def\hilite@defaultlang{text}
\def\hilitedefaultlang#1{%
\def\hilite@defaultlang{#1}%
}
\newenvironment{hilite}[1][\hilite@defaultlang]{%
\renewenvironment{RG*}{\hilite@block@start}{\hilite@block@end}%
\begingroup%
\gdef\rouge@lexer{#1}%
\@bsphack%
\shellout@shell@start{\rouge@bin'' -l \rouge@lexer'' --escape-with '<@' '@>' -f tex}%
\verbatim@start%
}{%
\@esphack%
\endgroup%
\noindent\shellout@shell@end%
}
\newcommand\hl[1][\hilite@defaultlang]{\gdef\thelexer{#1}\Collectverb{\hl@}}
\def\hl@#1{%
\shellout@setup{\rouge@bin'' -l \thelexer'' --escape-with '<@' '@>' -f tex}%
\immediate\openout\shellout@out\shellout@shell@infile%
\immediate\write\shellout@out{#1}%
\immediate\closeout\shellout@out%
\shellout@shell@perform%
}
\def\hiliteblockenv#1#2{
\def\hilite@block@start{#1}%
\def\hilite@block@end{#2}%
}
\def\hiliteinlineenv#1#2{
\def\hilite@inline@start{#1}%
\def\hilite@inline@end{#2}%
}
\makeatother
\hilitestyle{magritte}
\hilitedefaultlang{ruby}
\hiliteblockenv{\begin{custombox}\color{RG@fgcolor}\ttfamily}{\end{custombox}}
\hiliteinlineenv{\ttfamily}{\relax}
Code can be included in blocks:
\begin{hilite}
def hello
puts "a string\n"
end
\end{hilite}
with escaped content:
\begin{hilite}
def even?(x) x % 2 == 0; end
\end{hilite}
in other languages:
\begin{hilite}[python]
def is_even(n):
return n % 2 == 0
\end{hilite}
or even inline, like the \verb|\verb| command. Ruby methods are defined using \hl|def foo|,
javascript functions are defined with \hl[js]|function foo() { ... }|. Latex commands take the
form \hl[tex]|\def\commandname#1#2{body}|.
We can even escape back into TeX (the callout uses the \verb|--escape| flag):
\begin{hilite}
def fancy
puts _ <@ \hfill \textcolor{gray}{\emph{a fancy comment, rendered in beautiful LaTeX}} @>
end
\end{hilite}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment