From e55cfe0bc1cbadb950f799f2db81196bf2389210 Mon Sep 17 00:00:00 2001 From: n1474335 Date: Thu, 25 Jan 2018 18:41:47 +0000 Subject: [PATCH] Fixed 'Syntax highlighter' operation. Using highlight.js instead of google-code-prettify. --- package-lock.json | 10 +++++----- package.json | 2 +- src/core/config/OperationConfig.js | 7 +------ src/core/config/modules/Code.js | 2 +- src/core/operations/Code.js | 21 +++++++++------------ src/web/stylesheets/index.js | 2 +- 6 files changed, 18 insertions(+), 26 deletions(-) diff --git a/package-lock.json b/package-lock.json index 873938b..b6e640a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4341,11 +4341,6 @@ "pinkie-promise": "2.0.1" } }, - "google-code-prettify": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/google-code-prettify/-/google-code-prettify-1.0.5.tgz", - "integrity": "sha1-n0d/Ik2/piNy5e+AOn4VdBBAAIQ=" - }, "graceful-fs": { "version": "4.1.11", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.11.tgz", @@ -4745,6 +4740,11 @@ "integrity": "sha1-k0EP0hsAlzUVH4howvJx80J+I/0=", "dev": true }, + "highlight.js": { + "version": "9.12.0", + "resolved": "https://registry.npmjs.org/highlight.js/-/highlight.js-9.12.0.tgz", + "integrity": "sha1-5tnb5Xy+/mB1HwKvM2GVhwyQwB4=" + }, "hmac-drbg": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/hmac-drbg/-/hmac-drbg-1.0.1.tgz", diff --git a/package.json b/package.json index 51ab4b6..47f13f0 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,7 @@ "esprima": "^4.0.0", "exif-parser": "^0.1.12", "file-saver": "^1.3.3", - "google-code-prettify": "^1.0.5", + "highlight.js": "^9.12.0", "jquery": "^3.2.1", "js-crc": "^0.2.0", "js-sha3": "^0.7.0", diff --git a/src/core/config/OperationConfig.js b/src/core/config/OperationConfig.js index dd83a15..42bb2f9 100755 --- a/src/core/config/OperationConfig.js +++ b/src/core/config/OperationConfig.js @@ -3462,15 +3462,10 @@ const OperationConfig = { outputType: "html", args: [ { - name: "Language/File extension", + name: "Language", type: "option", value: Code.LANGUAGES }, - { - name: "Display line numbers", - type: "boolean", - value: Code.LINE_NUMS - } ] }, "TCP/IP Checksum": { diff --git a/src/core/config/modules/Code.js b/src/core/config/modules/Code.js index c480724..72ddbe3 100644 --- a/src/core/config/modules/Code.js +++ b/src/core/config/modules/Code.js @@ -11,7 +11,7 @@ import Code from "../../operations/Code.js"; * - xmldom * - xpath * - jpath - * - googlecodeprettify + * - highlight.js * * @author n1474335 [n1474335@gmail.com] * @copyright Crown Copyright 2017 diff --git a/src/core/operations/Code.js b/src/core/operations/Code.js index d6904e0..42a9bbb 100755 --- a/src/core/operations/Code.js +++ b/src/core/operations/Code.js @@ -1,12 +1,10 @@ import {camelCase, kebabCase, snakeCase} from "lodash"; - -import Utils from "../Utils.js"; import vkbeautify from "vkbeautify"; import {DOMParser} from "xmldom"; import xpath from "xpath"; import jpath from "jsonpath"; import nwmatcher from "nwmatcher"; -import prettyPrintOne from "imports-loader?window=>global!exports-loader?prettyPrintOne!google-code-prettify/bin/prettify.min.js"; +import hljs from "highlight.js"; /** @@ -24,12 +22,7 @@ const Code = { * @constant * @default */ - LANGUAGES: ["default-code", "default-markup", "bash", "bsh", "c", "cc", "coffee", "cpp", "cs", "csh", "cv", "cxx", "cyc", "htm", "html", "in.tag", "java", "javascript", "js", "json", "m", "mxml", "perl", "pl", "pm", "py", "python", "rb", "rc", "rs", "ruby", "rust", "sh", "uq.val", "xhtml", "xml", "xsl"], - /** - * @constant - * @default - */ - LINE_NUMS: false, + LANGUAGES: ["auto detect"].concat(hljs.listLanguages()), /** * Syntax highlighter operation. @@ -39,9 +32,13 @@ const Code = { * @returns {html} */ runSyntaxHighlight: function(input, args) { - let language = args[0], - lineNums = args[1]; - return "" + prettyPrintOne(Utils.escapeHtml(input), language, lineNums) + ""; + const language = args[0]; + + if (language === "auto detect") { + return hljs.highlightAuto(input).value; + } + + return hljs.highlight(language, input, true).value; }, diff --git a/src/web/stylesheets/index.js b/src/web/stylesheets/index.js index 8e5bbc8..860538b 100644 --- a/src/web/stylesheets/index.js +++ b/src/web/stylesheets/index.js @@ -7,7 +7,7 @@ */ /* Libraries */ -import "google-code-prettify/src/prettify.css"; +import "highlight.js/styles/vs.css"; /* Frameworks */ import "./vendors/bootstrap.less";