Skip to content
Browse files

Multi-Language Support

  • Loading branch information...
1 parent cbc59ca commit 1918481ee1e0c0c6834202394c968908ce6a8011 @SaintPeter SaintPeter committed with hallaathrad
Showing with 149 additions and 96 deletions.
  1. +0 −1 clearPages.sh
  2. +43 −38 convert_files.js
  3. +97 −49 create_structure.js
  4. +4 −4 deploy.sh
  5. +4 −3 package.json
  6. +1 −1 wiki-master
View
1 clearPages.sh
@@ -1,3 +1,2 @@
#!/bin/bash
rm -rf ./pages/*
-cp -r templates/* pages/
View
81 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);
});
});
View
146 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;
+ }
+ });
}
View
8 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
View
7 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"
}
}
2 wiki-master
@@ -1 +1 @@
-Subproject commit 9896da063a780127437235826f10fdff241c3beb
+Subproject commit e9fd4a9e0e073bd32f2fac883ae31d3382cf0a15

0 comments on commit 1918481

Please sign in to comment.
Something went wrong with that request. Please try again.