Add typo script
This commit is contained in:
98
scripts/page.js
Normal file
98
scripts/page.js
Normal file
@ -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
|
||||
}
|
33
scripts/typo.js
Normal file
33
scripts/typo.js
Normal file
@ -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);
|
||||
})
|
Reference in New Issue
Block a user