From 619eb5e840119cf1ea9b9b691bc5210859b3f085 Mon Sep 17 00:00:00 2001 From: gumi Date: Mon, 8 Jun 2020 22:59:03 +0000 Subject: use a YAML Front Matter instead of a separate YAML file this will prevent forgetting to update the policy metadata since now they are in the same file --- README.md | 37 ++++++++++++++++- policies/constitution.md | 6 +++ policies/forum-rules.md | 5 +++ policies/game-rules.md | 6 +++ policies/list.yml | 31 -------------- policies/privacy-policy.md | 8 ++++ policies/social-convention.md | 6 +++ src/build.ts | 97 +++++++++++++++++++++++++++---------------- 8 files changed, 127 insertions(+), 69 deletions(-) delete mode 100644 policies/list.yml diff --git a/README.md b/README.md index c864528..c2a600f 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,43 @@ # The Mana World Policies Contains the legal documents and policies observed by The Mana World. -## Dependencies -- [Deno](https://deno.land) 1.x (can be installed with `make deno`) +## Policy files +Policy files are are located in the [policies](policies) directory. +Currently, only Markdown is supported for policy files. + +### Front Matter +The Front Matter is a YAML document that defines the properties of the policy. + +#### Required properties +- name: the full name of the policy +- description: a short summary of the policy + +#### Optional properties +- aliases: an array of path aliases (redirects) +- ignore: prevents the policy file from being built + +### Example +```md +--- +name: Policy Name +description: A short sumary +aliases: [foo, bar] +--- + +# title +content +``` + +
+ +--- ## Generating the static site + +### Dependencies +- GNU Make (pre-installed in most linux distros) +- [Deno](https://deno.land) 1.x (can be installed with `make deno`) + ```sh make build ``` diff --git a/policies/constitution.md b/policies/constitution.md index 373a21d..17beba7 100644 --- a/policies/constitution.md +++ b/policies/constitution.md @@ -1,3 +1,9 @@ +--- +name: The Mana World Team (TMWT) +description: Constitution of The Mana World +aliases: [TMWC, TMWT] +--- + # The Mana World Team (TMWT) ## Abstract diff --git a/policies/forum-rules.md b/policies/forum-rules.md index 607c6ae..6c692e4 100644 --- a/policies/forum-rules.md +++ b/policies/forum-rules.md @@ -1,3 +1,8 @@ +--- +name: Forum Rules +description: Rules observed on the forums +--- + # Forum Rules ## Do not use offensive/rude language diff --git a/policies/game-rules.md b/policies/game-rules.md index 1da4c01..a36868f 100644 --- a/policies/game-rules.md +++ b/policies/game-rules.md @@ -1,3 +1,9 @@ +--- +name: Game Rules +description: Rules observed in-game +aliases: [rules] +--- + # Game Rules ## Do not AFK bot diff --git a/policies/list.yml b/policies/list.yml deleted file mode 100644 index 69b6b30..0000000 --- a/policies/list.yml +++ /dev/null @@ -1,31 +0,0 @@ -# List of policies - -constitution: - name: The Mana World Team (TMWT) - description: Constitution of The Mana World - aliases: - - TMWC - - TMWT - -game-rules: - name: Game Rules - description: Rules observed in-game - aliases: - - rules - -forum-rules: - name: Forum Rules - description: Rules observed on the forums - -social-convention: - name: TMW Social Convention (TSC) - description: Guidelines for social interaction - aliases: - - TSC - - ESC - -.privacy-policy: - name: Privacy Policy - description: How we handle your data - aliases: - - privacy diff --git a/policies/privacy-policy.md b/policies/privacy-policy.md index 91b1e04..a7d7364 100644 --- a/policies/privacy-policy.md +++ b/policies/privacy-policy.md @@ -1 +1,9 @@ +--- +name: Privacy Policy +description: How we handle your data +aliases: [privacy] + +ignore: yes # not ready for publication yet +--- + TO-DO diff --git a/policies/social-convention.md b/policies/social-convention.md index c1d79f6..3fd5cb9 100644 --- a/policies/social-convention.md +++ b/policies/social-convention.md @@ -1,3 +1,9 @@ +--- +name: TMW Social Convention (TSC) +description: Guidelines for social interaction +aliases: [TSC, ESC] +--- + # TMW Social Convention (TSC) This page lists a minimum set of guidelines for social interaction on The Mana World and other MMOs. These guidelines can be used by any MMO game. diff --git a/src/build.ts b/src/build.ts index 9328c7d..94022dc 100644 --- a/src/build.ts +++ b/src/build.ts @@ -1,20 +1,16 @@ import { Marked } from "https://deno.land/x/markdown/mod.ts" import * as yaml from "https://deno.land/x/js_yaml_port/js-yaml.js" -// the structure of policies/list.yml -interface policyList { - [file: string]: { - name: string; - description: string; - aliases?: string[]; - }; +// the structure of the front matter +interface PolicyYFM { + name: string + description: string; + aliases?: string[]; + ignore?: boolean; } const decoder = new TextDecoder("utf-8"); const encoder = new TextEncoder(); -const policyFile = "policies/list.yml"; -const rawPolicies = decoder.decode(await Deno.readFile(policyFile)); -const policies: policyList = yaml.load(rawPolicies); const index = { prefix: ` @@ -44,51 +40,80 @@ console.info(">> Building the static site..."); // empty the build directory await Deno.remove("build", { recursive: true }); -// loop through every policy markdown file -for (const [file, props] of Object.entries(policies)) { - if (file.startsWith(".")) { - console.log(`Ignoring disabled policy file: ${file}.md`); +// loop through policy files +for await (const dirEntry of Deno.readDir("policies")) { + const file = dirEntry.name; + const rawPolicy = decoder.decode(await Deno.readFile(`policies/${file}`)); + + if (!rawPolicy.trimStart().startsWith("---")) { + console.log(`Ignoring policy file with no front matter: ${file}`); + continue; + } + + // find the closing tag + const endFrontMatter = rawPolicy.slice(3).indexOf("---"); + + if (endFrontMatter === -1) { + throw new SyntaxError(`Couldn't find end of front matter in ${file}`); + } + + // mark the boundaries + const rawYAML = rawPolicy.slice(3, endFrontMatter + 3).trim(); + const rawBody = rawPolicy.slice(3 + endFrontMatter + 3).trim(); + + const [shortName, type] = file.split("."); + + if (type.toLowerCase() !== "md") { + console.log(`Unsupported policy file format: ${file}`); + continue; + } + + // parse the front matter as yaml + const YFM: PolicyYFM = yaml.safeLoad(rawYAML, {filename: file}); + + if (Reflect.has(YFM, "ignore") || file.startsWith(".")) { + console.log(`Ignoring disabled policy file: ${file}`); continue; } // add to the index page - index.list += `
  • ${props.name}
  • \n`; + index.list += `
  • ${YFM.name}
  • \n`; // add to the netlify redirect file - redirects += `/${file} /${file}/index.html 200!\n`; - + redirects += `/${shortName} /${shortName}/index.html 200!\n`; + // built-in aliases - if (file != file.replace("-", "_")) { - redirects += `/${file.replace("-", "_")} /${file} 302\n`; - } if (file != file.replace("-", "")) { - redirects += `/${file.replace("-", "")} /${file} 302\n`; + if (shortName != shortName.replace("-", "_")) { + redirects += `/${shortName.replace("-", "_")} /${shortName} 302\n`; + } if (shortName != shortName.replace("-", "")) { + redirects += `/${shortName.replace("-", "")} /${shortName} 302\n`; } - + // process path aliases - if (Reflect.has(props, "aliases")) { - for (const alias of props.aliases as string[]) { - redirects += `/${alias} /${file} 302\n`; + if (Reflect.has(YFM, "aliases")) { + for (const alias of YFM.aliases as string[]) { + redirects += `/${alias} /${shortName} 302\n`; if (alias != alias.toLowerCase()) { - redirects += `/${alias.toLocaleLowerCase()} /${file} 302\n`; + redirects += `/${alias.toLocaleLowerCase()} /${shortName} 302\n`; } } } // convert from markdown to html - const markdown = decoder.decode(await Deno.readFile(`policies/${file}.md`)); - const html: string = Marked.parse(markdown); - // wrap the generated html with our template + const html: string = Marked.parse(rawBody); + + // wrap the generated html inside our template const policyPage = encoder.encode(` - The Mana World – ${props.name} - + The Mana World – ${YFM.name} + - +
    @@ -100,10 +125,10 @@ ${html} `.trim()); // create a subdirectory for it - await Deno.mkdir(`build/${file}`, {recursive: true}); - await Deno.writeFile(`build/${file}/index.html`, policyPage, {create: true}); - await Deno.mkdir(`build/${file}/raw`, {recursive: true}); - await Deno.writeFile(`build/${file}/raw/index.html`, encoder.encode(html), {create: true}); + await Deno.mkdir(`build/${shortName}`, {recursive: true}); + await Deno.writeFile(`build/${shortName}/index.html`, policyPage, {create: true}); + await Deno.mkdir(`build/${shortName}/raw`, {recursive: true}); + await Deno.writeFile(`build/${shortName}/raw/index.html`, encoder.encode(html), {create: true}); } // write the index page -- cgit v1.2.3-60-g2f50