From b5e2e32c9eca1b1d7d13f2f9647cfead6b1892af Mon Sep 17 00:00:00 2001 From: Simon C Date: Thu, 27 Feb 2020 11:19:02 +0100 Subject: [PATCH] Add typo script --- .gitignore | 1 + package-lock.json | 38 ++++++++++++++++++ package.json | 11 ++++++ scripts/page.js | 98 +++++++++++++++++++++++++++++++++++++++++++++++ scripts/typo.js | 33 ++++++++++++++++ 5 files changed, 181 insertions(+) create mode 100644 .gitignore create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 scripts/page.js create mode 100644 scripts/typo.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..ed3b2e1 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,38 @@ +{ + "name": "hugo-theme-lowtech", + "version": "0.1.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@arr/flatten": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@arr/flatten/-/flatten-1.0.1.tgz", + "integrity": "sha512-elW/c6n0C03bshKz7QEy658r9ls6nKlVDBgl2nlA4a/T1mymvbAmU/leHvL27fhT6fz7RS2XiUwnvvcosdL63w==", + "dev": true + }, + "richtypo": { + "version": "4.0.7", + "resolved": "https://registry.npmjs.org/richtypo/-/richtypo-4.0.7.tgz", + "integrity": "sha512-4+qT1jj3sa114AlbbWa9kudXY6o5a2pyRoeWr3qexz8mbkhJVtrh+RHTIG3IzDqDCO9Fgr+G7VLRqWNhoJVabg==", + "dev": true, + "requires": { + "@arr/flatten": "^1.0.0" + } + }, + "richtypo-rules-common": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/richtypo-rules-common/-/richtypo-rules-common-4.0.2.tgz", + "integrity": "sha512-XsbQZdv89ednhkJQuzf6oXVJDlJY8Hb+MdCilXRrGPPzqlr9FpMQ8Ynu8F41cBIbn5EUYAIojbFfaqmDbW4Kgg==", + "dev": true + }, + "richtypo-rules-fr": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/richtypo-rules-fr/-/richtypo-rules-fr-4.0.2.tgz", + "integrity": "sha512-41pEKuJ1DEtsLygrjac75GeoJKAw9QYo0zvzbOTwvY3gRkyzgjCuE/zUTugCzcEAnsaMHPjj0+xNwr31NwsNRw==", + "dev": true, + "requires": { + "richtypo-rules-common": "^4.0.2" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..872c78a --- /dev/null +++ b/package.json @@ -0,0 +1,11 @@ +{ + "name": "hugo-theme-lowtech", + "version": "0.1.0", + "author": "Simon ", + "license": "GPL-3.0", + "devDependencies": { + "richtypo": "4.0.7", + "richtypo-rules-common": "4.0.2", + "richtypo-rules-fr": "4.0.2" + } +} diff --git a/scripts/page.js b/scripts/page.js new file mode 100644 index 0000000..676a9cf --- /dev/null +++ b/scripts/page.js @@ -0,0 +1,98 @@ +'use strict'; + +var fs = require('fs') +var path = require('path') + +/** + * Read directory recursively + * + * @param {String} directory path + * @param {Function} callback + * @return {Arrays} dirs & files + * @api public + */ + +const readdirr = function (dpath, cb) { + var dirs = [], files = [] + dirs.push(dpath) + ;(function loop (index) { + if (index == dirs.length) return cb(null, dirs, files) + fs.readdir(dirs[index], function (err, _files) { + if (err) return cb(err) + getstat(dirs[index], _files, function (err) { + if (err) return cb(err) + loop(++index) + }) + }) + }(0)) + + /** + * Get stat + * + * @param {String} directory path + * @param {Array} files + * @param {Function} callback + * @api private + */ + function getstat (dpath, _files, cb) { + ;(function loop (index) { + if (index == _files.length) return cb() + var fpath = path.join(dpath, _files[index]) + fs.stat(fpath, function (err, stats) { + if (err) return cb(err) + if (stats.isDirectory()) { + dirs.push(fpath) + } else { + files.push(fpath) + } + loop(++index) + }) + }(0)) + } +} + +const getHtmlFiles = function (folder) { + return new Promise((resolve) => { + readdirr(folder, function (err, dirs, files) { + const filesMatch = [] + for (const filePath of files) { + if (path.extname(filePath) === '.html') { + filesMatch.push(filePath) + } + } + resolve(filesMatch) + }) + }) +} + +const loadPages = function (folder) { + return new Promise((resolve, reject) => { + getHtmlFiles(folder).then(function (filesPath) { + const files = [] + for (const filePath of filesPath) { + files.push({ + path: filePath, + content: fs.readFileSync(filePath, { encoding: 'utf8' }) + }) + } + resolve(files) + }, function (error) { + reject(error) + }); + }) +} + +const savePage = function (page) { + fs.writeFileSync(page.path, page.content, { encoding: 'utf8' }) +} + +const savePages = function (pages) { + for (const page of pages) { + savePage(page) + } +} + +module.exports = { + loadPages: loadPages, + savePages: savePages +} diff --git a/scripts/typo.js b/scripts/typo.js new file mode 100644 index 0000000..6c55b00 --- /dev/null +++ b/scripts/typo.js @@ -0,0 +1,33 @@ +'use strict'; + +const path = require('path'); +const { loadPages, savePages } = require('./page'); +const richtypo = require('richtypo'); +const frRules = require('richtypo-rules-fr'); +const { definitions } = require('richtypo-rules-common'); + +console.log('Prepares your texts to publication on web: applies typography rules.'); + +const openingQuote = '« '; +const closingQuote = ' »'; +const quotes = text => text + .replace( + new RegExp(`${definitions.notInTag}(["“«]|“)((${definitions.tag})?(${definitions.dash}${definitions.space})?${definitions.letter})`, 'gmi'), + `${openingQuote}$2` + ) + .replace( + new RegExp(`${definitions.notInTag}(["”»]|”)`, 'gmi'), + `${closingQuote}` + ); + +const rt = richtypo.default([frRules.default, quotes]); + +const folder = path.resolve(process.cwd(), 'public') + +loadPages(folder).then(pages => { + for (const page of pages) { + page.content = rt(page.content) + } + + savePages(pages); +})