blob: cfecb3f980ad9bd36891592152b7a4907251ea1e [file] [log] [blame]
// Copyright (C) 2011 Martin S.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package prettify.lang;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import prettify.parser.Prettify;
/**
* This is similar to the lang-tex.js in JavaScript Prettify.
*
* All comments are adapted from the JavaScript Prettify.
*
* <p>
* Support for tex highlighting as discussed on
* <a href="http://meta.tex.stackexchange.com/questions/872/text-immediate-following-double-backslashes-is-highlighted-as-macro-inside-a-code/876#876">meta.tex.stackexchange.com</a>.
*
* @author Martin S.
*/
public class LangTex extends Lang {
public LangTex() {
List<List<Object>> _shortcutStylePatterns = new ArrayList<List<Object>>();
List<List<Object>> _fallthroughStylePatterns = new ArrayList<List<Object>>();
// whitespace
_shortcutStylePatterns.add(Arrays.asList(new Object[]{Prettify.PR_PLAIN, Pattern.compile("^[\\t\\n\\r \\xA0]+"), null, "\t\n\r " + Character.toString((char) 0xA0)}));
// all comments begin with '%'
_shortcutStylePatterns.add(Arrays.asList(new Object[]{Prettify.PR_COMMENT, Pattern.compile("^%[^\\r\\n]*"), null, "%"}));
//[PR['PR_DECLARATION'], /^\\([egx]?def|(new|renew|provide)(command|environment))\b/],
// any command starting with a \ and contains
// either only letters (a-z,A-Z), '@' (internal macros)
_fallthroughStylePatterns.add(Arrays.asList(new Object[]{Prettify.PR_KEYWORD, Pattern.compile("^\\\\[a-zA-Z@]+")}));
// or contains only one character
_fallthroughStylePatterns.add(Arrays.asList(new Object[]{Prettify.PR_KEYWORD, Pattern.compile("^\\\\.")}));
// Highlight dollar for math mode and ampersam for tabular
_fallthroughStylePatterns.add(Arrays.asList(new Object[]{Prettify.PR_TYPE, Pattern.compile("^[$&]")}));
// numeric measurement values with attached units
_fallthroughStylePatterns.add(Arrays.asList(new Object[]{Prettify.PR_LITERAL, Pattern.compile("[+-]?(?:\\.\\d+|\\d+(?:\\.\\d*)?)(cm|em|ex|in|pc|pt|bp|mm)", Pattern.CASE_INSENSITIVE)}));
// punctuation usually occurring within commands
_fallthroughStylePatterns.add(Arrays.asList(new Object[]{Prettify.PR_PUNCTUATION, Pattern.compile("^[{}()\\[\\]=]+")}));
setShortcutStylePatterns(_shortcutStylePatterns);
setFallthroughStylePatterns(_fallthroughStylePatterns);
}
public static List<String> getFileExtensions() {
return Arrays.asList(new String[]{"latex", "tex"});
}
}