wikity 1.3.0 → 1.3.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/bin/index.js ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ require('../dist/cli.js');
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -1,47 +1,58 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod };
5
- };
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- const index_1 = __importDefault(require("./index"));
8
- const common_1 = require("./common");
9
- const indent = (n) => ' '.repeat(n * 4);
10
- const usage = (command, ...desc) => {
11
- console.log('\n' + indent(2) + command);
12
- desc.forEach((msg) => console.log(indent(2.5) + msg));
13
- };
14
- const arg = (n) => process.argv[n + 1] || '';
15
- const args = process.argv.slice(1);
16
- if (!arg(1)) {
17
- console.log('Type `wikity help` for a list of commands.');
18
- }
19
- else if (arg(1).includes('h')) {
20
- console.log(`\n${indent(1)}Wikity CLI commands:`);
21
- usage(`wikity (help|-h)`, `Display this help message.`);
22
- usage(`wikity compile [<folder>] [-o <folder>] [-t <folder>] -i <folder>] [-e] [-d]`, `Compile wikitext files from a given folder.`, ` [<folder>]\n${indent(3.5)}Input folder ('.' (current folder) if unset).`, ` (-o|--outputFolder) <folder>\n${indent(3.5)}Folder that compiled HTML files are placed in ('wikity-out' if unset).`, ` (-t|--templatesFolder) <folder>\n${indent(3.5)}Where to place wiki templates ('templates' if unset).`, ` (-i|--imagesFolder) <folder>\n${indent(3.5)}Where to place wiki images ('images' if unset).`, ` (-e|--eleventy)\n${indent(3.5)}Compiles files with Eleventy front matter (false if unset).`, ` (-d|--defaultStyles)\n${indent(3.5)}Add default wiki styling to all pages (true if unset).`);
23
- usage(`wikity (parse|-p) "<input>"`, `Parse raw wikitext from the command line.`);
24
- usage(`wikity (version|-v)`, `Display the current version of Wikity.`);
25
- }
26
- else if (arg(1).includes('c')) {
27
- const configArgs = args.slice(2);
28
- const argsList = configArgs.join(' ');
29
- const getArgContent = (arg) => arg.test(argsList) && configArgs.filter((_, i) => arg.test(configArgs[i - 1])).join(' ') || '';
30
- const folder = arg(2) || '.';
31
- const outputFolder = getArgContent(/^-+o/);
32
- const templatesFolder = getArgContent(/^-+t/);
33
- const imagesFolder = getArgContent(/^-+i/);
34
- const eleventy = /^-+e/.test(argsList);
35
- const defaultStyles = /^-+d/.test(argsList);
36
- index_1.default.compile(folder, { outputFolder, templatesFolder, imagesFolder, eleventy, defaultStyles });
37
- }
38
- else if (arg(1).includes('p')) {
39
- const input = arg(2);
40
- console.log(index_1.default.parse(input));
41
- }
42
- else if (arg(1).includes('v')) {
43
- console.log('The current version of Wikity is ' + common_1.VERSION);
44
- }
45
- else {
46
- console.log('Unknown command; type `wikity help` for help');
47
- }
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const VERSION = require('../package.json').version;
7
+ const index_1 = __importDefault(require("./index"));
8
+ // Helper functions
9
+ const indent = (n) => ' '.repeat(n * 4);
10
+ const usage = (command, ...desc) => {
11
+ console.log('\n' + indent(2) + command);
12
+ desc.forEach((msg) => console.log(indent(2.5) + msg));
13
+ };
14
+ const arg = (n) => process.argv[n + 1] || '';
15
+ const args = process.argv.slice(1);
16
+ // Run CLI
17
+ if (!arg(1)) {
18
+ // No arguments
19
+ console.log('Type `wikity help` for a list of commands.');
20
+ }
21
+ else if (arg(1).includes('h')) {
22
+ // Show help message
23
+ console.log(`\n${indent(1)}Wikity CLI commands:`);
24
+ usage(`wikity (help|-h)`, `Display this help message.`);
25
+ usage(`wikity compile [<folder>] [-o <folder>] [-t <folder>] -i <folder>] [-e] [-d]`, `Compile wikitext files from a given folder.`, ` [<folder>]\n${indent(3.5)}Input folder ('.' (current folder) if unset).`, ` (-o|--outputFolder) <folder>\n${indent(3.5)}Folder that compiled HTML files are placed in ('wikity-out' if unset).`, ` (-t|--templatesFolder) <folder>\n${indent(3.5)}Where to place wiki templates ('templates' if unset).`, ` (-i|--imagesFolder) <folder>\n${indent(3.5)}Where to place wiki images ('images' if unset).`, ` (-e|--eleventy)\n${indent(3.5)}Compiles files with Eleventy front matter (false if unset).`, ` (-d|--defaultStyles)\n${indent(3.5)}Add default wiki styling to all pages (true if unset).`);
26
+ usage(`wikity (parse|-p) "<input>"`, `Parse raw wikitext from the command line.`);
27
+ usage(`wikity (version|-v)`, `Display the current version of Wikity.`);
28
+ }
29
+ else if (arg(1).includes('c')) {
30
+ // Run compilation
31
+ const configArgs = args.slice(2);
32
+ const argsList = configArgs.join(' ');
33
+ // retrieve item from arguments list
34
+ const getArgContent = (arg) => arg.test(argsList) && configArgs.filter((_, i) => arg.test(configArgs[i - 1])).join(' ') || '';
35
+ // Fetch user-supplied arguments
36
+ const folder = arg(2) || '.';
37
+ const outputFolder = getArgContent(/^-+o/);
38
+ const templatesFolder = getArgContent(/^-+t/);
39
+ const imagesFolder = getArgContent(/^-+i/);
40
+ const eleventy = /^-+e/.test(argsList);
41
+ const defaultStyles = /^-+d/.test(argsList);
42
+ index_1.default.compile(folder, { outputFolder, templatesFolder, imagesFolder, eleventy, defaultStyles });
43
+ }
44
+ else if (arg(1).includes('p')) {
45
+ // Run parsing
46
+ // second argument is inputted text
47
+ const input = arg(2);
48
+ const output = index_1.default.parse(input);
49
+ console.log(output);
50
+ }
51
+ else if (arg(1).includes('v')) {
52
+ // Show version
53
+ console.log('The current version of Wikity is ' + VERSION);
54
+ }
55
+ else {
56
+ // Unknown command
57
+ console.log('Unknown command; type `wikity help` for help');
58
+ }
@@ -0,0 +1,20 @@
1
+ export type Metadata = Record<string, any>;
2
+ export interface Config {
3
+ /** The folder that Wikity's compiled HTML files are outputted to (defaults to 'wikity-out') */
4
+ outputFolder?: string;
5
+ /** The folder that wiki templates are to be stored in relative to the root folder */
6
+ templatesFolder?: string;
7
+ /** The folder that images are to be stored in relative to the root folder */
8
+ imagesFolder?: string;
9
+ /** Whether to be set up for Eleventy integration (defaults to false) */
10
+ eleventy?: boolean;
11
+ /** Whether to use default wiki styling (defaults to true) */
12
+ defaultStyles?: boolean;
13
+ /** Custom CSS styles to add to the wiki pages */
14
+ customStyles?: string;
15
+ }
16
+ export interface Result {
17
+ data: string;
18
+ metadata: Metadata;
19
+ }
20
+ export declare function RegExpBuilder(regex: string, flag?: string): RegExp;
package/dist/common.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RegExpBuilder = void 0;
4
+ function RegExpBuilder(regex, flag = 'mgi') {
5
+ return RegExp(regex.replace(/ /g, '').replace(/\|\|.+?\|\|/g, ''), flag);
6
+ }
7
+ exports.RegExpBuilder = RegExpBuilder;
@@ -1,3 +1,3 @@
1
- import { Config } from './common';
2
- export declare function eleventyCompile(dir?: string, config?: Config): void;
3
- export declare function compile(dir?: string, config?: Config): void;
1
+ import { Config } from './common';
2
+ export declare function eleventyCompile(dir?: string, config?: Config): void;
3
+ export declare function compile(dir?: string, config?: Config): void;
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.compile = exports.eleventyCompile = void 0;
7
+ const fs = require('fs');
8
+ const glob = require('glob');
9
+ const dedent = require('dedent');
10
+ const formatter = require('html-formatter');
11
+ const parse_1 = require("./parse");
12
+ const common_1 = require("./common");
13
+ const wiki_css_1 = __importDefault(require("./wiki.css"));
14
+ const r = String.raw;
15
+ function eleventyCompile(dir = '.', config = {}) {
16
+ compile(dir, { eleventy: true, ...config });
17
+ }
18
+ exports.eleventyCompile = eleventyCompile;
19
+ function compile(dir = '.', config = {}) {
20
+ let stylesCreated = false;
21
+ // Write wikitext files
22
+ const files = glob.sync(dir + "/**/*.wiki", {});
23
+ files.forEach((file) => {
24
+ var _a;
25
+ const fileData = fs.readFileSync(file, { encoding: 'utf8' });
26
+ const { data: parsedContent, metadata } = (0, parse_1.parse)(fileData, config);
27
+ let outText = parsedContent;
28
+ const templatesFolder = config.templatesFolder || 'templates';
29
+ const imagesFolder = config.imagesFolder || 'images';
30
+ const outputFolder = config.outputFolder || 'wikity-out';
31
+ const [, folder, filename] = file.match((0, common_1.RegExpBuilder)(r `^(.+?[\/\\]) ((?:(?:${templatesFolder}|${imagesFolder})[\/\\])?[^\/\\]+)$`, '')) || [];
32
+ const outFolder = dir + '/' + outputFolder + '/';
33
+ const outFilename = filename.replace(/ /g, '_').replace('.wiki', '.html');
34
+ const urlPath = outFilename.replace(/(?<=^|\/)\w/g, m => m.toUpperCase());
35
+ const displayTitle = metadata.displayTitle || urlPath.replace('.html', '');
36
+ // Eleventy configuration
37
+ const frontMatter = config.eleventy ? dedent `
38
+ ---
39
+ permalink: /wiki/${urlPath}
40
+ ---
41
+ ` : '';
42
+ // Create TOC
43
+ if (!metadata.notoc && (metadata.toc || (((_a = outText.match(/<h\d[^>]*>/g)) === null || _a === void 0 ? void 0 : _a.length) || 0) > 3)) {
44
+ let toc = '';
45
+ let headings = Array.from(parsedContent.match(/<h\d[^>]*>.+?<\/h\d>/gs) || []);
46
+ headings.forEach(match => {
47
+ var _a;
48
+ const text = match.replace(/\s*<\/?h\d[^>]*>\s*/g, '');
49
+ const lvl = +(((_a = match.match(/\d/g)) === null || _a === void 0 ? void 0 : _a[0]) || -1);
50
+ toc += `${`<ol>`.repeat(lvl - 1)} <li> <a href="#${encodeURI(text.replace(/ /g, '_'))}">${text}</a> </li> ${`</ol>`.repeat(lvl - 1)}`;
51
+ });
52
+ const tocElem = dedent `
53
+ <div id="toc">
54
+ <span id="toc-heading">
55
+ <strong>Contents</strong>
56
+ [<a href="javascript:void(0)" onclick="
57
+ document.querySelector('#toc ol').setAttribute('style', this.innerText === 'hide' ? 'display: none;' : '');
58
+ this.innerText = this.innerText === 'hide' ? 'show' : 'hide';
59
+ ">hide</a>]
60
+ </span>
61
+ <ol>${toc}</ol>
62
+ </div>
63
+ `;
64
+ // Set TOC on page
65
+ if (outText.includes('<toc></toc>'))
66
+ outText = outText.replace('<toc></toc>', tocElem);
67
+ else
68
+ outText = outText.replace(/<h\d[^>]*>/, tocElem + '$&');
69
+ }
70
+ // Create plaintext of HTML for use as description/metadata property.
71
+ const plaintextData = parsedContent.replace(/<.+?>/gs, ' ');
72
+ // Create HTML
73
+ const html = dedent `
74
+ <html>
75
+ <head>
76
+ <meta charset="utf-8">
77
+ <meta name="viewport" content="initial-scale=1.0, width=device-width">
78
+ <meta name="description" content="${plaintextData.substring(0, 256)}...">
79
+ <title>${displayTitle}</title>
80
+ <link id="default-styles" rel="stylesheet" href="/wiki.css">
81
+ </head>
82
+ <body>
83
+ <header>
84
+ <h1 id="page-title">${displayTitle}</h1>
85
+ </header>
86
+ <main>
87
+ <p>\n${outText}
88
+ </p>
89
+ </main>
90
+ <footer>
91
+ <p id="credit_wikity">Created using <a href="https://github.com/Nixinova/Wikity">Wikity</a></p>
92
+ </footer>
93
+ </body>
94
+ </html>
95
+ `;
96
+ // Write to file
97
+ for (const path of ['', templatesFolder, imagesFolder]) {
98
+ if (!fs.existsSync(outFolder + path)) {
99
+ fs.mkdirSync(outFolder + path);
100
+ }
101
+ }
102
+ ;
103
+ const renderedHtml = formatter.render(html).replace(/(<\/\w+>)(\S)/g, '$1 $2');
104
+ fs.writeFileSync(outFolder + outFilename, frontMatter + '\n' + renderedHtml, 'utf8');
105
+ // Move images
106
+ glob(imagesFolder + '/*', {}, (err, files) => {
107
+ if (err) {
108
+ console.warn(err);
109
+ }
110
+ const outImagesFolder = outFolder + imagesFolder + '/';
111
+ if (!fs.existsSync(outImagesFolder)) {
112
+ fs.mkdirSync(outImagesFolder);
113
+ }
114
+ for (const file of files) {
115
+ fs.copyFileSync(file, outImagesFolder + file.split(/[/\\]/).pop());
116
+ }
117
+ ;
118
+ });
119
+ // Create site styles
120
+ if (!stylesCreated) {
121
+ stylesCreated = true;
122
+ let styles = '';
123
+ if (config.defaultStyles !== false) {
124
+ styles += wiki_css_1.default;
125
+ }
126
+ if (config.customStyles) {
127
+ styles += config.customStyles;
128
+ }
129
+ const cssOutput = dedent `
130
+ ---
131
+ permalink: /wiki.css
132
+ ---
133
+ ${styles}
134
+ `;
135
+ fs.writeFileSync(outFolder + 'wiki.css.njk', cssOutput);
136
+ }
137
+ });
138
+ }
139
+ exports.compile = compile;
@@ -1,8 +1,11 @@
1
- import { compile, eleventyCompile } from './compile';
2
- import { rawParse } from './parse';
3
- declare const _default: {
4
- parse: typeof rawParse;
5
- compile: typeof compile;
6
- eleventyPlugin: typeof eleventyCompile;
7
- };
8
- export = _default;
1
+ import { compile, eleventyCompile } from './compile';
2
+ import { rawParse } from './parse';
3
+ declare const _default: {
4
+ /** Parse wikitext from a string */
5
+ parse: typeof rawParse;
6
+ /** Compile a folder of wikitext files */
7
+ compile: typeof compile;
8
+ /** Compile a folder of wikitext files for output in Eleventy @deprecated */
9
+ eleventyPlugin: typeof eleventyCompile;
10
+ };
11
+ export = _default;
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ const compile_1 = require("./compile");
3
+ const parse_1 = require("./parse");
4
+ module.exports = {
5
+ /** Parse wikitext from a string */
6
+ parse: parse_1.rawParse,
7
+ /** Compile a folder of wikitext files */
8
+ compile: compile_1.compile,
9
+ /** Compile a folder of wikitext files for output in Eleventy @deprecated */
10
+ eleventyPlugin: compile_1.eleventyCompile,
11
+ };
@@ -1,3 +1,3 @@
1
- import { Config, Result } from './common';
2
- export declare function rawParse(data: string, config?: Config): string;
3
- export declare function parse(data: string, config?: Config): Result;
1
+ import { Config, Result } from './common';
2
+ export declare function rawParse(data: string, config?: Config): string;
3
+ export declare function parse(data: string, config?: Config): Result;
package/dist/parse.js ADDED
@@ -0,0 +1,259 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parse = exports.rawParse = void 0;
4
+ const fs = require('fs');
5
+ const htmlEscape = require('escape-html');
6
+ const dateFormat = require('dateformat');
7
+ const common_1 = require("./common");
8
+ const r = String.raw;
9
+ const MAX_RECURSION = 20;
10
+ const MAX_ARG_COUNT = 10;
11
+ const arg = r `\s*([^|}]+?)\s*`;
12
+ function rawParse(data, config = {}) {
13
+ return parse(data, config).data;
14
+ }
15
+ exports.rawParse = rawParse;
16
+ function parse(data, config = {}) {
17
+ const templatesFolder = config.templatesFolder || 'templates';
18
+ const imagesFolder = config.imagesFolder || 'images';
19
+ const vars = {};
20
+ const metadata = {};
21
+ const nowikis = [];
22
+ const refs = [];
23
+ let nowikiCount = 0;
24
+ let rawExtLinkCount = 0;
25
+ let refCount = 0;
26
+ let outText = data;
27
+ let stylesCreated = false;
28
+ for (let l = 0, last = ''; l < MAX_RECURSION; l++) {
29
+ if (last === outText)
30
+ break;
31
+ last = outText;
32
+ outText = outText
33
+ // Nowiki: <nowiki></nowiki>
34
+ .replace((0, common_1.RegExpBuilder)(r `<nowiki> ([^]+?) </nowiki>`), (_, m) => `%NOWIKI#${nowikis.push(m), nowikiCount++}%`)
35
+ // Sanitise unacceptable HTML
36
+ .replace((0, common_1.RegExpBuilder)(r `<(/?) \s* (?= script|link|meta|iframe|frameset|object|embed|applet|form|input|button|textarea )`), '&lt;$1')
37
+ .replace((0, common_1.RegExpBuilder)(r `(?<= <[^>]+ ) (\bon(\w+))`), 'data-$2')
38
+ // Comments: <!-- -->
39
+ .replace(/<!--[^]+?-->/g, '')
40
+ // Lines: ----
41
+ .replace(/^-{4,}/gm, '<hr>')
42
+ // Metadata: displayTitle, __NOTOC__, etc
43
+ .replace((0, common_1.RegExpBuilder)(r `{{ \s* displayTitle: ([^}]+) }}`), (_, title) => (metadata.displayTitle = title, ''))
44
+ .replace((0, common_1.RegExpBuilder)(r `__NOINDEX__`), () => (metadata.noindex = true, ''))
45
+ .replace((0, common_1.RegExpBuilder)(r `__NOTOC__`), () => (metadata.notoc = true, ''))
46
+ .replace((0, common_1.RegExpBuilder)(r `__FORCETOC__`), () => (metadata.toc = true, ''))
47
+ .replace((0, common_1.RegExpBuilder)(r `__TOC__`), () => (metadata.toc = true, `<toc></toc>`))
48
+ // Magic words: {{!}}, {{reflist}}, etc
49
+ .replace((0, common_1.RegExpBuilder)(r `{{ \s* ! \s* }}`), '&vert;')
50
+ .replace((0, common_1.RegExpBuilder)(r `{{ \s* = \s* }}`), '&equals;')
51
+ .replace((0, common_1.RegExpBuilder)(r `{{ \s* [Rr]eflist \s* }}`), '<references/>')
52
+ // String functions: {{lc:}}, {{ucfirst:}}, {{len:}}, etc
53
+ .replace((0, common_1.RegExpBuilder)(r `{{ \s* #? urlencode: ${arg} }}`), (_, m) => encodeURI(m))
54
+ .replace((0, common_1.RegExpBuilder)(r `{{ \s* #? urldecode: ${arg} }}`), (_, m) => decodeURI(m))
55
+ .replace((0, common_1.RegExpBuilder)(r `{{ \s* #? lc: ${arg} }}`), (_, m) => m.toLowerCase())
56
+ .replace((0, common_1.RegExpBuilder)(r `{{ \s* #? uc: ${arg} }}`), (_, m) => m.toUpperCase())
57
+ .replace((0, common_1.RegExpBuilder)(r `{{ \s* #? lcfirst: ${arg} }}`), (_, m) => m[0].toLowerCase() + m.substr(1))
58
+ .replace((0, common_1.RegExpBuilder)(r `{{ \s* #? ucfirst: ${arg} }}`), (_, m) => m[0].toUpperCase() + m.substr(1))
59
+ .replace((0, common_1.RegExpBuilder)(r `{{ \s* #? len: ${arg} }}`), (_, m) => m.length)
60
+ .replace((0, common_1.RegExpBuilder)(r `{{ \s* #? pos: ${arg} \|${arg} (?: \s*\|${arg} )? }}`), (_, find, str, n = 0) => find.substr(n).indexOf(str))
61
+ .replace((0, common_1.RegExpBuilder)(r `{{ \s* #? sub: ${arg} \|${arg} (?:\|${arg})? }}`), (_, str, from, len) => str.substr(+from - 1, +len))
62
+ .replace((0, common_1.RegExpBuilder)(r `{{ \s* #? padleft: ${arg} \|${arg} \|${arg} }}`), (_, str, n, char) => str.padStart(+n, char))
63
+ .replace((0, common_1.RegExpBuilder)(r `{{ \s* #? padright: ${arg} \|${arg} \|${arg} }}`), (_, str, n, char) => str.padEnd(+n, char))
64
+ .replace((0, common_1.RegExpBuilder)(r `{{ \s* #? replace: ${arg} \|${arg} \|${arg} }}`), (_, str, find, rep) => str.split(find).join(rep))
65
+ .replace((0, common_1.RegExpBuilder)(r `{{ \s* #? explode: ${arg} \|${arg} \|${arg} }}`), (_, str, delim, pos) => str.split(delim)[+pos])
66
+ // Parser functions: {{#if:}}, {{#switch:}}, etc
67
+ .replace((0, common_1.RegExpBuilder)(r `{{ \s* (#\w+) \s* : \s* ( [^{}]+ ) \s* }} ( ?!} )`), (_, name, content) => {
68
+ if (/{{\s*#/.test(content))
69
+ return _;
70
+ const args = content.trim().split(/\s*\|\s*/);
71
+ switch (name) {
72
+ case '#if':
73
+ return (args[0] ? args[1] : args[2]) || '';
74
+ case '#ifeq':
75
+ return (args[0] === args[1] ? args[2] : args[3]) || '';
76
+ case '#vardefine':
77
+ vars[args[0]] = args[1] || '';
78
+ return '';
79
+ case '#var':
80
+ if ((0, common_1.RegExpBuilder)(r `{{ \s* #vardefine \s* : \s* ${args[0]}`).test(outText))
81
+ return _; // wait until var is set
82
+ return vars[args[0]] || args[1] || '';
83
+ case '#switch':
84
+ return args.slice(1)
85
+ .map(arg => arg.split(/\s*=\s*/))
86
+ .filter(duo => args[0] === duo[0].replace('#default', args[0]))[0][1];
87
+ case '#time':
88
+ case '#date':
89
+ case '#datetime':
90
+ // make sure the characters are not inside a string
91
+ let parsedMatch = args[0].replace(/".+?"/g, '').replace(/'.+?'/g, '');
92
+ if (/[abcefgijkqruvx]/i.test(parsedMatch)) {
93
+ const errMsg = `Wikity does not use Wikipedia's #time function syntax. Use repetition-based formatting (e.g. yyyy-mm-dd) instead.`;
94
+ console.warn(`<Wikity> [WARN] ${errMsg}`);
95
+ }
96
+ return dateFormat(args[1] ? new Date(args[1]) : new Date(), args[0]);
97
+ }
98
+ })
99
+ // Templates: {{template}}
100
+ .replace((0, common_1.RegExpBuilder)(r `(?<!{) {{ \s* ([^#{}|]+?) (\|[^{}]+)? }} (?!})`), (_, title, params = '') => {
101
+ const page = templatesFolder + '/' + title.trim().replace(/ /g, '_');
102
+ let content = '';
103
+ // Try retrieve template content
104
+ try {
105
+ content = fs.readFileSync('./' + page + '.wiki', { encoding: 'utf8' });
106
+ }
107
+ catch {
108
+ // Return redlink if template doesn't exist
109
+ return `<a class="internal-link redlink" title="${title}" href="${page}">${title}</a>`;
110
+ }
111
+ // Remove non-template sections
112
+ content = content
113
+ .replace(/<noinclude>.*?<\/noinclude>/gs, '')
114
+ .replace(/.*<(includeonly|onlyinclude)>|<\/(includeonly|onlyinclude)>.*/gs, '');
115
+ // Substitute arguments
116
+ const argMatch = (arg) => (0, common_1.RegExpBuilder)(r `{{{ \s* ${arg} (?:\|([^}]*))? \s* }}}`);
117
+ const args = params.split('|');
118
+ // provided key=value template arguments
119
+ for (let i = 1; i < args.length; i++) {
120
+ const parts = args[i].split('=');
121
+ const [arg, val] = parts[1]
122
+ ? [parts[0], ...parts.slice(1)]
123
+ : [i.toString(), parts[0]];
124
+ content = content.replace(argMatch(arg), (_, defaultVal) => val || defaultVal || '');
125
+ }
126
+ return content;
127
+ })
128
+ // Unparsed arguments
129
+ .replace((0, common_1.RegExpBuilder)(r `{{{ \s* [^{}|]+? (?:\|([^}]*))? \s* }}}`), (_, _name, defaultVal) => {
130
+ return defaultVal !== null && defaultVal !== void 0 ? defaultVal : '';
131
+ })
132
+ // Images: [[File:Image.png|options|caption]]
133
+ .replace((0, common_1.RegExpBuilder)(r `\[\[ (?:File|Image): (.+?) (\|.+?)? \]\]`), (_, file, params = '') => {
134
+ if (/{{/.test(params))
135
+ return _;
136
+ const path = imagesFolder + '/' + file.trim().replace(/ /g, '_');
137
+ let caption = '';
138
+ let imageData = {};
139
+ let imageArgs = params.split('|').map((arg) => arg.replace(/"/g, '&quot;'));
140
+ for (const param of imageArgs) {
141
+ if (['left', 'right', 'center', 'none'].includes(param)) {
142
+ imageData.float = param;
143
+ }
144
+ if (['baseline', 'sub', 'super', 'top', 'text-bottom', 'middle', 'bottom', 'text-bottom'].includes(param)) {
145
+ imageData.align = param;
146
+ }
147
+ else if (['border', 'frameless', 'frame', 'framed', 'thumb', 'thumbnail'].includes(param)) {
148
+ imageData.type = { framed: 'frame', thumbnail: 'thumb' }[param] || param;
149
+ if (imageData.type === 'thumb') {
150
+ imageData.hasCaption = true;
151
+ }
152
+ }
153
+ else if (param.endsWith('px')) {
154
+ param.replace(/(?:(\w+)?(x))?(\w+)px/, (_, size1, auto, size2) => {
155
+ if (size1) {
156
+ Object.assign(imageData, { width: size1, height: size2 });
157
+ }
158
+ else if (auto) {
159
+ Object.assign(imageData, { width: 'auto', height: size2 });
160
+ }
161
+ else {
162
+ Object.assign(imageData, { width: size2, height: 'auto' });
163
+ }
164
+ return '';
165
+ });
166
+ }
167
+ else if (param.startsWith('upright=')) {
168
+ imageData.width = +param.replace('upright=', '') * 300;
169
+ }
170
+ else if (param.startsWith('link=')) {
171
+ imageData.link = param.replace('link=', '');
172
+ }
173
+ else if (param.startsWith('alt=')) {
174
+ imageData.alt = param.replace('alt=', '');
175
+ }
176
+ else if (param.startsWith('style=')) {
177
+ imageData.style = param.replace('style=', '');
178
+ }
179
+ else if (param.startsWith('class=')) {
180
+ imageData.class = param.replace('class=', '');
181
+ }
182
+ else {
183
+ caption = param;
184
+ }
185
+ }
186
+ let content = `
187
+ <figure
188
+ class="
189
+ ${imageData.class || ''}
190
+ image-container
191
+ image-${imageData.type || 'default'}
192
+ "
193
+ style="
194
+ float: ${imageData.float || 'none'};
195
+ vertical-align: ${imageData.align || 'unset'};
196
+ ${imageData.style || ''}
197
+ "
198
+ >
199
+ <img
200
+ src="${path}"
201
+ alt="${imageData.alt || file}"
202
+ width="${imageData.width || 300}"
203
+ height="${imageData.height || 300}"
204
+ >
205
+ ${imageData.hasCaption ? `<figcaption>${caption}</figcaption>` : ''}
206
+ </figure>
207
+ `;
208
+ if (imageData.link) {
209
+ content = `<a href="/${imageData.link}" title="${imageData.link}">${content}</a>`;
210
+ }
211
+ return content;
212
+ })
213
+ // Markup: '''bold''' and '''italic'''
214
+ .replace((0, common_1.RegExpBuilder)(r `''' ([^']+?) '''`), '<b>$1</b>')
215
+ .replace((0, common_1.RegExpBuilder)(r `'' ([^']+?) ''`), '<i>$1</i>')
216
+ // Headings: ==heading==
217
+ .replace((0, common_1.RegExpBuilder)(r `^ (=+) \s* (.+?) \s* \1 \s* $`), (_, lvl, txt) => `<h${lvl.length} id="${encodeURI(txt.replace(/ /g, '_'))}">${txt}</h${lvl.length}>`)
218
+ // Internal links: [[Page]] and [[Page|Text]]
219
+ .replace((0, common_1.RegExpBuilder)(r `\[\[ ([^\]|]+?) \]\]`), `<a class="internal-link" title="$1" href="$1">$1</a>`)
220
+ .replace((0, common_1.RegExpBuilder)(r `\[\[ ([^\]|]+?) \| ([^\]]+?) \]\]`), `<a class="internal-link" title="$1" href="/$1">$2</a>`)
221
+ .replace((0, common_1.RegExpBuilder)(r `(</a>)([a-z]+)`), '$2$1')
222
+ // External links: [href Page] and just [href]
223
+ .replace((0, common_1.RegExpBuilder)(r `\[ ((?:\w+:)?\/\/ [^\s\]]+) (\s [^\]]+?)? \]`), (_, href, txt) => `<a class="external-link" href="${href}">${txt || '[' + (++rawExtLinkCount) + ']'}</a>`)
224
+ // Bulleted list: *item
225
+ .replace((0, common_1.RegExpBuilder)(r `^ (\*+) (.+?) $`), (_, lvl, txt) => `${'<ul>'.repeat(lvl.length)}<li>${txt}</li>${'</ul>'.repeat(lvl.length)}`)
226
+ .replace((0, common_1.RegExpBuilder)(r `</ul> (\s*?) <ul>`), '$1')
227
+ // Numbered list: #item
228
+ .replace((0, common_1.RegExpBuilder)(r `^ (#+) (.+?) $`), (_, lvl, txt) => `${'<ol>'.repeat(lvl.length)}<li>${txt}</li>${'</ol>'.repeat(lvl.length)}`)
229
+ .replace((0, common_1.RegExpBuilder)(r `</ol> (\s*?) <ol>`), '$1')
230
+ // Definition list: ;head, :item
231
+ .replace((0, common_1.RegExpBuilder)(r `^ ; (.+) $`), '<dl><dt>$1</dt></dl>')
232
+ .replace((0, common_1.RegExpBuilder)(r `^ (:+) (.+?) $`), (_, lvl, txt) => `${'<dl>'.repeat(lvl.length)}<dd>${txt}</dd>${'</dl>'.repeat(lvl.length)}`)
233
+ .replace((0, common_1.RegExpBuilder)(r `</dl> (\s*?) <dl>`), '$1')
234
+ // Tables: {|, |+, !, |-, |, |}
235
+ .replace((0, common_1.RegExpBuilder)(r `^ \{\| (.*?) $`), (_, attrs) => `<table ${attrs}><tr>`)
236
+ .replace((0, common_1.RegExpBuilder)(r `^ ! ([^]+?) (?= \n^[!|] )`), (_, content) => `<th>${content}</th>`)
237
+ .replace((0, common_1.RegExpBuilder)(r `^ \|\+ (.*?) $`), (_, content) => `<caption>${content}</caption>`)
238
+ .replace((0, common_1.RegExpBuilder)(r `^ \|[^-+}] ([^]*?) (?= \n^[!|] )`), (_, content) => `<td>${content}</td>`)
239
+ .replace((0, common_1.RegExpBuilder)(r `^ \|- (.*?) $`), (_, attrs) => `</tr><tr ${attrs}>`)
240
+ .replace((0, common_1.RegExpBuilder)(r `^ \|\}`), `</tr></table>`)
241
+ // References: <ref></ref>, <references/>
242
+ .replace((0, common_1.RegExpBuilder)(r `<ref> (.+?) </ref>`), (_, text) => {
243
+ refs.push(text);
244
+ refCount++;
245
+ return `<sup><a id="cite-${refCount}" class="ref" href="#ref-${refCount}">[${refCount}]</a></sup>`;
246
+ })
247
+ .replace((0, common_1.RegExpBuilder)(r `<references \s* /?>`), '<ol>' + refs.map((ref, i) => `<li id="ref-${+i + 1}"> <a href="#cite-${+i + 1}">↑</a> ${ref} </li>`).join('\n') + '</ol>')
248
+ // Nonstandard: ``code`` and ```code blocks```
249
+ .replace((0, common_1.RegExpBuilder)(r ` \`\`\` ([^\`]+?) \`\`\` `), '<pre>$1</pre>')
250
+ .replace((0, common_1.RegExpBuilder)(r ` \`\` ([^\`]+?) \`\` `), '<code>$1</code>')
251
+ // Spacing
252
+ .replace(/(\r?\n){2}/g, '\n</p><p>\n')
253
+ // Restore nowiki contents
254
+ .replace(/%NOWIKI#(\d+)%/g, (_, n) => htmlEscape(nowikis[n]));
255
+ }
256
+ const result = { data: outText, metadata: metadata };
257
+ return result;
258
+ }
259
+ exports.parse = parse;
@@ -0,0 +1,2 @@
1
+ declare const _default: string;
2
+ export default _default;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = String.raw `
4
+ body {font-family: sans-serif; margin: 4em; max-width: 1000px; background: #eee;}
5
+ main {margin: 3em -1em; background: #fff; padding: 1em;}
6
+ h1, h2 {margin-bottom: 0.6em; font-weight: normal; border-bottom: 1px solid #a2a9b1;}
7
+ ul, ol {margin: 0.3em 0 0 1.6em; padding: 0;}
8
+ dt {font-weight: bold;}
9
+ dd, dl dl {margin-block: 0; margin-inline-start: 30px;}
10
+
11
+ figure {margin: 1em;}
12
+ .image-thumb, .image-frame {padding: 6px; border: 1px solid gray;}
13
+ figcaption {padding-top: 6px;}
14
+
15
+ table.wikitable {border-collapse: collapse;}
16
+ table.wikitable, table.wikitable th, table.wikitable td {border: 1px solid gray; padding: 6px;}
17
+ table.wikitable th {background-color: #eaecf0; text-align: center;}
18
+
19
+ a:not(:hover) {text-decoration: none;}
20
+ a.internal-link {color: #04a;}
21
+ a.internal-link:visited {color: #26d;}
22
+ a.external-link {color: #36b;}
23
+ a.external-link:visited {color: #58d;}
24
+ a.external-link::after {content: '\1f855';}
25
+ a.redlink {color: #d33;}
26
+ a.redlink:visited {color: #b44;}
27
+
28
+ #toc {display: inline-block; border: 1px solid #aab; padding: 8px; background-color: #f8f8f8; font-size: 95%;}
29
+ #toc-heading {display: block; text-align: center;}
30
+ #toc ol {margin: 0 0 0 1.3em;}
31
+ `;
package/license.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # ISC License
2
2
 
3
- Copyright &copy; 2021 Nixinova
3
+ Copyright &copy; 2021&ndash;2024 Nixinova
4
4
 
5
5
  Permission to use, copy, modify, and/or distribute this software for any
6
6
  purpose with or without fee is hereby granted, provided that the above