code-battles 1.6.4 → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +2234 -2004
- package/dist/cjs/styles.css +2 -1
- package/dist/cjs/types/components/DataTable.d.ts +21 -0
- package/dist/cjs/types/components/ResultStaticsTable.d.ts +3 -0
- package/dist/cjs/types/configuration.d.ts +2 -2
- package/dist/cjs/types/utilities.d.ts +2 -2
- package/dist/code_battles/__init__.py +1 -1
- package/dist/code_battles/battles.py +69 -28
- package/dist/code_battles/utilities.py +12 -3
- package/dist/esm/index.js +2235 -2005
- package/dist/esm/styles.css +2 -1
- package/dist/esm/types/components/DataTable.d.ts +21 -0
- package/dist/esm/types/components/ResultStaticsTable.d.ts +3 -0
- package/dist/esm/types/configuration.d.ts +2 -2
- package/dist/esm/types/utilities.d.ts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/pyscript/codemirror-eKulCBrd.js +2 -0
- package/dist/pyscript/codemirror-eKulCBrd.js.map +1 -0
- package/dist/pyscript/codemirror_commands-BVu_0Pnj.js +2 -0
- package/dist/pyscript/codemirror_commands-BVu_0Pnj.js.map +1 -0
- package/dist/pyscript/codemirror_lang-python-a2LxSo5G.js +2 -0
- package/dist/pyscript/codemirror_lang-python-a2LxSo5G.js.map +1 -0
- package/dist/pyscript/codemirror_language-_2pXlKJ9.js +2 -0
- package/dist/pyscript/codemirror_language-_2pXlKJ9.js.map +1 -0
- package/dist/pyscript/codemirror_view-DMW9kQlJ.js +2 -0
- package/dist/pyscript/codemirror_view-DMW9kQlJ.js.map +1 -0
- package/dist/pyscript/core-DezwbZpG.js +4 -0
- package/dist/pyscript/core-DezwbZpG.js.map +1 -0
- package/dist/pyscript/core.js +1 -1
- package/dist/pyscript/deprecations-manager-B1bDno27.js +2 -0
- package/dist/pyscript/deprecations-manager-B1bDno27.js.map +1 -0
- package/dist/pyscript/donkey-DNl0ASkM.js +2 -0
- package/dist/pyscript/donkey-DNl0ASkM.js.map +1 -0
- package/dist/pyscript/error-DmRE4Y-7.js +2 -0
- package/dist/pyscript/error-DmRE4Y-7.js.map +1 -0
- package/dist/pyscript/index-DkgvWIf-.js +2 -0
- package/dist/pyscript/index-DkgvWIf-.js.map +1 -0
- package/dist/pyscript/mpy-BoY0tVRD.js +2 -0
- package/dist/pyscript/mpy-BoY0tVRD.js.map +1 -0
- package/dist/pyscript/py-DsQqgZuM.js +2 -0
- package/dist/pyscript/py-DsQqgZuM.js.map +1 -0
- package/dist/pyscript/py-editor-DZkG_or6.js +2 -0
- package/dist/pyscript/py-editor-DZkG_or6.js.map +1 -0
- package/dist/pyscript/py-game-D0Z4YnzZ.js +2 -0
- package/dist/pyscript/py-game-D0Z4YnzZ.js.map +1 -0
- package/dist/pyscript/py-terminal-DYHK7LJ0.js +2 -0
- package/dist/pyscript/py-terminal-DYHK7LJ0.js.map +1 -0
- package/dist/pyscript/service-worker.js +1 -0
- package/dist/pyscript/storage.js +1 -1
- package/dist/pyscript/storage.js.map +1 -1
- package/dist/pyscript/xterm-CVv2fC4I.js +2 -0
- package/dist/pyscript/xterm-CVv2fC4I.js.map +1 -0
- package/dist/pyscript/zip-D13-hp9W.js +2 -0
- package/dist/pyscript/zip-D13-hp9W.js.map +1 -0
- package/package.json +14 -14
package/dist/esm/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import '@mantine/core/styles.css';
|
|
|
2
2
|
import '@mantine/dates/styles.css';
|
|
3
3
|
import '@mantine/notifications/styles.css';
|
|
4
4
|
import '@mantine/dropzone/styles.css';
|
|
5
|
-
import { TextInput, Button, Alert, Slider, Box, Select, NumberInput, Table, Badge, Autocomplete, Loader, MantineProvider } from '@mantine/core';
|
|
5
|
+
import { TextInput, Button, Alert, Slider, Box, Select, NumberInput, Table, Badge, Autocomplete, useMantineTheme, ActionIcon, UnstyledButton, Checkbox, Loader, MantineProvider } from '@mantine/core';
|
|
6
6
|
import { TimeInput, DatesProvider } from '@mantine/dates';
|
|
7
7
|
import { useColorScheme, useViewportSize } from '@mantine/hooks';
|
|
8
8
|
import { notifications, Notifications } from '@mantine/notifications';
|
|
@@ -372,10 +372,10 @@ const updatePointModifier = () => {
|
|
|
372
372
|
const pointModifier = {};
|
|
373
373
|
for (const round of rounds) {
|
|
374
374
|
if (results[round.players.join(", ")] &&
|
|
375
|
-
results[round.players.join(", ")][round.
|
|
376
|
-
for (const result of results[round.players.join(", ")][round.
|
|
377
|
-
const first = round.players[result[0]];
|
|
378
|
-
const second = round.players[result[1]];
|
|
375
|
+
results[round.players.join(", ")][JSON.stringify(round.parameters)]) {
|
|
376
|
+
for (const result of results[round.players.join(", ")][JSON.stringify(round.parameters)]) {
|
|
377
|
+
const first = round.players[result.places[0]];
|
|
378
|
+
const second = round.players[result.places[1]];
|
|
379
379
|
if (!pointModifier[first]) {
|
|
380
380
|
pointModifier[first] = 0;
|
|
381
381
|
}
|
|
@@ -401,11 +401,11 @@ const toPlacing = (n) => {
|
|
|
401
401
|
}
|
|
402
402
|
return n.toString() + DIGITS[n % 10];
|
|
403
403
|
};
|
|
404
|
-
const runNoUI = (
|
|
404
|
+
const runNoUI = (parameters, apis, playerBots, seed, verbose) => {
|
|
405
405
|
const players = playerBots.map((api) => (api === "None" ? "" : apis[api]));
|
|
406
406
|
tryUntilSuccess(() =>
|
|
407
407
|
// @ts-ignore
|
|
408
|
-
window._startSimulation(
|
|
408
|
+
window._startSimulation(parameters, players, playerBots, true, false, verbose, seed));
|
|
409
409
|
};
|
|
410
410
|
const tryUntilSuccess = (f, timeout = 500) => {
|
|
411
411
|
try {
|
|
@@ -884,9 +884,9 @@ function requireCheckPropTypes () {
|
|
|
884
884
|
var printWarning = function() {};
|
|
885
885
|
|
|
886
886
|
if (process.env.NODE_ENV !== 'production') {
|
|
887
|
-
var ReactPropTypesSecret = requireReactPropTypesSecret();
|
|
887
|
+
var ReactPropTypesSecret = /*@__PURE__*/ requireReactPropTypesSecret();
|
|
888
888
|
var loggedTypeFailures = {};
|
|
889
|
-
var has = requireHas();
|
|
889
|
+
var has = /*@__PURE__*/ requireHas();
|
|
890
890
|
|
|
891
891
|
printWarning = function(text) {
|
|
892
892
|
var message = 'Warning: ' + text;
|
|
@@ -995,9 +995,9 @@ function requireFactoryWithTypeCheckers () {
|
|
|
995
995
|
var ReactIs = requireReactIs();
|
|
996
996
|
var assign = requireObjectAssign();
|
|
997
997
|
|
|
998
|
-
var ReactPropTypesSecret = requireReactPropTypesSecret();
|
|
999
|
-
var has = requireHas();
|
|
1000
|
-
var checkPropTypes = requireCheckPropTypes();
|
|
998
|
+
var ReactPropTypesSecret = /*@__PURE__*/ requireReactPropTypesSecret();
|
|
999
|
+
var has = /*@__PURE__*/ requireHas();
|
|
1000
|
+
var checkPropTypes = /*@__PURE__*/ requireCheckPropTypes();
|
|
1001
1001
|
|
|
1002
1002
|
var printWarning = function() {};
|
|
1003
1003
|
|
|
@@ -1610,7 +1610,7 @@ function requireFactoryWithThrowingShims () {
|
|
|
1610
1610
|
if (hasRequiredFactoryWithThrowingShims) return factoryWithThrowingShims;
|
|
1611
1611
|
hasRequiredFactoryWithThrowingShims = 1;
|
|
1612
1612
|
|
|
1613
|
-
var ReactPropTypesSecret = requireReactPropTypesSecret();
|
|
1613
|
+
var ReactPropTypesSecret = /*@__PURE__*/ requireReactPropTypesSecret();
|
|
1614
1614
|
|
|
1615
1615
|
function emptyFunction() {}
|
|
1616
1616
|
function emptyFunctionWithReset() {}
|
|
@@ -1674,20 +1674,27 @@ function requireFactoryWithThrowingShims () {
|
|
|
1674
1674
|
* LICENSE file in the root directory of this source tree.
|
|
1675
1675
|
*/
|
|
1676
1676
|
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1677
|
+
var hasRequiredPropTypes;
|
|
1678
|
+
|
|
1679
|
+
function requirePropTypes () {
|
|
1680
|
+
if (hasRequiredPropTypes) return propTypes.exports;
|
|
1681
|
+
hasRequiredPropTypes = 1;
|
|
1682
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
1683
|
+
var ReactIs = requireReactIs();
|
|
1684
|
+
|
|
1685
|
+
// By explicitly using `prop-types` you are opting into new development behavior.
|
|
1686
|
+
// http://fb.me/prop-types-in-prod
|
|
1687
|
+
var throwOnDirectAccess = true;
|
|
1688
|
+
propTypes.exports = /*@__PURE__*/ requireFactoryWithTypeCheckers()(ReactIs.isElement, throwOnDirectAccess);
|
|
1689
|
+
} else {
|
|
1690
|
+
// By explicitly using `prop-types` you are opting into new production behavior.
|
|
1691
|
+
// http://fb.me/prop-types-in-prod
|
|
1692
|
+
propTypes.exports = /*@__PURE__*/ requireFactoryWithThrowingShims()();
|
|
1693
|
+
}
|
|
1694
|
+
return propTypes.exports;
|
|
1688
1695
|
}
|
|
1689
1696
|
|
|
1690
|
-
var propTypesExports =
|
|
1697
|
+
var propTypesExports = /*@__PURE__*/ requirePropTypes();
|
|
1691
1698
|
|
|
1692
1699
|
function _classCallCheck(instance, Constructor) {
|
|
1693
1700
|
if (!(instance instanceof Constructor)) {
|
|
@@ -2464,7 +2471,7 @@ const PickBotBlock = () => {
|
|
|
2464
2471
|
|
|
2465
2472
|
const BotSelector = ({ playerCount, setPlayerCount, playerBots, setPlayerBots, apis, }) => {
|
|
2466
2473
|
return (React.createElement(React.Fragment, null,
|
|
2467
|
-
React.createElement(NumberInput, {
|
|
2474
|
+
React.createElement(NumberInput, { label: "Player Count", value: playerCount, min: 1, onChange: (c) => {
|
|
2468
2475
|
if (typeof c === "number") {
|
|
2469
2476
|
setPlayerCount(c);
|
|
2470
2477
|
while (playerBots.length < c) {
|
|
@@ -2487,9 +2494,9 @@ const BotSelector = ({ playerCount, setPlayerCount, playerBots, setPlayerBots, a
|
|
|
2487
2494
|
const RunSimulationBlock = () => {
|
|
2488
2495
|
const [apis, loading] = useAPIs();
|
|
2489
2496
|
const configuration = useConfiguration();
|
|
2490
|
-
const [
|
|
2491
|
-
key: "
|
|
2492
|
-
defaultValue:
|
|
2497
|
+
const [parameters, setParameters] = useLocalStorage({
|
|
2498
|
+
key: "Parameters",
|
|
2499
|
+
defaultValue: {},
|
|
2493
2500
|
});
|
|
2494
2501
|
const [playerCount, setPlayerCount] = useLocalStorage({
|
|
2495
2502
|
key: "Player Count",
|
|
@@ -2510,19 +2517,30 @@ const RunSimulationBlock = () => {
|
|
|
2510
2517
|
if (Object.keys(runningNoUIN).length === 1) {
|
|
2511
2518
|
remaining = Math.max(...Object.values(runningNoUIN));
|
|
2512
2519
|
}
|
|
2520
|
+
const getFullParameters = () => {
|
|
2521
|
+
var _a;
|
|
2522
|
+
const result = {};
|
|
2523
|
+
for (const key in configuration.parameters) {
|
|
2524
|
+
result[key] = (_a = parameters[key]) !== null && _a !== void 0 ? _a : configuration.parameters[key][0];
|
|
2525
|
+
}
|
|
2526
|
+
return result;
|
|
2527
|
+
};
|
|
2513
2528
|
const run = () => {
|
|
2514
2529
|
// @ts-ignore
|
|
2515
2530
|
window._isSimulationFromFile = false;
|
|
2516
|
-
|
|
2531
|
+
const params = getFullParameters();
|
|
2532
|
+
navigate(`/simulation/${playerBots.map(encodeURIComponent).join(",")}?seed=${seed === "-" ? "" : seed}&${Object.keys(params)
|
|
2533
|
+
.map((p) => `${p}=${encodeURIComponent(params[p])}`)
|
|
2534
|
+
.join("&")}`);
|
|
2517
2535
|
};
|
|
2518
2536
|
const startRunNoUI = () => {
|
|
2519
2537
|
setRunningNoUI(true);
|
|
2520
|
-
runNoUI(
|
|
2538
|
+
runNoUI(getFullParameters(), apis, playerBots, seed.toString(), true);
|
|
2521
2539
|
};
|
|
2522
2540
|
const startRunNoUIN = (n) => {
|
|
2523
2541
|
setLocalStorage("Results", {});
|
|
2524
2542
|
setRunningNoUIN({ [n.toString()]: n });
|
|
2525
|
-
runNoUI(
|
|
2543
|
+
runNoUI(getFullParameters(), apis, playerBots, seed.toString(), false);
|
|
2526
2544
|
};
|
|
2527
2545
|
useEffect(() => {
|
|
2528
2546
|
// @ts-ignore
|
|
@@ -2551,10 +2569,10 @@ const RunSimulationBlock = () => {
|
|
|
2551
2569
|
for (const key in runningNoUIN) {
|
|
2552
2570
|
if (runningNoUIN[key] == 0) {
|
|
2553
2571
|
const results = getLocalStorage("Results");
|
|
2554
|
-
const currentResults = results[playerBots.join(", ")][
|
|
2572
|
+
const currentResults = results[playerBots.join(", ")][JSON.stringify(getFullParameters())];
|
|
2555
2573
|
const winCounts = {};
|
|
2556
2574
|
for (const result of currentResults) {
|
|
2557
|
-
const winner = playerBots[result[0]];
|
|
2575
|
+
const winner = playerBots[result.places[0]];
|
|
2558
2576
|
if (!winCounts[winner]) {
|
|
2559
2577
|
winCounts[winner] = 1;
|
|
2560
2578
|
}
|
|
@@ -2576,16 +2594,18 @@ const RunSimulationBlock = () => {
|
|
|
2576
2594
|
setLocalStorage("Results", {});
|
|
2577
2595
|
}
|
|
2578
2596
|
else {
|
|
2579
|
-
runNoUI(
|
|
2597
|
+
runNoUI(getFullParameters(), apis, playerBots, seed.toString(), false);
|
|
2580
2598
|
}
|
|
2581
2599
|
}
|
|
2582
2600
|
}, [runningNoUIN]);
|
|
2583
2601
|
return (React.createElement(Block, { title: "Run Simulation", logo: "fa-solid fa-display" },
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2602
|
+
Object.keys(configuration.parameters)
|
|
2603
|
+
.sort()
|
|
2604
|
+
.map((parameter, parameterIndex) => {
|
|
2605
|
+
var _a, _b;
|
|
2606
|
+
const icon = ((_a = configuration.parameterIcons) !== null && _a !== void 0 ? _a : {})[parameter];
|
|
2607
|
+
return (React.createElement(Select, { mb: "xs", key: parameterIndex, leftSection: icon ? React.createElement("i", { className: icon }) : undefined, label: parameter[0].toUpperCase() + parameter.slice(1), data: configuration.parameters[parameter], value: (_b = parameters[parameter]) !== null && _b !== void 0 ? _b : configuration.parameters[parameter][0], onChange: (s) => setParameters(Object.assign(Object.assign({}, parameters), { [parameter]: s !== null && s !== void 0 ? s : configuration.parameters[parameter][0] })) }));
|
|
2608
|
+
}),
|
|
2589
2609
|
React.createElement(BotSelector, { playerCount: playerCount, setPlayerCount: setPlayerCount, playerBots: playerBots, setPlayerBots: setPlayerBots, apis: apis }),
|
|
2590
2610
|
React.createElement(NumberInput, { mt: "xs", leftSection: React.createElement("i", { className: "fa-solid fa-dice" }), label: "Randomness Seed", min: 0, value: seed, onChange: setSeed }),
|
|
2591
2611
|
React.createElement(Button.Group, { mt: "xs" },
|
|
@@ -2773,7 +2793,156 @@ const NotFoundPage = () => {
|
|
|
2773
2793
|
React.createElement("p", null, "Make sure the URL is correct.")));
|
|
2774
2794
|
};
|
|
2775
2795
|
|
|
2776
|
-
|
|
2796
|
+
const DataTable = ({ tableName, data, renderValue, renderHead, disableSort, hideIfEmpty, selectable, selectedRows, setSelectedRows, defaultSort, defaultReversed, stickyFirstColumn, }) => {
|
|
2797
|
+
var _a, _b;
|
|
2798
|
+
const [sortByColumn, setSortByColumn] = useState(defaultSort !== null && defaultSort !== void 0 ? defaultSort : 0);
|
|
2799
|
+
const [reversed, setReversed] = useState(defaultReversed !== null && defaultReversed !== void 0 ? defaultReversed : false);
|
|
2800
|
+
const [search, setSearch] = useState("");
|
|
2801
|
+
const theme = useMantineTheme();
|
|
2802
|
+
const colorScheme = useColorScheme();
|
|
2803
|
+
if (hideIfEmpty && data.body.length === 0) {
|
|
2804
|
+
return React.createElement(React.Fragment, null);
|
|
2805
|
+
}
|
|
2806
|
+
const reverseValue = reversed ? -1 : 1;
|
|
2807
|
+
const id = (_, __, value) => value;
|
|
2808
|
+
renderValue = renderValue !== null && renderValue !== void 0 ? renderValue : id;
|
|
2809
|
+
const headId = (value) => value;
|
|
2810
|
+
renderHead = renderHead !== null && renderHead !== void 0 ? renderHead : headId;
|
|
2811
|
+
const bodyIndices = [];
|
|
2812
|
+
for (let i = 0; i < data.body.length; i++) {
|
|
2813
|
+
bodyIndices.push(i);
|
|
2814
|
+
}
|
|
2815
|
+
const sortedIndices = bodyIndices
|
|
2816
|
+
.filter((i) => data.body[i].some((x, j) => {
|
|
2817
|
+
var _a;
|
|
2818
|
+
return (x === null || x === void 0 ? void 0 : x.includes(search)) ||
|
|
2819
|
+
((_a = renderValue(i, data.head[j], x)) === null || _a === void 0 ? void 0 : _a.toString().includes(search));
|
|
2820
|
+
}))
|
|
2821
|
+
.sort((a, b) => {
|
|
2822
|
+
var _a, _b;
|
|
2823
|
+
if (disableSort) {
|
|
2824
|
+
return 1;
|
|
2825
|
+
}
|
|
2826
|
+
const aValue = (_a = data.body[a][sortByColumn]) !== null && _a !== void 0 ? _a : "";
|
|
2827
|
+
const bValue = (_b = data.body[b][sortByColumn]) !== null && _b !== void 0 ? _b : "";
|
|
2828
|
+
let difference = parseInt(bValue) - parseInt(aValue);
|
|
2829
|
+
if (isNaN(difference)) {
|
|
2830
|
+
return reverseValue * (aValue === null || aValue === void 0 ? void 0 : aValue.localeCompare(bValue));
|
|
2831
|
+
}
|
|
2832
|
+
return reverseValue * difference;
|
|
2833
|
+
});
|
|
2834
|
+
const displayData = {
|
|
2835
|
+
head: data.head.map(renderHead),
|
|
2836
|
+
body: sortedIndices.map((rowIndex) => data.body[rowIndex].map((value, columnIndex) => renderValue(rowIndex, data.head[columnIndex], value))),
|
|
2837
|
+
};
|
|
2838
|
+
return (React.createElement(React.Fragment, null,
|
|
2839
|
+
React.createElement(TextInput, { mb: "xs", w: 400, maw: "100%", placeholder: "Search", leftSection: React.createElement("i", { className: "fa-solid fa-magnifying-glass" }), rightSection: search !== "" ? (React.createElement(ActionIcon, { onClick: () => setSearch(""), variant: "transparent", color: "default", size: 15 },
|
|
2840
|
+
React.createElement("i", { className: "fa-solid fa-xmark" }))) : undefined, value: search, onChange: (e) => setSearch(e.currentTarget.value) }),
|
|
2841
|
+
React.createElement("div", { style: { maxWidth: "100%", overflow: "auto", maxHeight: 500 } },
|
|
2842
|
+
React.createElement(Table, { horizontalSpacing: 7.5, verticalSpacing: 7.5 },
|
|
2843
|
+
React.createElement(Table.Thead, { style: {
|
|
2844
|
+
position: "sticky",
|
|
2845
|
+
top: 0,
|
|
2846
|
+
backgroundColor: colorScheme === "dark" ? theme.colors.dark[7] : "white",
|
|
2847
|
+
zIndex: 101,
|
|
2848
|
+
} },
|
|
2849
|
+
React.createElement(Table.Tr, { h: 1 },
|
|
2850
|
+
selectable && React.createElement(Table.Th, null), (_a = displayData.head) === null || _a === void 0 ? void 0 :
|
|
2851
|
+
_a.map((element, columnIndex) => (React.createElement(Table.Th, { key: columnIndex, style: {
|
|
2852
|
+
padding: 0,
|
|
2853
|
+
verticalAlign: "top",
|
|
2854
|
+
height: "inherit",
|
|
2855
|
+
} }, element !== "" && (React.createElement(UnstyledButton, { h: "100%", className: "control", style: {
|
|
2856
|
+
display: "flex",
|
|
2857
|
+
flexDirection: "column",
|
|
2858
|
+
minWidth: 100,
|
|
2859
|
+
}, onClick: disableSort
|
|
2860
|
+
? undefined
|
|
2861
|
+
: () => {
|
|
2862
|
+
if (sortByColumn === columnIndex) {
|
|
2863
|
+
setReversed((r) => !r);
|
|
2864
|
+
}
|
|
2865
|
+
else {
|
|
2866
|
+
setSortByColumn(columnIndex);
|
|
2867
|
+
}
|
|
2868
|
+
} },
|
|
2869
|
+
disableSort || (React.createElement("i", { style: { fontSize: 14, marginBottom: 5 }, className: sortByColumn === columnIndex
|
|
2870
|
+
? reversed
|
|
2871
|
+
? "fa-solid fa-sort-up"
|
|
2872
|
+
: "fa-solid fa-sort-down"
|
|
2873
|
+
: "fa-solid fa-sort" })),
|
|
2874
|
+
element))))))),
|
|
2875
|
+
React.createElement(Table.Tbody, null, (_b = displayData.body) === null || _b === void 0 ? void 0 : _b.map((row, rowIndex) => {
|
|
2876
|
+
const realIndex = sortedIndices[rowIndex];
|
|
2877
|
+
return (React.createElement(Table.Tr, { key: rowIndex },
|
|
2878
|
+
selectable && (React.createElement(Table.Td, null,
|
|
2879
|
+
React.createElement(Checkbox, { checked: selectedRows === null || selectedRows === void 0 ? void 0 : selectedRows.includes(realIndex), onChange: (e) => {
|
|
2880
|
+
if (setSelectedRows) {
|
|
2881
|
+
if (e.currentTarget.checked) {
|
|
2882
|
+
setSelectedRows((s) => [...s, realIndex]);
|
|
2883
|
+
}
|
|
2884
|
+
else {
|
|
2885
|
+
setSelectedRows((s) => s.filter((x) => x !== realIndex));
|
|
2886
|
+
}
|
|
2887
|
+
}
|
|
2888
|
+
} }))),
|
|
2889
|
+
row.map((value, valueIndex) => (React.createElement(Table.Td, { key: valueIndex, style: Object.assign({ whiteSpace: "nowrap" }, (stickyFirstColumn && valueIndex === 0
|
|
2890
|
+
? {
|
|
2891
|
+
position: "sticky",
|
|
2892
|
+
right: 0,
|
|
2893
|
+
backgroundColor: colorScheme === "dark"
|
|
2894
|
+
? theme.colors.dark[7]
|
|
2895
|
+
: "white",
|
|
2896
|
+
zIndex: 100,
|
|
2897
|
+
}
|
|
2898
|
+
: {})) }, value)))));
|
|
2899
|
+
}))))));
|
|
2900
|
+
};
|
|
2901
|
+
|
|
2902
|
+
const ResultStatisticsTable = () => {
|
|
2903
|
+
var _a, _b, _c, _d, _e;
|
|
2904
|
+
const [results] = useLocalStorage({
|
|
2905
|
+
key: "Results",
|
|
2906
|
+
defaultValue: {},
|
|
2907
|
+
});
|
|
2908
|
+
const [rounds] = useLocalStorage({
|
|
2909
|
+
key: "Rounds",
|
|
2910
|
+
defaultValue: [],
|
|
2911
|
+
});
|
|
2912
|
+
const [currentRound] = useLocalStorage({
|
|
2913
|
+
key: "Current Round",
|
|
2914
|
+
defaultValue: 0,
|
|
2915
|
+
});
|
|
2916
|
+
const navigate = useNavigate();
|
|
2917
|
+
const round = rounds[currentRound];
|
|
2918
|
+
if (!round) {
|
|
2919
|
+
return React.createElement(React.Fragment, null);
|
|
2920
|
+
}
|
|
2921
|
+
const statistics = Object.keys((_d = (_c = ((_b = Object.values((_a = Object.values(results !== null && results !== void 0 ? results : {})[0]) !== null && _a !== void 0 ? _a : {})[0]) !== null && _b !== void 0 ? _b : [])[0]) === null || _c === void 0 ? void 0 : _c.statistics) !== null && _d !== void 0 ? _d : {});
|
|
2922
|
+
const roundResults = ((_e = (results !== null && results !== void 0 ? results : {})[round.players.join(", ")]) !== null && _e !== void 0 ? _e : {})[JSON.stringify(round.parameters)];
|
|
2923
|
+
if (!roundResults) {
|
|
2924
|
+
return React.createElement(React.Fragment, null);
|
|
2925
|
+
}
|
|
2926
|
+
return (React.createElement("div", { style: {
|
|
2927
|
+
display: "flex",
|
|
2928
|
+
flexDirection: "column",
|
|
2929
|
+
alignItems: "center",
|
|
2930
|
+
marginTop: 10,
|
|
2931
|
+
} },
|
|
2932
|
+
React.createElement(DataTable, { tableName: "Results", data: {
|
|
2933
|
+
head: ["Seed"].concat(statistics),
|
|
2934
|
+
body: roundResults.map((result) => [result.seed].concat(statistics.map((statistic) => { var _a; return ((_a = result === null || result === void 0 ? void 0 : result.statistics) !== null && _a !== void 0 ? _a : {})[statistic].toString(); }))),
|
|
2935
|
+
}, renderValue: (rowIndex, columnName, value) => {
|
|
2936
|
+
if (columnName === "Seed") {
|
|
2937
|
+
return (React.createElement(Button, { leftSection: React.createElement("i", { className: "fa-solid fa-play" }), onClick: () => navigate(`/simulation/${round.players.map(encodeURIComponent).join(",")}?seed=${value}&${Object.keys(round.parameters)
|
|
2938
|
+
.map((p) => `${p}=${encodeURIComponent(round.parameters[p])}`)
|
|
2939
|
+
.join("&")}`) }, "Play"));
|
|
2940
|
+
}
|
|
2941
|
+
return React.createElement(React.Fragment, { key: rowIndex }, value);
|
|
2942
|
+
} })));
|
|
2943
|
+
};
|
|
2944
|
+
|
|
2945
|
+
let currentParameters;
|
|
2777
2946
|
let currentPlayers;
|
|
2778
2947
|
const Round = () => {
|
|
2779
2948
|
const firestore = useFirestore();
|
|
@@ -2789,9 +2958,9 @@ const Round = () => {
|
|
|
2789
2958
|
key: "Player Bots",
|
|
2790
2959
|
defaultValue: ["None", "None"],
|
|
2791
2960
|
});
|
|
2792
|
-
const [
|
|
2793
|
-
key: "
|
|
2794
|
-
defaultValue:
|
|
2961
|
+
const [parameters, setParameters] = useLocalStorage({
|
|
2962
|
+
key: "Parameters",
|
|
2963
|
+
defaultValue: {},
|
|
2795
2964
|
});
|
|
2796
2965
|
const [rounds, setRounds] = useLocalStorage({
|
|
2797
2966
|
key: "Rounds",
|
|
@@ -2831,9 +3000,17 @@ const Round = () => {
|
|
|
2831
3000
|
}
|
|
2832
3001
|
useEffect(() => {
|
|
2833
3002
|
if (remaining > 0) {
|
|
2834
|
-
runNoUI(
|
|
3003
|
+
runNoUI(currentParameters, apis, currentPlayers, "", false);
|
|
2835
3004
|
}
|
|
2836
3005
|
}, [remaining]);
|
|
3006
|
+
const getFullParameters = () => {
|
|
3007
|
+
var _a;
|
|
3008
|
+
const result = {};
|
|
3009
|
+
for (const key in configuration.parameters) {
|
|
3010
|
+
result[key] = (_a = parameters[key]) !== null && _a !== void 0 ? _a : configuration.parameters[key][0];
|
|
3011
|
+
}
|
|
3012
|
+
return result;
|
|
3013
|
+
};
|
|
2837
3014
|
useEffect(updatePointModifier, [results]);
|
|
2838
3015
|
return (React.createElement(React.Fragment, null,
|
|
2839
3016
|
React.createElement("div", { style: {
|
|
@@ -2846,7 +3023,7 @@ const Round = () => {
|
|
|
2846
3023
|
React.createElement("table", { style: { textAlign: "center" } },
|
|
2847
3024
|
React.createElement("thead", null,
|
|
2848
3025
|
React.createElement("tr", null,
|
|
2849
|
-
React.createElement("th", { style: { width: "25%" } }, "
|
|
3026
|
+
React.createElement("th", { style: { width: "25%" } }, "Parameters"),
|
|
2850
3027
|
React.createElement("th", { style: { width: "35%" } }, "Players"),
|
|
2851
3028
|
roundIterations === 1 && (React.createElement(React.Fragment, null,
|
|
2852
3029
|
React.createElement("th", { style: { width: "17.5%" } }, "1st Place"),
|
|
@@ -2856,9 +3033,9 @@ const Round = () => {
|
|
|
2856
3033
|
React.createElement("tbody", null, rounds.map((round, index) => {
|
|
2857
3034
|
const winCounts = {};
|
|
2858
3035
|
if (results[round.players.join(", ")] !== undefined) {
|
|
2859
|
-
const currentResults = results[round.players.join(", ")][round.
|
|
3036
|
+
const currentResults = results[round.players.join(", ")][JSON.stringify(round.parameters)];
|
|
2860
3037
|
for (const result of currentResults) {
|
|
2861
|
-
const winner = round.players[result[0]];
|
|
3038
|
+
const winner = round.players[result.places[0]];
|
|
2862
3039
|
if (!winCounts[winner]) {
|
|
2863
3040
|
winCounts[winner] = 1;
|
|
2864
3041
|
}
|
|
@@ -2869,28 +3046,34 @@ const Round = () => {
|
|
|
2869
3046
|
}
|
|
2870
3047
|
const winners = Object.keys(winCounts).sort((a, b) => winCounts[b] - winCounts[a]);
|
|
2871
3048
|
return (React.createElement("tr", { key: index },
|
|
2872
|
-
React.createElement("td",
|
|
3049
|
+
React.createElement("td", { style: { whiteSpace: "pre" } }, Object.keys(round.parameters)
|
|
3050
|
+
.sort()
|
|
3051
|
+
.map((p) => `${p[0].toUpperCase()}${p.slice(1)}: ${round.parameters[p]}`)
|
|
3052
|
+
.join("\n")),
|
|
2873
3053
|
React.createElement("td", null, round.players.map((player, index) => (React.createElement("img", { key: index, src: `/images/teams/${player.toLowerCase()}.png`, width: 30, style: { marginInlineEnd: 10 } })))),
|
|
2874
3054
|
roundIterations === 1 && (React.createElement(React.Fragment, null,
|
|
2875
3055
|
React.createElement("td", null, results[round.players.join(", ")] !== undefined &&
|
|
2876
|
-
results[round.players.join(", ")][round.
|
|
2877
|
-
undefined && (React.createElement("img", { src: `/images/teams/${round.players[results[round.players.join(", ")][round.map][0][0]].toLowerCase()}.png`, width: 30, style: { marginInlineEnd: 10 } }))),
|
|
3056
|
+
results[round.players.join(", ")][JSON.stringify(round.parameters)] !== undefined && (React.createElement("img", { src: `/images/teams/${round.players[results[round.players.join(", ")][JSON.stringify(round.parameters)][0][0]].toLowerCase()}.png`, width: 30, style: { marginInlineEnd: 10 } }))),
|
|
2878
3057
|
React.createElement("td", null, results[round.players.join(", ")] !== undefined &&
|
|
2879
|
-
results[round.players.join(", ")][round.
|
|
2880
|
-
undefined && (React.createElement("img", { src: `/images/teams/${round.players[results[round.players.join(", ")][round.map][0][1]].toLowerCase()}.png`, width: 30, style: { marginInlineEnd: 10 } }))))),
|
|
3058
|
+
results[round.players.join(", ")][JSON.stringify(round.parameters)] !== undefined && (React.createElement("img", { src: `/images/teams/${round.players[results[round.players.join(", ")][JSON.stringify(round.parameters)][0][1]].toLowerCase()}.png`, width: 30, style: { marginInlineEnd: 10 } }))))),
|
|
2881
3059
|
roundIterations !== 1 && (React.createElement("td", null, winners.map((winner, index) => (React.createElement("p", { key: index },
|
|
2882
3060
|
winner,
|
|
2883
3061
|
": ",
|
|
2884
3062
|
winCounts[winner]))))),
|
|
2885
3063
|
React.createElement("td", null,
|
|
2886
3064
|
React.createElement(Button.Group, { orientation: "vertical" },
|
|
2887
|
-
React.createElement(Button, { leftSection: React.createElement("i", { className: "fa-solid fa-
|
|
3065
|
+
React.createElement(Button, { leftSection: React.createElement("i", { className: "fa-solid fa-hand-pointer" }), size: "xs", onClick: () => {
|
|
3066
|
+
setLocalStorage("Current Round", index);
|
|
3067
|
+
} }, "Select"),
|
|
3068
|
+
React.createElement(Button, { leftSection: React.createElement("i", { className: "fa-solid fa-play" }), size: "xs", onClick: () => navigate(`/simulation/${round.players.map(encodeURIComponent).join(",")}?showcase=true&${Object.keys(round.parameters)
|
|
3069
|
+
.map((p) => `${p}=${encodeURIComponent(round.parameters[p])}`)
|
|
3070
|
+
.join("&")}`) }, "Simulate"),
|
|
2888
3071
|
React.createElement(Button, { leftSection: React.createElement("i", { className: "fa-solid fa-forward" }), size: "xs", onClick: () => {
|
|
2889
3072
|
if (roundIterations === 1) {
|
|
2890
|
-
runNoUI(round.
|
|
3073
|
+
runNoUI(round.parameters, apis, round.players, "", false);
|
|
2891
3074
|
}
|
|
2892
3075
|
else {
|
|
2893
|
-
|
|
3076
|
+
currentParameters = round.parameters;
|
|
2894
3077
|
currentPlayers = round.players;
|
|
2895
3078
|
startRunNoUIN(roundIterations);
|
|
2896
3079
|
}
|
|
@@ -2901,12 +3084,14 @@ const Round = () => {
|
|
|
2901
3084
|
"Remaining Simulations: ",
|
|
2902
3085
|
remaining)),
|
|
2903
3086
|
location.search.includes("edit") && (React.createElement(React.Fragment, null,
|
|
2904
|
-
React.createElement(NumberInput, { value: roundIterations, onChange: (v) => { var _a; return setRoundIterations((_a = parseInt(v.toString(), 10)) !== null && _a !== void 0 ? _a : 1); }, label: "Round Iterations", leftSection: React.createElement("i", { className: "fa-solid fa-hashtag" }) }),
|
|
2905
|
-
|
|
2906
|
-
|
|
2907
|
-
|
|
2908
|
-
|
|
2909
|
-
|
|
3087
|
+
React.createElement(NumberInput, { mb: "xs", value: roundIterations, onChange: (v) => { var _a; return setRoundIterations((_a = parseInt(v.toString(), 10)) !== null && _a !== void 0 ? _a : 1); }, label: "Round Iterations", leftSection: React.createElement("i", { className: "fa-solid fa-hashtag" }) }),
|
|
3088
|
+
Object.keys(configuration.parameters)
|
|
3089
|
+
.sort()
|
|
3090
|
+
.map((parameter, parameterIndex) => {
|
|
3091
|
+
var _a, _b;
|
|
3092
|
+
const icon = ((_a = configuration.parameterIcons) !== null && _a !== void 0 ? _a : {})[parameter];
|
|
3093
|
+
return (React.createElement(Select, { key: parameterIndex, mb: "xs", leftSection: icon ? React.createElement("i", { className: icon }) : undefined, label: parameter[0].toUpperCase() + parameter.slice(1), data: configuration.parameters[parameter], value: (_b = parameters[parameter]) !== null && _b !== void 0 ? _b : configuration.parameters[parameter][0], onChange: (s) => setParameters(Object.assign(Object.assign({}, parameters), { [parameter]: s !== null && s !== void 0 ? s : configuration.parameters[parameter][0] })) }));
|
|
3094
|
+
}),
|
|
2910
3095
|
React.createElement(BotSelector, { playerCount: playerCount, setPlayerCount: setPlayerCount, playerBots: playerBots, setPlayerBots: setPlayerBots, apis: apis }),
|
|
2911
3096
|
React.createElement(Button, { color: "red", leftSection: React.createElement("i", { className: "fa-solid fa-trash" }), mt: "xs", onClick: () => {
|
|
2912
3097
|
setRounds([]);
|
|
@@ -2918,7 +3103,10 @@ const Round = () => {
|
|
|
2918
3103
|
React.createElement(Button, { leftSection: React.createElement("i", { className: "fa-solid fa-plus" }), mt: "xs", onClick: () => {
|
|
2919
3104
|
setRounds((rounds) => [
|
|
2920
3105
|
...rounds,
|
|
2921
|
-
{
|
|
3106
|
+
{
|
|
3107
|
+
players: playerBots,
|
|
3108
|
+
parameters: Object.assign({}, getFullParameters()),
|
|
3109
|
+
},
|
|
2922
3110
|
]);
|
|
2923
3111
|
}, style: { width: "30%" } }, "Add"),
|
|
2924
3112
|
React.createElement(Button, { leftSection: React.createElement("i", { className: "fa-solid fa-save" }), fullWidth: true, mt: "xs", onClick: () => __awaiter(void 0, void 0, void 0, function* () {
|
|
@@ -2938,7 +3126,7 @@ const Round = () => {
|
|
|
2938
3126
|
80, 80, 80, 80,
|
|
2939
3127
|
]) {
|
|
2940
3128
|
setRounds((rounds) => rounds.map((round) => ({
|
|
2941
|
-
|
|
3129
|
+
parameters: round.parameters,
|
|
2942
3130
|
players: shuffle(round.players),
|
|
2943
3131
|
})));
|
|
2944
3132
|
yield new Promise((resolve) => setTimeout(resolve, timeout));
|
|
@@ -2950,7 +3138,8 @@ const Round = () => {
|
|
|
2950
3138
|
flexDirection: clientWidth >= 700 ? "row" : "column",
|
|
2951
3139
|
} },
|
|
2952
3140
|
React.createElement(TournamentBlock, { title: "Before", inline: true }),
|
|
2953
|
-
React.createElement(TournamentBlock, { title: "After", inline: true, pointModifier: pointModifier })))
|
|
3141
|
+
React.createElement(TournamentBlock, { title: "After", inline: true, pointModifier: pointModifier }))),
|
|
3142
|
+
React.createElement(ResultStatisticsTable, null))));
|
|
2954
3143
|
};
|
|
2955
3144
|
|
|
2956
3145
|
const SettingsPage = () => {
|
|
@@ -3106,7 +3295,7 @@ const Simulation = () => {
|
|
|
3106
3295
|
var _a, _b, _c, _d;
|
|
3107
3296
|
const admin = useAdmin();
|
|
3108
3297
|
const [apis, loading] = useAPIs();
|
|
3109
|
-
let {
|
|
3298
|
+
let { playerapis } = useParams();
|
|
3110
3299
|
const location = useLocation();
|
|
3111
3300
|
const [searchParams] = useSearchParams();
|
|
3112
3301
|
const [winner, setWinner] = useState();
|
|
@@ -3145,7 +3334,10 @@ const Simulation = () => {
|
|
|
3145
3334
|
}
|
|
3146
3335
|
};
|
|
3147
3336
|
}, []);
|
|
3148
|
-
|
|
3337
|
+
const parameters = Object.fromEntries(searchParams);
|
|
3338
|
+
if (parameters.seed) {
|
|
3339
|
+
delete parameters.seed;
|
|
3340
|
+
}
|
|
3149
3341
|
playerapis = playerapis === null || playerapis === void 0 ? void 0 : playerapis.split("&")[0];
|
|
3150
3342
|
const playerNames = (_a = playerapis === null || playerapis === void 0 ? void 0 : playerapis.split(",").map(decodeURIComponent)) !== null && _a !== void 0 ? _a : [];
|
|
3151
3343
|
const players = playerNames.map((api) => (api === "None" ? "" : apis[api]));
|
|
@@ -3159,7 +3351,7 @@ const Simulation = () => {
|
|
|
3159
3351
|
const seed = (_a = searchParams.get("seed")) !== null && _a !== void 0 ? _a : "";
|
|
3160
3352
|
tryUntilSuccess(() =>
|
|
3161
3353
|
// @ts-ignore
|
|
3162
|
-
window._startSimulation(
|
|
3354
|
+
window._startSimulation(parameters, players, playerNames, false, !showcaseMode, true, seed));
|
|
3163
3355
|
}
|
|
3164
3356
|
}, [loading]);
|
|
3165
3357
|
const newRank = getRank(getLocalStorage("Cached tournament/info"), winner, getLocalStorage("Point Modifier")) + 1;
|
|
@@ -3312,7 +3504,7 @@ const App = ({ routes, blocks }) => {
|
|
|
3312
3504
|
React.createElement(Routes, null,
|
|
3313
3505
|
React.createElement(Route, { path: "/", element: React.createElement(HomePage, { blocks: blocks }) }),
|
|
3314
3506
|
React.createElement(Route, { path: "/view/:apiname", element: React.createElement(ViewAPI, null) }),
|
|
3315
|
-
React.createElement(Route, { path: "/simulation/:
|
|
3507
|
+
React.createElement(Route, { path: "/simulation/:playerapis", element: React.createElement(Simulation, null) }),
|
|
3316
3508
|
React.createElement(Route, { path: "/round", element: React.createElement(Round, null) }),
|
|
3317
3509
|
React.createElement(Route, { path: "/settings", element: React.createElement(SettingsPage, null) }),
|
|
3318
3510
|
React.createElement(Route, { path: "*", element: React.createElement(NotFoundPage, null) }),
|
|
@@ -3383,7 +3575,7 @@ const TopPane = () => {
|
|
|
3383
3575
|
};
|
|
3384
3576
|
|
|
3385
3577
|
const CodeBattles = ({ configuration, routes, blocks }) => {
|
|
3386
|
-
var _a, _b, _c, _d, _e, _f
|
|
3578
|
+
var _a, _b, _c, _d, _e, _f;
|
|
3387
3579
|
const colorScheme = useColorScheme();
|
|
3388
3580
|
const firebase = initializeApp(configuration.firebase);
|
|
3389
3581
|
const firestore = getFirestore(firebase);
|
|
@@ -3394,7 +3586,6 @@ const CodeBattles = ({ configuration, routes, blocks }) => {
|
|
|
3394
3586
|
locale: (_b = (_a = configuration.dates) === null || _a === void 0 ? void 0 : _a.locale) !== null && _b !== void 0 ? _b : "en",
|
|
3395
3587
|
firstDayOfWeek: (_d = (_c = configuration.dates) === null || _c === void 0 ? void 0 : _c.firstDayOfWeek) !== null && _d !== void 0 ? _d : 0,
|
|
3396
3588
|
weekendDays: (_f = (_e = configuration.dates) === null || _e === void 0 ? void 0 : _e.weekendDays) !== null && _f !== void 0 ? _f : [],
|
|
3397
|
-
timezone: (_h = (_g = configuration.dates) === null || _g === void 0 ? void 0 : _g.timezone) !== null && _h !== void 0 ? _h : "UTC",
|
|
3398
3589
|
} },
|
|
3399
3590
|
React.createElement(BrowserRouter, null,
|
|
3400
3591
|
React.createElement(Notifications, null),
|
|
@@ -3431,2272 +3622,2303 @@ const CodeBattles = ({ configuration, routes, blocks }) => {
|
|
|
3431
3622
|
|
|
3432
3623
|
var prism = {exports: {}};
|
|
3433
3624
|
|
|
3434
|
-
|
|
3435
|
-
/* **********************************************
|
|
3436
|
-
Begin prism-core.js
|
|
3437
|
-
********************************************** */
|
|
3438
|
-
|
|
3439
|
-
/// <reference lib="WebWorker"/>
|
|
3625
|
+
var hasRequiredPrism;
|
|
3440
3626
|
|
|
3441
|
-
|
|
3442
|
-
|
|
3443
|
-
|
|
3444
|
-
|
|
3445
|
-
|
|
3446
|
-
|
|
3447
|
-
|
|
3627
|
+
function requirePrism () {
|
|
3628
|
+
if (hasRequiredPrism) return prism.exports;
|
|
3629
|
+
hasRequiredPrism = 1;
|
|
3630
|
+
(function (module) {
|
|
3631
|
+
/* **********************************************
|
|
3632
|
+
Begin prism-core.js
|
|
3633
|
+
********************************************** */
|
|
3448
3634
|
|
|
3449
|
-
|
|
3450
|
-
* Prism: Lightweight, robust, elegant syntax highlighting
|
|
3451
|
-
*
|
|
3452
|
-
* @license MIT <https://opensource.org/licenses/MIT>
|
|
3453
|
-
* @author Lea Verou <https://lea.verou.me>
|
|
3454
|
-
* @namespace
|
|
3455
|
-
* @public
|
|
3456
|
-
*/
|
|
3457
|
-
var Prism = (function (_self) {
|
|
3635
|
+
/// <reference lib="WebWorker"/>
|
|
3458
3636
|
|
|
3459
|
-
|
|
3460
|
-
|
|
3461
|
-
|
|
3637
|
+
var _self = (typeof window !== 'undefined')
|
|
3638
|
+
? window // if in browser
|
|
3639
|
+
: (
|
|
3640
|
+
(typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope)
|
|
3641
|
+
? self // if in worker
|
|
3642
|
+
: {} // if in node js
|
|
3643
|
+
);
|
|
3462
3644
|
|
|
3463
|
-
|
|
3464
|
-
|
|
3645
|
+
/**
|
|
3646
|
+
* Prism: Lightweight, robust, elegant syntax highlighting
|
|
3647
|
+
*
|
|
3648
|
+
* @license MIT <https://opensource.org/licenses/MIT>
|
|
3649
|
+
* @author Lea Verou <https://lea.verou.me>
|
|
3650
|
+
* @namespace
|
|
3651
|
+
* @public
|
|
3652
|
+
*/
|
|
3653
|
+
var Prism = (function (_self) {
|
|
3465
3654
|
|
|
3655
|
+
// Private helper vars
|
|
3656
|
+
var lang = /(?:^|\s)lang(?:uage)?-([\w-]+)(?=\s|$)/i;
|
|
3657
|
+
var uniqueId = 0;
|
|
3466
3658
|
|
|
3467
|
-
|
|
3468
|
-
|
|
3469
|
-
* By default, Prism will attempt to highlight all code elements (by calling {@link Prism.highlightAll}) on the
|
|
3470
|
-
* current page after the page finished loading. This might be a problem if e.g. you wanted to asynchronously load
|
|
3471
|
-
* additional languages or plugins yourself.
|
|
3472
|
-
*
|
|
3473
|
-
* By setting this value to `true`, Prism will not automatically highlight all code elements on the page.
|
|
3474
|
-
*
|
|
3475
|
-
* You obviously have to change this value before the automatic highlighting started. To do this, you can add an
|
|
3476
|
-
* empty Prism object into the global scope before loading the Prism script like this:
|
|
3477
|
-
*
|
|
3478
|
-
* ```js
|
|
3479
|
-
* window.Prism = window.Prism || {};
|
|
3480
|
-
* Prism.manual = true;
|
|
3481
|
-
* // add a new <script> to load Prism's script
|
|
3482
|
-
* ```
|
|
3483
|
-
*
|
|
3484
|
-
* @default false
|
|
3485
|
-
* @type {boolean}
|
|
3486
|
-
* @memberof Prism
|
|
3487
|
-
* @public
|
|
3488
|
-
*/
|
|
3489
|
-
manual: _self.Prism && _self.Prism.manual,
|
|
3490
|
-
/**
|
|
3491
|
-
* By default, if Prism is in a web worker, it assumes that it is in a worker it created itself, so it uses
|
|
3492
|
-
* `addEventListener` to communicate with its parent instance. However, if you're using Prism manually in your
|
|
3493
|
-
* own worker, you don't want it to do this.
|
|
3494
|
-
*
|
|
3495
|
-
* By setting this value to `true`, Prism will not add its own listeners to the worker.
|
|
3496
|
-
*
|
|
3497
|
-
* You obviously have to change this value before Prism executes. To do this, you can add an
|
|
3498
|
-
* empty Prism object into the global scope before loading the Prism script like this:
|
|
3499
|
-
*
|
|
3500
|
-
* ```js
|
|
3501
|
-
* window.Prism = window.Prism || {};
|
|
3502
|
-
* Prism.disableWorkerMessageHandler = true;
|
|
3503
|
-
* // Load Prism's script
|
|
3504
|
-
* ```
|
|
3505
|
-
*
|
|
3506
|
-
* @default false
|
|
3507
|
-
* @type {boolean}
|
|
3508
|
-
* @memberof Prism
|
|
3509
|
-
* @public
|
|
3510
|
-
*/
|
|
3511
|
-
disableWorkerMessageHandler: _self.Prism && _self.Prism.disableWorkerMessageHandler,
|
|
3659
|
+
// The grammar object for plaintext
|
|
3660
|
+
var plainTextGrammar = {};
|
|
3512
3661
|
|
|
3513
|
-
/**
|
|
3514
|
-
* A namespace for utility methods.
|
|
3515
|
-
*
|
|
3516
|
-
* All function in this namespace that are not explicitly marked as _public_ are for __internal use only__ and may
|
|
3517
|
-
* change or disappear at any time.
|
|
3518
|
-
*
|
|
3519
|
-
* @namespace
|
|
3520
|
-
* @memberof Prism
|
|
3521
|
-
*/
|
|
3522
|
-
util: {
|
|
3523
|
-
encode: function encode(tokens) {
|
|
3524
|
-
if (tokens instanceof Token) {
|
|
3525
|
-
return new Token(tokens.type, encode(tokens.content), tokens.alias);
|
|
3526
|
-
} else if (Array.isArray(tokens)) {
|
|
3527
|
-
return tokens.map(encode);
|
|
3528
|
-
} else {
|
|
3529
|
-
return tokens.replace(/&/g, '&').replace(/</g, '<').replace(/\u00a0/g, ' ');
|
|
3530
|
-
}
|
|
3531
|
-
},
|
|
3532
3662
|
|
|
3663
|
+
var _ = {
|
|
3533
3664
|
/**
|
|
3534
|
-
*
|
|
3665
|
+
* By default, Prism will attempt to highlight all code elements (by calling {@link Prism.highlightAll}) on the
|
|
3666
|
+
* current page after the page finished loading. This might be a problem if e.g. you wanted to asynchronously load
|
|
3667
|
+
* additional languages or plugins yourself.
|
|
3535
3668
|
*
|
|
3536
|
-
*
|
|
3537
|
-
*
|
|
3538
|
-
*
|
|
3539
|
-
*
|
|
3540
|
-
*
|
|
3541
|
-
*
|
|
3542
|
-
*
|
|
3543
|
-
*
|
|
3544
|
-
*
|
|
3545
|
-
*
|
|
3546
|
-
*
|
|
3547
|
-
*
|
|
3669
|
+
* By setting this value to `true`, Prism will not automatically highlight all code elements on the page.
|
|
3670
|
+
*
|
|
3671
|
+
* You obviously have to change this value before the automatic highlighting started. To do this, you can add an
|
|
3672
|
+
* empty Prism object into the global scope before loading the Prism script like this:
|
|
3673
|
+
*
|
|
3674
|
+
* ```js
|
|
3675
|
+
* window.Prism = window.Prism || {};
|
|
3676
|
+
* Prism.manual = true;
|
|
3677
|
+
* // add a new <script> to load Prism's script
|
|
3678
|
+
* ```
|
|
3679
|
+
*
|
|
3680
|
+
* @default false
|
|
3681
|
+
* @type {boolean}
|
|
3682
|
+
* @memberof Prism
|
|
3683
|
+
* @public
|
|
3548
3684
|
*/
|
|
3549
|
-
|
|
3550
|
-
return Object.prototype.toString.call(o).slice(8, -1);
|
|
3551
|
-
},
|
|
3552
|
-
|
|
3685
|
+
manual: _self.Prism && _self.Prism.manual,
|
|
3553
3686
|
/**
|
|
3554
|
-
*
|
|
3687
|
+
* By default, if Prism is in a web worker, it assumes that it is in a worker it created itself, so it uses
|
|
3688
|
+
* `addEventListener` to communicate with its parent instance. However, if you're using Prism manually in your
|
|
3689
|
+
* own worker, you don't want it to do this.
|
|
3690
|
+
*
|
|
3691
|
+
* By setting this value to `true`, Prism will not add its own listeners to the worker.
|
|
3555
3692
|
*
|
|
3556
|
-
*
|
|
3557
|
-
*
|
|
3693
|
+
* You obviously have to change this value before Prism executes. To do this, you can add an
|
|
3694
|
+
* empty Prism object into the global scope before loading the Prism script like this:
|
|
3695
|
+
*
|
|
3696
|
+
* ```js
|
|
3697
|
+
* window.Prism = window.Prism || {};
|
|
3698
|
+
* Prism.disableWorkerMessageHandler = true;
|
|
3699
|
+
* // Load Prism's script
|
|
3700
|
+
* ```
|
|
3701
|
+
*
|
|
3702
|
+
* @default false
|
|
3703
|
+
* @type {boolean}
|
|
3704
|
+
* @memberof Prism
|
|
3705
|
+
* @public
|
|
3558
3706
|
*/
|
|
3559
|
-
|
|
3560
|
-
if (!obj['__id']) {
|
|
3561
|
-
Object.defineProperty(obj, '__id', { value: ++uniqueId });
|
|
3562
|
-
}
|
|
3563
|
-
return obj['__id'];
|
|
3564
|
-
},
|
|
3707
|
+
disableWorkerMessageHandler: _self.Prism && _self.Prism.disableWorkerMessageHandler,
|
|
3565
3708
|
|
|
3566
3709
|
/**
|
|
3567
|
-
*
|
|
3710
|
+
* A namespace for utility methods.
|
|
3568
3711
|
*
|
|
3569
|
-
*
|
|
3712
|
+
* All function in this namespace that are not explicitly marked as _public_ are for __internal use only__ and may
|
|
3713
|
+
* change or disappear at any time.
|
|
3570
3714
|
*
|
|
3571
|
-
* @
|
|
3572
|
-
* @
|
|
3573
|
-
* @returns {T}
|
|
3574
|
-
* @template T
|
|
3715
|
+
* @namespace
|
|
3716
|
+
* @memberof Prism
|
|
3575
3717
|
*/
|
|
3576
|
-
|
|
3577
|
-
|
|
3578
|
-
|
|
3579
|
-
|
|
3580
|
-
|
|
3581
|
-
|
|
3582
|
-
|
|
3583
|
-
|
|
3584
|
-
|
|
3585
|
-
|
|
3586
|
-
|
|
3587
|
-
|
|
3718
|
+
util: {
|
|
3719
|
+
encode: function encode(tokens) {
|
|
3720
|
+
if (tokens instanceof Token) {
|
|
3721
|
+
return new Token(tokens.type, encode(tokens.content), tokens.alias);
|
|
3722
|
+
} else if (Array.isArray(tokens)) {
|
|
3723
|
+
return tokens.map(encode);
|
|
3724
|
+
} else {
|
|
3725
|
+
return tokens.replace(/&/g, '&').replace(/</g, '<').replace(/\u00a0/g, ' ');
|
|
3726
|
+
}
|
|
3727
|
+
},
|
|
3728
|
+
|
|
3729
|
+
/**
|
|
3730
|
+
* Returns the name of the type of the given value.
|
|
3731
|
+
*
|
|
3732
|
+
* @param {any} o
|
|
3733
|
+
* @returns {string}
|
|
3734
|
+
* @example
|
|
3735
|
+
* type(null) === 'Null'
|
|
3736
|
+
* type(undefined) === 'Undefined'
|
|
3737
|
+
* type(123) === 'Number'
|
|
3738
|
+
* type('foo') === 'String'
|
|
3739
|
+
* type(true) === 'Boolean'
|
|
3740
|
+
* type([1, 2]) === 'Array'
|
|
3741
|
+
* type({}) === 'Object'
|
|
3742
|
+
* type(String) === 'Function'
|
|
3743
|
+
* type(/abc+/) === 'RegExp'
|
|
3744
|
+
*/
|
|
3745
|
+
type: function (o) {
|
|
3746
|
+
return Object.prototype.toString.call(o).slice(8, -1);
|
|
3747
|
+
},
|
|
3748
|
+
|
|
3749
|
+
/**
|
|
3750
|
+
* Returns a unique number for the given object. Later calls will still return the same number.
|
|
3751
|
+
*
|
|
3752
|
+
* @param {Object} obj
|
|
3753
|
+
* @returns {number}
|
|
3754
|
+
*/
|
|
3755
|
+
objId: function (obj) {
|
|
3756
|
+
if (!obj['__id']) {
|
|
3757
|
+
Object.defineProperty(obj, '__id', { value: ++uniqueId });
|
|
3758
|
+
}
|
|
3759
|
+
return obj['__id'];
|
|
3760
|
+
},
|
|
3588
3761
|
|
|
3589
|
-
|
|
3590
|
-
|
|
3591
|
-
|
|
3762
|
+
/**
|
|
3763
|
+
* Creates a deep clone of the given object.
|
|
3764
|
+
*
|
|
3765
|
+
* The main intended use of this function is to clone language definitions.
|
|
3766
|
+
*
|
|
3767
|
+
* @param {T} o
|
|
3768
|
+
* @param {Record<number, any>} [visited]
|
|
3769
|
+
* @returns {T}
|
|
3770
|
+
* @template T
|
|
3771
|
+
*/
|
|
3772
|
+
clone: function deepClone(o, visited) {
|
|
3773
|
+
visited = visited || {};
|
|
3774
|
+
|
|
3775
|
+
var clone; var id;
|
|
3776
|
+
switch (_.util.type(o)) {
|
|
3777
|
+
case 'Object':
|
|
3778
|
+
id = _.util.objId(o);
|
|
3779
|
+
if (visited[id]) {
|
|
3780
|
+
return visited[id];
|
|
3592
3781
|
}
|
|
3593
|
-
|
|
3782
|
+
clone = /** @type {Record<string, any>} */ ({});
|
|
3783
|
+
visited[id] = clone;
|
|
3784
|
+
|
|
3785
|
+
for (var key in o) {
|
|
3786
|
+
if (o.hasOwnProperty(key)) {
|
|
3787
|
+
clone[key] = deepClone(o[key], visited);
|
|
3788
|
+
}
|
|
3789
|
+
}
|
|
3790
|
+
|
|
3791
|
+
return /** @type {any} */ (clone);
|
|
3792
|
+
|
|
3793
|
+
case 'Array':
|
|
3794
|
+
id = _.util.objId(o);
|
|
3795
|
+
if (visited[id]) {
|
|
3796
|
+
return visited[id];
|
|
3797
|
+
}
|
|
3798
|
+
clone = [];
|
|
3799
|
+
visited[id] = clone;
|
|
3800
|
+
|
|
3801
|
+
(/** @type {Array} */(/** @type {any} */(o))).forEach(function (v, i) {
|
|
3802
|
+
clone[i] = deepClone(v, visited);
|
|
3803
|
+
});
|
|
3594
3804
|
|
|
3595
|
-
|
|
3805
|
+
return /** @type {any} */ (clone);
|
|
3596
3806
|
|
|
3597
|
-
|
|
3598
|
-
|
|
3599
|
-
|
|
3600
|
-
|
|
3807
|
+
default:
|
|
3808
|
+
return o;
|
|
3809
|
+
}
|
|
3810
|
+
},
|
|
3811
|
+
|
|
3812
|
+
/**
|
|
3813
|
+
* Returns the Prism language of the given element set by a `language-xxxx` or `lang-xxxx` class.
|
|
3814
|
+
*
|
|
3815
|
+
* If no language is set for the element or the element is `null` or `undefined`, `none` will be returned.
|
|
3816
|
+
*
|
|
3817
|
+
* @param {Element} element
|
|
3818
|
+
* @returns {string}
|
|
3819
|
+
*/
|
|
3820
|
+
getLanguage: function (element) {
|
|
3821
|
+
while (element) {
|
|
3822
|
+
var m = lang.exec(element.className);
|
|
3823
|
+
if (m) {
|
|
3824
|
+
return m[1].toLowerCase();
|
|
3601
3825
|
}
|
|
3602
|
-
|
|
3603
|
-
|
|
3826
|
+
element = element.parentElement;
|
|
3827
|
+
}
|
|
3828
|
+
return 'none';
|
|
3829
|
+
},
|
|
3830
|
+
|
|
3831
|
+
/**
|
|
3832
|
+
* Sets the Prism `language-xxxx` class of the given element.
|
|
3833
|
+
*
|
|
3834
|
+
* @param {Element} element
|
|
3835
|
+
* @param {string} language
|
|
3836
|
+
* @returns {void}
|
|
3837
|
+
*/
|
|
3838
|
+
setLanguage: function (element, language) {
|
|
3839
|
+
// remove all `language-xxxx` classes
|
|
3840
|
+
// (this might leave behind a leading space)
|
|
3841
|
+
element.className = element.className.replace(RegExp(lang, 'gi'), '');
|
|
3842
|
+
|
|
3843
|
+
// add the new `language-xxxx` class
|
|
3844
|
+
// (using `classList` will automatically clean up spaces for us)
|
|
3845
|
+
element.classList.add('language-' + language);
|
|
3846
|
+
},
|
|
3604
3847
|
|
|
3605
|
-
|
|
3606
|
-
|
|
3607
|
-
|
|
3848
|
+
/**
|
|
3849
|
+
* Returns the script element that is currently executing.
|
|
3850
|
+
*
|
|
3851
|
+
* This does __not__ work for line script element.
|
|
3852
|
+
*
|
|
3853
|
+
* @returns {HTMLScriptElement | null}
|
|
3854
|
+
*/
|
|
3855
|
+
currentScript: function () {
|
|
3856
|
+
if (typeof document === 'undefined') {
|
|
3857
|
+
return null;
|
|
3858
|
+
}
|
|
3859
|
+
if (document.currentScript && document.currentScript.tagName === 'SCRIPT' && 1 < 2 /* hack to trip TS' flow analysis */) {
|
|
3860
|
+
return /** @type {any} */ (document.currentScript);
|
|
3861
|
+
}
|
|
3608
3862
|
|
|
3609
|
-
|
|
3863
|
+
// IE11 workaround
|
|
3864
|
+
// we'll get the src of the current script by parsing IE11's error stack trace
|
|
3865
|
+
// this will not work for inline scripts
|
|
3866
|
+
|
|
3867
|
+
try {
|
|
3868
|
+
throw new Error();
|
|
3869
|
+
} catch (err) {
|
|
3870
|
+
// Get file src url from stack. Specifically works with the format of stack traces in IE.
|
|
3871
|
+
// A stack will look like this:
|
|
3872
|
+
//
|
|
3873
|
+
// Error
|
|
3874
|
+
// at _.util.currentScript (http://localhost/components/prism-core.js:119:5)
|
|
3875
|
+
// at Global code (http://localhost/components/prism-core.js:606:1)
|
|
3876
|
+
|
|
3877
|
+
var src = (/at [^(\r\n]*\((.*):[^:]+:[^:]+\)$/i.exec(err.stack) || [])[1];
|
|
3878
|
+
if (src) {
|
|
3879
|
+
var scripts = document.getElementsByTagName('script');
|
|
3880
|
+
for (var i in scripts) {
|
|
3881
|
+
if (scripts[i].src == src) {
|
|
3882
|
+
return scripts[i];
|
|
3883
|
+
}
|
|
3884
|
+
}
|
|
3885
|
+
}
|
|
3886
|
+
return null;
|
|
3887
|
+
}
|
|
3888
|
+
},
|
|
3610
3889
|
|
|
3611
|
-
|
|
3612
|
-
|
|
3890
|
+
/**
|
|
3891
|
+
* Returns whether a given class is active for `element`.
|
|
3892
|
+
*
|
|
3893
|
+
* The class can be activated if `element` or one of its ancestors has the given class and it can be deactivated
|
|
3894
|
+
* if `element` or one of its ancestors has the negated version of the given class. The _negated version_ of the
|
|
3895
|
+
* given class is just the given class with a `no-` prefix.
|
|
3896
|
+
*
|
|
3897
|
+
* Whether the class is active is determined by the closest ancestor of `element` (where `element` itself is
|
|
3898
|
+
* closest ancestor) that has the given class or the negated version of it. If neither `element` nor any of its
|
|
3899
|
+
* ancestors have the given class or the negated version of it, then the default activation will be returned.
|
|
3900
|
+
*
|
|
3901
|
+
* In the paradoxical situation where the closest ancestor contains __both__ the given class and the negated
|
|
3902
|
+
* version of it, the class is considered active.
|
|
3903
|
+
*
|
|
3904
|
+
* @param {Element} element
|
|
3905
|
+
* @param {string} className
|
|
3906
|
+
* @param {boolean} [defaultActivation=false]
|
|
3907
|
+
* @returns {boolean}
|
|
3908
|
+
*/
|
|
3909
|
+
isActive: function (element, className, defaultActivation) {
|
|
3910
|
+
var no = 'no-' + className;
|
|
3911
|
+
|
|
3912
|
+
while (element) {
|
|
3913
|
+
var classList = element.classList;
|
|
3914
|
+
if (classList.contains(className)) {
|
|
3915
|
+
return true;
|
|
3916
|
+
}
|
|
3917
|
+
if (classList.contains(no)) {
|
|
3918
|
+
return false;
|
|
3919
|
+
}
|
|
3920
|
+
element = element.parentElement;
|
|
3921
|
+
}
|
|
3922
|
+
return !!defaultActivation;
|
|
3613
3923
|
}
|
|
3614
3924
|
},
|
|
3615
3925
|
|
|
3616
3926
|
/**
|
|
3617
|
-
*
|
|
3618
|
-
*
|
|
3619
|
-
* If no language is set for the element or the element is `null` or `undefined`, `none` will be returned.
|
|
3927
|
+
* This namespace contains all currently loaded languages and the some helper functions to create and modify languages.
|
|
3620
3928
|
*
|
|
3621
|
-
* @
|
|
3622
|
-
* @
|
|
3929
|
+
* @namespace
|
|
3930
|
+
* @memberof Prism
|
|
3931
|
+
* @public
|
|
3623
3932
|
*/
|
|
3624
|
-
|
|
3625
|
-
|
|
3626
|
-
|
|
3627
|
-
|
|
3628
|
-
|
|
3933
|
+
languages: {
|
|
3934
|
+
/**
|
|
3935
|
+
* The grammar for plain, unformatted text.
|
|
3936
|
+
*/
|
|
3937
|
+
plain: plainTextGrammar,
|
|
3938
|
+
plaintext: plainTextGrammar,
|
|
3939
|
+
text: plainTextGrammar,
|
|
3940
|
+
txt: plainTextGrammar,
|
|
3941
|
+
|
|
3942
|
+
/**
|
|
3943
|
+
* Creates a deep copy of the language with the given id and appends the given tokens.
|
|
3944
|
+
*
|
|
3945
|
+
* If a token in `redef` also appears in the copied language, then the existing token in the copied language
|
|
3946
|
+
* will be overwritten at its original position.
|
|
3947
|
+
*
|
|
3948
|
+
* ## Best practices
|
|
3949
|
+
*
|
|
3950
|
+
* Since the position of overwriting tokens (token in `redef` that overwrite tokens in the copied language)
|
|
3951
|
+
* doesn't matter, they can technically be in any order. However, this can be confusing to others that trying to
|
|
3952
|
+
* understand the language definition because, normally, the order of tokens matters in Prism grammars.
|
|
3953
|
+
*
|
|
3954
|
+
* Therefore, it is encouraged to order overwriting tokens according to the positions of the overwritten tokens.
|
|
3955
|
+
* Furthermore, all non-overwriting tokens should be placed after the overwriting ones.
|
|
3956
|
+
*
|
|
3957
|
+
* @param {string} id The id of the language to extend. This has to be a key in `Prism.languages`.
|
|
3958
|
+
* @param {Grammar} redef The new tokens to append.
|
|
3959
|
+
* @returns {Grammar} The new language created.
|
|
3960
|
+
* @public
|
|
3961
|
+
* @example
|
|
3962
|
+
* Prism.languages['css-with-colors'] = Prism.languages.extend('css', {
|
|
3963
|
+
* // Prism.languages.css already has a 'comment' token, so this token will overwrite CSS' 'comment' token
|
|
3964
|
+
* // at its original position
|
|
3965
|
+
* 'comment': { ... },
|
|
3966
|
+
* // CSS doesn't have a 'color' token, so this token will be appended
|
|
3967
|
+
* 'color': /\b(?:red|green|blue)\b/
|
|
3968
|
+
* });
|
|
3969
|
+
*/
|
|
3970
|
+
extend: function (id, redef) {
|
|
3971
|
+
var lang = _.util.clone(_.languages[id]);
|
|
3972
|
+
|
|
3973
|
+
for (var key in redef) {
|
|
3974
|
+
lang[key] = redef[key];
|
|
3975
|
+
}
|
|
3976
|
+
|
|
3977
|
+
return lang;
|
|
3978
|
+
},
|
|
3979
|
+
|
|
3980
|
+
/**
|
|
3981
|
+
* Inserts tokens _before_ another token in a language definition or any other grammar.
|
|
3982
|
+
*
|
|
3983
|
+
* ## Usage
|
|
3984
|
+
*
|
|
3985
|
+
* This helper method makes it easy to modify existing languages. For example, the CSS language definition
|
|
3986
|
+
* not only defines CSS highlighting for CSS documents, but also needs to define highlighting for CSS embedded
|
|
3987
|
+
* in HTML through `<style>` elements. To do this, it needs to modify `Prism.languages.markup` and add the
|
|
3988
|
+
* appropriate tokens. However, `Prism.languages.markup` is a regular JavaScript object literal, so if you do
|
|
3989
|
+
* this:
|
|
3990
|
+
*
|
|
3991
|
+
* ```js
|
|
3992
|
+
* Prism.languages.markup.style = {
|
|
3993
|
+
* // token
|
|
3994
|
+
* };
|
|
3995
|
+
* ```
|
|
3996
|
+
*
|
|
3997
|
+
* then the `style` token will be added (and processed) at the end. `insertBefore` allows you to insert tokens
|
|
3998
|
+
* before existing tokens. For the CSS example above, you would use it like this:
|
|
3999
|
+
*
|
|
4000
|
+
* ```js
|
|
4001
|
+
* Prism.languages.insertBefore('markup', 'cdata', {
|
|
4002
|
+
* 'style': {
|
|
4003
|
+
* // token
|
|
4004
|
+
* }
|
|
4005
|
+
* });
|
|
4006
|
+
* ```
|
|
4007
|
+
*
|
|
4008
|
+
* ## Special cases
|
|
4009
|
+
*
|
|
4010
|
+
* If the grammars of `inside` and `insert` have tokens with the same name, the tokens in `inside`'s grammar
|
|
4011
|
+
* will be ignored.
|
|
4012
|
+
*
|
|
4013
|
+
* This behavior can be used to insert tokens after `before`:
|
|
4014
|
+
*
|
|
4015
|
+
* ```js
|
|
4016
|
+
* Prism.languages.insertBefore('markup', 'comment', {
|
|
4017
|
+
* 'comment': Prism.languages.markup.comment,
|
|
4018
|
+
* // tokens after 'comment'
|
|
4019
|
+
* });
|
|
4020
|
+
* ```
|
|
4021
|
+
*
|
|
4022
|
+
* ## Limitations
|
|
4023
|
+
*
|
|
4024
|
+
* The main problem `insertBefore` has to solve is iteration order. Since ES2015, the iteration order for object
|
|
4025
|
+
* properties is guaranteed to be the insertion order (except for integer keys) but some browsers behave
|
|
4026
|
+
* differently when keys are deleted and re-inserted. So `insertBefore` can't be implemented by temporarily
|
|
4027
|
+
* deleting properties which is necessary to insert at arbitrary positions.
|
|
4028
|
+
*
|
|
4029
|
+
* To solve this problem, `insertBefore` doesn't actually insert the given tokens into the target object.
|
|
4030
|
+
* Instead, it will create a new object and replace all references to the target object with the new one. This
|
|
4031
|
+
* can be done without temporarily deleting properties, so the iteration order is well-defined.
|
|
4032
|
+
*
|
|
4033
|
+
* However, only references that can be reached from `Prism.languages` or `insert` will be replaced. I.e. if
|
|
4034
|
+
* you hold the target object in a variable, then the value of the variable will not change.
|
|
4035
|
+
*
|
|
4036
|
+
* ```js
|
|
4037
|
+
* var oldMarkup = Prism.languages.markup;
|
|
4038
|
+
* var newMarkup = Prism.languages.insertBefore('markup', 'comment', { ... });
|
|
4039
|
+
*
|
|
4040
|
+
* assert(oldMarkup !== Prism.languages.markup);
|
|
4041
|
+
* assert(newMarkup === Prism.languages.markup);
|
|
4042
|
+
* ```
|
|
4043
|
+
*
|
|
4044
|
+
* @param {string} inside The property of `root` (e.g. a language id in `Prism.languages`) that contains the
|
|
4045
|
+
* object to be modified.
|
|
4046
|
+
* @param {string} before The key to insert before.
|
|
4047
|
+
* @param {Grammar} insert An object containing the key-value pairs to be inserted.
|
|
4048
|
+
* @param {Object<string, any>} [root] The object containing `inside`, i.e. the object that contains the
|
|
4049
|
+
* object to be modified.
|
|
4050
|
+
*
|
|
4051
|
+
* Defaults to `Prism.languages`.
|
|
4052
|
+
* @returns {Grammar} The new grammar object.
|
|
4053
|
+
* @public
|
|
4054
|
+
*/
|
|
4055
|
+
insertBefore: function (inside, before, insert, root) {
|
|
4056
|
+
root = root || /** @type {any} */ (_.languages);
|
|
4057
|
+
var grammar = root[inside];
|
|
4058
|
+
/** @type {Grammar} */
|
|
4059
|
+
var ret = {};
|
|
4060
|
+
|
|
4061
|
+
for (var token in grammar) {
|
|
4062
|
+
if (grammar.hasOwnProperty(token)) {
|
|
4063
|
+
|
|
4064
|
+
if (token == before) {
|
|
4065
|
+
for (var newToken in insert) {
|
|
4066
|
+
if (insert.hasOwnProperty(newToken)) {
|
|
4067
|
+
ret[newToken] = insert[newToken];
|
|
4068
|
+
}
|
|
4069
|
+
}
|
|
4070
|
+
}
|
|
4071
|
+
|
|
4072
|
+
// Do not insert token which also occur in insert. See #1525
|
|
4073
|
+
if (!insert.hasOwnProperty(token)) {
|
|
4074
|
+
ret[token] = grammar[token];
|
|
4075
|
+
}
|
|
4076
|
+
}
|
|
4077
|
+
}
|
|
4078
|
+
|
|
4079
|
+
var old = root[inside];
|
|
4080
|
+
root[inside] = ret;
|
|
4081
|
+
|
|
4082
|
+
// Update references in other language definitions
|
|
4083
|
+
_.languages.DFS(_.languages, function (key, value) {
|
|
4084
|
+
if (value === old && key != inside) {
|
|
4085
|
+
this[key] = ret;
|
|
4086
|
+
}
|
|
4087
|
+
});
|
|
4088
|
+
|
|
4089
|
+
return ret;
|
|
4090
|
+
},
|
|
4091
|
+
|
|
4092
|
+
// Traverse a language definition with Depth First Search
|
|
4093
|
+
DFS: function DFS(o, callback, type, visited) {
|
|
4094
|
+
visited = visited || {};
|
|
4095
|
+
|
|
4096
|
+
var objId = _.util.objId;
|
|
4097
|
+
|
|
4098
|
+
for (var i in o) {
|
|
4099
|
+
if (o.hasOwnProperty(i)) {
|
|
4100
|
+
callback.call(o, i, o[i], type || i);
|
|
4101
|
+
|
|
4102
|
+
var property = o[i];
|
|
4103
|
+
var propertyType = _.util.type(property);
|
|
4104
|
+
|
|
4105
|
+
if (propertyType === 'Object' && !visited[objId(property)]) {
|
|
4106
|
+
visited[objId(property)] = true;
|
|
4107
|
+
DFS(property, callback, null, visited);
|
|
4108
|
+
} else if (propertyType === 'Array' && !visited[objId(property)]) {
|
|
4109
|
+
visited[objId(property)] = true;
|
|
4110
|
+
DFS(property, callback, i, visited);
|
|
4111
|
+
}
|
|
4112
|
+
}
|
|
3629
4113
|
}
|
|
3630
|
-
element = element.parentElement;
|
|
3631
4114
|
}
|
|
3632
|
-
return 'none';
|
|
3633
4115
|
},
|
|
3634
4116
|
|
|
4117
|
+
plugins: {},
|
|
4118
|
+
|
|
3635
4119
|
/**
|
|
3636
|
-
*
|
|
4120
|
+
* This is the most high-level function in Prism’s API.
|
|
4121
|
+
* It fetches all the elements that have a `.language-xxxx` class and then calls {@link Prism.highlightElement} on
|
|
4122
|
+
* each one of them.
|
|
3637
4123
|
*
|
|
3638
|
-
*
|
|
3639
|
-
*
|
|
3640
|
-
* @
|
|
4124
|
+
* This is equivalent to `Prism.highlightAllUnder(document, async, callback)`.
|
|
4125
|
+
*
|
|
4126
|
+
* @param {boolean} [async=false] Same as in {@link Prism.highlightAllUnder}.
|
|
4127
|
+
* @param {HighlightCallback} [callback] Same as in {@link Prism.highlightAllUnder}.
|
|
4128
|
+
* @memberof Prism
|
|
4129
|
+
* @public
|
|
3641
4130
|
*/
|
|
3642
|
-
|
|
3643
|
-
|
|
3644
|
-
// (this might leave behind a leading space)
|
|
3645
|
-
element.className = element.className.replace(RegExp(lang, 'gi'), '');
|
|
3646
|
-
|
|
3647
|
-
// add the new `language-xxxx` class
|
|
3648
|
-
// (using `classList` will automatically clean up spaces for us)
|
|
3649
|
-
element.classList.add('language-' + language);
|
|
4131
|
+
highlightAll: function (async, callback) {
|
|
4132
|
+
_.highlightAllUnder(document, async, callback);
|
|
3650
4133
|
},
|
|
3651
4134
|
|
|
3652
4135
|
/**
|
|
3653
|
-
*
|
|
4136
|
+
* Fetches all the descendants of `container` that have a `.language-xxxx` class and then calls
|
|
4137
|
+
* {@link Prism.highlightElement} on each one of them.
|
|
3654
4138
|
*
|
|
3655
|
-
*
|
|
4139
|
+
* The following hooks will be run:
|
|
4140
|
+
* 1. `before-highlightall`
|
|
4141
|
+
* 2. `before-all-elements-highlight`
|
|
4142
|
+
* 3. All hooks of {@link Prism.highlightElement} for each element.
|
|
3656
4143
|
*
|
|
3657
|
-
* @
|
|
4144
|
+
* @param {ParentNode} container The root element, whose descendants that have a `.language-xxxx` class will be highlighted.
|
|
4145
|
+
* @param {boolean} [async=false] Whether each element is to be highlighted asynchronously using Web Workers.
|
|
4146
|
+
* @param {HighlightCallback} [callback] An optional callback to be invoked on each element after its highlighting is done.
|
|
4147
|
+
* @memberof Prism
|
|
4148
|
+
* @public
|
|
3658
4149
|
*/
|
|
3659
|
-
|
|
3660
|
-
|
|
3661
|
-
|
|
3662
|
-
|
|
3663
|
-
|
|
3664
|
-
|
|
3665
|
-
}
|
|
4150
|
+
highlightAllUnder: function (container, async, callback) {
|
|
4151
|
+
var env = {
|
|
4152
|
+
callback: callback,
|
|
4153
|
+
container: container,
|
|
4154
|
+
selector: 'code[class*="language-"], [class*="language-"] code, code[class*="lang-"], [class*="lang-"] code'
|
|
4155
|
+
};
|
|
3666
4156
|
|
|
3667
|
-
|
|
3668
|
-
|
|
3669
|
-
|
|
3670
|
-
|
|
3671
|
-
|
|
3672
|
-
|
|
3673
|
-
|
|
3674
|
-
|
|
3675
|
-
// A stack will look like this:
|
|
3676
|
-
//
|
|
3677
|
-
// Error
|
|
3678
|
-
// at _.util.currentScript (http://localhost/components/prism-core.js:119:5)
|
|
3679
|
-
// at Global code (http://localhost/components/prism-core.js:606:1)
|
|
3680
|
-
|
|
3681
|
-
var src = (/at [^(\r\n]*\((.*):[^:]+:[^:]+\)$/i.exec(err.stack) || [])[1];
|
|
3682
|
-
if (src) {
|
|
3683
|
-
var scripts = document.getElementsByTagName('script');
|
|
3684
|
-
for (var i in scripts) {
|
|
3685
|
-
if (scripts[i].src == src) {
|
|
3686
|
-
return scripts[i];
|
|
3687
|
-
}
|
|
3688
|
-
}
|
|
3689
|
-
}
|
|
3690
|
-
return null;
|
|
4157
|
+
_.hooks.run('before-highlightall', env);
|
|
4158
|
+
|
|
4159
|
+
env.elements = Array.prototype.slice.apply(env.container.querySelectorAll(env.selector));
|
|
4160
|
+
|
|
4161
|
+
_.hooks.run('before-all-elements-highlight', env);
|
|
4162
|
+
|
|
4163
|
+
for (var i = 0, element; (element = env.elements[i++]);) {
|
|
4164
|
+
_.highlightElement(element, async === true, env.callback);
|
|
3691
4165
|
}
|
|
3692
4166
|
},
|
|
3693
4167
|
|
|
3694
4168
|
/**
|
|
3695
|
-
*
|
|
4169
|
+
* Highlights the code inside a single element.
|
|
3696
4170
|
*
|
|
3697
|
-
* The
|
|
3698
|
-
*
|
|
3699
|
-
*
|
|
4171
|
+
* The following hooks will be run:
|
|
4172
|
+
* 1. `before-sanity-check`
|
|
4173
|
+
* 2. `before-highlight`
|
|
4174
|
+
* 3. All hooks of {@link Prism.highlight}. These hooks will be run by an asynchronous worker if `async` is `true`.
|
|
4175
|
+
* 4. `before-insert`
|
|
4176
|
+
* 5. `after-highlight`
|
|
4177
|
+
* 6. `complete`
|
|
3700
4178
|
*
|
|
3701
|
-
*
|
|
3702
|
-
*
|
|
3703
|
-
* ancestors have the given class or the negated version of it, then the default activation will be returned.
|
|
4179
|
+
* Some the above hooks will be skipped if the element doesn't contain any text or there is no grammar loaded for
|
|
4180
|
+
* the element's language.
|
|
3704
4181
|
*
|
|
3705
|
-
*
|
|
3706
|
-
*
|
|
4182
|
+
* @param {Element} element The element containing the code.
|
|
4183
|
+
* It must have a class of `language-xxxx` to be processed, where `xxxx` is a valid language identifier.
|
|
4184
|
+
* @param {boolean} [async=false] Whether the element is to be highlighted asynchronously using Web Workers
|
|
4185
|
+
* to improve performance and avoid blocking the UI when highlighting very large chunks of code. This option is
|
|
4186
|
+
* [disabled by default](https://prismjs.com/faq.html#why-is-asynchronous-highlighting-disabled-by-default).
|
|
3707
4187
|
*
|
|
3708
|
-
*
|
|
3709
|
-
*
|
|
3710
|
-
*
|
|
3711
|
-
* @
|
|
4188
|
+
* Note: All language definitions required to highlight the code must be included in the main `prism.js` file for
|
|
4189
|
+
* asynchronous highlighting to work. You can build your own bundle on the
|
|
4190
|
+
* [Download page](https://prismjs.com/download.html).
|
|
4191
|
+
* @param {HighlightCallback} [callback] An optional callback to be invoked after the highlighting is done.
|
|
4192
|
+
* Mostly useful when `async` is `true`, since in that case, the highlighting is done asynchronously.
|
|
4193
|
+
* @memberof Prism
|
|
4194
|
+
* @public
|
|
3712
4195
|
*/
|
|
3713
|
-
|
|
3714
|
-
|
|
4196
|
+
highlightElement: function (element, async, callback) {
|
|
4197
|
+
// Find language
|
|
4198
|
+
var language = _.util.getLanguage(element);
|
|
4199
|
+
var grammar = _.languages[language];
|
|
4200
|
+
|
|
4201
|
+
// Set language on the element, if not present
|
|
4202
|
+
_.util.setLanguage(element, language);
|
|
4203
|
+
|
|
4204
|
+
// Set language on the parent, for styling
|
|
4205
|
+
var parent = element.parentElement;
|
|
4206
|
+
if (parent && parent.nodeName.toLowerCase() === 'pre') {
|
|
4207
|
+
_.util.setLanguage(parent, language);
|
|
4208
|
+
}
|
|
3715
4209
|
|
|
3716
|
-
|
|
3717
|
-
|
|
3718
|
-
|
|
3719
|
-
|
|
3720
|
-
|
|
3721
|
-
|
|
3722
|
-
|
|
3723
|
-
|
|
3724
|
-
|
|
4210
|
+
var code = element.textContent;
|
|
4211
|
+
|
|
4212
|
+
var env = {
|
|
4213
|
+
element: element,
|
|
4214
|
+
language: language,
|
|
4215
|
+
grammar: grammar,
|
|
4216
|
+
code: code
|
|
4217
|
+
};
|
|
4218
|
+
|
|
4219
|
+
function insertHighlightedCode(highlightedCode) {
|
|
4220
|
+
env.highlightedCode = highlightedCode;
|
|
4221
|
+
|
|
4222
|
+
_.hooks.run('before-insert', env);
|
|
4223
|
+
|
|
4224
|
+
env.element.innerHTML = env.highlightedCode;
|
|
4225
|
+
|
|
4226
|
+
_.hooks.run('after-highlight', env);
|
|
4227
|
+
_.hooks.run('complete', env);
|
|
4228
|
+
callback && callback.call(env.element);
|
|
3725
4229
|
}
|
|
3726
|
-
return !!defaultActivation;
|
|
3727
|
-
}
|
|
3728
|
-
},
|
|
3729
4230
|
|
|
3730
|
-
|
|
3731
|
-
|
|
3732
|
-
|
|
3733
|
-
|
|
3734
|
-
|
|
3735
|
-
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3739
|
-
|
|
3740
|
-
|
|
3741
|
-
|
|
3742
|
-
|
|
3743
|
-
|
|
3744
|
-
|
|
4231
|
+
_.hooks.run('before-sanity-check', env);
|
|
4232
|
+
|
|
4233
|
+
// plugins may change/add the parent/element
|
|
4234
|
+
parent = env.element.parentElement;
|
|
4235
|
+
if (parent && parent.nodeName.toLowerCase() === 'pre' && !parent.hasAttribute('tabindex')) {
|
|
4236
|
+
parent.setAttribute('tabindex', '0');
|
|
4237
|
+
}
|
|
4238
|
+
|
|
4239
|
+
if (!env.code) {
|
|
4240
|
+
_.hooks.run('complete', env);
|
|
4241
|
+
callback && callback.call(env.element);
|
|
4242
|
+
return;
|
|
4243
|
+
}
|
|
4244
|
+
|
|
4245
|
+
_.hooks.run('before-highlight', env);
|
|
4246
|
+
|
|
4247
|
+
if (!env.grammar) {
|
|
4248
|
+
insertHighlightedCode(_.util.encode(env.code));
|
|
4249
|
+
return;
|
|
4250
|
+
}
|
|
4251
|
+
|
|
4252
|
+
if (async && _self.Worker) {
|
|
4253
|
+
var worker = new Worker(_.filename);
|
|
4254
|
+
|
|
4255
|
+
worker.onmessage = function (evt) {
|
|
4256
|
+
insertHighlightedCode(evt.data);
|
|
4257
|
+
};
|
|
4258
|
+
|
|
4259
|
+
worker.postMessage(JSON.stringify({
|
|
4260
|
+
language: env.language,
|
|
4261
|
+
code: env.code,
|
|
4262
|
+
immediateClose: true
|
|
4263
|
+
}));
|
|
4264
|
+
} else {
|
|
4265
|
+
insertHighlightedCode(_.highlight(env.code, env.grammar, env.language));
|
|
4266
|
+
}
|
|
4267
|
+
},
|
|
3745
4268
|
|
|
3746
4269
|
/**
|
|
3747
|
-
*
|
|
3748
|
-
*
|
|
3749
|
-
* If a token in `redef` also appears in the copied language, then the existing token in the copied language
|
|
3750
|
-
* will be overwritten at its original position.
|
|
4270
|
+
* Low-level function, only use if you know what you’re doing. It accepts a string of text as input
|
|
4271
|
+
* and the language definitions to use, and returns a string with the HTML produced.
|
|
3751
4272
|
*
|
|
3752
|
-
*
|
|
4273
|
+
* The following hooks will be run:
|
|
4274
|
+
* 1. `before-tokenize`
|
|
4275
|
+
* 2. `after-tokenize`
|
|
4276
|
+
* 3. `wrap`: On each {@link Token}.
|
|
3753
4277
|
*
|
|
3754
|
-
*
|
|
3755
|
-
*
|
|
3756
|
-
* understand the language definition because, normally, the order of tokens matters in Prism grammars.
|
|
4278
|
+
* @param {string} text A string with the code to be highlighted.
|
|
4279
|
+
* @param {Grammar} grammar An object containing the tokens to use.
|
|
3757
4280
|
*
|
|
3758
|
-
*
|
|
3759
|
-
*
|
|
3760
|
-
*
|
|
3761
|
-
* @
|
|
3762
|
-
* @param {Grammar} redef The new tokens to append.
|
|
3763
|
-
* @returns {Grammar} The new language created.
|
|
4281
|
+
* Usually a language definition like `Prism.languages.markup`.
|
|
4282
|
+
* @param {string} language The name of the language definition passed to `grammar`.
|
|
4283
|
+
* @returns {string} The highlighted HTML.
|
|
4284
|
+
* @memberof Prism
|
|
3764
4285
|
* @public
|
|
3765
4286
|
* @example
|
|
3766
|
-
* Prism.
|
|
3767
|
-
* // Prism.languages.css already has a 'comment' token, so this token will overwrite CSS' 'comment' token
|
|
3768
|
-
* // at its original position
|
|
3769
|
-
* 'comment': { ... },
|
|
3770
|
-
* // CSS doesn't have a 'color' token, so this token will be appended
|
|
3771
|
-
* 'color': /\b(?:red|green|blue)\b/
|
|
3772
|
-
* });
|
|
4287
|
+
* Prism.highlight('var foo = true;', Prism.languages.javascript, 'javascript');
|
|
3773
4288
|
*/
|
|
3774
|
-
|
|
3775
|
-
var
|
|
3776
|
-
|
|
3777
|
-
|
|
3778
|
-
|
|
4289
|
+
highlight: function (text, grammar, language) {
|
|
4290
|
+
var env = {
|
|
4291
|
+
code: text,
|
|
4292
|
+
grammar: grammar,
|
|
4293
|
+
language: language
|
|
4294
|
+
};
|
|
4295
|
+
_.hooks.run('before-tokenize', env);
|
|
4296
|
+
if (!env.grammar) {
|
|
4297
|
+
throw new Error('The language "' + env.language + '" has no grammar.');
|
|
3779
4298
|
}
|
|
3780
|
-
|
|
3781
|
-
|
|
4299
|
+
env.tokens = _.tokenize(env.code, env.grammar);
|
|
4300
|
+
_.hooks.run('after-tokenize', env);
|
|
4301
|
+
return Token.stringify(_.util.encode(env.tokens), env.language);
|
|
3782
4302
|
},
|
|
3783
4303
|
|
|
3784
4304
|
/**
|
|
3785
|
-
*
|
|
3786
|
-
*
|
|
3787
|
-
* ## Usage
|
|
4305
|
+
* This is the heart of Prism, and the most low-level function you can use. It accepts a string of text as input
|
|
4306
|
+
* and the language definitions to use, and returns an array with the tokenized code.
|
|
3788
4307
|
*
|
|
3789
|
-
*
|
|
3790
|
-
* not only defines CSS highlighting for CSS documents, but also needs to define highlighting for CSS embedded
|
|
3791
|
-
* in HTML through `<style>` elements. To do this, it needs to modify `Prism.languages.markup` and add the
|
|
3792
|
-
* appropriate tokens. However, `Prism.languages.markup` is a regular JavaScript object literal, so if you do
|
|
3793
|
-
* this:
|
|
4308
|
+
* When the language definition includes nested tokens, the function is called recursively on each of these tokens.
|
|
3794
4309
|
*
|
|
3795
|
-
*
|
|
3796
|
-
* Prism.languages.markup.style = {
|
|
3797
|
-
* // token
|
|
3798
|
-
* };
|
|
3799
|
-
* ```
|
|
4310
|
+
* This method could be useful in other contexts as well, as a very crude parser.
|
|
3800
4311
|
*
|
|
3801
|
-
*
|
|
3802
|
-
*
|
|
4312
|
+
* @param {string} text A string with the code to be highlighted.
|
|
4313
|
+
* @param {Grammar} grammar An object containing the tokens to use.
|
|
3803
4314
|
*
|
|
3804
|
-
*
|
|
3805
|
-
*
|
|
3806
|
-
*
|
|
3807
|
-
*
|
|
4315
|
+
* Usually a language definition like `Prism.languages.markup`.
|
|
4316
|
+
* @returns {TokenStream} An array of strings and tokens, a token stream.
|
|
4317
|
+
* @memberof Prism
|
|
4318
|
+
* @public
|
|
4319
|
+
* @example
|
|
4320
|
+
* let code = `var foo = 0;`;
|
|
4321
|
+
* let tokens = Prism.tokenize(code, Prism.languages.javascript);
|
|
4322
|
+
* tokens.forEach(token => {
|
|
4323
|
+
* if (token instanceof Prism.Token && token.type === 'number') {
|
|
4324
|
+
* console.log(`Found numeric literal: ${token.content}`);
|
|
3808
4325
|
* }
|
|
3809
4326
|
* });
|
|
3810
|
-
* ```
|
|
3811
|
-
*
|
|
3812
|
-
* ## Special cases
|
|
3813
|
-
*
|
|
3814
|
-
* If the grammars of `inside` and `insert` have tokens with the same name, the tokens in `inside`'s grammar
|
|
3815
|
-
* will be ignored.
|
|
3816
|
-
*
|
|
3817
|
-
* This behavior can be used to insert tokens after `before`:
|
|
3818
|
-
*
|
|
3819
|
-
* ```js
|
|
3820
|
-
* Prism.languages.insertBefore('markup', 'comment', {
|
|
3821
|
-
* 'comment': Prism.languages.markup.comment,
|
|
3822
|
-
* // tokens after 'comment'
|
|
3823
|
-
* });
|
|
3824
|
-
* ```
|
|
3825
|
-
*
|
|
3826
|
-
* ## Limitations
|
|
3827
|
-
*
|
|
3828
|
-
* The main problem `insertBefore` has to solve is iteration order. Since ES2015, the iteration order for object
|
|
3829
|
-
* properties is guaranteed to be the insertion order (except for integer keys) but some browsers behave
|
|
3830
|
-
* differently when keys are deleted and re-inserted. So `insertBefore` can't be implemented by temporarily
|
|
3831
|
-
* deleting properties which is necessary to insert at arbitrary positions.
|
|
3832
|
-
*
|
|
3833
|
-
* To solve this problem, `insertBefore` doesn't actually insert the given tokens into the target object.
|
|
3834
|
-
* Instead, it will create a new object and replace all references to the target object with the new one. This
|
|
3835
|
-
* can be done without temporarily deleting properties, so the iteration order is well-defined.
|
|
3836
|
-
*
|
|
3837
|
-
* However, only references that can be reached from `Prism.languages` or `insert` will be replaced. I.e. if
|
|
3838
|
-
* you hold the target object in a variable, then the value of the variable will not change.
|
|
3839
|
-
*
|
|
3840
|
-
* ```js
|
|
3841
|
-
* var oldMarkup = Prism.languages.markup;
|
|
3842
|
-
* var newMarkup = Prism.languages.insertBefore('markup', 'comment', { ... });
|
|
3843
|
-
*
|
|
3844
|
-
* assert(oldMarkup !== Prism.languages.markup);
|
|
3845
|
-
* assert(newMarkup === Prism.languages.markup);
|
|
3846
|
-
* ```
|
|
3847
|
-
*
|
|
3848
|
-
* @param {string} inside The property of `root` (e.g. a language id in `Prism.languages`) that contains the
|
|
3849
|
-
* object to be modified.
|
|
3850
|
-
* @param {string} before The key to insert before.
|
|
3851
|
-
* @param {Grammar} insert An object containing the key-value pairs to be inserted.
|
|
3852
|
-
* @param {Object<string, any>} [root] The object containing `inside`, i.e. the object that contains the
|
|
3853
|
-
* object to be modified.
|
|
3854
|
-
*
|
|
3855
|
-
* Defaults to `Prism.languages`.
|
|
3856
|
-
* @returns {Grammar} The new grammar object.
|
|
3857
|
-
* @public
|
|
3858
4327
|
*/
|
|
3859
|
-
|
|
3860
|
-
|
|
3861
|
-
|
|
3862
|
-
|
|
3863
|
-
|
|
3864
|
-
|
|
3865
|
-
for (var token in grammar) {
|
|
3866
|
-
if (grammar.hasOwnProperty(token)) {
|
|
3867
|
-
|
|
3868
|
-
if (token == before) {
|
|
3869
|
-
for (var newToken in insert) {
|
|
3870
|
-
if (insert.hasOwnProperty(newToken)) {
|
|
3871
|
-
ret[newToken] = insert[newToken];
|
|
3872
|
-
}
|
|
3873
|
-
}
|
|
3874
|
-
}
|
|
3875
|
-
|
|
3876
|
-
// Do not insert token which also occur in insert. See #1525
|
|
3877
|
-
if (!insert.hasOwnProperty(token)) {
|
|
3878
|
-
ret[token] = grammar[token];
|
|
3879
|
-
}
|
|
4328
|
+
tokenize: function (text, grammar) {
|
|
4329
|
+
var rest = grammar.rest;
|
|
4330
|
+
if (rest) {
|
|
4331
|
+
for (var token in rest) {
|
|
4332
|
+
grammar[token] = rest[token];
|
|
3880
4333
|
}
|
|
4334
|
+
|
|
4335
|
+
delete grammar.rest;
|
|
3881
4336
|
}
|
|
3882
4337
|
|
|
3883
|
-
var
|
|
3884
|
-
|
|
4338
|
+
var tokenList = new LinkedList();
|
|
4339
|
+
addAfter(tokenList, tokenList.head, text);
|
|
3885
4340
|
|
|
3886
|
-
|
|
3887
|
-
_.languages.DFS(_.languages, function (key, value) {
|
|
3888
|
-
if (value === old && key != inside) {
|
|
3889
|
-
this[key] = ret;
|
|
3890
|
-
}
|
|
3891
|
-
});
|
|
4341
|
+
matchGrammar(text, tokenList, grammar, tokenList.head, 0);
|
|
3892
4342
|
|
|
3893
|
-
return
|
|
4343
|
+
return toArray(tokenList);
|
|
3894
4344
|
},
|
|
3895
4345
|
|
|
3896
|
-
|
|
3897
|
-
|
|
3898
|
-
|
|
3899
|
-
|
|
3900
|
-
|
|
3901
|
-
|
|
3902
|
-
|
|
3903
|
-
|
|
3904
|
-
|
|
4346
|
+
/**
|
|
4347
|
+
* @namespace
|
|
4348
|
+
* @memberof Prism
|
|
4349
|
+
* @public
|
|
4350
|
+
*/
|
|
4351
|
+
hooks: {
|
|
4352
|
+
all: {},
|
|
4353
|
+
|
|
4354
|
+
/**
|
|
4355
|
+
* Adds the given callback to the list of callbacks for the given hook.
|
|
4356
|
+
*
|
|
4357
|
+
* The callback will be invoked when the hook it is registered for is run.
|
|
4358
|
+
* Hooks are usually directly run by a highlight function but you can also run hooks yourself.
|
|
4359
|
+
*
|
|
4360
|
+
* One callback function can be registered to multiple hooks and the same hook multiple times.
|
|
4361
|
+
*
|
|
4362
|
+
* @param {string} name The name of the hook.
|
|
4363
|
+
* @param {HookCallback} callback The callback function which is given environment variables.
|
|
4364
|
+
* @public
|
|
4365
|
+
*/
|
|
4366
|
+
add: function (name, callback) {
|
|
4367
|
+
var hooks = _.hooks.all;
|
|
4368
|
+
|
|
4369
|
+
hooks[name] = hooks[name] || [];
|
|
4370
|
+
|
|
4371
|
+
hooks[name].push(callback);
|
|
4372
|
+
},
|
|
3905
4373
|
|
|
3906
|
-
|
|
3907
|
-
|
|
4374
|
+
/**
|
|
4375
|
+
* Runs a hook invoking all registered callbacks with the given environment variables.
|
|
4376
|
+
*
|
|
4377
|
+
* Callbacks will be invoked synchronously and in the order in which they were registered.
|
|
4378
|
+
*
|
|
4379
|
+
* @param {string} name The name of the hook.
|
|
4380
|
+
* @param {Object<string, any>} env The environment variables of the hook passed to all callbacks registered.
|
|
4381
|
+
* @public
|
|
4382
|
+
*/
|
|
4383
|
+
run: function (name, env) {
|
|
4384
|
+
var callbacks = _.hooks.all[name];
|
|
4385
|
+
|
|
4386
|
+
if (!callbacks || !callbacks.length) {
|
|
4387
|
+
return;
|
|
4388
|
+
}
|
|
3908
4389
|
|
|
3909
|
-
|
|
3910
|
-
|
|
3911
|
-
DFS(property, callback, null, visited);
|
|
3912
|
-
} else if (propertyType === 'Array' && !visited[objId(property)]) {
|
|
3913
|
-
visited[objId(property)] = true;
|
|
3914
|
-
DFS(property, callback, i, visited);
|
|
3915
|
-
}
|
|
4390
|
+
for (var i = 0, callback; (callback = callbacks[i++]);) {
|
|
4391
|
+
callback(env);
|
|
3916
4392
|
}
|
|
3917
4393
|
}
|
|
3918
|
-
}
|
|
3919
|
-
|
|
4394
|
+
},
|
|
4395
|
+
|
|
4396
|
+
Token: Token
|
|
4397
|
+
};
|
|
4398
|
+
_self.Prism = _;
|
|
3920
4399
|
|
|
3921
|
-
|
|
4400
|
+
|
|
4401
|
+
// Typescript note:
|
|
4402
|
+
// The following can be used to import the Token type in JSDoc:
|
|
4403
|
+
//
|
|
4404
|
+
// @typedef {InstanceType<import("./prism-core")["Token"]>} Token
|
|
3922
4405
|
|
|
3923
4406
|
/**
|
|
3924
|
-
*
|
|
3925
|
-
* It fetches all the elements that have a `.language-xxxx` class and then calls {@link Prism.highlightElement} on
|
|
3926
|
-
* each one of them.
|
|
3927
|
-
*
|
|
3928
|
-
* This is equivalent to `Prism.highlightAllUnder(document, async, callback)`.
|
|
4407
|
+
* Creates a new token.
|
|
3929
4408
|
*
|
|
3930
|
-
* @param {
|
|
3931
|
-
* @param {
|
|
3932
|
-
* @
|
|
4409
|
+
* @param {string} type See {@link Token#type type}
|
|
4410
|
+
* @param {string | TokenStream} content See {@link Token#content content}
|
|
4411
|
+
* @param {string|string[]} [alias] The alias(es) of the token.
|
|
4412
|
+
* @param {string} [matchedStr=""] A copy of the full string this token was created from.
|
|
4413
|
+
* @class
|
|
4414
|
+
* @global
|
|
3933
4415
|
* @public
|
|
3934
4416
|
*/
|
|
3935
|
-
|
|
3936
|
-
|
|
3937
|
-
|
|
4417
|
+
function Token(type, content, alias, matchedStr) {
|
|
4418
|
+
/**
|
|
4419
|
+
* The type of the token.
|
|
4420
|
+
*
|
|
4421
|
+
* This is usually the key of a pattern in a {@link Grammar}.
|
|
4422
|
+
*
|
|
4423
|
+
* @type {string}
|
|
4424
|
+
* @see GrammarToken
|
|
4425
|
+
* @public
|
|
4426
|
+
*/
|
|
4427
|
+
this.type = type;
|
|
4428
|
+
/**
|
|
4429
|
+
* The strings or tokens contained by this token.
|
|
4430
|
+
*
|
|
4431
|
+
* This will be a token stream if the pattern matched also defined an `inside` grammar.
|
|
4432
|
+
*
|
|
4433
|
+
* @type {string | TokenStream}
|
|
4434
|
+
* @public
|
|
4435
|
+
*/
|
|
4436
|
+
this.content = content;
|
|
4437
|
+
/**
|
|
4438
|
+
* The alias(es) of the token.
|
|
4439
|
+
*
|
|
4440
|
+
* @type {string|string[]}
|
|
4441
|
+
* @see GrammarToken
|
|
4442
|
+
* @public
|
|
4443
|
+
*/
|
|
4444
|
+
this.alias = alias;
|
|
4445
|
+
// Copy of the full string this token was created from
|
|
4446
|
+
this.length = (matchedStr || '').length | 0;
|
|
4447
|
+
}
|
|
3938
4448
|
|
|
3939
4449
|
/**
|
|
3940
|
-
*
|
|
3941
|
-
* {@link Prism.highlightElement} on each one of them.
|
|
4450
|
+
* A token stream is an array of strings and {@link Token Token} objects.
|
|
3942
4451
|
*
|
|
3943
|
-
*
|
|
3944
|
-
*
|
|
3945
|
-
*
|
|
3946
|
-
*
|
|
4452
|
+
* Token streams have to fulfill a few properties that are assumed by most functions (mostly internal ones) that process
|
|
4453
|
+
* them.
|
|
4454
|
+
*
|
|
4455
|
+
* 1. No adjacent strings.
|
|
4456
|
+
* 2. No empty strings.
|
|
3947
4457
|
*
|
|
3948
|
-
*
|
|
3949
|
-
*
|
|
3950
|
-
* @
|
|
3951
|
-
* @
|
|
4458
|
+
* The only exception here is the token stream that only contains the empty string and nothing else.
|
|
4459
|
+
*
|
|
4460
|
+
* @typedef {Array<string | Token>} TokenStream
|
|
4461
|
+
* @global
|
|
3952
4462
|
* @public
|
|
3953
4463
|
*/
|
|
3954
|
-
highlightAllUnder: function (container, async, callback) {
|
|
3955
|
-
var env = {
|
|
3956
|
-
callback: callback,
|
|
3957
|
-
container: container,
|
|
3958
|
-
selector: 'code[class*="language-"], [class*="language-"] code, code[class*="lang-"], [class*="lang-"] code'
|
|
3959
|
-
};
|
|
3960
|
-
|
|
3961
|
-
_.hooks.run('before-highlightall', env);
|
|
3962
|
-
|
|
3963
|
-
env.elements = Array.prototype.slice.apply(env.container.querySelectorAll(env.selector));
|
|
3964
|
-
|
|
3965
|
-
_.hooks.run('before-all-elements-highlight', env);
|
|
3966
|
-
|
|
3967
|
-
for (var i = 0, element; (element = env.elements[i++]);) {
|
|
3968
|
-
_.highlightElement(element, async === true, env.callback);
|
|
3969
|
-
}
|
|
3970
|
-
},
|
|
3971
4464
|
|
|
3972
4465
|
/**
|
|
3973
|
-
*
|
|
4466
|
+
* Converts the given token or token stream to an HTML representation.
|
|
3974
4467
|
*
|
|
3975
4468
|
* The following hooks will be run:
|
|
3976
|
-
* 1. `
|
|
3977
|
-
* 2. `before-highlight`
|
|
3978
|
-
* 3. All hooks of {@link Prism.highlight}. These hooks will be run by an asynchronous worker if `async` is `true`.
|
|
3979
|
-
* 4. `before-insert`
|
|
3980
|
-
* 5. `after-highlight`
|
|
3981
|
-
* 6. `complete`
|
|
3982
|
-
*
|
|
3983
|
-
* Some the above hooks will be skipped if the element doesn't contain any text or there is no grammar loaded for
|
|
3984
|
-
* the element's language.
|
|
4469
|
+
* 1. `wrap`: On each {@link Token}.
|
|
3985
4470
|
*
|
|
3986
|
-
* @param {
|
|
3987
|
-
*
|
|
3988
|
-
* @
|
|
3989
|
-
*
|
|
3990
|
-
*
|
|
3991
|
-
*
|
|
3992
|
-
* Note: All language definitions required to highlight the code must be included in the main `prism.js` file for
|
|
3993
|
-
* asynchronous highlighting to work. You can build your own bundle on the
|
|
3994
|
-
* [Download page](https://prismjs.com/download.html).
|
|
3995
|
-
* @param {HighlightCallback} [callback] An optional callback to be invoked after the highlighting is done.
|
|
3996
|
-
* Mostly useful when `async` is `true`, since in that case, the highlighting is done asynchronously.
|
|
3997
|
-
* @memberof Prism
|
|
3998
|
-
* @public
|
|
4471
|
+
* @param {string | Token | TokenStream} o The token or token stream to be converted.
|
|
4472
|
+
* @param {string} language The name of current language.
|
|
4473
|
+
* @returns {string} The HTML representation of the token or token stream.
|
|
4474
|
+
* @memberof Token
|
|
4475
|
+
* @static
|
|
3999
4476
|
*/
|
|
4000
|
-
|
|
4001
|
-
|
|
4002
|
-
|
|
4003
|
-
|
|
4004
|
-
|
|
4005
|
-
|
|
4006
|
-
|
|
4007
|
-
|
|
4008
|
-
|
|
4009
|
-
|
|
4010
|
-
if (parent && parent.nodeName.toLowerCase() === 'pre') {
|
|
4011
|
-
_.util.setLanguage(parent, language);
|
|
4477
|
+
Token.stringify = function stringify(o, language) {
|
|
4478
|
+
if (typeof o == 'string') {
|
|
4479
|
+
return o;
|
|
4480
|
+
}
|
|
4481
|
+
if (Array.isArray(o)) {
|
|
4482
|
+
var s = '';
|
|
4483
|
+
o.forEach(function (e) {
|
|
4484
|
+
s += stringify(e, language);
|
|
4485
|
+
});
|
|
4486
|
+
return s;
|
|
4012
4487
|
}
|
|
4013
|
-
|
|
4014
|
-
var code = element.textContent;
|
|
4015
4488
|
|
|
4016
4489
|
var env = {
|
|
4017
|
-
|
|
4018
|
-
|
|
4019
|
-
|
|
4020
|
-
|
|
4490
|
+
type: o.type,
|
|
4491
|
+
content: stringify(o.content, language),
|
|
4492
|
+
tag: 'span',
|
|
4493
|
+
classes: ['token', o.type],
|
|
4494
|
+
attributes: {},
|
|
4495
|
+
language: language
|
|
4021
4496
|
};
|
|
4022
4497
|
|
|
4023
|
-
|
|
4024
|
-
|
|
4025
|
-
|
|
4026
|
-
|
|
4027
|
-
|
|
4028
|
-
|
|
4029
|
-
|
|
4030
|
-
_.hooks.run('after-highlight', env);
|
|
4031
|
-
_.hooks.run('complete', env);
|
|
4032
|
-
callback && callback.call(env.element);
|
|
4033
|
-
}
|
|
4034
|
-
|
|
4035
|
-
_.hooks.run('before-sanity-check', env);
|
|
4036
|
-
|
|
4037
|
-
// plugins may change/add the parent/element
|
|
4038
|
-
parent = env.element.parentElement;
|
|
4039
|
-
if (parent && parent.nodeName.toLowerCase() === 'pre' && !parent.hasAttribute('tabindex')) {
|
|
4040
|
-
parent.setAttribute('tabindex', '0');
|
|
4041
|
-
}
|
|
4042
|
-
|
|
4043
|
-
if (!env.code) {
|
|
4044
|
-
_.hooks.run('complete', env);
|
|
4045
|
-
callback && callback.call(env.element);
|
|
4046
|
-
return;
|
|
4498
|
+
var aliases = o.alias;
|
|
4499
|
+
if (aliases) {
|
|
4500
|
+
if (Array.isArray(aliases)) {
|
|
4501
|
+
Array.prototype.push.apply(env.classes, aliases);
|
|
4502
|
+
} else {
|
|
4503
|
+
env.classes.push(aliases);
|
|
4504
|
+
}
|
|
4047
4505
|
}
|
|
4048
4506
|
|
|
4049
|
-
_.hooks.run('
|
|
4507
|
+
_.hooks.run('wrap', env);
|
|
4050
4508
|
|
|
4051
|
-
|
|
4052
|
-
|
|
4053
|
-
|
|
4509
|
+
var attributes = '';
|
|
4510
|
+
for (var name in env.attributes) {
|
|
4511
|
+
attributes += ' ' + name + '="' + (env.attributes[name] || '').replace(/"/g, '"') + '"';
|
|
4054
4512
|
}
|
|
4055
4513
|
|
|
4056
|
-
|
|
4057
|
-
|
|
4058
|
-
|
|
4059
|
-
worker.onmessage = function (evt) {
|
|
4060
|
-
insertHighlightedCode(evt.data);
|
|
4061
|
-
};
|
|
4062
|
-
|
|
4063
|
-
worker.postMessage(JSON.stringify({
|
|
4064
|
-
language: env.language,
|
|
4065
|
-
code: env.code,
|
|
4066
|
-
immediateClose: true
|
|
4067
|
-
}));
|
|
4068
|
-
} else {
|
|
4069
|
-
insertHighlightedCode(_.highlight(env.code, env.grammar, env.language));
|
|
4070
|
-
}
|
|
4071
|
-
},
|
|
4514
|
+
return '<' + env.tag + ' class="' + env.classes.join(' ') + '"' + attributes + '>' + env.content + '</' + env.tag + '>';
|
|
4515
|
+
};
|
|
4072
4516
|
|
|
4073
4517
|
/**
|
|
4074
|
-
*
|
|
4075
|
-
*
|
|
4076
|
-
*
|
|
4077
|
-
*
|
|
4078
|
-
*
|
|
4079
|
-
* 2. `after-tokenize`
|
|
4080
|
-
* 3. `wrap`: On each {@link Token}.
|
|
4081
|
-
*
|
|
4082
|
-
* @param {string} text A string with the code to be highlighted.
|
|
4083
|
-
* @param {Grammar} grammar An object containing the tokens to use.
|
|
4084
|
-
*
|
|
4085
|
-
* Usually a language definition like `Prism.languages.markup`.
|
|
4086
|
-
* @param {string} language The name of the language definition passed to `grammar`.
|
|
4087
|
-
* @returns {string} The highlighted HTML.
|
|
4088
|
-
* @memberof Prism
|
|
4089
|
-
* @public
|
|
4090
|
-
* @example
|
|
4091
|
-
* Prism.highlight('var foo = true;', Prism.languages.javascript, 'javascript');
|
|
4518
|
+
* @param {RegExp} pattern
|
|
4519
|
+
* @param {number} pos
|
|
4520
|
+
* @param {string} text
|
|
4521
|
+
* @param {boolean} lookbehind
|
|
4522
|
+
* @returns {RegExpExecArray | null}
|
|
4092
4523
|
*/
|
|
4093
|
-
|
|
4094
|
-
|
|
4095
|
-
|
|
4096
|
-
|
|
4097
|
-
|
|
4098
|
-
|
|
4099
|
-
|
|
4100
|
-
|
|
4101
|
-
throw new Error('The language "' + env.language + '" has no grammar.');
|
|
4524
|
+
function matchPattern(pattern, pos, text, lookbehind) {
|
|
4525
|
+
pattern.lastIndex = pos;
|
|
4526
|
+
var match = pattern.exec(text);
|
|
4527
|
+
if (match && lookbehind && match[1]) {
|
|
4528
|
+
// change the match to remove the text matched by the Prism lookbehind group
|
|
4529
|
+
var lookbehindLength = match[1].length;
|
|
4530
|
+
match.index += lookbehindLength;
|
|
4531
|
+
match[0] = match[0].slice(lookbehindLength);
|
|
4102
4532
|
}
|
|
4103
|
-
|
|
4104
|
-
|
|
4105
|
-
return Token.stringify(_.util.encode(env.tokens), env.language);
|
|
4106
|
-
},
|
|
4533
|
+
return match;
|
|
4534
|
+
}
|
|
4107
4535
|
|
|
4108
4536
|
/**
|
|
4109
|
-
*
|
|
4110
|
-
*
|
|
4111
|
-
*
|
|
4112
|
-
*
|
|
4537
|
+
* @param {string} text
|
|
4538
|
+
* @param {LinkedList<string | Token>} tokenList
|
|
4539
|
+
* @param {any} grammar
|
|
4540
|
+
* @param {LinkedListNode<string | Token>} startNode
|
|
4541
|
+
* @param {number} startPos
|
|
4542
|
+
* @param {RematchOptions} [rematch]
|
|
4543
|
+
* @returns {void}
|
|
4544
|
+
* @private
|
|
4113
4545
|
*
|
|
4114
|
-
*
|
|
4115
|
-
*
|
|
4116
|
-
* @
|
|
4117
|
-
* @param {Grammar} grammar An object containing the tokens to use.
|
|
4118
|
-
*
|
|
4119
|
-
* Usually a language definition like `Prism.languages.markup`.
|
|
4120
|
-
* @returns {TokenStream} An array of strings and tokens, a token stream.
|
|
4121
|
-
* @memberof Prism
|
|
4122
|
-
* @public
|
|
4123
|
-
* @example
|
|
4124
|
-
* let code = `var foo = 0;`;
|
|
4125
|
-
* let tokens = Prism.tokenize(code, Prism.languages.javascript);
|
|
4126
|
-
* tokens.forEach(token => {
|
|
4127
|
-
* if (token instanceof Prism.Token && token.type === 'number') {
|
|
4128
|
-
* console.log(`Found numeric literal: ${token.content}`);
|
|
4129
|
-
* }
|
|
4130
|
-
* });
|
|
4131
|
-
*/
|
|
4132
|
-
tokenize: function (text, grammar) {
|
|
4133
|
-
var rest = grammar.rest;
|
|
4134
|
-
if (rest) {
|
|
4135
|
-
for (var token in rest) {
|
|
4136
|
-
grammar[token] = rest[token];
|
|
4137
|
-
}
|
|
4138
|
-
|
|
4139
|
-
delete grammar.rest;
|
|
4140
|
-
}
|
|
4141
|
-
|
|
4142
|
-
var tokenList = new LinkedList();
|
|
4143
|
-
addAfter(tokenList, tokenList.head, text);
|
|
4144
|
-
|
|
4145
|
-
matchGrammar(text, tokenList, grammar, tokenList.head, 0);
|
|
4146
|
-
|
|
4147
|
-
return toArray(tokenList);
|
|
4148
|
-
},
|
|
4149
|
-
|
|
4150
|
-
/**
|
|
4151
|
-
* @namespace
|
|
4152
|
-
* @memberof Prism
|
|
4153
|
-
* @public
|
|
4546
|
+
* @typedef RematchOptions
|
|
4547
|
+
* @property {string} cause
|
|
4548
|
+
* @property {number} reach
|
|
4154
4549
|
*/
|
|
4155
|
-
|
|
4156
|
-
|
|
4157
|
-
|
|
4158
|
-
|
|
4159
|
-
* Adds the given callback to the list of callbacks for the given hook.
|
|
4160
|
-
*
|
|
4161
|
-
* The callback will be invoked when the hook it is registered for is run.
|
|
4162
|
-
* Hooks are usually directly run by a highlight function but you can also run hooks yourself.
|
|
4163
|
-
*
|
|
4164
|
-
* One callback function can be registered to multiple hooks and the same hook multiple times.
|
|
4165
|
-
*
|
|
4166
|
-
* @param {string} name The name of the hook.
|
|
4167
|
-
* @param {HookCallback} callback The callback function which is given environment variables.
|
|
4168
|
-
* @public
|
|
4169
|
-
*/
|
|
4170
|
-
add: function (name, callback) {
|
|
4171
|
-
var hooks = _.hooks.all;
|
|
4172
|
-
|
|
4173
|
-
hooks[name] = hooks[name] || [];
|
|
4174
|
-
|
|
4175
|
-
hooks[name].push(callback);
|
|
4176
|
-
},
|
|
4177
|
-
|
|
4178
|
-
/**
|
|
4179
|
-
* Runs a hook invoking all registered callbacks with the given environment variables.
|
|
4180
|
-
*
|
|
4181
|
-
* Callbacks will be invoked synchronously and in the order in which they were registered.
|
|
4182
|
-
*
|
|
4183
|
-
* @param {string} name The name of the hook.
|
|
4184
|
-
* @param {Object<string, any>} env The environment variables of the hook passed to all callbacks registered.
|
|
4185
|
-
* @public
|
|
4186
|
-
*/
|
|
4187
|
-
run: function (name, env) {
|
|
4188
|
-
var callbacks = _.hooks.all[name];
|
|
4189
|
-
|
|
4190
|
-
if (!callbacks || !callbacks.length) {
|
|
4191
|
-
return;
|
|
4550
|
+
function matchGrammar(text, tokenList, grammar, startNode, startPos, rematch) {
|
|
4551
|
+
for (var token in grammar) {
|
|
4552
|
+
if (!grammar.hasOwnProperty(token) || !grammar[token]) {
|
|
4553
|
+
continue;
|
|
4192
4554
|
}
|
|
4193
4555
|
|
|
4194
|
-
|
|
4195
|
-
|
|
4196
|
-
}
|
|
4197
|
-
}
|
|
4198
|
-
},
|
|
4556
|
+
var patterns = grammar[token];
|
|
4557
|
+
patterns = Array.isArray(patterns) ? patterns : [patterns];
|
|
4199
4558
|
|
|
4200
|
-
|
|
4201
|
-
|
|
4202
|
-
|
|
4559
|
+
for (var j = 0; j < patterns.length; ++j) {
|
|
4560
|
+
if (rematch && rematch.cause == token + ',' + j) {
|
|
4561
|
+
return;
|
|
4562
|
+
}
|
|
4203
4563
|
|
|
4564
|
+
var patternObj = patterns[j];
|
|
4565
|
+
var inside = patternObj.inside;
|
|
4566
|
+
var lookbehind = !!patternObj.lookbehind;
|
|
4567
|
+
var greedy = !!patternObj.greedy;
|
|
4568
|
+
var alias = patternObj.alias;
|
|
4204
4569
|
|
|
4205
|
-
|
|
4206
|
-
|
|
4207
|
-
|
|
4208
|
-
|
|
4570
|
+
if (greedy && !patternObj.pattern.global) {
|
|
4571
|
+
// Without the global flag, lastIndex won't work
|
|
4572
|
+
var flags = patternObj.pattern.toString().match(/[imsuy]*$/)[0];
|
|
4573
|
+
patternObj.pattern = RegExp(patternObj.pattern.source, flags + 'g');
|
|
4574
|
+
}
|
|
4209
4575
|
|
|
4210
|
-
|
|
4211
|
-
|
|
4212
|
-
*
|
|
4213
|
-
* @param {string} type See {@link Token#type type}
|
|
4214
|
-
* @param {string | TokenStream} content See {@link Token#content content}
|
|
4215
|
-
* @param {string|string[]} [alias] The alias(es) of the token.
|
|
4216
|
-
* @param {string} [matchedStr=""] A copy of the full string this token was created from.
|
|
4217
|
-
* @class
|
|
4218
|
-
* @global
|
|
4219
|
-
* @public
|
|
4220
|
-
*/
|
|
4221
|
-
function Token(type, content, alias, matchedStr) {
|
|
4222
|
-
/**
|
|
4223
|
-
* The type of the token.
|
|
4224
|
-
*
|
|
4225
|
-
* This is usually the key of a pattern in a {@link Grammar}.
|
|
4226
|
-
*
|
|
4227
|
-
* @type {string}
|
|
4228
|
-
* @see GrammarToken
|
|
4229
|
-
* @public
|
|
4230
|
-
*/
|
|
4231
|
-
this.type = type;
|
|
4232
|
-
/**
|
|
4233
|
-
* The strings or tokens contained by this token.
|
|
4234
|
-
*
|
|
4235
|
-
* This will be a token stream if the pattern matched also defined an `inside` grammar.
|
|
4236
|
-
*
|
|
4237
|
-
* @type {string | TokenStream}
|
|
4238
|
-
* @public
|
|
4239
|
-
*/
|
|
4240
|
-
this.content = content;
|
|
4241
|
-
/**
|
|
4242
|
-
* The alias(es) of the token.
|
|
4243
|
-
*
|
|
4244
|
-
* @type {string|string[]}
|
|
4245
|
-
* @see GrammarToken
|
|
4246
|
-
* @public
|
|
4247
|
-
*/
|
|
4248
|
-
this.alias = alias;
|
|
4249
|
-
// Copy of the full string this token was created from
|
|
4250
|
-
this.length = (matchedStr || '').length | 0;
|
|
4251
|
-
}
|
|
4576
|
+
/** @type {RegExp} */
|
|
4577
|
+
var pattern = patternObj.pattern || patternObj;
|
|
4252
4578
|
|
|
4253
|
-
|
|
4254
|
-
|
|
4255
|
-
|
|
4256
|
-
|
|
4257
|
-
|
|
4258
|
-
*
|
|
4259
|
-
* 1. No adjacent strings.
|
|
4260
|
-
* 2. No empty strings.
|
|
4261
|
-
*
|
|
4262
|
-
* The only exception here is the token stream that only contains the empty string and nothing else.
|
|
4263
|
-
*
|
|
4264
|
-
* @typedef {Array<string | Token>} TokenStream
|
|
4265
|
-
* @global
|
|
4266
|
-
* @public
|
|
4267
|
-
*/
|
|
4579
|
+
for ( // iterate the token list and keep track of the current token/string position
|
|
4580
|
+
var currentNode = startNode.next, pos = startPos;
|
|
4581
|
+
currentNode !== tokenList.tail;
|
|
4582
|
+
pos += currentNode.value.length, currentNode = currentNode.next
|
|
4583
|
+
) {
|
|
4268
4584
|
|
|
4269
|
-
|
|
4270
|
-
|
|
4271
|
-
|
|
4272
|
-
* The following hooks will be run:
|
|
4273
|
-
* 1. `wrap`: On each {@link Token}.
|
|
4274
|
-
*
|
|
4275
|
-
* @param {string | Token | TokenStream} o The token or token stream to be converted.
|
|
4276
|
-
* @param {string} language The name of current language.
|
|
4277
|
-
* @returns {string} The HTML representation of the token or token stream.
|
|
4278
|
-
* @memberof Token
|
|
4279
|
-
* @static
|
|
4280
|
-
*/
|
|
4281
|
-
Token.stringify = function stringify(o, language) {
|
|
4282
|
-
if (typeof o == 'string') {
|
|
4283
|
-
return o;
|
|
4284
|
-
}
|
|
4285
|
-
if (Array.isArray(o)) {
|
|
4286
|
-
var s = '';
|
|
4287
|
-
o.forEach(function (e) {
|
|
4288
|
-
s += stringify(e, language);
|
|
4289
|
-
});
|
|
4290
|
-
return s;
|
|
4291
|
-
}
|
|
4585
|
+
if (rematch && pos >= rematch.reach) {
|
|
4586
|
+
break;
|
|
4587
|
+
}
|
|
4292
4588
|
|
|
4293
|
-
|
|
4294
|
-
type: o.type,
|
|
4295
|
-
content: stringify(o.content, language),
|
|
4296
|
-
tag: 'span',
|
|
4297
|
-
classes: ['token', o.type],
|
|
4298
|
-
attributes: {},
|
|
4299
|
-
language: language
|
|
4300
|
-
};
|
|
4589
|
+
var str = currentNode.value;
|
|
4301
4590
|
|
|
4302
|
-
|
|
4303
|
-
|
|
4304
|
-
|
|
4305
|
-
|
|
4306
|
-
} else {
|
|
4307
|
-
env.classes.push(aliases);
|
|
4308
|
-
}
|
|
4309
|
-
}
|
|
4591
|
+
if (tokenList.length > text.length) {
|
|
4592
|
+
// Something went terribly wrong, ABORT, ABORT!
|
|
4593
|
+
return;
|
|
4594
|
+
}
|
|
4310
4595
|
|
|
4311
|
-
|
|
4596
|
+
if (str instanceof Token) {
|
|
4597
|
+
continue;
|
|
4598
|
+
}
|
|
4312
4599
|
|
|
4313
|
-
|
|
4314
|
-
|
|
4315
|
-
attributes += ' ' + name + '="' + (env.attributes[name] || '').replace(/"/g, '"') + '"';
|
|
4316
|
-
}
|
|
4600
|
+
var removeCount = 1; // this is the to parameter of removeBetween
|
|
4601
|
+
var match;
|
|
4317
4602
|
|
|
4318
|
-
|
|
4319
|
-
|
|
4603
|
+
if (greedy) {
|
|
4604
|
+
match = matchPattern(pattern, pos, text, lookbehind);
|
|
4605
|
+
if (!match || match.index >= text.length) {
|
|
4606
|
+
break;
|
|
4607
|
+
}
|
|
4320
4608
|
|
|
4321
|
-
|
|
4322
|
-
|
|
4323
|
-
|
|
4324
|
-
* @param {string} text
|
|
4325
|
-
* @param {boolean} lookbehind
|
|
4326
|
-
* @returns {RegExpExecArray | null}
|
|
4327
|
-
*/
|
|
4328
|
-
function matchPattern(pattern, pos, text, lookbehind) {
|
|
4329
|
-
pattern.lastIndex = pos;
|
|
4330
|
-
var match = pattern.exec(text);
|
|
4331
|
-
if (match && lookbehind && match[1]) {
|
|
4332
|
-
// change the match to remove the text matched by the Prism lookbehind group
|
|
4333
|
-
var lookbehindLength = match[1].length;
|
|
4334
|
-
match.index += lookbehindLength;
|
|
4335
|
-
match[0] = match[0].slice(lookbehindLength);
|
|
4336
|
-
}
|
|
4337
|
-
return match;
|
|
4338
|
-
}
|
|
4609
|
+
var from = match.index;
|
|
4610
|
+
var to = match.index + match[0].length;
|
|
4611
|
+
var p = pos;
|
|
4339
4612
|
|
|
4340
|
-
|
|
4341
|
-
|
|
4342
|
-
|
|
4343
|
-
|
|
4344
|
-
|
|
4345
|
-
|
|
4346
|
-
|
|
4347
|
-
|
|
4348
|
-
|
|
4349
|
-
*
|
|
4350
|
-
* @typedef RematchOptions
|
|
4351
|
-
* @property {string} cause
|
|
4352
|
-
* @property {number} reach
|
|
4353
|
-
*/
|
|
4354
|
-
function matchGrammar(text, tokenList, grammar, startNode, startPos, rematch) {
|
|
4355
|
-
for (var token in grammar) {
|
|
4356
|
-
if (!grammar.hasOwnProperty(token) || !grammar[token]) {
|
|
4357
|
-
continue;
|
|
4358
|
-
}
|
|
4613
|
+
// find the node that contains the match
|
|
4614
|
+
p += currentNode.value.length;
|
|
4615
|
+
while (from >= p) {
|
|
4616
|
+
currentNode = currentNode.next;
|
|
4617
|
+
p += currentNode.value.length;
|
|
4618
|
+
}
|
|
4619
|
+
// adjust pos (and p)
|
|
4620
|
+
p -= currentNode.value.length;
|
|
4621
|
+
pos = p;
|
|
4359
4622
|
|
|
4360
|
-
|
|
4361
|
-
|
|
4623
|
+
// the current node is a Token, then the match starts inside another Token, which is invalid
|
|
4624
|
+
if (currentNode.value instanceof Token) {
|
|
4625
|
+
continue;
|
|
4626
|
+
}
|
|
4362
4627
|
|
|
4363
|
-
|
|
4364
|
-
|
|
4365
|
-
|
|
4366
|
-
|
|
4628
|
+
// find the last node which is affected by this match
|
|
4629
|
+
for (
|
|
4630
|
+
var k = currentNode;
|
|
4631
|
+
k !== tokenList.tail && (p < to || typeof k.value === 'string');
|
|
4632
|
+
k = k.next
|
|
4633
|
+
) {
|
|
4634
|
+
removeCount++;
|
|
4635
|
+
p += k.value.length;
|
|
4636
|
+
}
|
|
4637
|
+
removeCount--;
|
|
4638
|
+
|
|
4639
|
+
// replace with the new match
|
|
4640
|
+
str = text.slice(pos, p);
|
|
4641
|
+
match.index -= pos;
|
|
4642
|
+
} else {
|
|
4643
|
+
match = matchPattern(pattern, 0, str, lookbehind);
|
|
4644
|
+
if (!match) {
|
|
4645
|
+
continue;
|
|
4646
|
+
}
|
|
4647
|
+
}
|
|
4367
4648
|
|
|
4368
|
-
|
|
4369
|
-
|
|
4370
|
-
|
|
4371
|
-
|
|
4372
|
-
|
|
4649
|
+
// eslint-disable-next-line no-redeclare
|
|
4650
|
+
var from = match.index;
|
|
4651
|
+
var matchStr = match[0];
|
|
4652
|
+
var before = str.slice(0, from);
|
|
4653
|
+
var after = str.slice(from + matchStr.length);
|
|
4373
4654
|
|
|
4374
|
-
|
|
4375
|
-
|
|
4376
|
-
|
|
4377
|
-
|
|
4378
|
-
}
|
|
4655
|
+
var reach = pos + str.length;
|
|
4656
|
+
if (rematch && reach > rematch.reach) {
|
|
4657
|
+
rematch.reach = reach;
|
|
4658
|
+
}
|
|
4379
4659
|
|
|
4380
|
-
|
|
4381
|
-
var pattern = patternObj.pattern || patternObj;
|
|
4660
|
+
var removeFrom = currentNode.prev;
|
|
4382
4661
|
|
|
4383
|
-
|
|
4384
|
-
|
|
4385
|
-
|
|
4386
|
-
|
|
4387
|
-
) {
|
|
4662
|
+
if (before) {
|
|
4663
|
+
removeFrom = addAfter(tokenList, removeFrom, before);
|
|
4664
|
+
pos += before.length;
|
|
4665
|
+
}
|
|
4388
4666
|
|
|
4389
|
-
|
|
4390
|
-
break;
|
|
4391
|
-
}
|
|
4667
|
+
removeRange(tokenList, removeFrom, removeCount);
|
|
4392
4668
|
|
|
4393
|
-
|
|
4669
|
+
var wrapped = new Token(token, inside ? _.tokenize(matchStr, inside) : matchStr, alias, matchStr);
|
|
4670
|
+
currentNode = addAfter(tokenList, removeFrom, wrapped);
|
|
4394
4671
|
|
|
4395
|
-
|
|
4396
|
-
|
|
4397
|
-
|
|
4398
|
-
}
|
|
4672
|
+
if (after) {
|
|
4673
|
+
addAfter(tokenList, currentNode, after);
|
|
4674
|
+
}
|
|
4399
4675
|
|
|
4400
|
-
|
|
4401
|
-
|
|
4402
|
-
|
|
4676
|
+
if (removeCount > 1) {
|
|
4677
|
+
// at least one Token object was removed, so we have to do some rematching
|
|
4678
|
+
// this can only happen if the current pattern is greedy
|
|
4403
4679
|
|
|
4404
|
-
|
|
4405
|
-
|
|
4680
|
+
/** @type {RematchOptions} */
|
|
4681
|
+
var nestedRematch = {
|
|
4682
|
+
cause: token + ',' + j,
|
|
4683
|
+
reach: reach
|
|
4684
|
+
};
|
|
4685
|
+
matchGrammar(text, tokenList, grammar, currentNode.prev, pos, nestedRematch);
|
|
4406
4686
|
|
|
4407
|
-
|
|
4408
|
-
|
|
4409
|
-
|
|
4410
|
-
|
|
4687
|
+
// the reach might have been extended because of the rematching
|
|
4688
|
+
if (rematch && nestedRematch.reach > rematch.reach) {
|
|
4689
|
+
rematch.reach = nestedRematch.reach;
|
|
4690
|
+
}
|
|
4411
4691
|
}
|
|
4692
|
+
}
|
|
4693
|
+
}
|
|
4694
|
+
}
|
|
4695
|
+
}
|
|
4412
4696
|
|
|
4413
|
-
|
|
4414
|
-
|
|
4415
|
-
|
|
4416
|
-
|
|
4417
|
-
|
|
4418
|
-
|
|
4419
|
-
|
|
4420
|
-
|
|
4421
|
-
p += currentNode.value.length;
|
|
4422
|
-
}
|
|
4423
|
-
// adjust pos (and p)
|
|
4424
|
-
p -= currentNode.value.length;
|
|
4425
|
-
pos = p;
|
|
4697
|
+
/**
|
|
4698
|
+
* @typedef LinkedListNode
|
|
4699
|
+
* @property {T} value
|
|
4700
|
+
* @property {LinkedListNode<T> | null} prev The previous node.
|
|
4701
|
+
* @property {LinkedListNode<T> | null} next The next node.
|
|
4702
|
+
* @template T
|
|
4703
|
+
* @private
|
|
4704
|
+
*/
|
|
4426
4705
|
|
|
4427
|
-
|
|
4428
|
-
|
|
4429
|
-
|
|
4430
|
-
|
|
4706
|
+
/**
|
|
4707
|
+
* @template T
|
|
4708
|
+
* @private
|
|
4709
|
+
*/
|
|
4710
|
+
function LinkedList() {
|
|
4711
|
+
/** @type {LinkedListNode<T>} */
|
|
4712
|
+
var head = { value: null, prev: null, next: null };
|
|
4713
|
+
/** @type {LinkedListNode<T>} */
|
|
4714
|
+
var tail = { value: null, prev: head, next: null };
|
|
4715
|
+
head.next = tail;
|
|
4716
|
+
|
|
4717
|
+
/** @type {LinkedListNode<T>} */
|
|
4718
|
+
this.head = head;
|
|
4719
|
+
/** @type {LinkedListNode<T>} */
|
|
4720
|
+
this.tail = tail;
|
|
4721
|
+
this.length = 0;
|
|
4722
|
+
}
|
|
4431
4723
|
|
|
4432
|
-
|
|
4433
|
-
|
|
4434
|
-
|
|
4435
|
-
|
|
4436
|
-
|
|
4437
|
-
|
|
4438
|
-
|
|
4439
|
-
|
|
4440
|
-
|
|
4441
|
-
|
|
4724
|
+
/**
|
|
4725
|
+
* Adds a new node with the given value to the list.
|
|
4726
|
+
*
|
|
4727
|
+
* @param {LinkedList<T>} list
|
|
4728
|
+
* @param {LinkedListNode<T>} node
|
|
4729
|
+
* @param {T} value
|
|
4730
|
+
* @returns {LinkedListNode<T>} The added node.
|
|
4731
|
+
* @template T
|
|
4732
|
+
*/
|
|
4733
|
+
function addAfter(list, node, value) {
|
|
4734
|
+
// assumes that node != list.tail && values.length >= 0
|
|
4735
|
+
var next = node.next;
|
|
4442
4736
|
|
|
4443
|
-
|
|
4444
|
-
|
|
4445
|
-
|
|
4446
|
-
|
|
4447
|
-
match = matchPattern(pattern, 0, str, lookbehind);
|
|
4448
|
-
if (!match) {
|
|
4449
|
-
continue;
|
|
4450
|
-
}
|
|
4451
|
-
}
|
|
4737
|
+
var newNode = { value: value, prev: node, next: next };
|
|
4738
|
+
node.next = newNode;
|
|
4739
|
+
next.prev = newNode;
|
|
4740
|
+
list.length++;
|
|
4452
4741
|
|
|
4453
|
-
|
|
4454
|
-
|
|
4455
|
-
|
|
4456
|
-
|
|
4457
|
-
|
|
4742
|
+
return newNode;
|
|
4743
|
+
}
|
|
4744
|
+
/**
|
|
4745
|
+
* Removes `count` nodes after the given node. The given node will not be removed.
|
|
4746
|
+
*
|
|
4747
|
+
* @param {LinkedList<T>} list
|
|
4748
|
+
* @param {LinkedListNode<T>} node
|
|
4749
|
+
* @param {number} count
|
|
4750
|
+
* @template T
|
|
4751
|
+
*/
|
|
4752
|
+
function removeRange(list, node, count) {
|
|
4753
|
+
var next = node.next;
|
|
4754
|
+
for (var i = 0; i < count && next !== list.tail; i++) {
|
|
4755
|
+
next = next.next;
|
|
4756
|
+
}
|
|
4757
|
+
node.next = next;
|
|
4758
|
+
next.prev = node;
|
|
4759
|
+
list.length -= i;
|
|
4760
|
+
}
|
|
4761
|
+
/**
|
|
4762
|
+
* @param {LinkedList<T>} list
|
|
4763
|
+
* @returns {T[]}
|
|
4764
|
+
* @template T
|
|
4765
|
+
*/
|
|
4766
|
+
function toArray(list) {
|
|
4767
|
+
var array = [];
|
|
4768
|
+
var node = list.head.next;
|
|
4769
|
+
while (node !== list.tail) {
|
|
4770
|
+
array.push(node.value);
|
|
4771
|
+
node = node.next;
|
|
4772
|
+
}
|
|
4773
|
+
return array;
|
|
4774
|
+
}
|
|
4458
4775
|
|
|
4459
|
-
var reach = pos + str.length;
|
|
4460
|
-
if (rematch && reach > rematch.reach) {
|
|
4461
|
-
rematch.reach = reach;
|
|
4462
|
-
}
|
|
4463
4776
|
|
|
4464
|
-
|
|
4777
|
+
if (!_self.document) {
|
|
4778
|
+
if (!_self.addEventListener) {
|
|
4779
|
+
// in Node.js
|
|
4780
|
+
return _;
|
|
4781
|
+
}
|
|
4465
4782
|
|
|
4466
|
-
|
|
4467
|
-
|
|
4468
|
-
|
|
4783
|
+
if (!_.disableWorkerMessageHandler) {
|
|
4784
|
+
// In worker
|
|
4785
|
+
_self.addEventListener('message', function (evt) {
|
|
4786
|
+
var message = JSON.parse(evt.data);
|
|
4787
|
+
var lang = message.language;
|
|
4788
|
+
var code = message.code;
|
|
4789
|
+
var immediateClose = message.immediateClose;
|
|
4790
|
+
|
|
4791
|
+
_self.postMessage(_.highlight(code, _.languages[lang], lang));
|
|
4792
|
+
if (immediateClose) {
|
|
4793
|
+
_self.close();
|
|
4469
4794
|
}
|
|
4795
|
+
}, false);
|
|
4796
|
+
}
|
|
4470
4797
|
|
|
4471
|
-
|
|
4798
|
+
return _;
|
|
4799
|
+
}
|
|
4472
4800
|
|
|
4473
|
-
|
|
4474
|
-
|
|
4801
|
+
// Get current script and highlight
|
|
4802
|
+
var script = _.util.currentScript();
|
|
4475
4803
|
|
|
4476
|
-
|
|
4477
|
-
|
|
4478
|
-
}
|
|
4804
|
+
if (script) {
|
|
4805
|
+
_.filename = script.src;
|
|
4479
4806
|
|
|
4480
|
-
|
|
4481
|
-
|
|
4482
|
-
|
|
4807
|
+
if (script.hasAttribute('data-manual')) {
|
|
4808
|
+
_.manual = true;
|
|
4809
|
+
}
|
|
4810
|
+
}
|
|
4483
4811
|
|
|
4484
|
-
|
|
4485
|
-
|
|
4486
|
-
|
|
4487
|
-
|
|
4488
|
-
|
|
4489
|
-
matchGrammar(text, tokenList, grammar, currentNode.prev, pos, nestedRematch);
|
|
4812
|
+
function highlightAutomaticallyCallback() {
|
|
4813
|
+
if (!_.manual) {
|
|
4814
|
+
_.highlightAll();
|
|
4815
|
+
}
|
|
4816
|
+
}
|
|
4490
4817
|
|
|
4491
|
-
|
|
4492
|
-
|
|
4493
|
-
|
|
4494
|
-
|
|
4495
|
-
|
|
4818
|
+
if (!_.manual) {
|
|
4819
|
+
// If the document state is "loading", then we'll use DOMContentLoaded.
|
|
4820
|
+
// If the document state is "interactive" and the prism.js script is deferred, then we'll also use the
|
|
4821
|
+
// DOMContentLoaded event because there might be some plugins or languages which have also been deferred and they
|
|
4822
|
+
// might take longer one animation frame to execute which can create a race condition where only some plugins have
|
|
4823
|
+
// been loaded when Prism.highlightAll() is executed, depending on how fast resources are loaded.
|
|
4824
|
+
// See https://github.com/PrismJS/prism/issues/2102
|
|
4825
|
+
var readyState = document.readyState;
|
|
4826
|
+
if (readyState === 'loading' || readyState === 'interactive' && script && script.defer) {
|
|
4827
|
+
document.addEventListener('DOMContentLoaded', highlightAutomaticallyCallback);
|
|
4828
|
+
} else {
|
|
4829
|
+
if (window.requestAnimationFrame) {
|
|
4830
|
+
window.requestAnimationFrame(highlightAutomaticallyCallback);
|
|
4831
|
+
} else {
|
|
4832
|
+
window.setTimeout(highlightAutomaticallyCallback, 16);
|
|
4496
4833
|
}
|
|
4497
4834
|
}
|
|
4498
4835
|
}
|
|
4499
|
-
}
|
|
4500
4836
|
|
|
4501
|
-
|
|
4502
|
-
* @typedef LinkedListNode
|
|
4503
|
-
* @property {T} value
|
|
4504
|
-
* @property {LinkedListNode<T> | null} prev The previous node.
|
|
4505
|
-
* @property {LinkedListNode<T> | null} next The next node.
|
|
4506
|
-
* @template T
|
|
4507
|
-
* @private
|
|
4508
|
-
*/
|
|
4837
|
+
return _;
|
|
4509
4838
|
|
|
4510
|
-
|
|
4511
|
-
|
|
4512
|
-
|
|
4513
|
-
|
|
4514
|
-
function LinkedList() {
|
|
4515
|
-
/** @type {LinkedListNode<T>} */
|
|
4516
|
-
var head = { value: null, prev: null, next: null };
|
|
4517
|
-
/** @type {LinkedListNode<T>} */
|
|
4518
|
-
var tail = { value: null, prev: head, next: null };
|
|
4519
|
-
head.next = tail;
|
|
4520
|
-
|
|
4521
|
-
/** @type {LinkedListNode<T>} */
|
|
4522
|
-
this.head = head;
|
|
4523
|
-
/** @type {LinkedListNode<T>} */
|
|
4524
|
-
this.tail = tail;
|
|
4525
|
-
this.length = 0;
|
|
4839
|
+
}(_self));
|
|
4840
|
+
|
|
4841
|
+
if (module.exports) {
|
|
4842
|
+
module.exports = Prism;
|
|
4526
4843
|
}
|
|
4527
4844
|
|
|
4528
|
-
|
|
4529
|
-
|
|
4530
|
-
|
|
4531
|
-
|
|
4532
|
-
* @param {LinkedListNode<T>} node
|
|
4533
|
-
* @param {T} value
|
|
4534
|
-
* @returns {LinkedListNode<T>} The added node.
|
|
4535
|
-
* @template T
|
|
4536
|
-
*/
|
|
4537
|
-
function addAfter(list, node, value) {
|
|
4538
|
-
// assumes that node != list.tail && values.length >= 0
|
|
4539
|
-
var next = node.next;
|
|
4845
|
+
// hack for components to work correctly in node.js
|
|
4846
|
+
if (typeof commonjsGlobal !== 'undefined') {
|
|
4847
|
+
commonjsGlobal.Prism = Prism;
|
|
4848
|
+
}
|
|
4540
4849
|
|
|
4541
|
-
|
|
4542
|
-
node.next = newNode;
|
|
4543
|
-
next.prev = newNode;
|
|
4544
|
-
list.length++;
|
|
4850
|
+
// some additional documentation/types
|
|
4545
4851
|
|
|
4546
|
-
return newNode;
|
|
4547
|
-
}
|
|
4548
4852
|
/**
|
|
4549
|
-
*
|
|
4853
|
+
* The expansion of a simple `RegExp` literal to support additional properties.
|
|
4550
4854
|
*
|
|
4551
|
-
* @
|
|
4552
|
-
* @
|
|
4553
|
-
* @
|
|
4554
|
-
*
|
|
4555
|
-
|
|
4556
|
-
|
|
4557
|
-
|
|
4558
|
-
|
|
4559
|
-
|
|
4560
|
-
|
|
4561
|
-
|
|
4562
|
-
|
|
4563
|
-
|
|
4564
|
-
|
|
4565
|
-
|
|
4566
|
-
* @
|
|
4567
|
-
* @returns {T[]}
|
|
4568
|
-
* @template T
|
|
4855
|
+
* @typedef GrammarToken
|
|
4856
|
+
* @property {RegExp} pattern The regular expression of the token.
|
|
4857
|
+
* @property {boolean} [lookbehind=false] If `true`, then the first capturing group of `pattern` will (effectively)
|
|
4858
|
+
* behave as a lookbehind group meaning that the captured text will not be part of the matched text of the new token.
|
|
4859
|
+
* @property {boolean} [greedy=false] Whether the token is greedy.
|
|
4860
|
+
* @property {string|string[]} [alias] An optional alias or list of aliases.
|
|
4861
|
+
* @property {Grammar} [inside] The nested grammar of this token.
|
|
4862
|
+
*
|
|
4863
|
+
* The `inside` grammar will be used to tokenize the text value of each token of this kind.
|
|
4864
|
+
*
|
|
4865
|
+
* This can be used to make nested and even recursive language definitions.
|
|
4866
|
+
*
|
|
4867
|
+
* Note: This can cause infinite recursion. Be careful when you embed different languages or even the same language into
|
|
4868
|
+
* each another.
|
|
4869
|
+
* @global
|
|
4870
|
+
* @public
|
|
4569
4871
|
*/
|
|
4570
|
-
function toArray(list) {
|
|
4571
|
-
var array = [];
|
|
4572
|
-
var node = list.head.next;
|
|
4573
|
-
while (node !== list.tail) {
|
|
4574
|
-
array.push(node.value);
|
|
4575
|
-
node = node.next;
|
|
4576
|
-
}
|
|
4577
|
-
return array;
|
|
4578
|
-
}
|
|
4579
4872
|
|
|
4873
|
+
/**
|
|
4874
|
+
* @typedef Grammar
|
|
4875
|
+
* @type {Object<string, RegExp | GrammarToken | Array<RegExp | GrammarToken>>}
|
|
4876
|
+
* @property {Grammar} [rest] An optional grammar object that will be appended to this grammar.
|
|
4877
|
+
* @global
|
|
4878
|
+
* @public
|
|
4879
|
+
*/
|
|
4580
4880
|
|
|
4581
|
-
|
|
4582
|
-
|
|
4583
|
-
|
|
4584
|
-
|
|
4585
|
-
|
|
4586
|
-
|
|
4587
|
-
|
|
4588
|
-
|
|
4589
|
-
|
|
4590
|
-
var message = JSON.parse(evt.data);
|
|
4591
|
-
var lang = message.language;
|
|
4592
|
-
var code = message.code;
|
|
4593
|
-
var immediateClose = message.immediateClose;
|
|
4594
|
-
|
|
4595
|
-
_self.postMessage(_.highlight(code, _.languages[lang], lang));
|
|
4596
|
-
if (immediateClose) {
|
|
4597
|
-
_self.close();
|
|
4598
|
-
}
|
|
4599
|
-
}, false);
|
|
4600
|
-
}
|
|
4601
|
-
|
|
4602
|
-
return _;
|
|
4603
|
-
}
|
|
4604
|
-
|
|
4605
|
-
// Get current script and highlight
|
|
4606
|
-
var script = _.util.currentScript();
|
|
4881
|
+
/**
|
|
4882
|
+
* A function which will invoked after an element was successfully highlighted.
|
|
4883
|
+
*
|
|
4884
|
+
* @callback HighlightCallback
|
|
4885
|
+
* @param {Element} element The element successfully highlighted.
|
|
4886
|
+
* @returns {void}
|
|
4887
|
+
* @global
|
|
4888
|
+
* @public
|
|
4889
|
+
*/
|
|
4607
4890
|
|
|
4608
|
-
|
|
4609
|
-
|
|
4891
|
+
/**
|
|
4892
|
+
* @callback HookCallback
|
|
4893
|
+
* @param {Object<string, any>} env The environment variables of the hook.
|
|
4894
|
+
* @returns {void}
|
|
4895
|
+
* @global
|
|
4896
|
+
* @public
|
|
4897
|
+
*/
|
|
4610
4898
|
|
|
4611
|
-
if (script.hasAttribute('data-manual')) {
|
|
4612
|
-
_.manual = true;
|
|
4613
|
-
}
|
|
4614
|
-
}
|
|
4615
4899
|
|
|
4616
|
-
|
|
4617
|
-
|
|
4618
|
-
|
|
4619
|
-
}
|
|
4620
|
-
}
|
|
4900
|
+
/* **********************************************
|
|
4901
|
+
Begin prism-markup.js
|
|
4902
|
+
********************************************** */
|
|
4621
4903
|
|
|
4622
|
-
|
|
4623
|
-
|
|
4624
|
-
|
|
4625
|
-
|
|
4626
|
-
|
|
4627
|
-
|
|
4628
|
-
|
|
4629
|
-
|
|
4630
|
-
|
|
4631
|
-
|
|
4632
|
-
|
|
4633
|
-
|
|
4634
|
-
|
|
4635
|
-
|
|
4636
|
-
|
|
4904
|
+
Prism.languages.markup = {
|
|
4905
|
+
'comment': {
|
|
4906
|
+
pattern: /<!--(?:(?!<!--)[\s\S])*?-->/,
|
|
4907
|
+
greedy: true
|
|
4908
|
+
},
|
|
4909
|
+
'prolog': {
|
|
4910
|
+
pattern: /<\?[\s\S]+?\?>/,
|
|
4911
|
+
greedy: true
|
|
4912
|
+
},
|
|
4913
|
+
'doctype': {
|
|
4914
|
+
// https://www.w3.org/TR/xml/#NT-doctypedecl
|
|
4915
|
+
pattern: /<!DOCTYPE(?:[^>"'[\]]|"[^"]*"|'[^']*')+(?:\[(?:[^<"'\]]|"[^"]*"|'[^']*'|<(?!!--)|<!--(?:[^-]|-(?!->))*-->)*\]\s*)?>/i,
|
|
4916
|
+
greedy: true,
|
|
4917
|
+
inside: {
|
|
4918
|
+
'internal-subset': {
|
|
4919
|
+
pattern: /(^[^\[]*\[)[\s\S]+(?=\]>$)/,
|
|
4920
|
+
lookbehind: true,
|
|
4921
|
+
greedy: true,
|
|
4922
|
+
inside: null // see below
|
|
4923
|
+
},
|
|
4924
|
+
'string': {
|
|
4925
|
+
pattern: /"[^"]*"|'[^']*'/,
|
|
4926
|
+
greedy: true
|
|
4927
|
+
},
|
|
4928
|
+
'punctuation': /^<!|>$|[[\]]/,
|
|
4929
|
+
'doctype-tag': /^DOCTYPE/i,
|
|
4930
|
+
'name': /[^\s<>'"]+/
|
|
4637
4931
|
}
|
|
4638
|
-
}
|
|
4639
|
-
|
|
4640
|
-
|
|
4641
|
-
|
|
4642
|
-
|
|
4643
|
-
|
|
4644
|
-
|
|
4645
|
-
|
|
4646
|
-
|
|
4647
|
-
|
|
4648
|
-
|
|
4649
|
-
|
|
4650
|
-
|
|
4651
|
-
|
|
4652
|
-
|
|
4653
|
-
|
|
4654
|
-
|
|
4932
|
+
},
|
|
4933
|
+
'cdata': {
|
|
4934
|
+
pattern: /<!\[CDATA\[[\s\S]*?\]\]>/i,
|
|
4935
|
+
greedy: true
|
|
4936
|
+
},
|
|
4937
|
+
'tag': {
|
|
4938
|
+
pattern: /<\/?(?!\d)[^\s>\/=$<%]+(?:\s(?:\s*[^\s>\/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?\s*\/?>/,
|
|
4939
|
+
greedy: true,
|
|
4940
|
+
inside: {
|
|
4941
|
+
'tag': {
|
|
4942
|
+
pattern: /^<\/?[^\s>\/]+/,
|
|
4943
|
+
inside: {
|
|
4944
|
+
'punctuation': /^<\/?/,
|
|
4945
|
+
'namespace': /^[^\s>\/:]+:/
|
|
4946
|
+
}
|
|
4947
|
+
},
|
|
4948
|
+
'special-attr': [],
|
|
4949
|
+
'attr-value': {
|
|
4950
|
+
pattern: /=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+)/,
|
|
4951
|
+
inside: {
|
|
4952
|
+
'punctuation': [
|
|
4953
|
+
{
|
|
4954
|
+
pattern: /^=/,
|
|
4955
|
+
alias: 'attr-equals'
|
|
4956
|
+
},
|
|
4957
|
+
{
|
|
4958
|
+
pattern: /^(\s*)["']|["']$/,
|
|
4959
|
+
lookbehind: true
|
|
4960
|
+
}
|
|
4961
|
+
]
|
|
4962
|
+
}
|
|
4963
|
+
},
|
|
4964
|
+
'punctuation': /\/?>/,
|
|
4965
|
+
'attr-name': {
|
|
4966
|
+
pattern: /[^\s>\/]+/,
|
|
4967
|
+
inside: {
|
|
4968
|
+
'namespace': /^[^\s>\/:]+:/
|
|
4969
|
+
}
|
|
4970
|
+
}
|
|
4655
4971
|
|
|
4656
|
-
|
|
4657
|
-
|
|
4658
|
-
|
|
4659
|
-
|
|
4660
|
-
|
|
4661
|
-
|
|
4662
|
-
|
|
4663
|
-
|
|
4664
|
-
|
|
4665
|
-
|
|
4666
|
-
*
|
|
4667
|
-
* The `inside` grammar will be used to tokenize the text value of each token of this kind.
|
|
4668
|
-
*
|
|
4669
|
-
* This can be used to make nested and even recursive language definitions.
|
|
4670
|
-
*
|
|
4671
|
-
* Note: This can cause infinite recursion. Be careful when you embed different languages or even the same language into
|
|
4672
|
-
* each another.
|
|
4673
|
-
* @global
|
|
4674
|
-
* @public
|
|
4675
|
-
*/
|
|
4972
|
+
}
|
|
4973
|
+
},
|
|
4974
|
+
'entity': [
|
|
4975
|
+
{
|
|
4976
|
+
pattern: /&[\da-z]{1,8};/i,
|
|
4977
|
+
alias: 'named-entity'
|
|
4978
|
+
},
|
|
4979
|
+
/&#x?[\da-f]{1,8};/i
|
|
4980
|
+
]
|
|
4981
|
+
};
|
|
4676
4982
|
|
|
4677
|
-
|
|
4678
|
-
|
|
4679
|
-
|
|
4680
|
-
* @property {Grammar} [rest] An optional grammar object that will be appended to this grammar.
|
|
4681
|
-
* @global
|
|
4682
|
-
* @public
|
|
4683
|
-
*/
|
|
4983
|
+
Prism.languages.markup['tag'].inside['attr-value'].inside['entity'] =
|
|
4984
|
+
Prism.languages.markup['entity'];
|
|
4985
|
+
Prism.languages.markup['doctype'].inside['internal-subset'].inside = Prism.languages.markup;
|
|
4684
4986
|
|
|
4685
|
-
|
|
4686
|
-
|
|
4687
|
-
*
|
|
4688
|
-
* @callback HighlightCallback
|
|
4689
|
-
* @param {Element} element The element successfully highlighted.
|
|
4690
|
-
* @returns {void}
|
|
4691
|
-
* @global
|
|
4692
|
-
* @public
|
|
4693
|
-
*/
|
|
4987
|
+
// Plugin to make entity title show the real entity, idea by Roman Komarov
|
|
4988
|
+
Prism.hooks.add('wrap', function (env) {
|
|
4694
4989
|
|
|
4695
|
-
|
|
4696
|
-
|
|
4697
|
-
|
|
4698
|
-
|
|
4699
|
-
* @global
|
|
4700
|
-
* @public
|
|
4701
|
-
*/
|
|
4990
|
+
if (env.type === 'entity') {
|
|
4991
|
+
env.attributes['title'] = env.content.replace(/&/, '&');
|
|
4992
|
+
}
|
|
4993
|
+
});
|
|
4702
4994
|
|
|
4995
|
+
Object.defineProperty(Prism.languages.markup.tag, 'addInlined', {
|
|
4996
|
+
/**
|
|
4997
|
+
* Adds an inlined language to markup.
|
|
4998
|
+
*
|
|
4999
|
+
* An example of an inlined language is CSS with `<style>` tags.
|
|
5000
|
+
*
|
|
5001
|
+
* @param {string} tagName The name of the tag that contains the inlined language. This name will be treated as
|
|
5002
|
+
* case insensitive.
|
|
5003
|
+
* @param {string} lang The language key.
|
|
5004
|
+
* @example
|
|
5005
|
+
* addInlined('style', 'css');
|
|
5006
|
+
*/
|
|
5007
|
+
value: function addInlined(tagName, lang) {
|
|
5008
|
+
var includedCdataInside = {};
|
|
5009
|
+
includedCdataInside['language-' + lang] = {
|
|
5010
|
+
pattern: /(^<!\[CDATA\[)[\s\S]+?(?=\]\]>$)/i,
|
|
5011
|
+
lookbehind: true,
|
|
5012
|
+
inside: Prism.languages[lang]
|
|
5013
|
+
};
|
|
5014
|
+
includedCdataInside['cdata'] = /^<!\[CDATA\[|\]\]>$/i;
|
|
4703
5015
|
|
|
4704
|
-
|
|
4705
|
-
|
|
4706
|
-
|
|
5016
|
+
var inside = {
|
|
5017
|
+
'included-cdata': {
|
|
5018
|
+
pattern: /<!\[CDATA\[[\s\S]*?\]\]>/i,
|
|
5019
|
+
inside: includedCdataInside
|
|
5020
|
+
}
|
|
5021
|
+
};
|
|
5022
|
+
inside['language-' + lang] = {
|
|
5023
|
+
pattern: /[\s\S]+/,
|
|
5024
|
+
inside: Prism.languages[lang]
|
|
5025
|
+
};
|
|
4707
5026
|
|
|
4708
|
-
|
|
4709
|
-
|
|
4710
|
-
|
|
4711
|
-
greedy: true
|
|
4712
|
-
},
|
|
4713
|
-
'prolog': {
|
|
4714
|
-
pattern: /<\?[\s\S]+?\?>/,
|
|
4715
|
-
greedy: true
|
|
4716
|
-
},
|
|
4717
|
-
'doctype': {
|
|
4718
|
-
// https://www.w3.org/TR/xml/#NT-doctypedecl
|
|
4719
|
-
pattern: /<!DOCTYPE(?:[^>"'[\]]|"[^"]*"|'[^']*')+(?:\[(?:[^<"'\]]|"[^"]*"|'[^']*'|<(?!!--)|<!--(?:[^-]|-(?!->))*-->)*\]\s*)?>/i,
|
|
4720
|
-
greedy: true,
|
|
4721
|
-
inside: {
|
|
4722
|
-
'internal-subset': {
|
|
4723
|
-
pattern: /(^[^\[]*\[)[\s\S]+(?=\]>$)/,
|
|
5027
|
+
var def = {};
|
|
5028
|
+
def[tagName] = {
|
|
5029
|
+
pattern: RegExp(/(<__[^>]*>)(?:<!\[CDATA\[(?:[^\]]|\](?!\]>))*\]\]>|(?!<!\[CDATA\[)[\s\S])*?(?=<\/__>)/.source.replace(/__/g, function () { return tagName; }), 'i'),
|
|
4724
5030
|
lookbehind: true,
|
|
4725
5031
|
greedy: true,
|
|
4726
|
-
inside:
|
|
4727
|
-
}
|
|
4728
|
-
|
|
4729
|
-
|
|
4730
|
-
greedy: true
|
|
4731
|
-
},
|
|
4732
|
-
'punctuation': /^<!|>$|[[\]]/,
|
|
4733
|
-
'doctype-tag': /^DOCTYPE/i,
|
|
4734
|
-
'name': /[^\s<>'"]+/
|
|
5032
|
+
inside: inside
|
|
5033
|
+
};
|
|
5034
|
+
|
|
5035
|
+
Prism.languages.insertBefore('markup', 'cdata', def);
|
|
4735
5036
|
}
|
|
4736
|
-
}
|
|
4737
|
-
'
|
|
4738
|
-
|
|
4739
|
-
|
|
4740
|
-
|
|
4741
|
-
|
|
4742
|
-
|
|
4743
|
-
|
|
4744
|
-
|
|
4745
|
-
|
|
4746
|
-
|
|
4747
|
-
|
|
4748
|
-
|
|
4749
|
-
|
|
4750
|
-
|
|
4751
|
-
|
|
4752
|
-
|
|
4753
|
-
|
|
4754
|
-
|
|
5037
|
+
});
|
|
5038
|
+
Object.defineProperty(Prism.languages.markup.tag, 'addAttribute', {
|
|
5039
|
+
/**
|
|
5040
|
+
* Adds an pattern to highlight languages embedded in HTML attributes.
|
|
5041
|
+
*
|
|
5042
|
+
* An example of an inlined language is CSS with `style` attributes.
|
|
5043
|
+
*
|
|
5044
|
+
* @param {string} attrName The name of the tag that contains the inlined language. This name will be treated as
|
|
5045
|
+
* case insensitive.
|
|
5046
|
+
* @param {string} lang The language key.
|
|
5047
|
+
* @example
|
|
5048
|
+
* addAttribute('style', 'css');
|
|
5049
|
+
*/
|
|
5050
|
+
value: function (attrName, lang) {
|
|
5051
|
+
Prism.languages.markup.tag.inside['special-attr'].push({
|
|
5052
|
+
pattern: RegExp(
|
|
5053
|
+
/(^|["'\s])/.source + '(?:' + attrName + ')' + /\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))/.source,
|
|
5054
|
+
'i'
|
|
5055
|
+
),
|
|
5056
|
+
lookbehind: true,
|
|
4755
5057
|
inside: {
|
|
4756
|
-
'
|
|
4757
|
-
|
|
4758
|
-
|
|
4759
|
-
|
|
4760
|
-
|
|
4761
|
-
|
|
4762
|
-
|
|
4763
|
-
|
|
5058
|
+
'attr-name': /^[^\s=]+/,
|
|
5059
|
+
'attr-value': {
|
|
5060
|
+
pattern: /=[\s\S]+/,
|
|
5061
|
+
inside: {
|
|
5062
|
+
'value': {
|
|
5063
|
+
pattern: /(^=\s*(["']|(?!["'])))\S[\s\S]*(?=\2$)/,
|
|
5064
|
+
lookbehind: true,
|
|
5065
|
+
alias: [lang, 'language-' + lang],
|
|
5066
|
+
inside: Prism.languages[lang]
|
|
5067
|
+
},
|
|
5068
|
+
'punctuation': [
|
|
5069
|
+
{
|
|
5070
|
+
pattern: /^=/,
|
|
5071
|
+
alias: 'attr-equals'
|
|
5072
|
+
},
|
|
5073
|
+
/"|'/
|
|
5074
|
+
]
|
|
4764
5075
|
}
|
|
4765
|
-
|
|
4766
|
-
}
|
|
4767
|
-
},
|
|
4768
|
-
'punctuation': /\/?>/,
|
|
4769
|
-
'attr-name': {
|
|
4770
|
-
pattern: /[^\s>\/]+/,
|
|
4771
|
-
inside: {
|
|
4772
|
-
'namespace': /^[^\s>\/:]+:/
|
|
5076
|
+
}
|
|
4773
5077
|
}
|
|
4774
|
-
}
|
|
4775
|
-
|
|
5078
|
+
});
|
|
4776
5079
|
}
|
|
4777
|
-
}
|
|
4778
|
-
'entity': [
|
|
4779
|
-
{
|
|
4780
|
-
pattern: /&[\da-z]{1,8};/i,
|
|
4781
|
-
alias: 'named-entity'
|
|
4782
|
-
},
|
|
4783
|
-
/&#x?[\da-f]{1,8};/i
|
|
4784
|
-
]
|
|
4785
|
-
};
|
|
5080
|
+
});
|
|
4786
5081
|
|
|
4787
|
-
|
|
4788
|
-
Prism.languages.markup
|
|
4789
|
-
|
|
5082
|
+
Prism.languages.html = Prism.languages.markup;
|
|
5083
|
+
Prism.languages.mathml = Prism.languages.markup;
|
|
5084
|
+
Prism.languages.svg = Prism.languages.markup;
|
|
4790
5085
|
|
|
4791
|
-
|
|
4792
|
-
|
|
5086
|
+
Prism.languages.xml = Prism.languages.extend('markup', {});
|
|
5087
|
+
Prism.languages.ssml = Prism.languages.xml;
|
|
5088
|
+
Prism.languages.atom = Prism.languages.xml;
|
|
5089
|
+
Prism.languages.rss = Prism.languages.xml;
|
|
4793
5090
|
|
|
4794
|
-
if (env.type === 'entity') {
|
|
4795
|
-
env.attributes['title'] = env.content.replace(/&/, '&');
|
|
4796
|
-
}
|
|
4797
|
-
});
|
|
4798
5091
|
|
|
4799
|
-
|
|
4800
|
-
|
|
4801
|
-
|
|
4802
|
-
*
|
|
4803
|
-
* An example of an inlined language is CSS with `<style>` tags.
|
|
4804
|
-
*
|
|
4805
|
-
* @param {string} tagName The name of the tag that contains the inlined language. This name will be treated as
|
|
4806
|
-
* case insensitive.
|
|
4807
|
-
* @param {string} lang The language key.
|
|
4808
|
-
* @example
|
|
4809
|
-
* addInlined('style', 'css');
|
|
4810
|
-
*/
|
|
4811
|
-
value: function addInlined(tagName, lang) {
|
|
4812
|
-
var includedCdataInside = {};
|
|
4813
|
-
includedCdataInside['language-' + lang] = {
|
|
4814
|
-
pattern: /(^<!\[CDATA\[)[\s\S]+?(?=\]\]>$)/i,
|
|
4815
|
-
lookbehind: true,
|
|
4816
|
-
inside: Prism.languages[lang]
|
|
4817
|
-
};
|
|
4818
|
-
includedCdataInside['cdata'] = /^<!\[CDATA\[|\]\]>$/i;
|
|
5092
|
+
/* **********************************************
|
|
5093
|
+
Begin prism-css.js
|
|
5094
|
+
********************************************** */
|
|
4819
5095
|
|
|
4820
|
-
|
|
4821
|
-
'included-cdata': {
|
|
4822
|
-
pattern: /<!\[CDATA\[[\s\S]*?\]\]>/i,
|
|
4823
|
-
inside: includedCdataInside
|
|
4824
|
-
}
|
|
4825
|
-
};
|
|
4826
|
-
inside['language-' + lang] = {
|
|
4827
|
-
pattern: /[\s\S]+/,
|
|
4828
|
-
inside: Prism.languages[lang]
|
|
4829
|
-
};
|
|
5096
|
+
(function (Prism) {
|
|
4830
5097
|
|
|
4831
|
-
var
|
|
4832
|
-
def[tagName] = {
|
|
4833
|
-
pattern: RegExp(/(<__[^>]*>)(?:<!\[CDATA\[(?:[^\]]|\](?!\]>))*\]\]>|(?!<!\[CDATA\[)[\s\S])*?(?=<\/__>)/.source.replace(/__/g, function () { return tagName; }), 'i'),
|
|
4834
|
-
lookbehind: true,
|
|
4835
|
-
greedy: true,
|
|
4836
|
-
inside: inside
|
|
4837
|
-
};
|
|
5098
|
+
var string = /(?:"(?:\\(?:\r\n|[\s\S])|[^"\\\r\n])*"|'(?:\\(?:\r\n|[\s\S])|[^'\\\r\n])*')/;
|
|
4838
5099
|
|
|
4839
|
-
Prism.languages.
|
|
4840
|
-
|
|
4841
|
-
|
|
4842
|
-
|
|
4843
|
-
|
|
4844
|
-
|
|
4845
|
-
|
|
4846
|
-
|
|
4847
|
-
|
|
4848
|
-
|
|
4849
|
-
|
|
4850
|
-
|
|
4851
|
-
|
|
4852
|
-
|
|
4853
|
-
*/
|
|
4854
|
-
value: function (attrName, lang) {
|
|
4855
|
-
Prism.languages.markup.tag.inside['special-attr'].push({
|
|
4856
|
-
pattern: RegExp(
|
|
4857
|
-
/(^|["'\s])/.source + '(?:' + attrName + ')' + /\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))/.source,
|
|
4858
|
-
'i'
|
|
4859
|
-
),
|
|
4860
|
-
lookbehind: true,
|
|
4861
|
-
inside: {
|
|
4862
|
-
'attr-name': /^[^\s=]+/,
|
|
4863
|
-
'attr-value': {
|
|
4864
|
-
pattern: /=[\s\S]+/,
|
|
4865
|
-
inside: {
|
|
4866
|
-
'value': {
|
|
4867
|
-
pattern: /(^=\s*(["']|(?!["'])))\S[\s\S]*(?=\2$)/,
|
|
4868
|
-
lookbehind: true,
|
|
4869
|
-
alias: [lang, 'language-' + lang],
|
|
4870
|
-
inside: Prism.languages[lang]
|
|
4871
|
-
},
|
|
4872
|
-
'punctuation': [
|
|
4873
|
-
{
|
|
4874
|
-
pattern: /^=/,
|
|
4875
|
-
alias: 'attr-equals'
|
|
4876
|
-
},
|
|
4877
|
-
/"|'/
|
|
4878
|
-
]
|
|
5100
|
+
Prism.languages.css = {
|
|
5101
|
+
'comment': /\/\*[\s\S]*?\*\//,
|
|
5102
|
+
'atrule': {
|
|
5103
|
+
pattern: RegExp('@[\\w-](?:' + /[^;{\s"']|\s+(?!\s)/.source + '|' + string.source + ')*?' + /(?:;|(?=\s*\{))/.source),
|
|
5104
|
+
inside: {
|
|
5105
|
+
'rule': /^@[\w-]+/,
|
|
5106
|
+
'selector-function-argument': {
|
|
5107
|
+
pattern: /(\bselector\s*\(\s*(?![\s)]))(?:[^()\s]|\s+(?![\s)])|\((?:[^()]|\([^()]*\))*\))+(?=\s*\))/,
|
|
5108
|
+
lookbehind: true,
|
|
5109
|
+
alias: 'selector'
|
|
5110
|
+
},
|
|
5111
|
+
'keyword': {
|
|
5112
|
+
pattern: /(^|[^\w-])(?:and|not|only|or)(?![\w-])/,
|
|
5113
|
+
lookbehind: true
|
|
4879
5114
|
}
|
|
5115
|
+
// See rest below
|
|
4880
5116
|
}
|
|
4881
|
-
}
|
|
4882
|
-
|
|
4883
|
-
|
|
4884
|
-
|
|
4885
|
-
|
|
4886
|
-
|
|
4887
|
-
|
|
4888
|
-
|
|
5117
|
+
},
|
|
5118
|
+
'url': {
|
|
5119
|
+
// https://drafts.csswg.org/css-values-3/#urls
|
|
5120
|
+
pattern: RegExp('\\burl\\((?:' + string.source + '|' + /(?:[^\\\r\n()"']|\\[\s\S])*/.source + ')\\)', 'i'),
|
|
5121
|
+
greedy: true,
|
|
5122
|
+
inside: {
|
|
5123
|
+
'function': /^url/i,
|
|
5124
|
+
'punctuation': /^\(|\)$/,
|
|
5125
|
+
'string': {
|
|
5126
|
+
pattern: RegExp('^' + string.source + '$'),
|
|
5127
|
+
alias: 'url'
|
|
5128
|
+
}
|
|
5129
|
+
}
|
|
5130
|
+
},
|
|
5131
|
+
'selector': {
|
|
5132
|
+
pattern: RegExp('(^|[{}\\s])[^{}\\s](?:[^{};"\'\\s]|\\s+(?![\\s{])|' + string.source + ')*(?=\\s*\\{)'),
|
|
5133
|
+
lookbehind: true
|
|
5134
|
+
},
|
|
5135
|
+
'string': {
|
|
5136
|
+
pattern: string,
|
|
5137
|
+
greedy: true
|
|
5138
|
+
},
|
|
5139
|
+
'property': {
|
|
5140
|
+
pattern: /(^|[^-\w\xA0-\uFFFF])(?!\s)[-_a-z\xA0-\uFFFF](?:(?!\s)[-\w\xA0-\uFFFF])*(?=\s*:)/i,
|
|
5141
|
+
lookbehind: true
|
|
5142
|
+
},
|
|
5143
|
+
'important': /!important\b/i,
|
|
5144
|
+
'function': {
|
|
5145
|
+
pattern: /(^|[^-a-z0-9])[-a-z0-9]+(?=\()/i,
|
|
5146
|
+
lookbehind: true
|
|
5147
|
+
},
|
|
5148
|
+
'punctuation': /[(){};:,]/
|
|
5149
|
+
};
|
|
4889
5150
|
|
|
4890
|
-
|
|
4891
|
-
Prism.languages.ssml = Prism.languages.xml;
|
|
4892
|
-
Prism.languages.atom = Prism.languages.xml;
|
|
4893
|
-
Prism.languages.rss = Prism.languages.xml;
|
|
5151
|
+
Prism.languages.css['atrule'].inside.rest = Prism.languages.css;
|
|
4894
5152
|
|
|
5153
|
+
var markup = Prism.languages.markup;
|
|
5154
|
+
if (markup) {
|
|
5155
|
+
markup.tag.addInlined('style', 'css');
|
|
5156
|
+
markup.tag.addAttribute('style', 'css');
|
|
5157
|
+
}
|
|
4895
5158
|
|
|
4896
|
-
|
|
4897
|
-
Begin prism-css.js
|
|
4898
|
-
********************************************** */
|
|
5159
|
+
}(Prism));
|
|
4899
5160
|
|
|
4900
|
-
(function (Prism) {
|
|
4901
5161
|
|
|
4902
|
-
|
|
5162
|
+
/* **********************************************
|
|
5163
|
+
Begin prism-clike.js
|
|
5164
|
+
********************************************** */
|
|
4903
5165
|
|
|
4904
|
-
Prism.languages.
|
|
4905
|
-
'comment':
|
|
4906
|
-
|
|
4907
|
-
|
|
4908
|
-
|
|
4909
|
-
|
|
4910
|
-
|
|
4911
|
-
|
|
4912
|
-
|
|
4913
|
-
|
|
4914
|
-
|
|
4915
|
-
'keyword': {
|
|
4916
|
-
pattern: /(^|[^\w-])(?:and|not|only|or)(?![\w-])/,
|
|
4917
|
-
lookbehind: true
|
|
4918
|
-
}
|
|
4919
|
-
// See rest below
|
|
4920
|
-
}
|
|
4921
|
-
},
|
|
4922
|
-
'url': {
|
|
4923
|
-
// https://drafts.csswg.org/css-values-3/#urls
|
|
4924
|
-
pattern: RegExp('\\burl\\((?:' + string.source + '|' + /(?:[^\\\r\n()"']|\\[\s\S])*/.source + ')\\)', 'i'),
|
|
4925
|
-
greedy: true,
|
|
4926
|
-
inside: {
|
|
4927
|
-
'function': /^url/i,
|
|
4928
|
-
'punctuation': /^\(|\)$/,
|
|
4929
|
-
'string': {
|
|
4930
|
-
pattern: RegExp('^' + string.source + '$'),
|
|
4931
|
-
alias: 'url'
|
|
4932
|
-
}
|
|
5166
|
+
Prism.languages.clike = {
|
|
5167
|
+
'comment': [
|
|
5168
|
+
{
|
|
5169
|
+
pattern: /(^|[^\\])\/\*[\s\S]*?(?:\*\/|$)/,
|
|
5170
|
+
lookbehind: true,
|
|
5171
|
+
greedy: true
|
|
5172
|
+
},
|
|
5173
|
+
{
|
|
5174
|
+
pattern: /(^|[^\\:])\/\/.*/,
|
|
5175
|
+
lookbehind: true,
|
|
5176
|
+
greedy: true
|
|
4933
5177
|
}
|
|
4934
|
-
|
|
4935
|
-
'selector': {
|
|
4936
|
-
pattern: RegExp('(^|[{}\\s])[^{}\\s](?:[^{};"\'\\s]|\\s+(?![\\s{])|' + string.source + ')*(?=\\s*\\{)'),
|
|
4937
|
-
lookbehind: true
|
|
4938
|
-
},
|
|
5178
|
+
],
|
|
4939
5179
|
'string': {
|
|
4940
|
-
pattern:
|
|
5180
|
+
pattern: /(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
|
|
4941
5181
|
greedy: true
|
|
4942
5182
|
},
|
|
4943
|
-
'
|
|
4944
|
-
pattern: /(
|
|
4945
|
-
lookbehind: true
|
|
4946
|
-
|
|
4947
|
-
|
|
4948
|
-
|
|
4949
|
-
pattern: /(^|[^-a-z0-9])[-a-z0-9]+(?=\()/i,
|
|
4950
|
-
lookbehind: true
|
|
5183
|
+
'class-name': {
|
|
5184
|
+
pattern: /(\b(?:class|extends|implements|instanceof|interface|new|trait)\s+|\bcatch\s+\()[\w.\\]+/i,
|
|
5185
|
+
lookbehind: true,
|
|
5186
|
+
inside: {
|
|
5187
|
+
'punctuation': /[.\\]/
|
|
5188
|
+
}
|
|
4951
5189
|
},
|
|
4952
|
-
'
|
|
5190
|
+
'keyword': /\b(?:break|catch|continue|do|else|finally|for|function|if|in|instanceof|new|null|return|throw|try|while)\b/,
|
|
5191
|
+
'boolean': /\b(?:false|true)\b/,
|
|
5192
|
+
'function': /\b\w+(?=\()/,
|
|
5193
|
+
'number': /\b0x[\da-f]+\b|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[+-]?\d+)?/i,
|
|
5194
|
+
'operator': /[<>]=?|[!=]=?=?|--?|\+\+?|&&?|\|\|?|[?*/~^%]/,
|
|
5195
|
+
'punctuation': /[{}[\];(),.:]/
|
|
4953
5196
|
};
|
|
4954
5197
|
|
|
4955
|
-
Prism.languages.css['atrule'].inside.rest = Prism.languages.css;
|
|
4956
5198
|
|
|
4957
|
-
|
|
4958
|
-
|
|
4959
|
-
|
|
4960
|
-
markup.tag.addAttribute('style', 'css');
|
|
4961
|
-
}
|
|
5199
|
+
/* **********************************************
|
|
5200
|
+
Begin prism-javascript.js
|
|
5201
|
+
********************************************** */
|
|
4962
5202
|
|
|
4963
|
-
|
|
4964
|
-
|
|
4965
|
-
|
|
4966
|
-
|
|
4967
|
-
|
|
4968
|
-
|
|
4969
|
-
|
|
4970
|
-
|
|
4971
|
-
|
|
4972
|
-
|
|
4973
|
-
|
|
4974
|
-
|
|
4975
|
-
|
|
5203
|
+
Prism.languages.javascript = Prism.languages.extend('clike', {
|
|
5204
|
+
'class-name': [
|
|
5205
|
+
Prism.languages.clike['class-name'],
|
|
5206
|
+
{
|
|
5207
|
+
pattern: /(^|[^$\w\xA0-\uFFFF])(?!\s)[_$A-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\.(?:constructor|prototype))/,
|
|
5208
|
+
lookbehind: true
|
|
5209
|
+
}
|
|
5210
|
+
],
|
|
5211
|
+
'keyword': [
|
|
5212
|
+
{
|
|
5213
|
+
pattern: /((?:^|\})\s*)catch\b/,
|
|
5214
|
+
lookbehind: true
|
|
5215
|
+
},
|
|
5216
|
+
{
|
|
5217
|
+
pattern: /(^|[^.]|\.\.\.\s*)\b(?:as|assert(?=\s*\{)|async(?=\s*(?:function\b|\(|[$\w\xA0-\uFFFF]|$))|await|break|case|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally(?=\s*(?:\{|$))|for|from(?=\s*(?:['"]|$))|function|(?:get|set)(?=\s*(?:[#\[$\w\xA0-\uFFFF]|$))|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|static|super|switch|this|throw|try|typeof|undefined|var|void|while|with|yield)\b/,
|
|
5218
|
+
lookbehind: true
|
|
5219
|
+
},
|
|
5220
|
+
],
|
|
5221
|
+
// Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
|
|
5222
|
+
'function': /#?(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*(?:\.\s*(?:apply|bind|call)\s*)?\()/,
|
|
5223
|
+
'number': {
|
|
5224
|
+
pattern: RegExp(
|
|
5225
|
+
/(^|[^\w$])/.source +
|
|
5226
|
+
'(?:' +
|
|
5227
|
+
(
|
|
5228
|
+
// constant
|
|
5229
|
+
/NaN|Infinity/.source +
|
|
5230
|
+
'|' +
|
|
5231
|
+
// binary integer
|
|
5232
|
+
/0[bB][01]+(?:_[01]+)*n?/.source +
|
|
5233
|
+
'|' +
|
|
5234
|
+
// octal integer
|
|
5235
|
+
/0[oO][0-7]+(?:_[0-7]+)*n?/.source +
|
|
5236
|
+
'|' +
|
|
5237
|
+
// hexadecimal integer
|
|
5238
|
+
/0[xX][\dA-Fa-f]+(?:_[\dA-Fa-f]+)*n?/.source +
|
|
5239
|
+
'|' +
|
|
5240
|
+
// decimal bigint
|
|
5241
|
+
/\d+(?:_\d+)*n/.source +
|
|
5242
|
+
'|' +
|
|
5243
|
+
// decimal number (integer or float) but no bigint
|
|
5244
|
+
/(?:\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\.\d+(?:_\d+)*)(?:[Ee][+-]?\d+(?:_\d+)*)?/.source
|
|
5245
|
+
) +
|
|
5246
|
+
')' +
|
|
5247
|
+
/(?![\w$])/.source
|
|
5248
|
+
),
|
|
5249
|
+
lookbehind: true
|
|
4976
5250
|
},
|
|
4977
|
-
{
|
|
4978
|
-
|
|
4979
|
-
lookbehind: true,
|
|
4980
|
-
greedy: true
|
|
4981
|
-
}
|
|
4982
|
-
],
|
|
4983
|
-
'string': {
|
|
4984
|
-
pattern: /(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
|
|
4985
|
-
greedy: true
|
|
4986
|
-
},
|
|
4987
|
-
'class-name': {
|
|
4988
|
-
pattern: /(\b(?:class|extends|implements|instanceof|interface|new|trait)\s+|\bcatch\s+\()[\w.\\]+/i,
|
|
4989
|
-
lookbehind: true,
|
|
4990
|
-
inside: {
|
|
4991
|
-
'punctuation': /[.\\]/
|
|
4992
|
-
}
|
|
4993
|
-
},
|
|
4994
|
-
'keyword': /\b(?:break|catch|continue|do|else|finally|for|function|if|in|instanceof|new|null|return|throw|try|while)\b/,
|
|
4995
|
-
'boolean': /\b(?:false|true)\b/,
|
|
4996
|
-
'function': /\b\w+(?=\()/,
|
|
4997
|
-
'number': /\b0x[\da-f]+\b|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[+-]?\d+)?/i,
|
|
4998
|
-
'operator': /[<>]=?|[!=]=?=?|--?|\+\+?|&&?|\|\|?|[?*/~^%]/,
|
|
4999
|
-
'punctuation': /[{}[\];(),.:]/
|
|
5000
|
-
};
|
|
5001
|
-
|
|
5251
|
+
'operator': /--|\+\+|\*\*=?|=>|&&=?|\|\|=?|[!=]==|<<=?|>>>?=?|[-+*/%&|^!=<>]=?|\.{3}|\?\?=?|\?\.?|[~:]/
|
|
5252
|
+
});
|
|
5002
5253
|
|
|
5003
|
-
|
|
5004
|
-
Begin prism-javascript.js
|
|
5005
|
-
********************************************** */
|
|
5254
|
+
Prism.languages.javascript['class-name'][0].pattern = /(\b(?:class|extends|implements|instanceof|interface|new)\s+)[\w.\\]+/;
|
|
5006
5255
|
|
|
5007
|
-
|
|
5008
|
-
|
|
5009
|
-
|
|
5010
|
-
|
|
5011
|
-
|
|
5012
|
-
|
|
5013
|
-
|
|
5014
|
-
|
|
5015
|
-
|
|
5016
|
-
|
|
5017
|
-
|
|
5018
|
-
|
|
5019
|
-
|
|
5020
|
-
{
|
|
5021
|
-
pattern: /(^|[^.]|\.\.\.\s*)\b(?:as|assert(?=\s*\{)|async(?=\s*(?:function\b|\(|[$\w\xA0-\uFFFF]|$))|await|break|case|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally(?=\s*(?:\{|$))|for|from(?=\s*(?:['"]|$))|function|(?:get|set)(?=\s*(?:[#\[$\w\xA0-\uFFFF]|$))|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|static|super|switch|this|throw|try|typeof|undefined|var|void|while|with|yield)\b/,
|
|
5022
|
-
lookbehind: true
|
|
5023
|
-
},
|
|
5024
|
-
],
|
|
5025
|
-
// Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
|
|
5026
|
-
'function': /#?(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*(?:\.\s*(?:apply|bind|call)\s*)?\()/,
|
|
5027
|
-
'number': {
|
|
5028
|
-
pattern: RegExp(
|
|
5029
|
-
/(^|[^\w$])/.source +
|
|
5030
|
-
'(?:' +
|
|
5031
|
-
(
|
|
5032
|
-
// constant
|
|
5033
|
-
/NaN|Infinity/.source +
|
|
5034
|
-
'|' +
|
|
5035
|
-
// binary integer
|
|
5036
|
-
/0[bB][01]+(?:_[01]+)*n?/.source +
|
|
5037
|
-
'|' +
|
|
5038
|
-
// octal integer
|
|
5039
|
-
/0[oO][0-7]+(?:_[0-7]+)*n?/.source +
|
|
5040
|
-
'|' +
|
|
5041
|
-
// hexadecimal integer
|
|
5042
|
-
/0[xX][\dA-Fa-f]+(?:_[\dA-Fa-f]+)*n?/.source +
|
|
5043
|
-
'|' +
|
|
5044
|
-
// decimal bigint
|
|
5045
|
-
/\d+(?:_\d+)*n/.source +
|
|
5256
|
+
Prism.languages.insertBefore('javascript', 'keyword', {
|
|
5257
|
+
'regex': {
|
|
5258
|
+
pattern: RegExp(
|
|
5259
|
+
// lookbehind
|
|
5260
|
+
// eslint-disable-next-line regexp/no-dupe-characters-character-class
|
|
5261
|
+
/((?:^|[^$\w\xA0-\uFFFF."'\])\s]|\b(?:return|yield))\s*)/.source +
|
|
5262
|
+
// Regex pattern:
|
|
5263
|
+
// There are 2 regex patterns here. The RegExp set notation proposal added support for nested character
|
|
5264
|
+
// classes if the `v` flag is present. Unfortunately, nested CCs are both context-free and incompatible
|
|
5265
|
+
// with the only syntax, so we have to define 2 different regex patterns.
|
|
5266
|
+
/\//.source +
|
|
5267
|
+
'(?:' +
|
|
5268
|
+
/(?:\[(?:[^\]\\\r\n]|\\.)*\]|\\.|[^/\\\[\r\n])+\/[dgimyus]{0,7}/.source +
|
|
5046
5269
|
'|' +
|
|
5047
|
-
//
|
|
5048
|
-
/(?:\
|
|
5049
|
-
|
|
5050
|
-
|
|
5051
|
-
|
|
5052
|
-
|
|
5053
|
-
lookbehind: true
|
|
5054
|
-
},
|
|
5055
|
-
'operator': /--|\+\+|\*\*=?|=>|&&=?|\|\|=?|[!=]==|<<=?|>>>?=?|[-+*/%&|^!=<>]=?|\.{3}|\?\?=?|\?\.?|[~:]/
|
|
5056
|
-
});
|
|
5057
|
-
|
|
5058
|
-
Prism.languages.javascript['class-name'][0].pattern = /(\b(?:class|extends|implements|instanceof|interface|new)\s+)[\w.\\]+/;
|
|
5059
|
-
|
|
5060
|
-
Prism.languages.insertBefore('javascript', 'keyword', {
|
|
5061
|
-
'regex': {
|
|
5062
|
-
pattern: RegExp(
|
|
5063
|
-
// lookbehind
|
|
5064
|
-
// eslint-disable-next-line regexp/no-dupe-characters-character-class
|
|
5065
|
-
/((?:^|[^$\w\xA0-\uFFFF."'\])\s]|\b(?:return|yield))\s*)/.source +
|
|
5066
|
-
// Regex pattern:
|
|
5067
|
-
// There are 2 regex patterns here. The RegExp set notation proposal added support for nested character
|
|
5068
|
-
// classes if the `v` flag is present. Unfortunately, nested CCs are both context-free and incompatible
|
|
5069
|
-
// with the only syntax, so we have to define 2 different regex patterns.
|
|
5070
|
-
/\//.source +
|
|
5071
|
-
'(?:' +
|
|
5072
|
-
/(?:\[(?:[^\]\\\r\n]|\\.)*\]|\\.|[^/\\\[\r\n])+\/[dgimyus]{0,7}/.source +
|
|
5073
|
-
'|' +
|
|
5074
|
-
// `v` flag syntax. This supports 3 levels of nested character classes.
|
|
5075
|
-
/(?:\[(?:[^[\]\\\r\n]|\\.|\[(?:[^[\]\\\r\n]|\\.|\[(?:[^[\]\\\r\n]|\\.)*\])*\])*\]|\\.|[^/\\\[\r\n])+\/[dgimyus]{0,7}v[dgimyus]{0,7}/.source +
|
|
5076
|
-
')' +
|
|
5077
|
-
// lookahead
|
|
5078
|
-
/(?=(?:\s|\/\*(?:[^*]|\*(?!\/))*\*\/)*(?:$|[\r\n,.;:})\]]|\/\/))/.source
|
|
5079
|
-
),
|
|
5080
|
-
lookbehind: true,
|
|
5081
|
-
greedy: true,
|
|
5082
|
-
inside: {
|
|
5083
|
-
'regex-source': {
|
|
5084
|
-
pattern: /^(\/)[\s\S]+(?=\/[a-z]*$)/,
|
|
5085
|
-
lookbehind: true,
|
|
5086
|
-
alias: 'language-regex',
|
|
5087
|
-
inside: Prism.languages.regex
|
|
5088
|
-
},
|
|
5089
|
-
'regex-delimiter': /^\/|\/$/,
|
|
5090
|
-
'regex-flags': /^[a-z]+$/,
|
|
5091
|
-
}
|
|
5092
|
-
},
|
|
5093
|
-
// This must be declared before keyword because we use "function" inside the look-forward
|
|
5094
|
-
'function-variable': {
|
|
5095
|
-
pattern: /#?(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*[=:]\s*(?:async\s*)?(?:\bfunction\b|(?:\((?:[^()]|\([^()]*\))*\)|(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*)\s*=>))/,
|
|
5096
|
-
alias: 'function'
|
|
5097
|
-
},
|
|
5098
|
-
'parameter': [
|
|
5099
|
-
{
|
|
5100
|
-
pattern: /(function(?:\s+(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*)?\s*\(\s*)(?!\s)(?:[^()\s]|\s+(?![\s)])|\([^()]*\))+(?=\s*\))/,
|
|
5101
|
-
lookbehind: true,
|
|
5102
|
-
inside: Prism.languages.javascript
|
|
5103
|
-
},
|
|
5104
|
-
{
|
|
5105
|
-
pattern: /(^|[^$\w\xA0-\uFFFF])(?!\s)[_$a-z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*=>)/i,
|
|
5270
|
+
// `v` flag syntax. This supports 3 levels of nested character classes.
|
|
5271
|
+
/(?:\[(?:[^[\]\\\r\n]|\\.|\[(?:[^[\]\\\r\n]|\\.|\[(?:[^[\]\\\r\n]|\\.)*\])*\])*\]|\\.|[^/\\\[\r\n])+\/[dgimyus]{0,7}v[dgimyus]{0,7}/.source +
|
|
5272
|
+
')' +
|
|
5273
|
+
// lookahead
|
|
5274
|
+
/(?=(?:\s|\/\*(?:[^*]|\*(?!\/))*\*\/)*(?:$|[\r\n,.;:})\]]|\/\/))/.source
|
|
5275
|
+
),
|
|
5106
5276
|
lookbehind: true,
|
|
5107
|
-
|
|
5277
|
+
greedy: true,
|
|
5278
|
+
inside: {
|
|
5279
|
+
'regex-source': {
|
|
5280
|
+
pattern: /^(\/)[\s\S]+(?=\/[a-z]*$)/,
|
|
5281
|
+
lookbehind: true,
|
|
5282
|
+
alias: 'language-regex',
|
|
5283
|
+
inside: Prism.languages.regex
|
|
5284
|
+
},
|
|
5285
|
+
'regex-delimiter': /^\/|\/$/,
|
|
5286
|
+
'regex-flags': /^[a-z]+$/,
|
|
5287
|
+
}
|
|
5108
5288
|
},
|
|
5109
|
-
|
|
5110
|
-
|
|
5111
|
-
|
|
5112
|
-
|
|
5289
|
+
// This must be declared before keyword because we use "function" inside the look-forward
|
|
5290
|
+
'function-variable': {
|
|
5291
|
+
pattern: /#?(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*[=:]\s*(?:async\s*)?(?:\bfunction\b|(?:\((?:[^()]|\([^()]*\))*\)|(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*)\s*=>))/,
|
|
5292
|
+
alias: 'function'
|
|
5113
5293
|
},
|
|
5114
|
-
|
|
5115
|
-
|
|
5116
|
-
|
|
5117
|
-
|
|
5118
|
-
|
|
5119
|
-
],
|
|
5120
|
-
'constant': /\b[A-Z](?:[A-Z_]|\dx?)*\b/
|
|
5121
|
-
});
|
|
5122
|
-
|
|
5123
|
-
Prism.languages.insertBefore('javascript', 'string', {
|
|
5124
|
-
'hashbang': {
|
|
5125
|
-
pattern: /^#!.*/,
|
|
5126
|
-
greedy: true,
|
|
5127
|
-
alias: 'comment'
|
|
5128
|
-
},
|
|
5129
|
-
'template-string': {
|
|
5130
|
-
pattern: /`(?:\\[\s\S]|\$\{(?:[^{}]|\{(?:[^{}]|\{[^}]*\})*\})+\}|(?!\$\{)[^\\`])*`/,
|
|
5131
|
-
greedy: true,
|
|
5132
|
-
inside: {
|
|
5133
|
-
'template-punctuation': {
|
|
5134
|
-
pattern: /^`|`$/,
|
|
5135
|
-
alias: 'string'
|
|
5294
|
+
'parameter': [
|
|
5295
|
+
{
|
|
5296
|
+
pattern: /(function(?:\s+(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*)?\s*\(\s*)(?!\s)(?:[^()\s]|\s+(?![\s)])|\([^()]*\))+(?=\s*\))/,
|
|
5297
|
+
lookbehind: true,
|
|
5298
|
+
inside: Prism.languages.javascript
|
|
5136
5299
|
},
|
|
5137
|
-
|
|
5138
|
-
pattern: /(
|
|
5300
|
+
{
|
|
5301
|
+
pattern: /(^|[^$\w\xA0-\uFFFF])(?!\s)[_$a-z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*=>)/i,
|
|
5139
5302
|
lookbehind: true,
|
|
5140
|
-
inside:
|
|
5141
|
-
'interpolation-punctuation': {
|
|
5142
|
-
pattern: /^\$\{|\}$/,
|
|
5143
|
-
alias: 'punctuation'
|
|
5144
|
-
},
|
|
5145
|
-
rest: Prism.languages.javascript
|
|
5146
|
-
}
|
|
5303
|
+
inside: Prism.languages.javascript
|
|
5147
5304
|
},
|
|
5148
|
-
|
|
5305
|
+
{
|
|
5306
|
+
pattern: /(\(\s*)(?!\s)(?:[^()\s]|\s+(?![\s)])|\([^()]*\))+(?=\s*\)\s*=>)/,
|
|
5307
|
+
lookbehind: true,
|
|
5308
|
+
inside: Prism.languages.javascript
|
|
5309
|
+
},
|
|
5310
|
+
{
|
|
5311
|
+
pattern: /((?:\b|\s|^)(?!(?:as|async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|undefined|var|void|while|with|yield)(?![$\w\xA0-\uFFFF]))(?:(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*\s*)\(\s*|\]\s*\(\s*)(?!\s)(?:[^()\s]|\s+(?![\s)])|\([^()]*\))+(?=\s*\)\s*\{)/,
|
|
5312
|
+
lookbehind: true,
|
|
5313
|
+
inside: Prism.languages.javascript
|
|
5314
|
+
}
|
|
5315
|
+
],
|
|
5316
|
+
'constant': /\b[A-Z](?:[A-Z_]|\dx?)*\b/
|
|
5317
|
+
});
|
|
5318
|
+
|
|
5319
|
+
Prism.languages.insertBefore('javascript', 'string', {
|
|
5320
|
+
'hashbang': {
|
|
5321
|
+
pattern: /^#!.*/,
|
|
5322
|
+
greedy: true,
|
|
5323
|
+
alias: 'comment'
|
|
5324
|
+
},
|
|
5325
|
+
'template-string': {
|
|
5326
|
+
pattern: /`(?:\\[\s\S]|\$\{(?:[^{}]|\{(?:[^{}]|\{[^}]*\})*\})+\}|(?!\$\{)[^\\`])*`/,
|
|
5327
|
+
greedy: true,
|
|
5328
|
+
inside: {
|
|
5329
|
+
'template-punctuation': {
|
|
5330
|
+
pattern: /^`|`$/,
|
|
5331
|
+
alias: 'string'
|
|
5332
|
+
},
|
|
5333
|
+
'interpolation': {
|
|
5334
|
+
pattern: /((?:^|[^\\])(?:\\{2})*)\$\{(?:[^{}]|\{(?:[^{}]|\{[^}]*\})*\})+\}/,
|
|
5335
|
+
lookbehind: true,
|
|
5336
|
+
inside: {
|
|
5337
|
+
'interpolation-punctuation': {
|
|
5338
|
+
pattern: /^\$\{|\}$/,
|
|
5339
|
+
alias: 'punctuation'
|
|
5340
|
+
},
|
|
5341
|
+
rest: Prism.languages.javascript
|
|
5342
|
+
}
|
|
5343
|
+
},
|
|
5344
|
+
'string': /[\s\S]+/
|
|
5345
|
+
}
|
|
5346
|
+
},
|
|
5347
|
+
'string-property': {
|
|
5348
|
+
pattern: /((?:^|[,{])[ \t]*)(["'])(?:\\(?:\r\n|[\s\S])|(?!\2)[^\\\r\n])*\2(?=\s*:)/m,
|
|
5349
|
+
lookbehind: true,
|
|
5350
|
+
greedy: true,
|
|
5351
|
+
alias: 'property'
|
|
5149
5352
|
}
|
|
5150
|
-
}
|
|
5151
|
-
'string-property': {
|
|
5152
|
-
pattern: /((?:^|[,{])[ \t]*)(["'])(?:\\(?:\r\n|[\s\S])|(?!\2)[^\\\r\n])*\2(?=\s*:)/m,
|
|
5153
|
-
lookbehind: true,
|
|
5154
|
-
greedy: true,
|
|
5155
|
-
alias: 'property'
|
|
5156
|
-
}
|
|
5157
|
-
});
|
|
5353
|
+
});
|
|
5158
5354
|
|
|
5159
|
-
|
|
5160
|
-
|
|
5161
|
-
|
|
5162
|
-
|
|
5163
|
-
|
|
5164
|
-
|
|
5165
|
-
|
|
5355
|
+
Prism.languages.insertBefore('javascript', 'operator', {
|
|
5356
|
+
'literal-property': {
|
|
5357
|
+
pattern: /((?:^|[,{])[ \t]*)(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*:)/m,
|
|
5358
|
+
lookbehind: true,
|
|
5359
|
+
alias: 'property'
|
|
5360
|
+
},
|
|
5361
|
+
});
|
|
5166
5362
|
|
|
5167
|
-
|
|
5168
|
-
|
|
5363
|
+
if (Prism.languages.markup) {
|
|
5364
|
+
Prism.languages.markup.tag.addInlined('script', 'javascript');
|
|
5169
5365
|
|
|
5170
|
-
|
|
5171
|
-
|
|
5172
|
-
|
|
5173
|
-
|
|
5174
|
-
|
|
5175
|
-
|
|
5176
|
-
|
|
5366
|
+
// add attribute support for all DOM events.
|
|
5367
|
+
// https://developer.mozilla.org/en-US/docs/Web/Events#Standard_events
|
|
5368
|
+
Prism.languages.markup.tag.addAttribute(
|
|
5369
|
+
/on(?:abort|blur|change|click|composition(?:end|start|update)|dblclick|error|focus(?:in|out)?|key(?:down|up)|load|mouse(?:down|enter|leave|move|out|over|up)|reset|resize|scroll|select|slotchange|submit|unload|wheel)/.source,
|
|
5370
|
+
'javascript'
|
|
5371
|
+
);
|
|
5372
|
+
}
|
|
5177
5373
|
|
|
5178
|
-
|
|
5374
|
+
Prism.languages.js = Prism.languages.javascript;
|
|
5179
5375
|
|
|
5180
5376
|
|
|
5181
|
-
|
|
5182
|
-
|
|
5183
|
-
|
|
5377
|
+
/* **********************************************
|
|
5378
|
+
Begin prism-file-highlight.js
|
|
5379
|
+
********************************************** */
|
|
5184
5380
|
|
|
5185
|
-
|
|
5381
|
+
(function () {
|
|
5186
5382
|
|
|
5187
|
-
|
|
5188
|
-
|
|
5189
|
-
|
|
5383
|
+
if (typeof Prism === 'undefined' || typeof document === 'undefined') {
|
|
5384
|
+
return;
|
|
5385
|
+
}
|
|
5190
5386
|
|
|
5191
|
-
|
|
5192
|
-
|
|
5193
|
-
|
|
5194
|
-
|
|
5387
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/Element/matches#Polyfill
|
|
5388
|
+
if (!Element.prototype.matches) {
|
|
5389
|
+
Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
|
|
5390
|
+
}
|
|
5195
5391
|
|
|
5196
|
-
|
|
5197
|
-
|
|
5198
|
-
|
|
5199
|
-
|
|
5200
|
-
|
|
5201
|
-
|
|
5202
|
-
|
|
5203
|
-
|
|
5204
|
-
|
|
5205
|
-
|
|
5206
|
-
|
|
5207
|
-
|
|
5208
|
-
|
|
5209
|
-
|
|
5210
|
-
|
|
5211
|
-
|
|
5212
|
-
|
|
5392
|
+
var LOADING_MESSAGE = 'Loading…';
|
|
5393
|
+
var FAILURE_MESSAGE = function (status, message) {
|
|
5394
|
+
return '✖ Error ' + status + ' while fetching file: ' + message;
|
|
5395
|
+
};
|
|
5396
|
+
var FAILURE_EMPTY_MESSAGE = '✖ Error: File does not exist or is empty';
|
|
5397
|
+
|
|
5398
|
+
var EXTENSIONS = {
|
|
5399
|
+
'js': 'javascript',
|
|
5400
|
+
'py': 'python',
|
|
5401
|
+
'rb': 'ruby',
|
|
5402
|
+
'ps1': 'powershell',
|
|
5403
|
+
'psm1': 'powershell',
|
|
5404
|
+
'sh': 'bash',
|
|
5405
|
+
'bat': 'batch',
|
|
5406
|
+
'h': 'c',
|
|
5407
|
+
'tex': 'latex'
|
|
5408
|
+
};
|
|
5213
5409
|
|
|
5214
|
-
|
|
5215
|
-
|
|
5216
|
-
|
|
5217
|
-
|
|
5410
|
+
var STATUS_ATTR = 'data-src-status';
|
|
5411
|
+
var STATUS_LOADING = 'loading';
|
|
5412
|
+
var STATUS_LOADED = 'loaded';
|
|
5413
|
+
var STATUS_FAILED = 'failed';
|
|
5218
5414
|
|
|
5219
|
-
|
|
5220
|
-
|
|
5415
|
+
var SELECTOR = 'pre[data-src]:not([' + STATUS_ATTR + '="' + STATUS_LOADED + '"])'
|
|
5416
|
+
+ ':not([' + STATUS_ATTR + '="' + STATUS_LOADING + '"])';
|
|
5221
5417
|
|
|
5222
|
-
|
|
5223
|
-
|
|
5224
|
-
|
|
5225
|
-
|
|
5226
|
-
|
|
5227
|
-
|
|
5228
|
-
|
|
5229
|
-
|
|
5230
|
-
|
|
5231
|
-
|
|
5232
|
-
|
|
5233
|
-
|
|
5234
|
-
|
|
5235
|
-
|
|
5236
|
-
} else {
|
|
5237
|
-
if (xhr.status >= 400) {
|
|
5238
|
-
error(FAILURE_MESSAGE(xhr.status, xhr.statusText));
|
|
5418
|
+
/**
|
|
5419
|
+
* Loads the given file.
|
|
5420
|
+
*
|
|
5421
|
+
* @param {string} src The URL or path of the source file to load.
|
|
5422
|
+
* @param {(result: string) => void} success
|
|
5423
|
+
* @param {(reason: string) => void} error
|
|
5424
|
+
*/
|
|
5425
|
+
function loadFile(src, success, error) {
|
|
5426
|
+
var xhr = new XMLHttpRequest();
|
|
5427
|
+
xhr.open('GET', src, true);
|
|
5428
|
+
xhr.onreadystatechange = function () {
|
|
5429
|
+
if (xhr.readyState == 4) {
|
|
5430
|
+
if (xhr.status < 400 && xhr.responseText) {
|
|
5431
|
+
success(xhr.responseText);
|
|
5239
5432
|
} else {
|
|
5240
|
-
|
|
5433
|
+
if (xhr.status >= 400) {
|
|
5434
|
+
error(FAILURE_MESSAGE(xhr.status, xhr.statusText));
|
|
5435
|
+
} else {
|
|
5436
|
+
error(FAILURE_EMPTY_MESSAGE);
|
|
5437
|
+
}
|
|
5241
5438
|
}
|
|
5242
5439
|
}
|
|
5243
|
-
}
|
|
5244
|
-
|
|
5245
|
-
|
|
5246
|
-
}
|
|
5440
|
+
};
|
|
5441
|
+
xhr.send(null);
|
|
5442
|
+
}
|
|
5247
5443
|
|
|
5248
|
-
|
|
5249
|
-
|
|
5250
|
-
|
|
5251
|
-
|
|
5252
|
-
|
|
5253
|
-
|
|
5254
|
-
|
|
5255
|
-
|
|
5256
|
-
|
|
5257
|
-
|
|
5258
|
-
|
|
5259
|
-
|
|
5260
|
-
|
|
5261
|
-
|
|
5262
|
-
|
|
5263
|
-
|
|
5264
|
-
|
|
5265
|
-
|
|
5266
|
-
|
|
5267
|
-
|
|
5444
|
+
/**
|
|
5445
|
+
* Parses the given range.
|
|
5446
|
+
*
|
|
5447
|
+
* This returns a range with inclusive ends.
|
|
5448
|
+
*
|
|
5449
|
+
* @param {string | null | undefined} range
|
|
5450
|
+
* @returns {[number, number | undefined] | undefined}
|
|
5451
|
+
*/
|
|
5452
|
+
function parseRange(range) {
|
|
5453
|
+
var m = /^\s*(\d+)\s*(?:(,)\s*(?:(\d+)\s*)?)?$/.exec(range || '');
|
|
5454
|
+
if (m) {
|
|
5455
|
+
var start = Number(m[1]);
|
|
5456
|
+
var comma = m[2];
|
|
5457
|
+
var end = m[3];
|
|
5458
|
+
|
|
5459
|
+
if (!comma) {
|
|
5460
|
+
return [start, start];
|
|
5461
|
+
}
|
|
5462
|
+
if (!end) {
|
|
5463
|
+
return [start, undefined];
|
|
5464
|
+
}
|
|
5465
|
+
return [start, Number(end)];
|
|
5268
5466
|
}
|
|
5269
|
-
return
|
|
5467
|
+
return undefined;
|
|
5270
5468
|
}
|
|
5271
|
-
return undefined;
|
|
5272
|
-
}
|
|
5273
5469
|
|
|
5274
|
-
|
|
5275
|
-
|
|
5276
|
-
|
|
5470
|
+
Prism.hooks.add('before-highlightall', function (env) {
|
|
5471
|
+
env.selector += ', ' + SELECTOR;
|
|
5472
|
+
});
|
|
5277
5473
|
|
|
5278
|
-
|
|
5279
|
-
|
|
5280
|
-
|
|
5281
|
-
|
|
5474
|
+
Prism.hooks.add('before-sanity-check', function (env) {
|
|
5475
|
+
var pre = /** @type {HTMLPreElement} */ (env.element);
|
|
5476
|
+
if (pre.matches(SELECTOR)) {
|
|
5477
|
+
env.code = ''; // fast-path the whole thing and go to complete
|
|
5282
5478
|
|
|
5283
|
-
|
|
5479
|
+
pre.setAttribute(STATUS_ATTR, STATUS_LOADING); // mark as loading
|
|
5284
5480
|
|
|
5285
|
-
|
|
5286
|
-
|
|
5287
|
-
|
|
5481
|
+
// add code element with loading message
|
|
5482
|
+
var code = pre.appendChild(document.createElement('CODE'));
|
|
5483
|
+
code.textContent = LOADING_MESSAGE;
|
|
5288
5484
|
|
|
5289
|
-
|
|
5485
|
+
var src = pre.getAttribute('data-src');
|
|
5290
5486
|
|
|
5291
|
-
|
|
5292
|
-
|
|
5293
|
-
|
|
5294
|
-
|
|
5295
|
-
|
|
5296
|
-
|
|
5297
|
-
|
|
5487
|
+
var language = env.language;
|
|
5488
|
+
if (language === 'none') {
|
|
5489
|
+
// the language might be 'none' because there is no language set;
|
|
5490
|
+
// in this case, we want to use the extension as the language
|
|
5491
|
+
var extension = (/\.(\w+)$/.exec(src) || [, 'none'])[1];
|
|
5492
|
+
language = EXTENSIONS[extension] || extension;
|
|
5493
|
+
}
|
|
5298
5494
|
|
|
5299
|
-
|
|
5300
|
-
|
|
5301
|
-
|
|
5495
|
+
// set language classes
|
|
5496
|
+
Prism.util.setLanguage(code, language);
|
|
5497
|
+
Prism.util.setLanguage(pre, language);
|
|
5302
5498
|
|
|
5303
|
-
|
|
5304
|
-
|
|
5305
|
-
|
|
5306
|
-
|
|
5307
|
-
|
|
5499
|
+
// preload the language
|
|
5500
|
+
var autoloader = Prism.plugins.autoloader;
|
|
5501
|
+
if (autoloader) {
|
|
5502
|
+
autoloader.loadLanguages(language);
|
|
5503
|
+
}
|
|
5308
5504
|
|
|
5309
|
-
|
|
5310
|
-
|
|
5311
|
-
|
|
5312
|
-
|
|
5313
|
-
|
|
5314
|
-
|
|
5505
|
+
// load file
|
|
5506
|
+
loadFile(
|
|
5507
|
+
src,
|
|
5508
|
+
function (text) {
|
|
5509
|
+
// mark as loaded
|
|
5510
|
+
pre.setAttribute(STATUS_ATTR, STATUS_LOADED);
|
|
5315
5511
|
|
|
5316
|
-
|
|
5317
|
-
|
|
5318
|
-
|
|
5319
|
-
|
|
5512
|
+
// handle data-range
|
|
5513
|
+
var range = parseRange(pre.getAttribute('data-range'));
|
|
5514
|
+
if (range) {
|
|
5515
|
+
var lines = text.split(/\r\n?|\n/g);
|
|
5320
5516
|
|
|
5321
|
-
|
|
5322
|
-
|
|
5323
|
-
|
|
5517
|
+
// the range is one-based and inclusive on both ends
|
|
5518
|
+
var start = range[0];
|
|
5519
|
+
var end = range[1] == null ? lines.length : range[1];
|
|
5324
5520
|
|
|
5325
|
-
|
|
5326
|
-
|
|
5327
|
-
|
|
5328
|
-
|
|
5521
|
+
if (start < 0) { start += lines.length; }
|
|
5522
|
+
start = Math.max(0, Math.min(start - 1, lines.length));
|
|
5523
|
+
if (end < 0) { end += lines.length; }
|
|
5524
|
+
end = Math.max(0, Math.min(end, lines.length));
|
|
5329
5525
|
|
|
5330
|
-
|
|
5526
|
+
text = lines.slice(start, end).join('\n');
|
|
5331
5527
|
|
|
5332
|
-
|
|
5333
|
-
|
|
5334
|
-
|
|
5528
|
+
// add data-start for line numbers
|
|
5529
|
+
if (!pre.hasAttribute('data-start')) {
|
|
5530
|
+
pre.setAttribute('data-start', String(start + 1));
|
|
5531
|
+
}
|
|
5335
5532
|
}
|
|
5336
|
-
}
|
|
5337
5533
|
|
|
5338
|
-
|
|
5339
|
-
|
|
5340
|
-
|
|
5341
|
-
|
|
5342
|
-
|
|
5343
|
-
|
|
5344
|
-
|
|
5534
|
+
// highlight code
|
|
5535
|
+
code.textContent = text;
|
|
5536
|
+
Prism.highlightElement(code);
|
|
5537
|
+
},
|
|
5538
|
+
function (error) {
|
|
5539
|
+
// mark as failed
|
|
5540
|
+
pre.setAttribute(STATUS_ATTR, STATUS_FAILED);
|
|
5345
5541
|
|
|
5346
|
-
|
|
5347
|
-
|
|
5348
|
-
|
|
5349
|
-
|
|
5350
|
-
|
|
5542
|
+
code.textContent = error;
|
|
5543
|
+
}
|
|
5544
|
+
);
|
|
5545
|
+
}
|
|
5546
|
+
});
|
|
5351
5547
|
|
|
5352
|
-
|
|
5353
|
-
|
|
5354
|
-
|
|
5355
|
-
|
|
5356
|
-
|
|
5357
|
-
|
|
5358
|
-
|
|
5359
|
-
|
|
5360
|
-
|
|
5361
|
-
|
|
5548
|
+
Prism.plugins.fileHighlight = {
|
|
5549
|
+
/**
|
|
5550
|
+
* Executes the File Highlight plugin for all matching `pre` elements under the given container.
|
|
5551
|
+
*
|
|
5552
|
+
* Note: Elements which are already loaded or currently loading will not be touched by this method.
|
|
5553
|
+
*
|
|
5554
|
+
* @param {ParentNode} [container=document]
|
|
5555
|
+
*/
|
|
5556
|
+
highlight: function highlight(container) {
|
|
5557
|
+
var elements = (container || document).querySelectorAll(SELECTOR);
|
|
5362
5558
|
|
|
5363
|
-
|
|
5364
|
-
|
|
5559
|
+
for (var i = 0, element; (element = elements[i++]);) {
|
|
5560
|
+
Prism.highlightElement(element);
|
|
5561
|
+
}
|
|
5365
5562
|
}
|
|
5366
|
-
}
|
|
5367
|
-
};
|
|
5368
|
-
|
|
5369
|
-
var logged = false;
|
|
5370
|
-
/** @deprecated Use `Prism.plugins.fileHighlight.highlight` instead. */
|
|
5371
|
-
Prism.fileHighlight = function () {
|
|
5372
|
-
if (!logged) {
|
|
5373
|
-
console.warn('Prism.fileHighlight is deprecated. Use `Prism.plugins.fileHighlight.highlight` instead.');
|
|
5374
|
-
logged = true;
|
|
5375
|
-
}
|
|
5376
|
-
Prism.plugins.fileHighlight.highlight.apply(this, arguments);
|
|
5377
|
-
};
|
|
5563
|
+
};
|
|
5378
5564
|
|
|
5379
|
-
|
|
5380
|
-
|
|
5381
|
-
|
|
5382
|
-
|
|
5383
|
-
|
|
5384
|
-
|
|
5385
|
-
lookbehind: true,
|
|
5386
|
-
greedy: true
|
|
5387
|
-
},
|
|
5388
|
-
'string-interpolation': {
|
|
5389
|
-
pattern: /(?:f|fr|rf)(?:("""|''')[\s\S]*?\1|("|')(?:\\.|(?!\2)[^\\\r\n])*\2)/i,
|
|
5390
|
-
greedy: true,
|
|
5391
|
-
inside: {
|
|
5392
|
-
'interpolation': {
|
|
5393
|
-
// "{" <expression> <optional "!s", "!r", or "!a"> <optional ":" format specifier> "}"
|
|
5394
|
-
pattern: /((?:^|[^{])(?:\{\{)*)\{(?!\{)(?:[^{}]|\{(?!\{)(?:[^{}]|\{(?!\{)(?:[^{}])+\})+\})+\}/,
|
|
5395
|
-
lookbehind: true,
|
|
5396
|
-
inside: {
|
|
5397
|
-
'format-spec': {
|
|
5398
|
-
pattern: /(:)[^:(){}]+(?=\}$)/,
|
|
5399
|
-
lookbehind: true
|
|
5400
|
-
},
|
|
5401
|
-
'conversion-option': {
|
|
5402
|
-
pattern: //,
|
|
5403
|
-
alias: 'punctuation'
|
|
5404
|
-
},
|
|
5405
|
-
rest: null
|
|
5565
|
+
var logged = false;
|
|
5566
|
+
/** @deprecated Use `Prism.plugins.fileHighlight.highlight` instead. */
|
|
5567
|
+
Prism.fileHighlight = function () {
|
|
5568
|
+
if (!logged) {
|
|
5569
|
+
console.warn('Prism.fileHighlight is deprecated. Use `Prism.plugins.fileHighlight.highlight` instead.');
|
|
5570
|
+
logged = true;
|
|
5406
5571
|
}
|
|
5407
|
-
|
|
5408
|
-
|
|
5409
|
-
}
|
|
5410
|
-
},
|
|
5411
|
-
'triple-quoted-string': {
|
|
5412
|
-
pattern: /(?:[rub]|br|rb)?("""|''')[\s\S]*?\1/i,
|
|
5413
|
-
greedy: true,
|
|
5414
|
-
alias: 'string'
|
|
5415
|
-
},
|
|
5416
|
-
'string': {
|
|
5417
|
-
pattern: /(?:[rub]|br|rb)?("|')(?:\\.|(?!\1)[^\\\r\n])*\1/i,
|
|
5418
|
-
greedy: true
|
|
5419
|
-
},
|
|
5420
|
-
'function': {
|
|
5421
|
-
pattern: /((?:^|\s)def[ \t]+)[a-zA-Z_]\w*(?=\s*\()/g,
|
|
5422
|
-
lookbehind: true
|
|
5423
|
-
},
|
|
5424
|
-
'class-name': {
|
|
5425
|
-
pattern: /(\bclass\s+)\w+/i,
|
|
5426
|
-
lookbehind: true
|
|
5427
|
-
},
|
|
5428
|
-
'decorator': {
|
|
5429
|
-
pattern: /(^[\t ]*)@\w+(?:\.\w+)*/m,
|
|
5430
|
-
lookbehind: true,
|
|
5431
|
-
alias: ['annotation', 'punctuation'],
|
|
5432
|
-
inside: {
|
|
5433
|
-
'punctuation': /\./
|
|
5434
|
-
}
|
|
5435
|
-
},
|
|
5436
|
-
'keyword': /\b(?:_(?=\s*:)|and|as|assert|async|await|break|case|class|continue|def|del|elif|else|except|exec|finally|for|from|global|if|import|in|is|lambda|match|nonlocal|not|or|pass|print|raise|return|try|while|with|yield)\b/,
|
|
5437
|
-
'builtin': /\b(?:__import__|abs|all|any|apply|ascii|basestring|bin|bool|buffer|bytearray|bytes|callable|chr|classmethod|cmp|coerce|compile|complex|delattr|dict|dir|divmod|enumerate|eval|execfile|file|filter|float|format|frozenset|getattr|globals|hasattr|hash|help|hex|id|input|int|intern|isinstance|issubclass|iter|len|list|locals|long|map|max|memoryview|min|next|object|oct|open|ord|pow|property|range|raw_input|reduce|reload|repr|reversed|round|set|setattr|slice|sorted|staticmethod|str|sum|super|tuple|type|unichr|unicode|vars|xrange|zip)\b/,
|
|
5438
|
-
'boolean': /\b(?:False|None|True)\b/,
|
|
5439
|
-
'number': /\b0(?:b(?:_?[01])+|o(?:_?[0-7])+|x(?:_?[a-f0-9])+)\b|(?:\b\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\B\.\d+(?:_\d+)*)(?:e[+-]?\d+(?:_\d+)*)?j?(?!\w)/i,
|
|
5440
|
-
'operator': /[-+%=]=?|!=|:=|\*\*?=?|\/\/?=?|<[<=>]?|>[=>]?|[&|^~]/,
|
|
5441
|
-
'punctuation': /[{}[\];(),.:]/
|
|
5442
|
-
};
|
|
5572
|
+
Prism.plugins.fileHighlight.highlight.apply(this, arguments);
|
|
5573
|
+
};
|
|
5443
5574
|
|
|
5444
|
-
|
|
5575
|
+
}());
|
|
5576
|
+
} (prism));
|
|
5577
|
+
return prism.exports;
|
|
5578
|
+
}
|
|
5445
5579
|
|
|
5446
|
-
|
|
5580
|
+
requirePrism();
|
|
5447
5581
|
|
|
5448
|
-
|
|
5582
|
+
var prismPython = {};
|
|
5449
5583
|
|
|
5450
|
-
|
|
5451
|
-
return;
|
|
5452
|
-
}
|
|
5584
|
+
var hasRequiredPrismPython;
|
|
5453
5585
|
|
|
5454
|
-
|
|
5455
|
-
|
|
5456
|
-
|
|
5457
|
-
|
|
5458
|
-
|
|
5459
|
-
|
|
5586
|
+
function requirePrismPython () {
|
|
5587
|
+
if (hasRequiredPrismPython) return prismPython;
|
|
5588
|
+
hasRequiredPrismPython = 1;
|
|
5589
|
+
Prism.languages.python = {
|
|
5590
|
+
'comment': {
|
|
5591
|
+
pattern: /(^|[^\\])#.*/,
|
|
5592
|
+
lookbehind: true,
|
|
5593
|
+
greedy: true
|
|
5594
|
+
},
|
|
5595
|
+
'string-interpolation': {
|
|
5596
|
+
pattern: /(?:f|fr|rf)(?:("""|''')[\s\S]*?\1|("|')(?:\\.|(?!\2)[^\\\r\n])*\2)/i,
|
|
5597
|
+
greedy: true,
|
|
5598
|
+
inside: {
|
|
5599
|
+
'interpolation': {
|
|
5600
|
+
// "{" <expression> <optional "!s", "!r", or "!a"> <optional ":" format specifier> "}"
|
|
5601
|
+
pattern: /((?:^|[^{])(?:\{\{)*)\{(?!\{)(?:[^{}]|\{(?!\{)(?:[^{}]|\{(?!\{)(?:[^{}])+\})+\})+\}/,
|
|
5602
|
+
lookbehind: true,
|
|
5603
|
+
inside: {
|
|
5604
|
+
'format-spec': {
|
|
5605
|
+
pattern: /(:)[^:(){}]+(?=\}$)/,
|
|
5606
|
+
lookbehind: true
|
|
5607
|
+
},
|
|
5608
|
+
'conversion-option': {
|
|
5609
|
+
pattern: //,
|
|
5610
|
+
alias: 'punctuation'
|
|
5611
|
+
},
|
|
5612
|
+
rest: null
|
|
5613
|
+
}
|
|
5614
|
+
},
|
|
5615
|
+
'string': /[\s\S]+/
|
|
5616
|
+
}
|
|
5617
|
+
},
|
|
5618
|
+
'triple-quoted-string': {
|
|
5619
|
+
pattern: /(?:[rub]|br|rb)?("""|''')[\s\S]*?\1/i,
|
|
5620
|
+
greedy: true,
|
|
5621
|
+
alias: 'string'
|
|
5622
|
+
},
|
|
5623
|
+
'string': {
|
|
5624
|
+
pattern: /(?:[rub]|br|rb)?("|')(?:\\.|(?!\1)[^\\\r\n])*\1/i,
|
|
5625
|
+
greedy: true
|
|
5626
|
+
},
|
|
5627
|
+
'function': {
|
|
5628
|
+
pattern: /((?:^|\s)def[ \t]+)[a-zA-Z_]\w*(?=\s*\()/g,
|
|
5629
|
+
lookbehind: true
|
|
5630
|
+
},
|
|
5631
|
+
'class-name': {
|
|
5632
|
+
pattern: /(\bclass\s+)\w+/i,
|
|
5633
|
+
lookbehind: true
|
|
5634
|
+
},
|
|
5635
|
+
'decorator': {
|
|
5636
|
+
pattern: /(^[\t ]*)@\w+(?:\.\w+)*/m,
|
|
5637
|
+
lookbehind: true,
|
|
5638
|
+
alias: ['annotation', 'punctuation'],
|
|
5639
|
+
inside: {
|
|
5640
|
+
'punctuation': /\./
|
|
5641
|
+
}
|
|
5642
|
+
},
|
|
5643
|
+
'keyword': /\b(?:_(?=\s*:)|and|as|assert|async|await|break|case|class|continue|def|del|elif|else|except|exec|finally|for|from|global|if|import|in|is|lambda|match|nonlocal|not|or|pass|print|raise|return|try|while|with|yield)\b/,
|
|
5644
|
+
'builtin': /\b(?:__import__|abs|all|any|apply|ascii|basestring|bin|bool|buffer|bytearray|bytes|callable|chr|classmethod|cmp|coerce|compile|complex|delattr|dict|dir|divmod|enumerate|eval|execfile|file|filter|float|format|frozenset|getattr|globals|hasattr|hash|help|hex|id|input|int|intern|isinstance|issubclass|iter|len|list|locals|long|map|max|memoryview|min|next|object|oct|open|ord|pow|property|range|raw_input|reduce|reload|repr|reversed|round|set|setattr|slice|sorted|staticmethod|str|sum|super|tuple|type|unichr|unicode|vars|xrange|zip)\b/,
|
|
5645
|
+
'boolean': /\b(?:False|None|True)\b/,
|
|
5646
|
+
'number': /\b0(?:b(?:_?[01])+|o(?:_?[0-7])+|x(?:_?[a-f0-9])+)\b|(?:\b\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\B\.\d+(?:_\d+)*)(?:e[+-]?\d+(?:_\d+)*)?j?(?!\w)/i,
|
|
5647
|
+
'operator': /[-+%=]=?|!=|:=|\*\*?=?|\/\/?=?|<[<=>]?|>[=>]?|[&|^~]/,
|
|
5648
|
+
'punctuation': /[{}[\];(),.:]/
|
|
5649
|
+
};
|
|
5460
5650
|
|
|
5461
|
-
|
|
5462
|
-
* Regular expression used for determining line breaks
|
|
5463
|
-
*
|
|
5464
|
-
* @type {RegExp}
|
|
5465
|
-
*/
|
|
5466
|
-
var NEW_LINE_EXP = /\n(?!$)/g;
|
|
5651
|
+
Prism.languages.python['string-interpolation'].inside['interpolation'].inside.rest = Prism.languages.python;
|
|
5467
5652
|
|
|
5653
|
+
Prism.languages.py = Prism.languages.python;
|
|
5654
|
+
return prismPython;
|
|
5655
|
+
}
|
|
5468
5656
|
|
|
5469
|
-
|
|
5470
|
-
* Global exports
|
|
5471
|
-
*/
|
|
5472
|
-
var config = Prism.plugins.lineNumbers = {
|
|
5473
|
-
/**
|
|
5474
|
-
* Get node for provided line number
|
|
5475
|
-
*
|
|
5476
|
-
* @param {Element} element pre element
|
|
5477
|
-
* @param {number} number line number
|
|
5478
|
-
* @returns {Element|undefined}
|
|
5479
|
-
*/
|
|
5480
|
-
getLine: function (element, number) {
|
|
5481
|
-
if (element.tagName !== 'PRE' || !element.classList.contains(PLUGIN_NAME)) {
|
|
5482
|
-
return;
|
|
5483
|
-
}
|
|
5657
|
+
requirePrismPython();
|
|
5484
5658
|
|
|
5485
|
-
|
|
5486
|
-
if (!lineNumberRows) {
|
|
5487
|
-
return;
|
|
5488
|
-
}
|
|
5489
|
-
var lineNumberStart = parseInt(element.getAttribute('data-start'), 10) || 1;
|
|
5490
|
-
var lineNumberEnd = lineNumberStart + (lineNumberRows.children.length - 1);
|
|
5659
|
+
var prismLineNumbers = {};
|
|
5491
5660
|
|
|
5492
|
-
|
|
5493
|
-
number = lineNumberStart;
|
|
5494
|
-
}
|
|
5495
|
-
if (number > lineNumberEnd) {
|
|
5496
|
-
number = lineNumberEnd;
|
|
5497
|
-
}
|
|
5661
|
+
var hasRequiredPrismLineNumbers;
|
|
5498
5662
|
|
|
5499
|
-
|
|
5663
|
+
function requirePrismLineNumbers () {
|
|
5664
|
+
if (hasRequiredPrismLineNumbers) return prismLineNumbers;
|
|
5665
|
+
hasRequiredPrismLineNumbers = 1;
|
|
5666
|
+
(function () {
|
|
5500
5667
|
|
|
5501
|
-
|
|
5502
|
-
|
|
5668
|
+
if (typeof Prism === 'undefined' || typeof document === 'undefined') {
|
|
5669
|
+
return;
|
|
5670
|
+
}
|
|
5503
5671
|
|
|
5504
5672
|
/**
|
|
5505
|
-
*
|
|
5673
|
+
* Plugin name which is used as a class name for <pre> which is activating the plugin
|
|
5506
5674
|
*
|
|
5507
|
-
*
|
|
5508
|
-
*
|
|
5509
|
-
* @param {HTMLElement} element A `<pre>` element with line numbers.
|
|
5510
|
-
* @returns {void}
|
|
5675
|
+
* @type {string}
|
|
5511
5676
|
*/
|
|
5512
|
-
|
|
5513
|
-
resizeElements([element]);
|
|
5514
|
-
},
|
|
5677
|
+
var PLUGIN_NAME = 'line-numbers';
|
|
5515
5678
|
|
|
5516
5679
|
/**
|
|
5517
|
-
*
|
|
5518
|
-
* the current viewport.
|
|
5519
|
-
*
|
|
5520
|
-
* Setting this to `true` will allow the plugin to do certain optimizations for better performance.
|
|
5680
|
+
* Regular expression used for determining line breaks
|
|
5521
5681
|
*
|
|
5522
|
-
*
|
|
5523
|
-
*
|
|
5524
|
-
* @type {boolean}
|
|
5682
|
+
* @type {RegExp}
|
|
5525
5683
|
*/
|
|
5526
|
-
|
|
5527
|
-
};
|
|
5684
|
+
var NEW_LINE_EXP = /\n(?!$)/g;
|
|
5528
5685
|
|
|
5529
|
-
/**
|
|
5530
|
-
* Resizes the given elements.
|
|
5531
|
-
*
|
|
5532
|
-
* @param {HTMLElement[]} elements
|
|
5533
|
-
*/
|
|
5534
|
-
function resizeElements(elements) {
|
|
5535
|
-
elements = elements.filter(function (e) {
|
|
5536
|
-
var codeStyles = getStyles(e);
|
|
5537
|
-
var whiteSpace = codeStyles['white-space'];
|
|
5538
|
-
return whiteSpace === 'pre-wrap' || whiteSpace === 'pre-line';
|
|
5539
|
-
});
|
|
5540
5686
|
|
|
5541
|
-
|
|
5542
|
-
|
|
5543
|
-
|
|
5687
|
+
/**
|
|
5688
|
+
* Global exports
|
|
5689
|
+
*/
|
|
5690
|
+
var config = Prism.plugins.lineNumbers = {
|
|
5691
|
+
/**
|
|
5692
|
+
* Get node for provided line number
|
|
5693
|
+
*
|
|
5694
|
+
* @param {Element} element pre element
|
|
5695
|
+
* @param {number} number line number
|
|
5696
|
+
* @returns {Element|undefined}
|
|
5697
|
+
*/
|
|
5698
|
+
getLine: function (element, number) {
|
|
5699
|
+
if (element.tagName !== 'PRE' || !element.classList.contains(PLUGIN_NAME)) {
|
|
5700
|
+
return;
|
|
5701
|
+
}
|
|
5544
5702
|
|
|
5545
|
-
|
|
5546
|
-
|
|
5547
|
-
|
|
5548
|
-
|
|
5549
|
-
|
|
5550
|
-
|
|
5703
|
+
var lineNumberRows = element.querySelector('.line-numbers-rows');
|
|
5704
|
+
if (!lineNumberRows) {
|
|
5705
|
+
return;
|
|
5706
|
+
}
|
|
5707
|
+
var lineNumberStart = parseInt(element.getAttribute('data-start'), 10) || 1;
|
|
5708
|
+
var lineNumberEnd = lineNumberStart + (lineNumberRows.children.length - 1);
|
|
5709
|
+
|
|
5710
|
+
if (number < lineNumberStart) {
|
|
5711
|
+
number = lineNumberStart;
|
|
5712
|
+
}
|
|
5713
|
+
if (number > lineNumberEnd) {
|
|
5714
|
+
number = lineNumberEnd;
|
|
5715
|
+
}
|
|
5551
5716
|
|
|
5552
|
-
|
|
5553
|
-
|
|
5554
|
-
|
|
5717
|
+
var lineIndex = number - lineNumberStart;
|
|
5718
|
+
|
|
5719
|
+
return lineNumberRows.children[lineIndex];
|
|
5720
|
+
},
|
|
5721
|
+
|
|
5722
|
+
/**
|
|
5723
|
+
* Resizes the line numbers of the given element.
|
|
5724
|
+
*
|
|
5725
|
+
* This function will not add line numbers. It will only resize existing ones.
|
|
5726
|
+
*
|
|
5727
|
+
* @param {HTMLElement} element A `<pre>` element with line numbers.
|
|
5728
|
+
* @returns {void}
|
|
5729
|
+
*/
|
|
5730
|
+
resize: function (element) {
|
|
5731
|
+
resizeElements([element]);
|
|
5732
|
+
},
|
|
5733
|
+
|
|
5734
|
+
/**
|
|
5735
|
+
* Whether the plugin can assume that the units font sizes and margins are not depended on the size of
|
|
5736
|
+
* the current viewport.
|
|
5737
|
+
*
|
|
5738
|
+
* Setting this to `true` will allow the plugin to do certain optimizations for better performance.
|
|
5739
|
+
*
|
|
5740
|
+
* Set this to `false` if you use any of the following CSS units: `vh`, `vw`, `vmin`, `vmax`.
|
|
5741
|
+
*
|
|
5742
|
+
* @type {boolean}
|
|
5743
|
+
*/
|
|
5744
|
+
assumeViewportIndependence: true
|
|
5745
|
+
};
|
|
5555
5746
|
|
|
5556
|
-
|
|
5557
|
-
|
|
5558
|
-
|
|
5747
|
+
/**
|
|
5748
|
+
* Resizes the given elements.
|
|
5749
|
+
*
|
|
5750
|
+
* @param {HTMLElement[]} elements
|
|
5751
|
+
*/
|
|
5752
|
+
function resizeElements(elements) {
|
|
5753
|
+
elements = elements.filter(function (e) {
|
|
5754
|
+
var codeStyles = getStyles(e);
|
|
5755
|
+
var whiteSpace = codeStyles['white-space'];
|
|
5756
|
+
return whiteSpace === 'pre-wrap' || whiteSpace === 'pre-line';
|
|
5757
|
+
});
|
|
5559
5758
|
|
|
5560
|
-
|
|
5759
|
+
if (elements.length == 0) {
|
|
5760
|
+
return;
|
|
5561
5761
|
}
|
|
5562
5762
|
|
|
5563
|
-
|
|
5564
|
-
|
|
5763
|
+
var infos = elements.map(function (element) {
|
|
5764
|
+
var codeElement = element.querySelector('code');
|
|
5765
|
+
var lineNumbersWrapper = element.querySelector('.line-numbers-rows');
|
|
5766
|
+
if (!codeElement || !lineNumbersWrapper) {
|
|
5767
|
+
return undefined;
|
|
5768
|
+
}
|
|
5565
5769
|
|
|
5566
|
-
|
|
5567
|
-
|
|
5770
|
+
/** @type {HTMLElement} */
|
|
5771
|
+
var lineNumberSizer = element.querySelector('.line-numbers-sizer');
|
|
5772
|
+
var codeLines = codeElement.textContent.split(NEW_LINE_EXP);
|
|
5568
5773
|
|
|
5569
|
-
|
|
5570
|
-
|
|
5571
|
-
|
|
5572
|
-
|
|
5573
|
-
|
|
5574
|
-
sizer: lineNumberSizer,
|
|
5575
|
-
};
|
|
5576
|
-
}).filter(Boolean);
|
|
5577
|
-
|
|
5578
|
-
infos.forEach(function (info) {
|
|
5579
|
-
var lineNumberSizer = info.sizer;
|
|
5580
|
-
var lines = info.lines;
|
|
5581
|
-
var lineHeights = info.lineHeights;
|
|
5582
|
-
var oneLinerHeight = info.oneLinerHeight;
|
|
5583
|
-
|
|
5584
|
-
lineHeights[lines.length - 1] = undefined;
|
|
5585
|
-
lines.forEach(function (line, index) {
|
|
5586
|
-
if (line && line.length > 1) {
|
|
5587
|
-
var e = lineNumberSizer.appendChild(document.createElement('span'));
|
|
5588
|
-
e.style.display = 'block';
|
|
5589
|
-
e.textContent = line;
|
|
5590
|
-
} else {
|
|
5591
|
-
lineHeights[index] = oneLinerHeight;
|
|
5774
|
+
if (!lineNumberSizer) {
|
|
5775
|
+
lineNumberSizer = document.createElement('span');
|
|
5776
|
+
lineNumberSizer.className = 'line-numbers-sizer';
|
|
5777
|
+
|
|
5778
|
+
codeElement.appendChild(lineNumberSizer);
|
|
5592
5779
|
}
|
|
5780
|
+
|
|
5781
|
+
lineNumberSizer.innerHTML = '0';
|
|
5782
|
+
lineNumberSizer.style.display = 'block';
|
|
5783
|
+
|
|
5784
|
+
var oneLinerHeight = lineNumberSizer.getBoundingClientRect().height;
|
|
5785
|
+
lineNumberSizer.innerHTML = '';
|
|
5786
|
+
|
|
5787
|
+
return {
|
|
5788
|
+
element: element,
|
|
5789
|
+
lines: codeLines,
|
|
5790
|
+
lineHeights: [],
|
|
5791
|
+
oneLinerHeight: oneLinerHeight,
|
|
5792
|
+
sizer: lineNumberSizer,
|
|
5793
|
+
};
|
|
5794
|
+
}).filter(Boolean);
|
|
5795
|
+
|
|
5796
|
+
infos.forEach(function (info) {
|
|
5797
|
+
var lineNumberSizer = info.sizer;
|
|
5798
|
+
var lines = info.lines;
|
|
5799
|
+
var lineHeights = info.lineHeights;
|
|
5800
|
+
var oneLinerHeight = info.oneLinerHeight;
|
|
5801
|
+
|
|
5802
|
+
lineHeights[lines.length - 1] = undefined;
|
|
5803
|
+
lines.forEach(function (line, index) {
|
|
5804
|
+
if (line && line.length > 1) {
|
|
5805
|
+
var e = lineNumberSizer.appendChild(document.createElement('span'));
|
|
5806
|
+
e.style.display = 'block';
|
|
5807
|
+
e.textContent = line;
|
|
5808
|
+
} else {
|
|
5809
|
+
lineHeights[index] = oneLinerHeight;
|
|
5810
|
+
}
|
|
5811
|
+
});
|
|
5593
5812
|
});
|
|
5594
|
-
});
|
|
5595
5813
|
|
|
5596
|
-
|
|
5597
|
-
|
|
5598
|
-
|
|
5814
|
+
infos.forEach(function (info) {
|
|
5815
|
+
var lineNumberSizer = info.sizer;
|
|
5816
|
+
var lineHeights = info.lineHeights;
|
|
5599
5817
|
|
|
5600
|
-
|
|
5601
|
-
|
|
5602
|
-
|
|
5603
|
-
|
|
5818
|
+
var childIndex = 0;
|
|
5819
|
+
for (var i = 0; i < lineHeights.length; i++) {
|
|
5820
|
+
if (lineHeights[i] === undefined) {
|
|
5821
|
+
lineHeights[i] = lineNumberSizer.children[childIndex++].getBoundingClientRect().height;
|
|
5822
|
+
}
|
|
5604
5823
|
}
|
|
5605
|
-
}
|
|
5606
|
-
});
|
|
5824
|
+
});
|
|
5607
5825
|
|
|
5608
|
-
|
|
5609
|
-
|
|
5610
|
-
|
|
5826
|
+
infos.forEach(function (info) {
|
|
5827
|
+
var lineNumberSizer = info.sizer;
|
|
5828
|
+
var wrapper = info.element.querySelector('.line-numbers-rows');
|
|
5611
5829
|
|
|
5612
|
-
|
|
5613
|
-
|
|
5830
|
+
lineNumberSizer.style.display = 'none';
|
|
5831
|
+
lineNumberSizer.innerHTML = '';
|
|
5614
5832
|
|
|
5615
|
-
|
|
5616
|
-
|
|
5833
|
+
info.lineHeights.forEach(function (height, lineNumber) {
|
|
5834
|
+
wrapper.children[lineNumber].style.height = height + 'px';
|
|
5835
|
+
});
|
|
5617
5836
|
});
|
|
5618
|
-
});
|
|
5619
|
-
}
|
|
5620
|
-
|
|
5621
|
-
/**
|
|
5622
|
-
* Returns style declarations for the element
|
|
5623
|
-
*
|
|
5624
|
-
* @param {Element} element
|
|
5625
|
-
*/
|
|
5626
|
-
function getStyles(element) {
|
|
5627
|
-
if (!element) {
|
|
5628
|
-
return null;
|
|
5629
5837
|
}
|
|
5630
5838
|
|
|
5631
|
-
|
|
5632
|
-
|
|
5839
|
+
/**
|
|
5840
|
+
* Returns style declarations for the element
|
|
5841
|
+
*
|
|
5842
|
+
* @param {Element} element
|
|
5843
|
+
*/
|
|
5844
|
+
function getStyles(element) {
|
|
5845
|
+
if (!element) {
|
|
5846
|
+
return null;
|
|
5847
|
+
}
|
|
5633
5848
|
|
|
5634
|
-
|
|
5635
|
-
window.addEventListener('resize', function () {
|
|
5636
|
-
if (config.assumeViewportIndependence && lastWidth === window.innerWidth) {
|
|
5637
|
-
return;
|
|
5849
|
+
return window.getComputedStyle ? getComputedStyle(element) : (element.currentStyle || null);
|
|
5638
5850
|
}
|
|
5639
|
-
lastWidth = window.innerWidth;
|
|
5640
5851
|
|
|
5641
|
-
|
|
5642
|
-
|
|
5852
|
+
var lastWidth = undefined;
|
|
5853
|
+
window.addEventListener('resize', function () {
|
|
5854
|
+
if (config.assumeViewportIndependence && lastWidth === window.innerWidth) {
|
|
5855
|
+
return;
|
|
5856
|
+
}
|
|
5857
|
+
lastWidth = window.innerWidth;
|
|
5643
5858
|
|
|
5644
|
-
|
|
5645
|
-
|
|
5646
|
-
return;
|
|
5647
|
-
}
|
|
5859
|
+
resizeElements(Array.prototype.slice.call(document.querySelectorAll('pre.' + PLUGIN_NAME)));
|
|
5860
|
+
});
|
|
5648
5861
|
|
|
5649
|
-
|
|
5650
|
-
|
|
5862
|
+
Prism.hooks.add('complete', function (env) {
|
|
5863
|
+
if (!env.code) {
|
|
5864
|
+
return;
|
|
5865
|
+
}
|
|
5651
5866
|
|
|
5652
|
-
|
|
5653
|
-
|
|
5654
|
-
return;
|
|
5655
|
-
}
|
|
5867
|
+
var code = /** @type {Element} */ (env.element);
|
|
5868
|
+
var pre = /** @type {HTMLElement} */ (code.parentNode);
|
|
5656
5869
|
|
|
5657
|
-
|
|
5658
|
-
|
|
5659
|
-
|
|
5660
|
-
|
|
5870
|
+
// works only for <code> wrapped inside <pre> (not inline)
|
|
5871
|
+
if (!pre || !/pre/i.test(pre.nodeName)) {
|
|
5872
|
+
return;
|
|
5873
|
+
}
|
|
5661
5874
|
|
|
5662
|
-
|
|
5663
|
-
|
|
5664
|
-
|
|
5665
|
-
|
|
5875
|
+
// Abort if line numbers already exists
|
|
5876
|
+
if (code.querySelector('.line-numbers-rows')) {
|
|
5877
|
+
return;
|
|
5878
|
+
}
|
|
5666
5879
|
|
|
5667
|
-
|
|
5668
|
-
|
|
5669
|
-
|
|
5670
|
-
|
|
5880
|
+
// only add line numbers if <code> or one of its ancestors has the `line-numbers` class
|
|
5881
|
+
if (!Prism.util.isActive(code, PLUGIN_NAME)) {
|
|
5882
|
+
return;
|
|
5883
|
+
}
|
|
5671
5884
|
|
|
5672
|
-
|
|
5673
|
-
|
|
5674
|
-
|
|
5885
|
+
// Remove the class 'line-numbers' from the <code>
|
|
5886
|
+
code.classList.remove(PLUGIN_NAME);
|
|
5887
|
+
// Add the class 'line-numbers' to the <pre>
|
|
5888
|
+
pre.classList.add(PLUGIN_NAME);
|
|
5675
5889
|
|
|
5676
|
-
|
|
5890
|
+
var match = env.code.match(NEW_LINE_EXP);
|
|
5891
|
+
var linesNum = match ? match.length + 1 : 1;
|
|
5892
|
+
var lineNumbersWrapper;
|
|
5677
5893
|
|
|
5678
|
-
|
|
5679
|
-
lineNumbersWrapper.setAttribute('aria-hidden', 'true');
|
|
5680
|
-
lineNumbersWrapper.className = 'line-numbers-rows';
|
|
5681
|
-
lineNumbersWrapper.innerHTML = lines;
|
|
5894
|
+
var lines = new Array(linesNum + 1).join('<span></span>');
|
|
5682
5895
|
|
|
5683
|
-
|
|
5684
|
-
|
|
5685
|
-
|
|
5896
|
+
lineNumbersWrapper = document.createElement('span');
|
|
5897
|
+
lineNumbersWrapper.setAttribute('aria-hidden', 'true');
|
|
5898
|
+
lineNumbersWrapper.className = 'line-numbers-rows';
|
|
5899
|
+
lineNumbersWrapper.innerHTML = lines;
|
|
5900
|
+
|
|
5901
|
+
if (pre.hasAttribute('data-start')) {
|
|
5902
|
+
pre.style.counterReset = 'linenumber ' + (parseInt(pre.getAttribute('data-start'), 10) - 1);
|
|
5903
|
+
}
|
|
5904
|
+
|
|
5905
|
+
env.element.appendChild(lineNumbersWrapper);
|
|
5686
5906
|
|
|
5687
|
-
|
|
5907
|
+
resizeElements([pre]);
|
|
5688
5908
|
|
|
5689
|
-
|
|
5909
|
+
Prism.hooks.run('line-numbers', env);
|
|
5910
|
+
});
|
|
5690
5911
|
|
|
5691
|
-
Prism.hooks.
|
|
5692
|
-
|
|
5912
|
+
Prism.hooks.add('line-numbers', function (env) {
|
|
5913
|
+
env.plugins = env.plugins || {};
|
|
5914
|
+
env.plugins.lineNumbers = true;
|
|
5915
|
+
});
|
|
5693
5916
|
|
|
5694
|
-
|
|
5695
|
-
|
|
5696
|
-
|
|
5697
|
-
});
|
|
5917
|
+
}());
|
|
5918
|
+
return prismLineNumbers;
|
|
5919
|
+
}
|
|
5698
5920
|
|
|
5699
|
-
|
|
5921
|
+
requirePrismLineNumbers();
|
|
5700
5922
|
|
|
5701
5923
|
const initialize = () => {
|
|
5702
5924
|
// @ts-ignore
|
|
@@ -5716,15 +5938,23 @@ const initialize = () => {
|
|
|
5716
5938
|
}
|
|
5717
5939
|
};
|
|
5718
5940
|
// @ts-ignore
|
|
5719
|
-
window.setResults = (playerNames, places,
|
|
5941
|
+
window.setResults = (playerNames, seed, places, statistics, parameters, verbose) => {
|
|
5942
|
+
// @ts-ignore
|
|
5943
|
+
parameters = parameters.toJs();
|
|
5944
|
+
// @ts-ignore
|
|
5945
|
+
statistics = statistics.toJs();
|
|
5720
5946
|
const results = getLocalStorage("Results");
|
|
5721
5947
|
if (!results[playerNames.join(", ")]) {
|
|
5722
5948
|
results[playerNames.join(", ")] = {};
|
|
5723
5949
|
}
|
|
5724
|
-
if (!results[playerNames.join(", ")][
|
|
5725
|
-
results[playerNames.join(", ")][
|
|
5950
|
+
if (!results[playerNames.join(", ")][JSON.stringify(parameters)]) {
|
|
5951
|
+
results[playerNames.join(", ")][JSON.stringify(parameters)] = [];
|
|
5726
5952
|
}
|
|
5727
|
-
results[playerNames.join(", ")][
|
|
5953
|
+
results[playerNames.join(", ")][JSON.stringify(parameters)].push({
|
|
5954
|
+
seed,
|
|
5955
|
+
places,
|
|
5956
|
+
statistics,
|
|
5957
|
+
});
|
|
5728
5958
|
setLocalStorage("Results", results);
|
|
5729
5959
|
updatePointModifier();
|
|
5730
5960
|
// @ts-ignore
|