diff --git a/package.json b/package.json index 94ca28b..787280f 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "type": "git", "url": "git+https://github.com/biosfood/schrodinger.git" }, + "type": "module", "author": "Lukas Eisenhauer", "license": "GPL-3.0-or-later", "bugs": { diff --git a/package.json b/package.json index 94ca28b..787280f 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "type": "git", "url": "git+https://github.com/biosfood/schrodinger.git" }, + "type": "module", "author": "Lukas Eisenhauer", "license": "GPL-3.0-or-later", "bugs": { diff --git a/src/article/main.md b/src/article/main.md index e6c6d3c..bda6218 100644 --- a/src/article/main.md +++ b/src/article/main.md @@ -519,7 +519,7 @@ For big $t$, we get evenly spaced maxima in the wave function, corresponding to bright spots on a real-world experiment. There will also be minima at every %German -F"or gro"se $t$ ergeben sich gleichm"a"sig verteilte Maxima in der Wellenfunktion, die in realen Experimenten hellen +F"ur gro"se $t$ ergeben sich gleichm"a"sig verteilte Maxima in der Wellenfunktion, die in realen Experimenten hellen Punkten auf einem Schirm entsprechen. Auch gibt es Minima, jeweils bei %common diff --git a/package.json b/package.json index 94ca28b..787280f 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "type": "git", "url": "git+https://github.com/biosfood/schrodinger.git" }, + "type": "module", "author": "Lukas Eisenhauer", "license": "GPL-3.0-or-later", "bugs": { diff --git a/src/article/main.md b/src/article/main.md index e6c6d3c..bda6218 100644 --- a/src/article/main.md +++ b/src/article/main.md @@ -519,7 +519,7 @@ For big $t$, we get evenly spaced maxima in the wave function, corresponding to bright spots on a real-world experiment. There will also be minima at every %German -F"or gro"se $t$ ergeben sich gleichm"a"sig verteilte Maxima in der Wellenfunktion, die in realen Experimenten hellen +F"ur gro"se $t$ ergeben sich gleichm"a"sig verteilte Maxima in der Wellenfunktion, die in realen Experimenten hellen Punkten auf einem Schirm entsprechen. Auch gibt es Minima, jeweils bei %common diff --git a/src/build.js b/src/build.js index 6ace44c..45ef943 100644 --- a/src/build.js +++ b/src/build.js @@ -1,5 +1,6 @@ -const fs = require('fs'); -const { exec } = require("child_process"); +import { rmSync, existsSync, mkdirSync, readdirSync, readFileSync, writeFile } from 'fs' +import { exec } from "child_process" +import {processMarkdown} from './markdown.js' const buildFolder = "build" const articleFolder = "src/article" @@ -13,114 +14,29 @@ console.log("clearing the build folder..."); -fs.rmSync(`${buildFolder}`, { recursive: true, force: true }, (error) => {if (error) {console.error(error);}}); -if (!fs.existsSync(buildFolder)) { - fs.mkdirSync(buildFolder); +rmSync(`${buildFolder}`, { recursive: true, force: true }, (error) => {if (error) {console.error(error);}}); +if (!existsSync(buildFolder)) { + mkdirSync(buildFolder); } console.log("linking static files..."); exec(`ls src/static | while read in; do ln ../src/static/$in ${buildFolder} -s; done`) console.log("reading article files..."); -const articleEntries = fs.readdirSync(articleFolder).reduce((o, fileName) => ({ +const articleEntries = readdirSync(articleFolder).reduce((o, fileName) => ({ ...o, - [fileName.replace(".md", "")]: fs.readFileSync(`${articleFolder}/${fileName}`, 'utf8').split("\n") + [fileName.replace(".md", "")]: readFileSync(`${articleFolder}/${fileName}`, 'utf8').split("\n") }), {}); console.log("processing article entries..."); -const applyBigMathMode = line => line.replace(/\$\$/, bigMathMode ? "\\]" : "\\[") -const applyMathMode = line => line.replace(/\$/, mathMode ? "\\)" : "\\(") - -function processMathModes(line) { - var bigMathModeReplace = applyBigMathMode(line); - while (bigMathModeReplace != line) { - line = bigMathModeReplace; - bigMathMode = !bigMathMode; - bigMathModeReplace = applyBigMathMode(line); - } - - var mathModeReplace = applyMathMode(line) - while (mathModeReplace != line) { - line = mathModeReplace; - mathMode = !mathMode; - mathModeReplace = applyMathMode(line); - } - return line -} - -function processLinks(line) { - const links = line.match(/\[.*\]\(.*\)/) - if (!links) { - return line; - } - links.forEach(link => { - const target = link.match(/(?<=\().*(?=\))/)[0] - const name = link.match(/(?<=\[).*(?=\])/)[0] - line = line.replace(link, `${name}`) - }); - return line; -} - var result = Object.keys(articleEntries).reduce((o, name) => ({ ...o, - [name]: languages.reduce((o, language) => ({ - ...o, - [language]: "" - }), {}) + [name]: processMarkdown(articleEntries[name], languages) }), {}) -const validUmlaute = "aeouAEOU" - -function processUmlaute(line) { - const matches = line.match(/(?!\\)\"[aeouAEOU]/g) - if (!matches) { - return line - } - matches.forEach(umlaut => { - line = line.replaceAll(umlaut, `&${umlaut.substring(1)}uml;`) - }) - return line -} - -for (const [name, data] of Object.entries(articleEntries)) { - var mathMode = false - var bigMathMode = false - var language = "common" - data.forEach((line, index) => { - const noSpaces = line.replace(" ", "") - if (noSpaces == "") { - languages.forEach(language => { - result[name][language] += "

" - }) - return - } - if (noSpaces.startsWith("%")) { - language = noSpaces.substring(1) - return - } - for (var i = 1; i < 5; i++) { - const headerStart = `${"#".repeat(i)} ` - if (line.startsWith(headerStart)) { - line = `${line.substring(i+1)}` - } - } - line = line.replaceAll(/(?!\\)\"s/g, "ß") - line = processUmlaute(line) - line = processLinks(line) - line = processMathModes(line) - if (language == "common") { - languages.forEach(language => { - result[name][language] += `${line} ` - }) - } else { - result[name][language] += `${line} ` - } - }); -} - console.log("loading template..."); -const template = fs.readFileSync('src/template.html', 'utf8'); +const template = readFileSync('src/template.html', 'utf8'); function generateLanguageSwitch(name) { var result = "" @@ -133,7 +49,7 @@ console.log("writing articles..."); for (const [name, data] of Object.entries(result)) { for (const [language, text] of Object.entries(data)) { - fs.writeFile(`${buildFolder}/${name}_${language}.html`, + writeFile(`${buildFolder}/${name}_${language}.html`, template.replace("", text) .replace("", generateLanguageSwitch(name)), (error) => {}); diff --git a/package.json b/package.json index 94ca28b..787280f 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "type": "git", "url": "git+https://github.com/biosfood/schrodinger.git" }, + "type": "module", "author": "Lukas Eisenhauer", "license": "GPL-3.0-or-later", "bugs": { diff --git a/src/article/main.md b/src/article/main.md index e6c6d3c..bda6218 100644 --- a/src/article/main.md +++ b/src/article/main.md @@ -519,7 +519,7 @@ For big $t$, we get evenly spaced maxima in the wave function, corresponding to bright spots on a real-world experiment. There will also be minima at every %German -F"or gro"se $t$ ergeben sich gleichm"a"sig verteilte Maxima in der Wellenfunktion, die in realen Experimenten hellen +F"ur gro"se $t$ ergeben sich gleichm"a"sig verteilte Maxima in der Wellenfunktion, die in realen Experimenten hellen Punkten auf einem Schirm entsprechen. Auch gibt es Minima, jeweils bei %common diff --git a/src/build.js b/src/build.js index 6ace44c..45ef943 100644 --- a/src/build.js +++ b/src/build.js @@ -1,5 +1,6 @@ -const fs = require('fs'); -const { exec } = require("child_process"); +import { rmSync, existsSync, mkdirSync, readdirSync, readFileSync, writeFile } from 'fs' +import { exec } from "child_process" +import {processMarkdown} from './markdown.js' const buildFolder = "build" const articleFolder = "src/article" @@ -13,114 +14,29 @@ console.log("clearing the build folder..."); -fs.rmSync(`${buildFolder}`, { recursive: true, force: true }, (error) => {if (error) {console.error(error);}}); -if (!fs.existsSync(buildFolder)) { - fs.mkdirSync(buildFolder); +rmSync(`${buildFolder}`, { recursive: true, force: true }, (error) => {if (error) {console.error(error);}}); +if (!existsSync(buildFolder)) { + mkdirSync(buildFolder); } console.log("linking static files..."); exec(`ls src/static | while read in; do ln ../src/static/$in ${buildFolder} -s; done`) console.log("reading article files..."); -const articleEntries = fs.readdirSync(articleFolder).reduce((o, fileName) => ({ +const articleEntries = readdirSync(articleFolder).reduce((o, fileName) => ({ ...o, - [fileName.replace(".md", "")]: fs.readFileSync(`${articleFolder}/${fileName}`, 'utf8').split("\n") + [fileName.replace(".md", "")]: readFileSync(`${articleFolder}/${fileName}`, 'utf8').split("\n") }), {}); console.log("processing article entries..."); -const applyBigMathMode = line => line.replace(/\$\$/, bigMathMode ? "\\]" : "\\[") -const applyMathMode = line => line.replace(/\$/, mathMode ? "\\)" : "\\(") - -function processMathModes(line) { - var bigMathModeReplace = applyBigMathMode(line); - while (bigMathModeReplace != line) { - line = bigMathModeReplace; - bigMathMode = !bigMathMode; - bigMathModeReplace = applyBigMathMode(line); - } - - var mathModeReplace = applyMathMode(line) - while (mathModeReplace != line) { - line = mathModeReplace; - mathMode = !mathMode; - mathModeReplace = applyMathMode(line); - } - return line -} - -function processLinks(line) { - const links = line.match(/\[.*\]\(.*\)/) - if (!links) { - return line; - } - links.forEach(link => { - const target = link.match(/(?<=\().*(?=\))/)[0] - const name = link.match(/(?<=\[).*(?=\])/)[0] - line = line.replace(link, `${name}`) - }); - return line; -} - var result = Object.keys(articleEntries).reduce((o, name) => ({ ...o, - [name]: languages.reduce((o, language) => ({ - ...o, - [language]: "" - }), {}) + [name]: processMarkdown(articleEntries[name], languages) }), {}) -const validUmlaute = "aeouAEOU" - -function processUmlaute(line) { - const matches = line.match(/(?!\\)\"[aeouAEOU]/g) - if (!matches) { - return line - } - matches.forEach(umlaut => { - line = line.replaceAll(umlaut, `&${umlaut.substring(1)}uml;`) - }) - return line -} - -for (const [name, data] of Object.entries(articleEntries)) { - var mathMode = false - var bigMathMode = false - var language = "common" - data.forEach((line, index) => { - const noSpaces = line.replace(" ", "") - if (noSpaces == "") { - languages.forEach(language => { - result[name][language] += "

" - }) - return - } - if (noSpaces.startsWith("%")) { - language = noSpaces.substring(1) - return - } - for (var i = 1; i < 5; i++) { - const headerStart = `${"#".repeat(i)} ` - if (line.startsWith(headerStart)) { - line = `${line.substring(i+1)}` - } - } - line = line.replaceAll(/(?!\\)\"s/g, "ß") - line = processUmlaute(line) - line = processLinks(line) - line = processMathModes(line) - if (language == "common") { - languages.forEach(language => { - result[name][language] += `${line} ` - }) - } else { - result[name][language] += `${line} ` - } - }); -} - console.log("loading template..."); -const template = fs.readFileSync('src/template.html', 'utf8'); +const template = readFileSync('src/template.html', 'utf8'); function generateLanguageSwitch(name) { var result = "" @@ -133,7 +49,7 @@ console.log("writing articles..."); for (const [name, data] of Object.entries(result)) { for (const [language, text] of Object.entries(data)) { - fs.writeFile(`${buildFolder}/${name}_${language}.html`, + writeFile(`${buildFolder}/${name}_${language}.html`, template.replace("", text) .replace("", generateLanguageSwitch(name)), (error) => {}); diff --git a/src/markdown.js b/src/markdown.js new file mode 100644 index 0000000..51d2b69 --- /dev/null +++ b/src/markdown.js @@ -0,0 +1,92 @@ +const validUmlaute = "aeouAEOU" +const doReplace = (line, toggle) => line.replace(toggle.regex, toggle.on ? toggle.close : toggle.open) +const makeToggles = () => +{return [ + { + on: false, + open: '\\[', + close: '\\]', + regex: /\$\$/ + }, + { + on: false, + open: '\\(', + close: '\\)', + regex: /\$/ + } +]} + +function processLinks(line) { + const links = line.match(/\[.*\]\(.*\)/) + if (!links) { + return line; + } + links.forEach(link => { + const target = link.match(/(?<=\().*(?=\))/)[0] + const name = link.match(/(?<=\[).*(?=\])/)[0] + line = line.replace(link, `${name}`) + }); + return line; +} + +function processUmlaute(line) { + const matches = line.match(/(?!\\)\"[aeouAEOU]/g) + if (!matches) { + return line + } + matches.forEach(umlaut => { + line = line.replaceAll(umlaut, `&${umlaut.substring(1)}uml;`) + }) + return line +} + +function processToggles(line, toggles) { + for (const toggle of toggles) { + var newLine = doReplace(line, toggle) + while (newLine != line) { + line = newLine + toggle.on = !toggle.on + newLine = doReplace(line, toggle) + } + } + return line +} + +export function processMarkdown(data, languages) { + var result = languages.reduce((o, language) => ({ + ...o, + [language]: "" + }), {}) + var toggles = makeToggles() + var language = "common" + data.forEach((line, index) => { + const noSpaces = line.replace(" ", "") + if (noSpaces == "") { + languages.forEach(language => { + result[language] += "

" + }) + return + } + if (noSpaces.startsWith("%")) { + language = noSpaces.substring(1) + return + } + for (var i = 1; i < 5; i++) { + const headerStart = `${"#".repeat(i)} ` + if (line.startsWith(headerStart)) { + line = `${line.substring(i+1)}` + } + } + line = line.replaceAll(/(?!\\)\"s/g, "ß") + line = processToggles(line, toggles) + line = processUmlaute(line) + if (language == "common") { + languages.forEach(language => { + result[language] += `${line} ` + }) + } else { + result[language] += `${line} ` + } + }); + return result +}