Skip to content
This repository has been archived by the owner on Nov 21, 2018. It is now read-only.

Commit

Permalink
Templating / i18n working
Browse files Browse the repository at this point in the history
  • Loading branch information
therebelrobot committed Jan 22, 2015
1 parent e8112ee commit 595da4f
Show file tree
Hide file tree
Showing 14 changed files with 230 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
npm-debug.log

This comment has been minimized.

Copy link
@therebelrobot

therebelrobot Jan 22, 2015

Author Contributor

because I break npm more than I should :P

4 changes: 4 additions & 0 deletions content/en/template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"[i18n-button-one]":"Press Me!",
"[i18n-button-two]":"Don't press me!"
}
7 changes: 7 additions & 0 deletions content/en/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This is a test
*a real one!*
- beep
- boop
- bop

**test**
4 changes: 4 additions & 0 deletions content/en/test2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# this is another test
## a more better test
### beep
#### boop
4 changes: 4 additions & 0 deletions content/es/template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"[i18n-button-one]":"Prensa mí!",
"[i18n-button-two]":"No me presione!"
}
5 changes: 3 additions & 2 deletions gulp/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ module.exports = {
}
},
templates: {
src: src + "/templates/**/*.md",
content: content,
templateSrc: src + "/templates/**/*.html",
contentSrc: content + "/**/*.md",
templateJSONsrc: content + "/**/template.json",
dest: dest
},
images: {
Expand Down
53 changes: 48 additions & 5 deletions gulp/tasks/templates.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,53 @@
var gulp = require('gulp');
var config = require('../config').templates;
var _ = require('lodash'); // to handle collections gracefully
var config = require('../config').templates; // pull in the pathing config file
var fs = require('fs'); // used to work with substack's module
var glob = require('glob'); // to dynamically read in all content md files
var gulp = require('gulp'); // because this is a gulp task. duh.
var HTMLtemplate = require('html-template'); // substack's html template implementation
var md = require('markdown-it')(); // to convert markdown to html
var source = require('vinyl-source-stream'); // used to convert substack's readStream to vinylStream

gulp.task('templates', function() {
var htmlObjHolder = {}; // these holders are needed later to keep write streams separate
var i18nObjHolder = {};

This comment has been minimized.

Copy link
@Fishrock123

Fishrock123 Jan 22, 2015

Contributor

are these really necessary? regular variables seems to work fine in the last foreach

This comment has been minimized.

Copy link
@therebelrobot

therebelrobot Jan 22, 2015

Author Contributor

Yeah, those were left in from troubleshooting an unrelated issue. I can flatten them tonight if you like, or if you want you can as well.

var contentFiles = glob.sync(config.contentSrc); // read in all content files in the repo

// loop over content folder here
var languages = _.uniq(_.map(contentFiles, function(str) { // extrapolate the languages from the content filepaths
return str.split('/')[2];
}));

// play with build and templates here
_.forEach(languages, function(lang) { // loop through languages to make separate folder outputs
htmlObjHolder[lang] = {}; // expanding the stream holders for later
i18nObjHolder[lang] = {};
var templateJSON = require('../../content/' + lang + '/template.json'); // read in the template JSON file

});
var templateFiles = _.where(contentFiles, function(str) { // return list of content files in this language alone
return str.indexOf('./content/' + lang) > -1;
});

templateFilesInThisLang = _.map(templateFiles, function(str) { // expand the file list to include the extrapolated filename
var obj = {};
obj.srcPath = str;
obj.filename = str.split('/');
obj.filename = obj.filename[obj.filename.length - 1].split('.md')[0];
return obj;
});

_.forEach(templateFilesInThisLang, function(file) { // iterate over the md files present in this language to apply the template to them
var markdown = String(fs.readFileSync(file.srcPath)); // read in the md file, convert buffer to string
var html = md.render(markdown); // convert md string to html string
var thisFileJSON = _.cloneDeep(templateJSON); // clone in the template JSON object
thisFileJSON['[i18n-content]'] = html; // Attach md2html string to the interpolation object
htmlObjHolder[lang][file.filename] = HTMLtemplate(); // finally using that holder for the template stream
i18nObjHolder[lang][file.filename] = htmlObjHolder[lang][file.filename].template('i18n'); // same
var filepath = __dirname.split('gulp/tasks')[0] + 'source/templates/test.html'; // get the main template file location. There can be multiple, this is just a proof of concept

fs.createReadStream(filepath) // pulling this code from substack's example on html-template
.pipe(htmlObjHolder[lang][file.filename])
.pipe(source(file.filename + '.html')) // converting the readStream to a vinyl stream so gulp can write it back to the disk
.pipe(gulp.dest('public_test/' + lang + '/')); // dump it in the appropriate language subfolder
i18nObjHolder[lang][file.filename].write(thisFileJSON); // write the interpolation JSON to the template
i18nObjHolder[lang][file.filename].end(); // saving? this is taken from substack too.
});
});
});

This comment has been minimized.

Copy link
@therebelrobot

therebelrobot Jan 22, 2015

Author Contributor

This file was so much fun to build! I'll most likely use this in some of my side projects even if we decide against it here.

12 changes: 8 additions & 4 deletions gulp/tasks/watch.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
var gulp = require('gulp');
var config = require('../config');
var gulp = require('gulp');
var config = require('../config');

