Skip to content

Commit

Permalink
1.1.0
Browse files Browse the repository at this point in the history
- Add contents, nowiki, magic words, and string functions
- Change #time to use length-based timecodes
- Fix some bugs
  • Loading branch information
Nixinova committed Mar 28, 2021
1 parent c5815d5 commit 564be68
Show file tree
Hide file tree
Showing 14 changed files with 202 additions and 78 deletions.
14 changes: 14 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Changelog

## 1.1.0
*2021-03-28*
- Added `parse` CLI command to implement `parse()`.
- Added support for a table of contents.
- Added support for `nowiki` tag.
- Added support for `onlyinclude`, `includeonly`, and `noinclude` tags in templates.
- Added support for magic words `__TOC__`, `__FORCETOC__`, `__NOTOC__`, and `__NOINDEX__`.
- Added support for control function `{{displaytitle:}}` to control the page's displayed title.
- Added support for string functions `lc:`, `uc:`, `lcfirst:`, `ucfirst:`, `replace:`, `explode:`, `sub:`, `len:`, `pos:`, `padleft:`, `padright:`, `urlencode:`, and `urldecode:`.
- Added support for horizontal rules using `----`.
- Changed time codes in `#datetime`/`#date`/`#time` function to be based on reduplication instead of unique characters with escaping based on quoting instead of prefixing with a backslash (i.e., `{{#time: j F Y (\n\o\w)}}` → `{{#time: dd mmm yyyy "(now)"}}`).
- Fixed inline tags removing whitespace from either end.
- Fixed single-line-only syntax not being parsed correctly.

