2020-10-12 18:53:31 +02:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-10-16 09:40:10 +02:00
|
|
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
2020-10-12 18:53:31 +02:00
|
|
|
const css = require("css");
|
2020-10-16 09:40:10 +02:00
|
|
|
|
|
|
|
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
2020-10-12 18:53:31 +02:00
|
|
|
const prettier = require("prettier");
|
|
|
|
|
|
2020-10-16 09:40:10 +02:00
|
|
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
|
|
|
const camel = require("to-camel-case");
|
|
|
|
|
|
|
|
|
|
const path = require("path");
|
|
|
|
|
const fs = require("fs");
|
|
|
|
|
|
|
|
|
|
const licenseHeader = `/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/`;
|
|
|
|
|
|
2020-10-12 18:53:31 +02:00
|
|
|
function createJavascriptStyleSheet(directory, inputFile, outputFile) {
|
|
|
|
|
fs.readFile(path.join(__dirname, `${directory}/${inputFile}`), "utf-8", (err, data) => {
|
|
|
|
|
const javacriptStylesheet = css.parse(data).stylesheet.rules.reduce((sheet, rule) => {
|
|
|
|
|
if (rule.type === "rule") {
|
|
|
|
|
const style = rule.selectors.reduce((selectors, selector) => {
|
|
|
|
|
const selectorObject = rule.declarations.reduce((declarations, declaration) => {
|
|
|
|
|
if (declaration.type === "declaration" && declaration.property) {
|
|
|
|
|
const camelCaseDeclarationProp = camel(declaration.property);
|
|
|
|
|
const key =
|
|
|
|
|
camelCaseDeclarationProp.includes("moz") ||
|
|
|
|
|
camelCaseDeclarationProp.includes("webkit") ||
|
|
|
|
|
(camelCaseDeclarationProp[0] === "o" && !camelCaseDeclarationProp.includes("overflow"))
|
|
|
|
|
? `${camelCaseDeclarationProp.substring(0, 1).toUpperCase()}${camelCaseDeclarationProp.substring(1)}`
|
|
|
|
|
: camelCaseDeclarationProp;
|
2020-10-16 09:40:10 +02:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2020-10-12 18:53:31 +02:00
|
|
|
declarations[key] = declaration.value;
|
|
|
|
|
}
|
|
|
|
|
return declarations;
|
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
|
|
if (selector.substring(0, 6) === ".token") {
|
2020-10-16 09:40:10 +02:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2020-10-12 18:53:31 +02:00
|
|
|
selector = selector.substring(7);
|
|
|
|
|
|
|
|
|
|
// Regex to fix Prism theme selectors
|
|
|
|
|
// - Remove further `.token` classes
|
|
|
|
|
// - Remove the space (descendant combinator)
|
|
|
|
|
// to allow for styling multiple classes
|
|
|
|
|
// Ref: https://github.com/react-syntax-highlighter/react-syntax-highlighter/pull/305
|
2020-10-16 09:40:10 +02:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2020-10-12 18:53:31 +02:00
|
|
|
selector = selector.replace(/(?<=\w) (\.token)?(?=\.)/g, "");
|
|
|
|
|
}
|
2020-10-16 09:40:10 +02:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2020-10-12 18:53:31 +02:00
|
|
|
selectors[selector] = selectorObject;
|
|
|
|
|
return selectors;
|
|
|
|
|
}, {});
|
2020-10-16 09:40:10 +02:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2020-10-12 18:53:31 +02:00
|
|
|
sheet = Object.keys(style).reduce((stylesheet, selector) => {
|
|
|
|
|
if (stylesheet[selector]) {
|
2020-10-16 09:40:10 +02:00
|
|
|
// eslint-disable-next-line
|
2020-10-12 18:53:31 +02:00
|
|
|
stylesheet[selector] = { ...stylesheet[selector], ...style[selector] };
|
|
|
|
|
} else {
|
2020-10-16 09:40:10 +02:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2020-10-12 18:53:31 +02:00
|
|
|
stylesheet[selector] = style[selector];
|
|
|
|
|
}
|
|
|
|
|
return stylesheet;
|
|
|
|
|
}, sheet);
|
|
|
|
|
}
|
|
|
|
|
return sheet;
|
|
|
|
|
}, {});
|
|
|
|
|
fs.writeFile(
|
|
|
|
|
path.join(__dirname, directory, outputFile),
|
2020-10-16 09:40:10 +02:00
|
|
|
prettier.format(`${licenseHeader}
|
|
|
|
|
|
|
|
|
|
/* --- DO NOT EDIT --- */
|
2020-10-12 18:53:31 +02:00
|
|
|
/* Auto-generated from ${inputFile} */
|
|
|
|
|
|
|
|
|
|
export default ${JSON.stringify(javacriptStylesheet, null, 2)}`),
|
|
|
|
|
() => {}
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-23 13:06:36 +02:00
|
|
|
createJavascriptStyleSheet(path.join("..", "src"), "syntax-highlighting.module.css", "syntax-highlighting.ts");
|