|
| 1 | +// cleanup.js |
| 2 | +/* |
| 3 | +The MIT License (MIT) |
| 4 | +
|
| 5 | +Copyright (c) 2015 Eric Elliott |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +of this software and associated documentation files (the "Software"), to deal |
| 9 | +in the Software without restriction, including without limitation the rights |
| 10 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +copies of the Software, and to permit persons to whom the Software is |
| 12 | +furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be included in all |
| 15 | +copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +SOFTWARE. |
| 24 | +
|
| 25 | +Original author of this script: [email protected] |
| 26 | +*/ |
| 27 | + |
| 28 | + |
| 29 | +var fs = require("fs"); |
| 30 | +var doclistPath = '/Users/sperberx/dev/essential-javascript-links/'; |
| 31 | +var doclistName = 'README'; |
| 32 | +var doclistAddon = '-new'; |
| 33 | +var doclistExtension = '.md'; |
| 34 | + |
| 35 | +/* |
| 36 | +files I'm testing with |
| 37 | +/Users/sperberx/dev/essential-javascript-links/misc/some-wrong-apos.md |
| 38 | +/Users/sperberx/dev/essential-javascript-links/README.md |
| 39 | +/Users/sperberx/dev/essential-javascript-links/misc/README-ee.md |
| 40 | +/Users/sperberx/dev/essential-javascript-links/misc/README-short1.md |
| 41 | +*/ |
| 42 | + |
| 43 | + |
| 44 | +// identify each type of change, particularly for the apostrophe, since global change (e.g., /.'./g or /(\w|\d)'(\w|\d)/g) is just too risky |
| 45 | +// By making only known changes, stray apostrophes and quotes can be located easily |
| 46 | +var aposD = /'d\b/g; // I'd |
| 47 | +var aposLl = /'ll\b/g; // you'll |
| 48 | +var aposM = /'m\b/g; // I'm |
| 49 | +var aposRe = /'re\b/g; // you're |
| 50 | +var aposS = /'s\b/g; // it's |
| 51 | +var aposT = /'t\b/g; // don't |
| 52 | +var aposVe = /'ve\b/g; // I've |
| 53 | +var oAposR = /O'R/g; // O'Reilly |
| 54 | +var spaceQuot = / "\b/g; // open quote (eg, precedes a 'word boundary') |
| 55 | +var quotSpace = /\b"/g; // close quote (eg, is preceded by a 'word boundary') |
| 56 | +var spaceDashSpace = / - /g; |
| 57 | +// not yet defined: single quotes within double quotes |
| 58 | +// var spaceApos = / '/ |
| 59 | +// var aposSpace = /' / |
| 60 | + |
| 61 | +// identify the replacements for each change above |
| 62 | +var rsqD = "’d"; |
| 63 | +var rsqLl = "’ll"; |
| 64 | +var rsqM = "’m"; |
| 65 | +var rsqRe = "’re"; |
| 66 | +var rsqS = "’s"; |
| 67 | +var rsqT = "’t"; |
| 68 | +var rsqVe = "’ve"; |
| 69 | +var OrsqR = "O’R"; |
| 70 | +var spaceLdq = ' “'; |
| 71 | +var rdqSpace = '”'; |
| 72 | +var spaceEmDashSpace = " — "; |
| 73 | + |
| 74 | + |
| 75 | +/* store components of path */ |
| 76 | +var pathAndFile = doclistPath + doclistName + doclistExtension; // /Users/sperberx/dev/essential-javascript-links/README.md |
| 77 | +var pathAndFileNew = doclistPath + doclistName+ doclistAddon + doclistExtension; // /Users/sperberx/dev/essential-javascript-links/README-new.md |
| 78 | + |
| 79 | +var aFile = fs.readFile(pathAndFile, 'utf8', function (err,data) { |
| 80 | + if (err) { |
| 81 | + return console.log(err); |
| 82 | + } |
| 83 | + |
| 84 | + // This seems really awkward to me, but it's working |
| 85 | + function cleanUp(someFile) { |
| 86 | + var removes = [aposD, aposLl, aposM, aposRe, aposS, aposT, aposVe, oAposR, spaceQuot, quotSpace, spaceDashSpace]; |
| 87 | + var replaceWiths = [rsqD, rsqLl, rsqM, rsqRe, rsqS, rsqT, rsqVe, OrsqR, spaceLdq, rdqSpace, spaceEmDashSpace]; |
| 88 | + |
| 89 | + for (var counter = 0; counter < removes.length; counter++) { |
| 90 | + someFile = someFile.replace(removes[counter], replaceWiths[counter]); |
| 91 | + } |
| 92 | + return someFile; |
| 93 | + } |
| 94 | + var result = cleanUp(data); |
| 95 | + |
| 96 | +// console.log(result); |
| 97 | + console.log('got result back'); |
| 98 | + |
| 99 | + fs.writeFile(pathAndFileNew, result, function (err) { |
| 100 | + if (err) throw err; |
| 101 | + console.log('It\'s saved!'); |
| 102 | + }); |
| 103 | + |
| 104 | +}); |
| 105 | + |
0 commit comments