Add txt language Test: bazelisk build //:gitiles && tools/run_dev.sh Bug: NA Change-Id: I5ae500fabb68a9dacc2cad7287c86babb69d6e28
diff --git a/BUILD b/BUILD index 848e651..3879c4a 100644 --- a/BUILD +++ b/BUILD
@@ -3,6 +3,7 @@ srcs = glob(["src/**/*.java"]), resources = [ "src/prettify/example/example.html", + "src/prettify/example/text.txt", ], visibility = ["//visibility:public"], )
diff --git a/src/prettify/PrettifyParser.java b/src/prettify/PrettifyParser.java index c02bc71..0cae2e2 100644 --- a/src/prettify/PrettifyParser.java +++ b/src/prettify/PrettifyParser.java
@@ -26,6 +26,7 @@ prettify = new Prettify(); } + // Note to gitiles developers: This is gitiles entry point into java-prettifier lib. @Override public List<ParseResult> parse(String fileExtension, String content) { Job job = new Job(0, content);
diff --git a/src/prettify/example/Example.java b/src/prettify/example/Example.java index 45d4e93..579077b 100644 --- a/src/prettify/example/Example.java +++ b/src/prettify/example/Example.java
@@ -16,6 +16,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; import java.util.Arrays; import java.util.logging.Level; import java.util.logging.Logger; @@ -99,9 +100,17 @@ highlighter.setFirstLine(10); // set to highlight line 13, 27, 28, 38, 42, 43 and 53 highlighter.setHighlightedLineList(Arrays.asList(13, 27, 28, 38, 42, 43, 53)); + + String resourcePath = "/prettify/example/example.html"; + if (args.length > 0 && args[0].equals("txt")) { + resourcePath = "/prettify/example/text.txt"; + highlighter.setFirstLine(0); + highlighter.setHighlightedLineList(new ArrayList<>()); + } + try { // set the content of the script, the example.html is located in the jar: /prettify/example/example.html - highlighter.setContent(new String(readResourceFile("/prettify/example/example.html"))); + highlighter.setContent(new String(readResourceFile(resourcePath)), resourcePath); } catch (IOException ex) { LOG.log(Level.SEVERE, null, ex); }
diff --git a/src/prettify/example/text.txt b/src/prettify/example/text.txt new file mode 100644 index 0000000..36c50a6 --- /dev/null +++ b/src/prettify/example/text.txt
@@ -0,0 +1,11 @@ +noindentation + oneindentation + twoindentation + threeidentation + TABindentation +1start_with_a_number +A line with a ' unclosed character, like an apostrophe. +A line with 1 as a number. +A line with a ' which is closed here'. +A line with a " which is unclosed. +A line with a " which is closed here".
diff --git a/src/prettify/lang/LangText.java b/src/prettify/lang/LangText.java new file mode 100644 index 0000000..a60f02a --- /dev/null +++ b/src/prettify/lang/LangText.java
@@ -0,0 +1,25 @@ +package prettify.lang; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Pattern; +import prettify.parser.Prettify; + +public class LangText extends Lang { + + public LangText() { + List<List<Object>> _shortcutStylePatterns = new ArrayList<List<Object>>(); + List<List<Object>> _fallthroughStylePatterns = new ArrayList<List<Object>>(); + + _fallthroughStylePatterns.add(Arrays.asList(new Object[]{Prettify.PR_PLAIN, Pattern.compile(".", Pattern.CASE_INSENSITIVE)})); + + setShortcutStylePatterns(_shortcutStylePatterns); + setFallthroughStylePatterns(_fallthroughStylePatterns); + } + + public static List<String> getFileExtensions() { + return Arrays.asList(new String[]{"txt", "TXT"}); + } + +}
diff --git a/src/prettify/parser/Prettify.java b/src/prettify/parser/Prettify.java index 95c1e08..82b02a6 100644 --- a/src/prettify/parser/Prettify.java +++ b/src/prettify/parser/Prettify.java
@@ -45,6 +45,7 @@ import prettify.lang.LangSql; import prettify.lang.LangTcl; import prettify.lang.LangTex; +import prettify.lang.LangText; import prettify.lang.LangVb; import prettify.lang.LangVhdl; import prettify.lang.LangWiki; @@ -392,6 +393,7 @@ register(LangWiki.class); register(LangXq.class); register(LangYaml.class); + register(LangText.class); } catch (Exception ex) { LOG.log(Level.SEVERE, null, ex); }
diff --git a/src/syntaxhighlight/SyntaxHighlighter.java b/src/syntaxhighlight/SyntaxHighlighter.java index e948acc..0f651c6 100644 --- a/src/syntaxhighlight/SyntaxHighlighter.java +++ b/src/syntaxhighlight/SyntaxHighlighter.java
@@ -57,6 +57,13 @@ protected String content; /** + * The type of this.content, empty string if unknown. + */ + private String extension; + + private static final String UNKNOWN_EXTENSION = ""; + + /** * Constructor. * @param parser the parser to use * @param theme the theme for the syntax highlighter @@ -105,7 +112,7 @@ if (content != null) { // stop the change listener on the row header to speed up rendering highlighterRowHeader.setListenToDocumentUpdate(false); - highlighter.setStyle(parser.parse(null, content)); + highlighter.setStyle(parser.parse(extension, content)); // resume the change listener on the row header highlighterRowHeader.setListenToDocumentUpdate(true); // notify the row header to update its information related to the SyntaxHighlighterPane @@ -246,7 +253,11 @@ * @throws IOException error occurred when reading the file */ public void setContent(File file) throws IOException { - setContent(readFile(file)); + setContent(readFile(file), getExtension(file.getAbsolutePath())); + } + + public void setContent(String content) { + setContent(content, UNKNOWN_EXTENSION); } /** @@ -254,12 +265,21 @@ * settings first and set this the last. * @param content the content to set, null means no content */ - public void setContent(String content) { + public void setContent(String content, String filename) { this.content = content; + this.extension = getExtension(filename); highlighter.setContent(content); render(); } + private static String getExtension(String path) { + int extensionIndex = path.lastIndexOf("."); + if (extensionIndex != -1) { + return path.substring(extensionIndex + 1); + } + return ""; + } + /** * Get the string content of a file. * @param file the file to retrieve the content from