From 1918481ee1e0c0c6834202394c968908ce6a8011 Mon Sep 17 00:00:00 2001
From: SaintPeter <rex.schrader@gmail.com>
Date: Sun, 7 Feb 2016 20:55:49 -0800
Subject: [PATCH] Multi-Language Support

---
 clearPages.sh       |   1 -
 convert_files.js    |  81 +++++++++++++++--------------
 create_structure.js | 146 ++++++++++++++++++++++++++++++++++------------------
 deploy.sh           |   8 +--
 package.json        |   7 +--
 wiki-master         |   2 +-
 6 files changed, 149 insertions(+), 96 deletions(-)

diff --git a/clearPages.sh b/clearPages.sh
index 1b10cb3..ace0d17 100755
--- a/clearPages.sh
+++ b/clearPages.sh
@@ -1,3 +1,2 @@
 #!/bin/bash
 rm -rf ./pages/*
-cp -r templates/* pages/
diff --git a/convert_files.js b/convert_files.js
index 1c6790f..7d10d06 100644
--- a/convert_files.js
+++ b/convert_files.js
@@ -1,17 +1,4 @@
-/*
-Automated cleanup / Setup -- node script
--- All in "Pages"
-- Add Headers to each file
-  - Title, Order (whatever that is)
-- ? Automated cleanup of Markdown
-IE: ###NoSpace === ### With Space
-Regex :   /^(\s+#{1,4})(\S)/ => /$1 $2/
-- List of regex to automate
-JSON Array/Object
-  - Link modification/cleanup
-*/
-
-var fs = require('fs');
+var fs = require('fs-extra');
 var incomingLink = /github\.com\/freecodecamp\/freecodecamp\/wiki/gi;
 var outgoingLink = 'freecodecamp.com/wiki';
 
