Permalink
Browse files

Templates are now outputted in alphabetical order. Tests have been up…

…dated to follow this as well.
  • Loading branch information...
krunkosaurus committed Sep 19, 2011
1 parent 24aeee4 commit f0f5b6133d3c72de1fb66db6598f8a1a790a3dd7
Showing with 45 additions and 142 deletions.
  1. +31 −8 index.js
  2. +13 −13 tests/assets/index.html
  3. +0 −121 tests/node_modules/nodeinterval/index.js
  4. +1 −0 tests/node_modules/nodeinterval/index.js
View
@@ -61,7 +61,7 @@ var fs = require('fs'),
return this;
},
- // Recursively return an array of all files in a directory.
+ // Recursively return an array of all files (paths) in a directory.
getFilesFrom: function(dir){
var filesAr = [];
var recurse;
@@ -114,17 +114,41 @@ var fs = require('fs'),
// Update target page with new templates.
updateIndex: function(){
var date = new Date();
- var filesAr = this.getFilesFrom(this.watchFolder);
- var content = this.stringFromFiles(filesAr);
+ // Ar of sorted ids from each template.
+ var idsAr = [];
+ // Lookup hash of ids to template content.
+ var fileHash = {};
+ // Final concated text content.
+ var content = '';
var template;
- var i;
+ var f;
+
+ // For each file get its template id so we can sort alphabetically.
+ _.each(this.getFilesFrom(this.watchFolder), function(file, i, list){
+ var contents = this.getFileContents(file);
+ var id = contents.match(/id=['"](.*)['"]/);
+ if (id){
+ id = id[1];
+ // Add to filehash
+ fileHash[id] = contents;
+ idsAr.push(id);
+ }else{
+ throw new Error('Template missing id: ' + file);
+ }
+ }, this);
+
+ idsAr.sort();
+
+ _.each(idsAr, function(id){
+ content += fileHash[id];
+ });
if (this.outputFile instanceof Array){
- for (i = 0; i < this.outputFile.length; i++){
- template = this.getFileContents(this.inputFile[i]);
+ for (f = 0; f < this.outputFile.length; f++){
+ template = this.getFileContents(this.inputFile[f]);
template = template.replace(this.replacementString, content);
- this.writeToFile(this.outputFile[i], template);
+ this.writeToFile(this.outputFile[f], template);
}
}else{
template = this.getFileContents(this.inputFile);
@@ -133,7 +157,6 @@ var fs = require('fs'),
this.writeToFile(this.outputFile, template);
}
-
log.info('Completed in ' + ((new Date() - date) / 1000) + ' seconds.');
}
});
View
@@ -6,19 +6,7 @@
<body>
<h1>Test Test page</h1>
<p>My templates below</p>
- <script type="text/template" id="template-09">
- This is my template 09.
-</script>
-
-<script type="text/template" id="template-07">
- This is my template 07.
-</script>
-
-<script type="text/template" id="template-08">
- This is my template 08.
-</script>
-
-<script type="text/template" id="template-01">
+ <script type="text/template" id="template-01">
This is my template 01.
</script><script type="text/template" id="template-02">
This is my template 02.
@@ -40,6 +28,18 @@ <h1>Test Test page</h1>
This is my template 06.
</script>
+<script type="text/template" id="template-07">
+ This is my template 07.
+</script>
+
+<script type="text/template" id="template-08">
+ This is my template 08.
+</script>
+
+<script type="text/template" id="template-09">
+ This is my template 09.
+</script>
+
<p>My templates above</p>
</body>

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit f0f5b61

Please sign in to comment.