Add typo script
This commit is contained in:
parent
ca2764655a
commit
b5e2e32c9e
|
@ -0,0 +1 @@
|
|||
node_modules
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"name": "hugo-theme-lowtech",
|
||||
"version": "0.1.0",
|
||||
"author": "Simon <simon@lamelio.fr>",
|
||||
"license": "GPL-3.0",
|
||||
"devDependencies": {
|
||||
"richtypo": "4.0.7",
|
||||
"richtypo-rules-common": "4.0.2",
|
||||
"richtypo-rules-fr": "4.0.2"
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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);
|
||||
})
|
Loading…
Reference in New Issue