## 1.0.1
*2021-03-28*
- Changed HTML output to be prettified.
Expand Down
25 changes: 19 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wikity",
"version": "1.0.1",
"version": "1.1.0",
"description": "Wikitext as a templating language, with built-in Eleventy support.",
"main": "src/index.js",
"scripts": {
Expand All @@ -21,6 +21,8 @@
"author": "Nixinova (https://nixinova.com)",
"license": "ISC",
"dependencies": {
"dateformat": "^4.5.1",
"escape-html": "^1.0.3",
"glob": "^7.1.6",
"html-formatter": "^0.1.9",
"novasheets": "1.0.0-pre3"
Expand Down
15 changes: 12 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Any wiki templates (called using `{{template name}}`) must be inside the `templa
| `##sub-numbered` | <ol><ol><li>sub-numbered</li></ol></ol> |
| `;term` | <dt>**term**</dt> |
| `:definition` | <dd>&emsp;definition</dd> |
| `<ref>Text</ref>` | <sup id=cite-1>[[1]](#ref-1)</sup> |
| `<references/>` | 1. <a id=ref-1>[](#cite-1)</a> Text |
| `[[internal link]]` | [internal link](#internal_link) |
| `[[link\|display text]]` | [display text](#link) |
| `[external-link]` | [[1]](#external-link) |
Expand All @@ -79,11 +81,18 @@ Any wiki templates (called using `{{template name}}`) must be inside the `templa
| `{{#var:varname}}` | text *(from memory)* |
| `{{#var:varname\|default val}}` | *(ditto but 'default val' if unset)* |
| `{{#switch:a\|a=1\|b=2\|c=3}}` | 1 |
| `{{#time:d-M-y\|2021-03-28}}` | 28-Mar-21 |
| `{{#time:dd/mm/yy\|2021-03-28}}` | 28/03/21 |
| `{{lc:TEXT}}` | text |
| `{{ucfirst:text}}` | Text |
| `<ref>Text</ref>` | <sup id=cite-1>[[1]](#ref-1)</sup> |
| `<references/>` | 1. <a id=ref-1>[](#cite-1)</a> Text |
| `{{len:12345}}` | 5 |
| `{{sub:string|2|4}}` | ring |
| `{{pos:text|x}}` | 2 |
| `{{padleft:text|5|_}}` | _text |
| `{{padright:msg|5|_}}` | msg__ |
| `{{replace:Message|e|3}}` | M3ssag3 |
| `{{explode:A-B-C-D|-|2}}` | C |
| `{{urlencode:t e x t}}` | t%20e%20x%20t |
| `{{urldecode:a%20b%27c}}` | a b'c |
| `<noinclude>No</noinclude>` | *(blank outside a template)* |
| `<onlyinclude>Yes</onlyinclude>` | Yes |
| `<includeonly>Yes</includeonly>` | Yes *(blank inside a template)* |
Expand Down
13 changes: 8 additions & 5 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import wikity from './index';

const VERSION = '1.0.0';
const VERSION = '1.1.0';

const indent = (n: number): string => ' '.repeat(n * 4);
const usage = (command: string, ...desc: string[]): void => {
Expand All @@ -16,14 +16,17 @@ if (!arg(1)) {
console.log('Type `wikity help` for a list of commands.');
}
else if (arg(1).includes('h')) {
console.log(`\n${indent(1)}Wikity commands:`);
usage(`wikity help`,
console.log(`\n${indent(1)}Wikity CLI commands:`);
usage(`wikity [--]h[elp]`,
`Display this help message.`,
);
usage(`wikity compile [<folder>]`,
usage(`wikity [--]c[ompile] [<folder>]`,
`Compile wikitext files from a given folder.`,
)
usage(`wikity version`,
usage(`wikity [--]p[arse] "<input>"`,
`Parse raw wikitext from the command line.`,
)
usage(`wikity [--]v[ersion]`,
`Display the current version of Wikity.`,
);
}
Expand Down
7 changes: 1 addition & 6 deletions src/compile.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
declare type Config = {
eleventy?: boolean;
defaultStyles?: boolean;
customStyles?: string;
};
import { Config } from './types';
export declare function compile(dir?: string, config?: Config): void;
export {};
49 changes: 37 additions & 12 deletions src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,24 @@ const formatter = require('html-formatter');
const novasheets = require('novasheets');

import { parse } from './parse';
import { Config, Result } from './types';

const OUT_FOLDER = 'wikity-out/';

type Config = {
eleventy?: boolean,
defaultStyles?: boolean,
customStyles?: string,
}

export function compile(dir: string = '.', config: Config = {}): void {
let stylesCreated = false;
// Write wikitext files
const files = glob.sync((dir || '.') + "/**/*.wiki", {});
files.forEach((file: string) => {
let data: string = fs.readFileSync(file, { encoding: 'utf8' });
let content: Result = parse(data);
let outText: string = content.toString();

let [, folder, filename]: string[] = file.match(/^(.+?[\/\\])((?:templates[\/\\])?[^\/\\]+)$/) as RegExpMatchArray;
let outFolder: string = (dir || folder || '.') + '/' + OUT_FOLDER;
let outFilename: string = filename.replace(/ /g, '_').replace('.wiki', '.html');
let url: string = outFilename.replace(/(?<=^|\/)\w/g, m => m.toUpperCase())
let displayTitle: string = url.replace('.html', '');

let outText = parse(data);
let displayTitle: string = content.metadata.displayTitle || url.replace('.html', '');

// Eleventy configuration
let frontMatter: string = '';
Expand All @@ -39,18 +34,42 @@ export function compile(dir: string = '.', config: Config = {}): void {
}

// Create HTML
let toc: string = '';
if (!content.metadata.notoc && (content.metadata.toc || (outText.match(/<h\d[^>]*>/g)?.length || 0) > 3)) {
let headings = Array.from(content.match(/<h\d[^>]*>.+?<\/h\d>/gs) || []);
headings.forEach(match => {
const text: string = match.replace(/\s*<\/?h\d[^>]*>\s*/g, '');
const lvl: number = +(match.match(/\d/g)?.[0] || -1);
toc += `${`<ol>`.repeat(lvl - 1)} <li> <a href="#${encodeURI(text.replace(/ /g, '_'))}">${text}</a> </li> ${`</ol>`.repeat(lvl - 1)}`;
});
toc = `
<div id="toc">
<span id="toc-heading">
<strong>Contents</strong>
[<a href="javascript:void(0)" onclick="
this.parentNode.parentNode.setAttribute('class', this.innerText === 'hide' ? 'toc-hidden' : '');
this.innerText = this.innerText === 'hide' ? 'show' : 'hide';
">hide</a>]
</span>
<ol>${toc}</ol>
</div>
`;
if (outText.includes('<toc></toc>')) outText = outText.replace('<toc></toc>', toc);
else outText = outText.replace(/<h\d[^>]*>/, toc + '$&');
}

let html = `
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, width=device-width">
<meta name="description" content="${data.substr(0, 256)}">
<title>${displayTitle}</title>
<link rel="stylesheet" href="/wiki.css">
<link id="default-styles" rel="stylesheet" href="/wiki.css">
</head>
<body>
<header>
<h1>${displayTitle}</h1>
<h1 id="page-title">${displayTitle}</h1>
</header>
<main>
<p>\n${outText}
Expand All @@ -68,7 +87,8 @@ export function compile(dir: string = '.', config: Config = {}): void {
fs.mkdirSync(outFolder);
fs.mkdirSync(outFolder + 'templates/');
}
fs.writeFileSync(outFolder + outFilename, frontMatter + formatter.render(html), 'utf8');
let renderedHtml = formatter.render(html).replace(/(<\/\w+>)(\S)/g, '$1 $2');
fs.writeFileSync(outFolder + outFilename, frontMatter + renderedHtml, 'utf8');

// Create site files
if (stylesCreated) return;
Expand All @@ -87,6 +107,11 @@ export function compile(dir: string = '.', config: Config = {}): void {
a.internal-link {color: #04a;} &:visited {color: #26d;}
a.external-link {color: #36b;} &:visited {color: #58d;} &::after {content: '\\1f855';}
a.redlink {color: #d33;} &:visited {color: #b44;}
#toc {display: inline-block; border: 1px solid #aab; padding: 8px; background-color: #f8f8f8; font-size: 95%;}
&-heading {display: block; text-align: center;}
& ol {margin: 0 0 0 1.3em;}
&.toc-hidden {height: 1em;} % ol {display: none;}
`.replace(/^\s+/gm, ''));
}
if (config.customStyles) styles += config.customStyles;
Expand Down
3 changes: 1 addition & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { compile } from './compile';
import { parse } from './parse';
declare const _default: {
compile: typeof compile;
parse: typeof parse;
parse: (data: string) => string;
};
export = _default;
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { parse } from './parse';

export = {
compile,
parse,
parse: (data: string): string => parse(data).toString(),
};
3 changes: 2 additions & 1 deletion src/parse.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export declare function parse(data: string): string;
import { Result } from './types';
export declare function parse(data: string): Result;
Loading

0 comments on commit 564be68

Please sign in to comment.