gulp.task('watch', function(callback) {
gulp.watch(config.stylus.src, ['stylus']);
gulp.watch(config.templates.src, ['templates']);
gulp.watch(config.stylus.src, ['stylus']);
gulp.watch([
config.templates.templateSrc,
config.templates.contentSrc,
config.templates.templateJSONsrc
], ['templates']);
});
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"browserify": "^8.1.1",
"csswring": "^3.0.0",
"del": "^1.1.1",
"glob": "^4.3.5",
"gulp": "^3.8.10",
"gulp-changed": "^1.1.0",
"gulp-filesize": "0.0.6",
Expand All @@ -53,7 +54,11 @@
"gulp-postcss": "^4.0.2",
"gulp-rename": "^1.2.0",
"gulp-stylus": "^2.0.0",
"html-template": "^1.1.0",
"lodash": "^2.4.1",
"markdown-it": "^3.0.4",
"require-dir": "^0.1.0",
"run-sequence": "^1.0.2"
"run-sequence": "^1.0.2",
"vinyl-source-stream": "^1.0.0"
}
}
45 changes: 45 additions & 0 deletions public_test/en/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<html>

<body class="i18n">
<div class="wrapper" template="i18n" style="display:none">

This comment has been minimized.

Copy link
@therebelrobot

therebelrobot Jan 22, 2015

Author Contributor

This duplicating of the template object is something that I'm contacting James (substack) about, even in his example it included a copy of the original template.

Here's the open issue: https://github.com/substack/html-template/issues/3

<h1>list of skills</h1>
<button i18n-button-one></button>
<button i18n-button-two></button>
<div i18n-content></div>
<table class="skill">
<tr>
<th>skill name</th>
<th>skill level</th>
</tr>
<tr>
<td key="name"></td>
<td key="level"></td>
</tr>
</table>
</div><div class="wrapper">
<h1>list of skills</h1>
<button i18n-button-one>Press Me!</button>
<button i18n-button-two>Don't press me!</button>
<div i18n-content><h1>This is a test</h1>
<p><em>a real one!</em></p>
<ul>
<li>beep</li>
<li>boop</li>
<li>bop</li>
</ul>
<p><strong>test</strong></p>
</div>
<table class="skill">
<tr>
<th>skill name</th>
<th>skill level</th>
</tr>
<tr>
<td key="name"></td>
<td key="level"></td>
</tr>
</table>
</div>
</body>

</html>
41 changes: 41 additions & 0 deletions public_test/en/test2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<html>

<body class="i18n">
<div class="wrapper" template="i18n" style="display:none">

This comment has been minimized.

Copy link
@therebelrobot

therebelrobot Jan 22, 2015

Author Contributor
<h1>list of skills</h1>
<button i18n-button-one></button>
<button i18n-button-two></button>
<div i18n-content></div>
<table class="skill">
<tr>
<th>skill name</th>
<th>skill level</th>
</tr>
<tr>
<td key="name"></td>
<td key="level"></td>
</tr>
</table>
</div><div class="wrapper">
<h1>list of skills</h1>
<button i18n-button-one>Press Me!</button>
<button i18n-button-two>Don't press me!</button>
<div i18n-content><h1>this is another test</h1>
<h2>a more better test</h2>
<h3>beep</h3>
<h4>boop</h4>
</div>
<table class="skill">
<tr>
<th>skill name</th>
<th>skill level</th>
</tr>
<tr>
<td key="name"></td>
<td key="level"></td>
</tr>
</table>
</div>
</body>

</html>
37 changes: 37 additions & 0 deletions public_test/es/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<html>

<body class="i18n">
<div class="wrapper" template="i18n" style="display:none">

This comment has been minimized.

Copy link
@therebelrobot

therebelrobot Jan 22, 2015

Author Contributor
<h1>list of skills</h1>
<button i18n-button-one></button>
<button i18n-button-two></button>
<div i18n-content></div>
<table class="skill">
<tr>
<th>skill name</th>
<th>skill level</th>
</tr>
<tr>
<td key="name"></td>
<td key="level"></td>
</tr>
</table>
</div><div class="wrapper">
<h1>list of skills</h1>
<button i18n-button-one>Prensa mí!</button>
<button i18n-button-two>No me presione!</button>
<div i18n-content></div>
<table class="skill">
<tr>
<th>skill name</th>
<th>skill level</th>
</tr>
<tr>
<td key="name"></td>
<td key="level"></td>
</tr>
</table>
</div>
</body>

</html>
1 change: 0 additions & 1 deletion public_test/test.min.css

This file was deleted.

22 changes: 22 additions & 0 deletions source/templates/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<html>

<body class="i18n">
<div class="wrapper" template="i18n">
<h1>list of skills</h1>
<button i18n-button-one></button>

This comment has been minimized.

Copy link
@therebelrobot

therebelrobot Jan 22, 2015

Author Contributor

I was right in assuming we could use any attribute we can select with css. I chose i18n-* for simplicity's sake

<button i18n-button-two></button>
<div i18n-content></div>
<table class="skill">
<tr>
<th>skill name</th>
<th>skill level</th>
</tr>
<tr>
<td key="name"></td>
<td key="level"></td>
</tr>
</table>
</div>
</body>

</html>

0 comments on commit 595da4f

Please sign in to comment.