Commit c9ec813b6660e3ef9e58bafbc5dcca5a44c90a4a

Andreas Rohner 2014-08-10T14:53:23

Extend Latex support This patch extends the support for Latex in the following way: * Support for various equation environments and for the verbatim environment * Certain functions like `\ref` or `\begin` always contain a reference or a keyword and they are highlighted as such. * Chapter and section headlines are written in bold to make them stand out more, because they structure the document. * Since `selector` and `string` have the same color in the default theme, the token type `function` is used to highlight Latex commands. * Add support for the `\url` command * Add alias support

diff --git a/components/prism-latex.js b/components/prism-latex.js
index c8902f6..5e0a0e6 100644
--- a/components/prism-latex.js
+++ b/components/prism-latex.js
@@ -1,6 +1,61 @@
-Prism.languages.latex = {
-	'comment': /%.*?(\r?\n|$)$/m,
-	'string': /(\$)(\\?.)*?\1/g,
-	'punctuation': /[{}]/g,
-	'selector': /\\[a-z;,:\.]*/i
-}
\ No newline at end of file
+(function(Prism) {
+	var funcPattern = /\\([^a-z()[\]]|[a-z\*]+)/gi,
+	    insideEqu = {
+		    'equation-command': {
+			    pattern: funcPattern,
+			    alias: 'regex'
+		    }
+	    };
+
+	Prism.languages.latex = {
+		'comment': /%.*?$/gm,
+		// the verbatim environment prints whitespace to the document
+		'cdata':  {
+			pattern: /(\\begin\{((?:verbatim|lstlisting)\*?)\})([\w\W]*?)(?=\\end\{\2\})/,
+			lookbehind: true
+		},
+		/*
+		 * equations can be between $ $ or \( \) or \[ \]
+		 * (all are multiline)
+		 */
+		'equation': [
+			{
+				pattern: /(\$(\\?[\w\W])*?\$|\\\((\\?[\w\W])*?\\\)|\\\[(\\?[\w\W])*?\\\])/g,
+				inside: insideEqu,
+				alias: 'string'
+			},
+			{
+				pattern: /(\\begin\{((?:equation|math|eqnarray|align|multline|gather)\*?)\})([\w\W]*?)(?=\\end\{\2\})/,
+				lookbehind: true,
+				inside: insideEqu,
+				alias: 'string'
+			}
+		],
+		/*
+		 * arguments which are keywords or references are highlighted
+		 * as keywords
+		 */
+		'keyword': {
+			pattern: /(\\(?:begin|end|ref|cite|label|usepackage|documentclass)(?:\[[^\]]+\])?\{)[^}]+(?=\}(?:\[[^\]]+\])?)/,
+			lookbehind: true
+		},
+		'url': {
+			pattern: /(\\url\{)[^}]+(?=\})/,
+			lookbehind: true
+		},
+		/*
+		 * section or chapter headlines are highlighted as bold so that
+		 * they stand out more
+		 */
+		'headline': {
+			pattern: /(\\(?:part|chapter|section|subsection|frametitle|subsubsection|paragraph|subparagraph|subsubparagraph|subsubsubparagraph)\*?(?:\[[^\]]+\])?\{)[^}]+(?=\}(?:\[[^\]]+\])?)/,
+			lookbehind: true,
+			alias: 'class-name'
+		},
+		'function': {
+			pattern: funcPattern,
+			alias: 'selector'
+		},
+		'punctuation': /[[\]{}&]/g,
+	};
+})(Prism);
diff --git a/components/prism-latex.min.js b/components/prism-latex.min.js
index 29f9024..2699cd2 100644
--- a/components/prism-latex.min.js
+++ b/components/prism-latex.min.js
@@ -1 +1 @@
-Prism.languages.latex={comment:/%.*?(\r?\n|$)$/m,string:/(\$)(\\?.)*?\1/g,punctuation:/[{}]/g,selector:/\\[a-z;,:\.]*/i};
\ No newline at end of file
+!function(a){var e=/\\([^a-z()[\]]|[a-z\*]+)/gi,n={"equation-command":{pattern:e,alias:"regex"}};a.languages.latex={comment:/%.*?$/gm,cdata:{pattern:/(\\begin\{((?:verbatim|lstlisting)\*?)\})([\w\W]*?)(?=\\end\{\2\})/,lookbehind:!0},equation:[{pattern:/(\$(\\?[\w\W])*?\$|\\\((\\?[\w\W])*?\\\)|\\\[(\\?[\w\W])*?\\\])/g,inside:n,alias:"string"},{pattern:/(\\begin\{((?:equation|math|eqnarray|align|multline|gather)\*?)\})([\w\W]*?)(?=\\end\{\2\})/,lookbehind:!0,inside:n,alias:"string"}],keyword:{pattern:/(\\(?:begin|end|ref|cite|label|usepackage|documentclass)(?:\[[^\]]+\])?\{)[^}]+(?=\}(?:\[[^\]]+\])?)/,lookbehind:!0},url:{pattern:/(\\url\{)[^}]+(?=\})/,lookbehind:!0},headline:{pattern:/(\\(?:part|chapter|section|subsection|frametitle|subsubsection|paragraph|subparagraph|subsubparagraph|subsubsubparagraph)\*?(?:\[[^\]]+\])?\{)[^}]+(?=\}(?:\[[^\]]+\])?)/,lookbehind:!0,alias:"class-name"},"function":{pattern:e,alias:"selector"},punctuation:/[[\]{}&]/g}}(Prism);
\ No newline at end of file