Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81459 / js2html / highlight.js
diff options
context:
space:
mode:
Commitaeb0d4f887a0c18c36b59acc7b5df08e62020c50 (patch)
Author
 Dimitar Trendafilov <dimitar@coherent-labs.com>
2020-10-11 21:32:48 +0300
Committer
 Dimitar Trendafilov <dimitar@coherent-labs.com>
2020-10-11 21:33:38 +0300
Filesb6abdc6e1810326b587f2a160f39570f1f568f27 /homework/81459/js2html/highlight.js
IPL-aeb0d4f887a0c18c36b59acc7b5df08e62020c50.tar
IPL-aeb0d4f887a0c18c36b59acc7b5df08e62020c50.tar.gz
IPL-aeb0d4f887a0c18c36b59acc7b5df08e62020c50.zip
Parent154b7cd30f21af1554ca26986ff1643e28857727 (diff)
Removed old homeworks
1 files changed, 0 insertions, 103 deletions
-rw-r--r--homework/81459/js2html/highlight.js103
diff --git a/homework/81459/js2html/highlight.js b/homework/81459/js2html/highlight.js
deleted file mode 100644
index 947ae5f..0000000
--- a/homework/81459/js2html/highlight.js
+++ /dev/null
@@ -1,103 +0,0 @@
-(() => {
- const inputFile = process.argv[2];
- const outputFile = process.argv[3];
-
- const fs = require('fs');
-
- fs.readFile(inputFile, 'utf8', (err, contents) => {
- if (err) {
- throw err;
- }
-
- const html = generateHtml(contents);
-
- if (outputFile) {
- fs.writeFile(outputFile, html, 'utf8', (err) => {
- if (err) {
- throw err;
- }
- });
- } else {
- console.log(html);
- }
- });
-})();
-
-const tokens = [
- {type: 'KEYWORD', className: 'orange', regex: /^(break|case|catch|class|const|continue|debugger|default|delete|do|else|export|extends|finally|for|function|if|import|in|instanceof|new|of|return|super|switch|this|throw|try|typeof|var|void|while|with|yield)\b/},
- {type: 'WHITESPACE', className: '', regex: /^\s+/},
- {type: 'BRACKET', className: 'white', regex: /^[\[\]{}()]/},
- {type: 'PUNCTUATION', className: 'white', regex: /^[:;,.]/},
- {type: 'COMMENT', className: 'grey', regex: /^\/\/.*/},
- {type: 'OPERATOR', className: 'red', regex: /^(\+|-|\/|={1,3}|!={0,2}|&{1,2}|\|{1,2}|\*|~|\^)/},
- {type: 'BOOLEAN_LITERAL', className: 'purple', regex: /^(true|false)\b/},
- {type: 'NUMBER_LITERAL', className: 'blue', regex: /^-?\d+(?:\.\d+)?(?:e[+\-]?\d+)?/i},
- {type: 'STRING_LITERAL', className: 'green', regex: /^("(?:\\.|[^"])*"|'(?:\\.|[^'])*')/},
- {type: 'NULL', className: 'orange', regex: /^(null|undefined)\b/},
- {type: 'IDENTIFIER', className: 'white', regex: /^\w+/},
- {type: "UNKNOWN", className: 'white', regex: /^./}
-];
-
-function generateHtml(code) {
- let highlitedTokens = [];
-
- while (code && code.length) {
- for (const token of tokens) {
- if (token.regex.test(code)) {
- const match = token.regex.exec(code);
- highlitedTokens.push(highlightToken(match[0], token));
- code = code.substring(match[0].length);
- break;
- }
- }
- }
-
- return `${getHtmlHeader()}${highlitedTokens.join('')}${getHtmlFooter()}`
-}
-
-function highlightToken(token, tokenType) {
- return `<span class="${tokenType.className}">${token}</span>`
-}
-
-const getHtmlHeader = () => `<!doctype html>
-<html>
- <head>
- <style>
- .orange {
- color: orange;
- }
- .white {
- color: white;
- }
- .grey {
- color: grey;
- }
- .green {
- color: green;
- }
- .purple {
- color: purple;
- }
- .red {
- color: red;
- }
- .blue {
- color: cyan;
- }
- .gold {
- color: gold;
- }
- .background {
- background: #2d2d2d;
- }
- </style>
- </head>
- <body class="background">
- <pre>
-`;
-
-const getHtmlFooter = () => `
- </pre>
- </body>
-</html>
-`;