From eac47845c1a0fcbdbb6ee531043b449414d6760b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Sat, 7 Apr 2018 17:38:13 +0200 Subject: [PATCH] add archived-page static files --- scripts/web.js | 58 ++++++++++++++++ static-parts/archived-page-end.html | 7 ++ static-parts/archived-page-start.html | 98 +++++++++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 static-parts/archived-page-end.html create mode 100644 static-parts/archived-page-start.html diff --git a/scripts/web.js b/scripts/web.js index 0115e206c..384212d9d 100644 --- a/scripts/web.js +++ b/scripts/web.js @@ -48,10 +48,12 @@ sass.render( ); // Set variables for paths const snippetsPath = './snippets', + archivedSnippetsPath = './snippets_archive', staticPartsPath = './static-parts', docsPath = './docs'; // Set variables for script let snippets = {}, + archivedSnippets = {}, beginnerSnippetNames = ['everyNth', 'filterNonUnique', 'last', 'maxN', 'minN', 'nthElement', 'sample', 'similarity', 'tail', 'currentURL', 'hasClass', 'getMeridiemSuffixOfInteger', 'factorial', 'fibonacci', 'gcd', 'isDivisible', 'isEven', 'isPrime', 'lcm', 'randomIntegerInRange', 'sum', 'reverseString', 'truncateString'], startPart = '', endPart = '', @@ -59,6 +61,10 @@ let snippets = {}, beginnerStartPart = '', beginnerEndPart = '', beginnerOutput = '', + archivedStartPart = '', + archivedEndPart = '', + archivedOutput = '', + indexStaticFile = '', pagesOutput = []; tagDbData = {}; @@ -66,6 +72,8 @@ let snippets = {}, console.time('Webber'); // Synchronously read all snippets and sort them as necessary (case-insensitive) snippets = util.readSnippets(snippetsPath); +archivedSnippets = util.readSnippets(archivedSnippetsPath); + // Load static parts for all pages try { @@ -75,6 +83,9 @@ try { beginnerStartPart = fs.readFileSync(path.join(staticPartsPath, 'beginner-page-start.html'), 'utf8'); beginnerEndPart = fs.readFileSync(path.join(staticPartsPath, 'beginner-page-end.html'), 'utf8'); + archivedStartPart = fs.readFileSync(path.join(staticPartsPath, 'archived-page-start.html'), 'utf8'); + archivedEndPart = fs.readFileSync(path.join(staticPartsPath, 'archived-page-end.html'), 'utf8'); + indexStaticFile = fs.readFileSync(path.join(staticPartsPath, 'index.html'), 'utf8'); } catch (err) { // Handle errors (hopefully not!) @@ -271,5 +282,52 @@ try { process.exit(1); } +// Create the output for the beginner.html file +try { + // Add the static part + beginnerOutput += `${beginnerStartPart + '\n'}`; + + // Filter begginer snippets + const filteredBeginnerSnippets = Object.keys(snippets) + .filter(key => beginnerSnippetNames.map(name => name+'.md').includes(key)) + .reduce((obj, key) => { + obj[key] = snippets[key]; + return obj; + }, {}); + + for (let snippet of Object.entries(filteredBeginnerSnippets)) + beginnerOutput += + '
' + + '
' + + '
' + + md + .render(`\n${snippets[snippet[0]]}`) + .replace(/

/g, `${snippet[1].includes('advanced')?'advanced':''}

`) + .replace(/<\/h3>/g, '
') + .replace(/
([^\0]*?)<\/code><\/pre>/gm, (match, p1) => `
${Prism.highlight(unescapeHTML(p1), Prism.languages.javascript)}
`) + .replace(/<\/pre>\s+
📋 Copy to clipboard' +
+          '
'; + + // Optimize punctuation nodes + beginnerOutput = util.optimizeNodes(beginnerOutput, /([^\0<]*?)<\/span>([\n\r\s]*)([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `${p1}${p2}${p3}`); + // Optimize operator nodes + beginnerOutput = util.optimizeNodes(beginnerOutput, /([^\0<]*?)<\/span>([\n\r\s]*)([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `${p1}${p2}${p3}`); + // Optimize keyword nodes + beginnerOutput = util.optimizeNodes(beginnerOutput, /([^\0<]*?)<\/span>([\n\r\s]*)([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `${p1}${p2}${p3}`); + + + beginnerOutput += `${beginnerEndPart}`; + + // Generate 'beginner.html' file + fs.writeFileSync(path.join(docsPath, 'beginner.html'), beginnerOutput); + console.log(`${chalk.green('SUCCESS!')} beginner.html file generated!`); + +} catch (err) { + console.log(`${chalk.red('ERROR!')} During beginner.html generation: ${err}`); + process.exit(1); +} + // Log the time taken console.timeEnd('Webber'); diff --git a/static-parts/archived-page-end.html b/static-parts/archived-page-end.html new file mode 100644 index 000000000..a2f9630f0 --- /dev/null +++ b/static-parts/archived-page-end.html @@ -0,0 +1,7 @@ + + + +
+ + + diff --git a/static-parts/archived-page-start.html b/static-parts/archived-page-start.html new file mode 100644 index 000000000..213214776 --- /dev/null +++ b/static-parts/archived-page-start.html @@ -0,0 +1,98 @@ + + + + + + + + Snippets for Beginners - 30 seconds of code + + + + + + + + + + + + + +
+

logo 30 seconds of code

+

Curated collection of useful JavaScript snippets
that you can understand in 30 seconds or less.

+ +
+
+
+
+

Snippets Archive

+

These snippets, while useful and interesting, didn't quite make it into the repository due to either having very specific use-cases or being outdated. However we felt like they might still be useful to some readers, so here they are.


+
+