@@ -22,35 +9,53 @@ langFolders.filter(langFolder => {
   return /^\w{2}$/.test(langFolder);
 }).forEach(langFolder => {
   // Get File list
-  fs.readdir('./pages/' + langFolder + '/', function(err, folders) {
-    if(err) throw err;
-    var fileList = folders.filter(function(folder) {
-      // Remove stupid hidden folders
-      return !/^\.|\.md$|^_/.test(folder);
-    }).map(function(folder) {
+  fs.readdir('./pages/' + langFolder + '/', function (err, folders) {
+    if (err) throw err;
+    var fileList = folders.filter(function (folder) {
+      // Ignore Hidden Folders and template files
+      return !/^\.|^_/.test(folder);
+    }).map(function (folder) {
       // Make directories/filenames
-      var filename = folder+'/index.md';
-      var title = folder.replace(/-/g, ' ').replace('.md', '');
-      return { filename: filename, title: title};
+      if (/index\.md/.test(folder)) {
+        return {
+          isHome: true,
+          filename: `index.md`,
+          title: "Welcome to the Free Code Camp Wiki"
+        };
+      } else {
+        return {
+          isHome: false,
+          filename: folder + '/index.md',
+          title: folder.replace(/-/g, ' ').replace('.md', '')
+        };
+      }
     });
-  
-    fileList.forEach(function(fileobj) {
-      // Create directory
-  
-      var newFileName = './pages/' + langFolder + '/' +fileobj.filename;
-  
-      var data = fs.readFileSync(newFileName, 'utf-8'); //read existing contents into data
-      var fd = fs.openSync(newFileName, 'w+');
-  
+
+    // Modify each index.md file
+    //  * Add Headers
+    //  * Update Links
+    fileList.forEach(function (fileObj) {
+      var newFileName = './pages/' + langFolder + '/' + fileObj.filename;
+
+      // Read existing contents into data
+      var data = fs.readFileSync(newFileName, 'utf-8');
+
+      // Update Links
       data = data.replace(incomingLink, outgoingLink)
-                 .replace(/\.\/images/gi,'../images');  // Update image links to be relative
+        .replace(/\.\/images/gi, '../images');  // Update image links to be relative
       var newData = new Buffer(data);
-  
-      var header = '---\ntitle: ' + fileobj.title + '\norder: 5\n---\n';
+
+      // Make the "Home" file display at the top
+      var order = (fileObj.isHome) ? 0 : 5;
+
+      // Create Header
+      var header = `---\ntitle: ${fileObj.title}\norder: ${order}\n---\n`;
       var buffer = new Buffer(header);
-  
-      fs.writeSync(fd, buffer, 0, buffer.length); //write new data
-      fs.writeSync(fd, newData, 0, newData.length); //append old data
+
+      // Output File
+      var fd = fs.openSync(newFileName, 'w+');
+      fs.writeSync(fd, buffer, 0, buffer.length); // Write header
+      fs.writeSync(fd, newData, 0, newData.length); // Append remaining data
       fs.close(fd);
     });
   });
diff --git a/create_structure.js b/create_structure.js
index 79d3b22..7f81069 100644
--- a/create_structure.js
+++ b/create_structure.js
@@ -1,66 +1,114 @@
-/*
-Automate converting the raw "wiki" into the proper hierarchy -- node script
-- Readin in the file list
-- Create top-level folders based on file name (Assume flat structure)
-- Copy the files into their folders as index.md
--- All in "Pages"
-*/
-
 var mkdirp = require('mkdirp');
-var fs = require('fs');
+var fs = require('fs-extra');
+
+// Initialize Language folders files to copy
+var languageFolders = [
+  {
+    inputFile: `./wiki-master/Home.md`,
+    outputFile: `./pages/en/index.md`
+  },
+  {
+    inputFile: `./templates/index.md`,
+    outputFile: `./pages/index.md`
+  },
+  {
+    inputFile: `./templates/lang/_template.jsx`,
+    outputFile: `./pages/en/_template.jsx`
+  },
+  {
+    inputFile: `./templates/_template.jsx`,
+    outputFile: `./pages/_template.jsx`
+  }
+];
 
 // Get File list
-fs.readdir('./wiki-master', function(err, files) {
-  if(err) throw err;
-  
+fs.readdir('./wiki-master', function (err, files) {
+  if (err) throw err;
+
   // List all of the *.lang folders for later
   var folderList = files.filter(file => {
     return /\w{2}\.lang/.test(file);
   });
-  
+
   // Get English/Top Level files
-  var fileList = files.filter(function(file) {
-    return (/\.md$/.test(file) && !/^_|\w{2}\.lang|Home\.md/.test(file));
-  }).map(function(file) {
+  var fileList = files.filter(function (file) {
+    return (/\.md$/.test(file) && !/^_|\w{2}\.lang/.test(file));
+  }).map(function (file) {
     // Make directories/filenames
-    var filename = file;
-    var dir = 'en/' + file.replace('.md', '');
-    return { filename: filename, dir: dir};
+    if (/Home\.md/i.test(file)) {
+      return {
+        inputFile: file,
+        outputDir: 'en/'
+      };
+    } else {
+      return {
+        inputFile: file,
+        outputDir: 'en/' + file.replace('.md', '')
+      };
+    }
   });
-  
+
   // Get non-english files
-  var extra = folderList.reduce((thisList, subFolder) => {
-    var langDir = subFolder.match(/^\w{2}/)[0] + '/',
-        langFiles = fs.readdirSync('./wiki-master/' + subFolder);
-    return thisList.concat(langFiles.filter(function(file) {
-      return (/\.md$/.test(file) && !/^_|\w{2}\.lang|Home\.md/.test(file));
-    }).map(function(file) {
-      // Make directories/filenames
-      var filename = subFolder + '/' + file;
-      var dir = langDir + file.replace('.md', '');
-      return { filename: filename, dir: dir};
-    }));
-  },[]);
-  
-  //  [ [ stuff] ]
-  //  [ stuff ]
-  
-  console.log('extra', extra);  
-  
-  fileList = fileList.concat(extra);
-  
+  var extra = folderList.reduce((thisList, langSubFolder) => {
+    var langDir = langSubFolder.match(/^\w{2}/)[0] + '/',
+      langFiles = fs.readdirSync('./wiki-master/' + langSubFolder);
+
+    // Setup copies for later
+    languageFolders.push({
+      inputFile: `./wiki-master/` + langSubFolder + `/Home.md`,
+      outputFile: `./pages/` + langDir + 'index.md'
+    });
+    languageFolders.push({
+      inputFile: `./templates/lang/_template.jsx`,
+      outputFile: `./pages/` + langDir + '_template.jsx'
+    });
+
+    // Append foreign language files to the copy list
+    return thisList.concat(
+      langFiles.filter(function (file) {
+        return (/\.md$/.test(file) && !/^_|\w{2}\.lang/.test(file));
+      }).map(function (file) {
+        // Make directories/filenames
+        if (/Home\.md/i.test(file)) {
+          return {
+            inputFile: file,
+            outputDir: langDir
+          };
+        } else {
+          return {
+            inputFile: langSubFolder + '/' + file,
+            outputDir: langDir + file.replace('.md', '')
+          };
+        }
+      })
+    );
+  }, []);
+
+  // Create folders and copy *.md files
   createFolders(fileList);
+
+  // Copy language templates and templates files to each language
+  try {
+    languageFolders.forEach(file => {
+      fs.copySync(file.inputFile, file.outputFile);
+    });
+  } catch (err) {
+    throw err;
+  }
 });
 
+// Create a folder base
 function createFolders(fileList) {
-  fileList.forEach(function(fileobj) {
-    // Create directory
-    console.log(fileobj.dir);
-    mkdirp('./pages/' + fileobj.dir, function(err) {
-      if(err) throw err;
+  fileList.forEach(function (fileobj) {
+    try {
+      // Create directory
+      fs.mkdirsSync('./pages/' + fileobj.outputDir);
+
       // Copy File
-      var newFileName = './pages/'+fileobj.dir+"/index.md";
-      fs.createReadStream('./wiki-master/' + fileobj.filename).pipe(fs.createWriteStream(newFileName));
-    });
-  });  
+      fs.copySync('./wiki-master/' + fileobj.inputFile,
+        './pages/' + fileobj.outputDir + '/index.md');
+    } catch (err) {
+      throw err;
+    }
+  });
 }
diff --git a/deploy.sh b/deploy.sh
index 1dd9f00..0ec920e 100755
--- a/deploy.sh
+++ b/deploy.sh
@@ -28,18 +28,18 @@ node convert_files.js
 # Build Output
 gatsby build --prefix-links
 
+# TODO:  Clear out existing folders before copy
+
 # Copy generated output to gh-pages on live wiki
 cp -r wiki-master/images/* wiki-gh-pages/images/
+# TODO:  Copy language images folders
 cp -r public/* wiki-gh-pages/
 cp css/* wiki-gh-pages/css/
-cp public/bundle.js wiki-gh-pages/
-cp public/index.html wiki-gh-pages/
-cp public/bundle.js.map wiki-gh-pages/
 
 # Update gh-pages branch with new files
 cd wiki-gh-pages/
 git pull origin gh-pages
-git add .
+git add -A
 git commit -m "Page built on `date +"%d-%m-%Y %T"`"
 
 # Push gh-pages live
diff --git a/package.json b/package.json
index c419846..35d9588 100644
--- a/package.json
+++ b/package.json
@@ -19,7 +19,10 @@
   "dependencies": {
     "chroma-js": "0.7.2",
     "color-pairs-picker": "^1.3.5",
+    "fs-extra": "^0.26.5",
     "lodash": "^3.10.1",
+    "mkdirp": "^0.5.1",
+    "promise": "^7.0.4",
     "react": "^0.14.3",
     "react-document-title": "^2.0.1",
     "react-dom": "^0.14.3",
@@ -27,8 +30,6 @@
     "react-responsive-grid": "^0.2.1",
     "react-router": "^0.13.5",
     "typography": "^0.6.2",
-    "underscore.string": "^3.2.2",
-    "mkdirp": "^0.5.1",
-    "promise": "^7.0.4"
+    "underscore.string": "^3.2.2"
   }
 }
diff --git a/wiki-master b/wiki-master
index 9896da0..e9fd4a9 160000
--- a/wiki-master
+++ b/wiki-master
@@ -1 +1 @@
-Subproject commit 9896da063a780127437235826f10fdff241c3beb
+Subproject commit e9fd4a9e0e073bd32f2fac883ae31d3382cf0a15
