Skip to content

Instantly share code, notes, and snippets.

@rigwild
Created May 3, 2020 17:21
Show Gist options
  • Save rigwild/7f561f0c5cf27b611ba66337ab9b4e39 to your computer and use it in GitHub Desktop.
Save rigwild/7f561f0c5cf27b611ba66337ab9b4e39 to your computer and use it in GitHub Desktop.
Color ES6 JavaScript in LaTeX
% Code support
\usepackage{listings}
\usepackage{textcomp}
%
% ECMAScript 2015 (ES6) definition by Gary Hammock
%
\lstdefinelanguage[ECMAScript2015]{JavaScript}[]{JavaScript}{
morekeywords=[1]{await, async, case, catch, class, const, default, do,
enum, export, extends, finally, from, implements, import, instanceof,
let, static, super, switch, throw, try},
morestring=[b]` % Interpolation strings.
}
%
% JavaScript version 1.1 by Gary Hammock
%
% Reference:
% B. Eich and C. Rand Mckinney, "JavaScript Language Specification
% (Preliminary Draft)", JavaScript 1.1. 1996-11-18. [Online]
% http://hepunx.rl.ac.uk/~adye/jsspec11/titlepg2.htm
%
\lstdefinelanguage{JavaScript}{
morekeywords=[1]{break, continue, delete, else, for, function, if, in,
new, return, this, typeof, var, void, while, with},
% Literals, primitive types, and reference types.
morekeywords=[2]{false, null, true, boolean, number, undefined,
Array, Boolean, Date, Math, Number, String, Object},
% Built-ins.
morekeywords=[3]{eval, parseInt, parseFloat, escape, unescape},
sensitive,
morecomment=[s]{/*}{*/},
morecomment=[l]//,
morecomment=[s]{/**}{*/}, % JavaDoc style comments
morestring=[b]',
morestring=[b]"
}[keywords, comments, strings]
\lstalias[]{ES6}[ECMAScript2015]{JavaScript}
% Requires package: color.
\definecolor{mediumgray}{rgb}{0.3, 0.4, 0.4}
\definecolor{mediumblue}{rgb}{0.0, 0.0, 0.8}
\definecolor{forestgreen}{rgb}{0.13, 0.55, 0.13}
\definecolor{darkviolet}{rgb}{0.58, 0.0, 0.83}
\definecolor{royalblue}{rgb}{0.25, 0.41, 0.88}
\definecolor{crimson}{rgb}{0.86, 0.8, 0.24}
\lstdefinestyle{JSES6Base}{
backgroundcolor=\color{white},
basicstyle=\ttfamily,
breakatwhitespace=false,
breaklines=true`',
columns=fullflexible,
commentstyle=\color{mediumgray}\upshape,
emph={},
emphstyle=\color{crimson},
extendedchars=true, % requires inputenc
fontadjust=true,
frame=single,
identifierstyle=\color{black},
keepspaces=true,
keywordstyle=\color{mediumblue},
keywordstyle={[2]\color{darkviolet}},
keywordstyle={[3]\color{royalblue}},
numbers=left,
numbersep=5pt,
numberstyle=\tiny\color{black},
rulecolor=\color{black},
showlines=true,
showspaces=false,
showstringspaces=false,
showtabs=false,
stringstyle=\color{forestgreen},
tabsize=2,
upquote=true % requires textcomp
}
\lstdefinestyle{JavaScript}{
language=JavaScript,
style=JSES6Base
}
\lstdefinestyle{ES6}{
language=ES6,
style=JSES6Base
}
\lstdefinestyle{json}{
backgroundcolor=\color{white},
basicstyle=\ttfamily,
breakatwhitespace=false,
breaklines=true`',
columns=fullflexible,
comment=[l]{:},
commentstyle=\color{mediumgray}\upshape,
emph={},
emphstyle=\color{crimson},
extendedchars=true, % requires inputenc
fontadjust=true,
frame=single,
identifierstyle=\color{black},
keepspaces=true,
numbers=left,
numbersep=5pt,
numberstyle=\tiny\color{black},
rulecolor=\color{black},
showlines=true,
showspaces=false,
showstringspaces=false,
showtabs=false,
string=[s]{"}{"},
stringstyle=\color{royalblue},
tabsize=2,
upquote=true % requires textcomp
}
% this is how to use it
% output: https://user-images.githubusercontent.com/26366184/80920928-4107b100-8d73-11ea-9a63-6216be0e930d.png
\begin{figure}[hbt!]
\begin{lstlisting}[style=ES6]
import express from 'express'
import { promises as fs } from 'fs'
import path from 'path'
import { asyncMiddleware } from '../functions'
import { imagesPath } from '../../config'
const router = express.Router()
/**
* Get the list of all files in the images directory
*
* @returns {String[]} The list of files
* @throws The directory does not exist or is not accessible
*/
export const getSceneList = () => fs.readdir(imagesPath).catch(() => {
throw new Error(`Can't access the "${path.basename(imagesPath)}" directory. Check it exists and you have read permission on it.`)
})
// Actual API route definition
router.get('/listScenes', asyncMiddleware(async (req, res) =>
res.json({ data: await getSceneList() })))
export default router
\end{lstlisting}
\caption{Code d'une route d'API \emph{/api/listScenes} (Fichier : \emph{/server/routes/listScenes.js})}
\label{fig:code-route-api}
\end{figure}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment