- code improvement: add function 'getVariableValueAsBoolean' in JavaScript style
diff --git a/src/prettify/parser/Prettify.java b/src/prettify/parser/Prettify.java index 622b2d8..e14ff14 100644 --- a/src/prettify/parser/Prettify.java +++ b/src/prettify/parser/Prettify.java
@@ -611,13 +611,13 @@ protected CreateSimpleLexer sourceDecorator(Map<String, Object> options) throws Exception { List<List<Object>> shortcutStylePatterns = new ArrayList<List<Object>>(); List<List<Object>> fallthroughStylePatterns = new ArrayList<List<Object>>(); - if (options.get("tripleQuotedStrings") != null) { + if (Util.getVariableValueAsBoolean(options.get("tripleQuotedStrings"))) { // '''multi-line-string''', 'single-line-string', and double-quoted shortcutStylePatterns.add(Arrays.asList(new Object[]{PR_STRING, Pattern.compile("^(?:\\'\\'\\'(?:[^\\'\\\\]|\\\\[\\s\\S]|\\'{1,2}(?=[^\\']))*(?:\\'\\'\\'|$)|\\\"\\\"\\\"(?:[^\\\"\\\\]|\\\\[\\s\\S]|\\\"{1,2}(?=[^\\\"]))*(?:\\\"\\\"\\\"|$)|\\'(?:[^\\\\\\']|\\\\[\\s\\S])*(?:\\'|$)|\\\"(?:[^\\\\\\\"]|\\\\[\\s\\S])*(?:\\\"|$))"), null, "'\""})); - } else if (options.get("multiLineStrings") != null) { + } else if (Util.getVariableValueAsBoolean(options.get("multiLineStrings"))) { // 'multi-line-string', "multi-line-string" shortcutStylePatterns.add(Arrays.asList(new Object[]{PR_STRING, Pattern.compile("^(?:\\'(?:[^\\\\\\']|\\\\[\\s\\S])*(?:\\'|$)|\\\"(?:[^\\\\\\\"]|\\\\[\\s\\S])*(?:\\\"|$)|\\`(?:[^\\\\\\`]|\\\\[\\s\\S])*(?:\\`|$))"), @@ -630,15 +630,15 @@ null, "\"'"})); } - if (options.get("verbatimStrings") != null) { + if (Util.getVariableValueAsBoolean(options.get("verbatimStrings"))) { // verbatim-string-literal production from the C# grammar. See issue 93. fallthroughStylePatterns.add(Arrays.asList(new Object[]{PR_STRING, Pattern.compile("^@\\\"(?:[^\\\"]|\\\"\\\")*(?:\\\"|$)"), null})); } Object hc = options.get("hashComments"); - if (hc != null) { - if (options.get("cStyleComments") != null) { + if (Util.getVariableValueAsBoolean(hc)) { + if (Util.getVariableValueAsBoolean(options.get("cStyleComments"))) { if ((hc instanceof Integer) && (Integer) hc > 1) { // multiline hash comments shortcutStylePatterns.add(Arrays.asList(new Object[]{PR_COMMENT, Pattern.compile("^#(?:##(?:[^#]|#(?!##))*(?:###|$)|.*)"), @@ -662,7 +662,7 @@ "#"})); } } - if (options.get("cStyleComments") != null) { + if (Util.getVariableValueAsBoolean(options.get("cStyleComments"))) { fallthroughStylePatterns.add(Arrays.asList(new Object[]{PR_COMMENT, Pattern.compile("^\\/\\/[^\r\n]*"), null})); @@ -672,7 +672,7 @@ null})); } Object regexLiterals = options.get("regexLiterals"); - if (regexLiterals != null) { + if (Util.getVariableValueAsBoolean(regexLiterals)) { /** * @const */ @@ -706,12 +706,13 @@ } Pattern types = (Pattern) options.get("types"); - if (types != null) { + if (Util.getVariableValueAsBoolean(types)) { fallthroughStylePatterns.add(Arrays.asList(new Object[]{PR_TYPE, types})); } - if (options.get("keywords") != null) { - String keywords = ((String) options.get("keywords")).replaceAll("^ | $", ""); + String keywords = (String) options.get("keywords"); + if (keywords != null) { + keywords = keywords.replaceAll("^ | $", ""); if (keywords.length() != 0) { fallthroughStylePatterns.add(Arrays.asList(new Object[]{PR_KEYWORD, Pattern.compile("^(?:" + keywords.replaceAll("[\\s,]+", "|") + ")\\b"), @@ -790,7 +791,7 @@ // when hc is truthy to include # in the run of punctuation characters // only when not followint [|&;<>]. String punctuation = "^.[^\\s\\w.$@'\"`/\\\\]*"; - if (options.get("regexLiterals") != null) { + if (Util.getVariableValueAsBoolean(options.get("regexLiterals"))) { punctuation += "(?!\\s*/)"; } fallthroughStylePatterns.add(Arrays.asList(new Object[]{PR_PUNCTUATION,
diff --git a/src/prettify/parser/Util.java b/src/prettify/parser/Util.java index 4613ef8..7147bd7 100644 --- a/src/prettify/parser/Util.java +++ b/src/prettify/parser/Util.java
@@ -32,6 +32,32 @@ } /** + * Treat a variable as an boolean in JavaScript style. Note this function can + * only handle string, integer and boolean currently. All other data type, if + * null, return false, not null return true. + * + * @param var the variable to get value from + * @return the boolean value + */ + public static Boolean getVariableValueAsBoolean(Object var) { + Boolean returnResult = null; + + if (var == null) { + returnResult = false; + } else if (var instanceof String) { + returnResult = !((String) var).isEmpty(); + } else if (var instanceof Integer) { + returnResult = ((Integer) var) != 0; + } else if (var instanceof Boolean) { + returnResult = (Boolean) var; + } else { + returnResult = true; + } + + return returnResult; + } + + /** * Treat a variable as an integer in JavaScript style. Note this function can * only handle integer and boolean currently. *