Update: I am still improving the code style. You can find the latest version here:
https://www.writelatex.com/74567mmxwkw - I appreciate anyone who is testing :)
If you want to get a copy of the source code, you can get it here: https://dl.dropboxusercontent.com/u/74217418/stackexchange/tex/html5_listings_sample.tex
After much trial and error and the help of people here from the community I have come to a very attractive result.
Screenshot:
The code is pretty intense, but it works. It would be nice if others could try it. Thus it is ensured that there are no errors. Unfortunately, all HTML elements and attributes, and CSS properties have to be entered manually. Maybe we can put together a list of all these items?
Together with the W3C specification it should be possible.
Here is the code:
\documentclass{scrreprt}
\usepackage{color}
\definecolor{editorGray}{rgb}{0.95, 0.95, 0.95}
\definecolor{editorOcher}{rgb}{1, 0.5, 0} % #FF7F00 -> rgb(239, 169, 0)
\definecolor{editorGreen}{rgb}{0, 0.5, 0} % #007C00 -> rgb(0, 124, 0)
\usepackage{upquote}
\usepackage{listings}
\lstdefinelanguage{JavaScript}{
morekeywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break},
morecomment=[s]{/*}{*/},
morecomment=[l]//,
morestring=[b]",
morestring=[b]'
}
\lstdefinelanguage{HTML5}{
language=html,
sensitive=true,
alsoletter={<>=-},
otherkeywords={
% HTML tags
<html>, <head>, <title>, </title>, <meta, />, </head>, <body>,
<canvas, \/canvas>, <script>, </script>, </body>, </html>, <!, html>, <style>, </style>, ><
},
ndkeywords={
% General
=,
% HTML attributes
charset=, id=, width=, height=,
% CSS properties
border:, transform:, -moz-transform:, transition-duration:, transition-property:, transition-timing-function:
},
morecomment=[s]{<!--}{-->},
tag=[s]
}
\lstset{%
% Basic design
backgroundcolor=\color{editorGray},
basicstyle={\small\ttfamily},
frame=l,
% Line numbers
xleftmargin={0.75cm},
numbers=left,
stepnumber=1,
firstnumber=1,
numberfirstline=true,
% Code design
keywordstyle=\color{blue}\bfseries,
commentstyle=\color{darkgray}\ttfamily,
ndkeywordstyle=\color{editorGreen}\bfseries,
stringstyle=\color{editorOcher},
% Code
language=HTML5,
alsolanguage=JavaScript,
alsodigit={.:;},
tabsize=2,
showtabs=false,
showspaces=false,
showstringspaces=false,
extendedchars=true,
breaklines=true,
% Support for German umlauts
literate=%
{Ö}{{\"O}}1
{Ä}{{\"A}}1
{Ü}{{\"U}}1
{ß}{{\ss}}1
{ü}{{\"u}}1
{ä}{{\"a}}1
{ö}{{\"o}}1
}
\begin{document}
\begin{lstlisting}
<!DOCTYPE html>
<html>
<head>
<title>Canvas</title>
<meta charset="UTF-8" />
<style>
#square {
border: 1px solid black;
transform: scale(10) rotate(3deg) translateX(0px);
-moz-transform: scale(10) rotate(3deg) translateX(0px);
}
.box {
transition-duration: 2s;
transition-property: transform;
transition-timing-function: linear;
}
</style>
</head>
<body>
<canvas id="square" width="200" height="200"></canvas>
<script>
var canvas = document.createElement('canvas');
canvas.width = 200;
canvas.height = 200;
var image = new Image();
image.src = 'images/card.png';
image.width = 114;
image.height = 158;
image.onload = window.setInterval(function() {
rotation();
}, 1000/60);
</script>
</body>
</html>
\end{lstlisting}
\end{document}
Use the listings
package. Unfortunately, there are no syntax highlighting patterns for Javascript in listings
.
Because most of the lines in your code are too wide for the page you should apply my solution for lstlisting line wrapping.
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\lstset{
basicstyle=\ttfamily,
columns=fullflexible,
breaklines=true,
postbreak=\raisebox{0ex}[0ex][0ex]{\color{red}$\hookrightarrow$\space}
}
\begin{document}
\begin{lstlisting}[frame=single]
var api = new ParseServer({
databaseURI: databaseUri || 'mongodb://localhost:27017/dev', // Endereço do banco de dados mongodb com a porta padrão
cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
appId: process.env.APP_ID || 'myAppId', //Identificação da aplicação
masterKey: process.env.MASTER_KEY || '', //Add your master key here$ //Chave secreta para o aplicativo se conectar ao servidor
serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse',$
//URL que vai estar disponível após o parse-server iniciar
liveQuery: {
classNames: ["Posts", "Comments"] // List of classes to support f$
}
\end{lstlisting}
\begin{lstlisting}[frame=single]
# HTTPS — proxy all requests to the Node app
server {
# Adiciona o HTTP/2
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name app.example.com;
}
\end{lstlisting}
\end{document}
Best Answer
Overleaf have a tutorial for code listing properly, like in this post.
Like in the example:
Also I recommend this post to enable easy copy code.