forked from practicalplants/archive.org-recovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.js
85 lines (76 loc) · 3.74 KB
/
update.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const { program } = require('commander');
const path = require('path');
const fs = require("fs-extra");
const readdirp = require("readdirp");
program
.version("0.2.0")
.arguments("<dir>");
program.parse(process.argv);
main(program);
const swaps = [
// [/\s*\<div class="thanks-to" id="thanks-cernunnos".*?Cernunnos.\<\/p\>\s*\<\/div\>/ms,''],
// [/\s*\<ul class="tabs"\>\s*\<li class="active"><a href="\/wiki">Plants Wiki<\/a><\/li>.*? accesskey="o">Log in<\/a><\/li>\s*<\/ul>\s*<\/div>\s*<\/div>/ms, ''],
// [/\s*?<ul class="nav">\s*<li><a href="\/wiki\/">.*?<\/ul>\s*<ul class="nav pull-right">\s*<li id="search-nav">.*?<\/ul>/ms, ''],
// [/<div id="article-state" class="article-state-box">.*?<\/div>/ms, ''],
// [/<\/title>/ms, `</title>
// <link rel="stylesheet" href="../_resources/fonts/crete-round/stylesheet.css" media="screen" />
// <link rel="stylesheet" href="../_resources/css/global.css" media="screen" />
// <link rel="stylesheet" href="../_resources/css/masthead.css" media="screen" />
// <link rel="stylesheet" href="../_resources/css/inline-1.css" media="screen" />
// <link rel="stylesheet" href="../_resources/css/load-1.css" media="screen" />
// `],̦̦
// [/<article id="main-entry" class="wiki-entry">/ms, `<article id="main-entry" class="wiki-entry">
// <div id="article-state" class="article-state-box">
// <p>This is an archived copy of this article, recovered after a server failure in January 2022.</p>
// <p>Some links may be broken, and editing is disabled. We are working to bring back full functionality.</p>
// </div>
// `
// ],
//<script src="http://practicalplants.org/w/load.php?debug=false&lang=en&modules=startup&only=scripts&skin=practicalplants&*"></script>
// [/<link rel="EditURI" .*? \/>/ms, ''],
// [/<link rel="ExportRDF".*?\/>/ms, ''],
// [/<link rel="search" type="application\/opensearchdescription.*?\/>/, ''],
// [/\s*<script src="https?:\/\/practicalplants.org.*?<\/script>/msg, ''],
// [/<link rel="alternate" type="application\/atom.*?name="ResourceLoaderDynamicStyles" content="" \/>/ms, ''],
// [/<link rel="stylesheet" href="https?:\/\/practicalplants.org.*?\s\/>/msg, ''],
// [/<script>.*?<\/script>/msg, ''],
// [/<div id="page-buttons">.*?<\/div>\s*<div id="toc-container">/msi, `<div id="toc-container">`],
// [/<\/body><\/html>/ms, `
// <script type="text/javascript" src="../_resources/old-skin/resources/js/libs/jquery-1.8.2.min.js"></script>
// <script type="text/javascript" src="../_resources/old-skin/resources/bootstrap/js/bootstrap.min.js"></script>
// <script type="text/javascript" src="../_resources/old-skin/resources/js/jquery.qtip-1.0.0-rc3.min.js"></script>
// <script type="text/javascript" src="../_resources/old-skin/resources/js/practicalplants.init.article.js"></script>
// </body></html>`],
[/<!-- Piwik -->.*?-->/msi, '']
]
async function main () {
const dirPath = path.resolve(path.normalize(program.args[0]));
const files = [];
for await (const entry of readdirp(dirPath, {fileFilter: '*.html', directoryFilter: ['!.git'], depth: 2, type: "files"})) {
const {path, fullPath, basename} = entry;
const htmlOrig = await fs.readFile(fullPath, "utf8");
let html = htmlOrig
swaps.forEach(swap => {
html = html.replace(swap[0], swap[1])
});
if (html === htmlOrig) {
continue;
}
await fs.writeFile(
fullPath,
html
).then(res => {
console.log(`Wrote to ${fullPath}`)
}).catch(()=>{
console.log(`Error writing to ${fullPath}`)
})
// swaps.forEach(swap => {
// const match = htmlOrig.match(swap[0]);
// if (match) {
// console.log('matched', match.index)
// }else {
// console.log('nomatch')
// }
// })
}
}