Permalink
Browse files

feature(plugin): page collection preprocessing

Adding a pre processing plugin to allow adding properties to
pages in the pages collection.

feature(plugin): page collection preprocessing

Adding a couple of preprocessing functions for testing the
page collection preprocessing plugin.

feature(plugins): page collection preprocessing

Adding the default isActive and relativeLink property functions
to the plugin itself.
  • Loading branch information...
1 parent eab6463 commit 05d87ab7b4228e41cfead4aba6d8d53b8fb456ab @doowb doowb committed Dec 2, 2013
Showing with 20,221 additions and 2,699 deletions.
  1. +15 −0 Gruntfile.js
  2. +1 −1 README.md
  3. +1 −1 bower.json
  4. +17 −17 docs/plugins.json
  5. +65 −0 lib/plugins/page-collection-properties.js
  6. +10 −5 test/actual/assets_base.html
  7. +10 −5 test/actual/assets_blank_path.html
  8. +10 −5 test/actual/assets_dot_slash.html
  9. +10 −5 test/actual/assets_nested.html
  10. +10 −5 test/actual/assets_trailing_slash.html
  11. +64 −31 test/actual/collections/asc/alert.html
  12. +64 −31 test/actual/collections/asc/collections-categories.html
  13. +64 −31 test/actual/collections/asc/collections-tags.html
  14. +64 −31 test/actual/collections/asc/collections.html
  15. +64 −31 test/actual/collections/asc/complex.html
  16. +196 −97 test/actual/collections/asc/context.html
  17. +62 −31 test/actual/collections/asc/debug-helpers.html
  18. +64 −31 test/actual/collections/asc/example.html
  19. +64 −31 test/actual/collections/asc/gist-helper.html
  20. +66 −32 test/actual/collections/asc/md-helper.html
  21. +64 −31 test/actual/collections/asc/no-yfm.html
  22. +64 −31 test/actual/collections/asc/yfm-context.html
  23. +64 −31 test/actual/collections/asc/yfm.html
  24. +64 −31 test/actual/collections/complex/alert.html
  25. +64 −31 test/actual/collections/complex/collections-categories.html
  26. +64 −31 test/actual/collections/complex/collections-tags.html
  27. +64 −31 test/actual/collections/complex/collections.html
  28. +64 −31 test/actual/collections/complex/complex.html
  29. +196 −97 test/actual/collections/complex/context.html
  30. +62 −31 test/actual/collections/complex/debug-helpers.html
  31. +64 −31 test/actual/collections/complex/example.html
  32. +64 −31 test/actual/collections/complex/gist-helper.html
  33. +66 −32 test/actual/collections/complex/md-helper.html
  34. +64 −31 test/actual/collections/complex/no-yfm.html
  35. +64 −31 test/actual/collections/complex/yfm-context.html
  36. +64 −31 test/actual/collections/complex/yfm.html
  37. +64 −31 test/actual/collections/custom/alert.html
  38. +64 −31 test/actual/collections/custom/collections-categories.html
  39. +64 −31 test/actual/collections/custom/collections-tags.html
  40. +64 −31 test/actual/collections/custom/collections.html
  41. +64 −31 test/actual/collections/custom/complex.html
  42. +196 −97 test/actual/collections/custom/context.html
  43. +62 −31 test/actual/collections/custom/debug-helpers.html
  44. +64 −31 test/actual/collections/custom/example.html
  45. +64 −31 test/actual/collections/custom/gist-helper.html
  46. +66 −32 test/actual/collections/custom/md-helper.html
  47. +64 −31 test/actual/collections/custom/no-yfm.html
  48. +64 −31 test/actual/collections/custom/yfm-context.html
  49. +64 −31 test/actual/collections/custom/yfm.html
  50. +64 −31 test/actual/collections/desc/alert.html
  51. +64 −31 test/actual/collections/desc/collections-categories.html
  52. +64 −31 test/actual/collections/desc/collections-tags.html
  53. +64 −31 test/actual/collections/desc/collections.html
  54. +64 −31 test/actual/collections/desc/complex.html
  55. +196 −97 test/actual/collections/desc/context.html
  56. +62 −31 test/actual/collections/desc/debug-helpers.html
  57. +64 −31 test/actual/collections/desc/example.html
  58. +64 −31 test/actual/collections/desc/gist-helper.html
  59. +66 −32 test/actual/collections/desc/md-helper.html
  60. +64 −31 test/actual/collections/desc/no-yfm.html
  61. +64 −31 test/actual/collections/desc/yfm-context.html
  62. +64 −31 test/actual/collections/desc/yfm.html
  63. +6 −3 test/actual/noyfm/no-yfm.html
  64. +1 −1 test/actual/pages_array/index.html
  65. +1 −1 test/actual/pages_metadata/index.html
  66. +1 −1 test/actual/pages_object/index.html
  67. +64 −31 test/actual/paths/alert.html
  68. +64 −31 test/actual/paths/collections-categories.html
  69. +64 −31 test/actual/paths/collections-tags.html
  70. +64 −31 test/actual/paths/collections.html
  71. +64 −31 test/actual/paths/complex.html
  72. +196 −97 test/actual/paths/context.html
  73. +62 −31 test/actual/paths/debug-helpers.html
  74. +64 −31 test/actual/paths/example.html
  75. +64 −31 test/actual/paths/gist-helper.html
  76. +66 −32 test/actual/paths/md-helper.html
  77. +64 −31 test/actual/paths/no-yfm.html
  78. +64 −31 test/actual/paths/yfm-context.html
  79. +64 −31 test/actual/paths/yfm.html
  80. +1,090 −0 test/actual/plugin_preprocess/alert.html
  81. +1,107 −0 test/actual/plugin_preprocess/collections-categories.html
  82. +1,099 −0 test/actual/plugin_preprocess/collections-tags.html
  83. +1,125 −0 test/actual/plugin_preprocess/collections.html
  84. +1,088 −0 test/actual/plugin_preprocess/complex.html
  85. +1,529 −0 test/actual/plugin_preprocess/context.html
  86. +1,055 −0 test/actual/plugin_preprocess/debug-helpers.html
  87. +1,089 −0 test/actual/plugin_preprocess/example.html
  88. +1,058 −0 test/actual/plugin_preprocess/gist-helper.html
  89. +1,105 −0 test/actual/plugin_preprocess/md-helper.html
  90. +1,058 −0 test/actual/plugin_preprocess/no-yfm.html
  91. +1,087 −0 test/actual/plugin_preprocess/yfm-context.html
  92. +1,067 −0 test/actual/plugin_preprocess/yfm.html
  93. +0 −1 test/actual/plugin_untitled.html
  94. +0 −1 test/actual/postprocess.html
  95. +6 −3 test/actual/postprocess2.html
  96. +14 −7 test/actual/single_page.html
  97. +47 −26 test/actual/yfm/associative-arrays.html
  98. +44 −21 test/actual/yfm/block-literals.html
  99. +45 −22 test/actual/yfm/comments.html
  100. +44 −21 test/actual/yfm/data-files.html
  101. +53 −30 test/actual/yfm/data-types.html
  102. +58 −35 test/actual/yfm/document.html
  103. +62 −37 test/actual/yfm/lists.html
  104. +75 −52 test/actual/yfm/relational-trees.html
  105. +44 −21 test/actual/yfm/underscore.html
  106. +44 −21 test/actual/yfm/variables.html
  107. +128 −0 test/fixtures/layouts/preprocess.hbs
  108. +21 −0 test/plugins/page_collection_preprocessing.js
  109. +1 −1 test/plugins_test.js
View
@@ -165,6 +165,21 @@ module.exports = function(grunt) {
'test/actual/not_real.html': 'test/fixtures/plugins/after.hbs'
}
},
+ // should add isActive and relativeLink to each page
+ // in the pages collection
+ plugin_preprocess_page_collection: {
+ options: {
+ partials: 'test/fixtures/partials/*.hbs',
+ data: 'test/fixtures/data/*.{json,yml}',
+ layout: 'preprocess.hbs',
+ pageCollection: {
+ preprocess: require('./test/plugins/page_collection_preprocessing.js')
+ }
+ },
+ files: {
+ 'test/actual/plugin_preprocess/': ['test/fixtures/pages/*.hbs']
+ }
+ },
// Path construction based on built-in variables
// Should automatically calculate relative paths correctly
paths: {
View
@@ -341,7 +341,7 @@ Released under the MIT license
***
-_This file was generated by [grunt-readme](https://github.com/assemble/grunt-readme) on Saturday, November 30, 2013._
+_This file was generated by [grunt-readme](https://github.com/assemble/grunt-readme) on Monday, December 2, 2013._
[grunt]: http://gruntjs.com/
[Getting Started]: https://github.com/gruntjs/grunt/blob/devel/docs/getting_started.md
View
@@ -1,6 +1,6 @@
{
"name": "assemble",
- "version": "0.4.29",
+ "version": "0.4.30",
"main": [
"./lib/assemble"
]
View
@@ -7,7 +7,7 @@
"owner": {
"login": "assemble",
"id": 2645080,
- "avatar_url": "https://0.gravatar.com/avatar/d68b5947524ac7869e76da923a228dcf?d=https%3A%2F%2Fidenticons.github.com%2Ff6d99a3829b704a4a102b1bbec4499b0.png&r=x",
+ "avatar_url": "https://2.gravatar.com/avatar/d68b5947524ac7869e76da923a228dcf?d=https%3A%2F%2Fidenticons.github.com%2Ff6d99a3829b704a4a102b1bbec4499b0.png&r=x",
"gravatar_id": "d68b5947524ac7869e76da923a228dcf",
"url": "https://api.github.com/users/assemble",
"html_url": "https://github.com/assemble",
@@ -101,7 +101,7 @@
"owner": {
"login": "assemble",
"id": 2645080,
- "avatar_url": "https://0.gravatar.com/avatar/d68b5947524ac7869e76da923a228dcf?d=https%3A%2F%2Fidenticons.github.com%2Ff6d99a3829b704a4a102b1bbec4499b0.png&r=x",
+ "avatar_url": "https://2.gravatar.com/avatar/d68b5947524ac7869e76da923a228dcf?d=https%3A%2F%2Fidenticons.github.com%2Ff6d99a3829b704a4a102b1bbec4499b0.png&r=x",
"gravatar_id": "d68b5947524ac7869e76da923a228dcf",
"url": "https://api.github.com/users/assemble",
"html_url": "https://github.com/assemble",
@@ -195,7 +195,7 @@
"owner": {
"login": "assemble",
"id": 2645080,
- "avatar_url": "https://0.gravatar.com/avatar/d68b5947524ac7869e76da923a228dcf?d=https%3A%2F%2Fidenticons.github.com%2Ff6d99a3829b704a4a102b1bbec4499b0.png&r=x",
+ "avatar_url": "https://2.gravatar.com/avatar/d68b5947524ac7869e76da923a228dcf?d=https%3A%2F%2Fidenticons.github.com%2Ff6d99a3829b704a4a102b1bbec4499b0.png&r=x",
"gravatar_id": "d68b5947524ac7869e76da923a228dcf",
"url": "https://api.github.com/users/assemble",
"html_url": "https://github.com/assemble",
@@ -289,7 +289,7 @@
"owner": {
"login": "assemble",
"id": 2645080,
- "avatar_url": "https://0.gravatar.com/avatar/d68b5947524ac7869e76da923a228dcf?d=https%3A%2F%2Fidenticons.github.com%2Ff6d99a3829b704a4a102b1bbec4499b0.png&r=x",
+ "avatar_url": "https://2.gravatar.com/avatar/d68b5947524ac7869e76da923a228dcf?d=https%3A%2F%2Fidenticons.github.com%2Ff6d99a3829b704a4a102b1bbec4499b0.png&r=x",
"gravatar_id": "d68b5947524ac7869e76da923a228dcf",
"url": "https://api.github.com/users/assemble",
"html_url": "https://github.com/assemble",
@@ -383,7 +383,7 @@
"owner": {
"login": "assemble",
"id": 2645080,
- "avatar_url": "https://0.gravatar.com/avatar/d68b5947524ac7869e76da923a228dcf?d=https%3A%2F%2Fidenticons.github.com%2Ff6d99a3829b704a4a102b1bbec4499b0.png&r=x",
+ "avatar_url": "https://2.gravatar.com/avatar/d68b5947524ac7869e76da923a228dcf?d=https%3A%2F%2Fidenticons.github.com%2Ff6d99a3829b704a4a102b1bbec4499b0.png&r=x",
"gravatar_id": "d68b5947524ac7869e76da923a228dcf",
"url": "https://api.github.com/users/assemble",
"html_url": "https://github.com/assemble",
@@ -477,7 +477,7 @@
"owner": {
"login": "assemble",
"id": 2645080,
- "avatar_url": "https://0.gravatar.com/avatar/d68b5947524ac7869e76da923a228dcf?d=https%3A%2F%2Fidenticons.github.com%2Ff6d99a3829b704a4a102b1bbec4499b0.png&r=x",
+ "avatar_url": "https://2.gravatar.com/avatar/d68b5947524ac7869e76da923a228dcf?d=https%3A%2F%2Fidenticons.github.com%2Ff6d99a3829b704a4a102b1bbec4499b0.png&r=x",
"gravatar_id": "d68b5947524ac7869e76da923a228dcf",
"url": "https://api.github.com/users/assemble",
"html_url": "https://github.com/assemble",
@@ -571,7 +571,7 @@
"owner": {
"login": "assemble",
"id": 2645080,
- "avatar_url": "https://0.gravatar.com/avatar/d68b5947524ac7869e76da923a228dcf?d=https%3A%2F%2Fidenticons.github.com%2Ff6d99a3829b704a4a102b1bbec4499b0.png&r=x",
+ "avatar_url": "https://2.gravatar.com/avatar/d68b5947524ac7869e76da923a228dcf?d=https%3A%2F%2Fidenticons.github.com%2Ff6d99a3829b704a4a102b1bbec4499b0.png&r=x",
"gravatar_id": "d68b5947524ac7869e76da923a228dcf",
"url": "https://api.github.com/users/assemble",
"html_url": "https://github.com/assemble",
@@ -628,14 +628,14 @@
"labels_url": "https://api.github.com/repos/assemble/assemble-contrib-permalinks/labels{/name}",
"releases_url": "https://api.github.com/repos/assemble/assemble-contrib-permalinks/releases{/id}",
"created_at": "2013-10-03T20:10:32Z",
- "updated_at": "2013-11-28T20:00:47Z",
- "pushed_at": "2013-11-28T20:00:45Z",
+ "updated_at": "2013-11-30T11:46:49Z",
+ "pushed_at": "2013-11-30T11:46:43Z",
"git_url": "git://github.com/assemble/assemble-contrib-permalinks.git",
"ssh_url": "git@github.com:assemble/assemble-contrib-permalinks.git",
"clone_url": "https://github.com/assemble/assemble-contrib-permalinks.git",
"svn_url": "https://github.com/assemble/assemble-contrib-permalinks",
"homepage": "",
- "size": 693,
+ "size": 728,
"stargazers_count": 13,
"watchers_count": 13,
"language": "JavaScript",
@@ -665,7 +665,7 @@
"owner": {
"login": "assemble",
"id": 2645080,
- "avatar_url": "https://0.gravatar.com/avatar/d68b5947524ac7869e76da923a228dcf?d=https%3A%2F%2Fidenticons.github.com%2Ff6d99a3829b704a4a102b1bbec4499b0.png&r=x",
+ "avatar_url": "https://2.gravatar.com/avatar/d68b5947524ac7869e76da923a228dcf?d=https%3A%2F%2Fidenticons.github.com%2Ff6d99a3829b704a4a102b1bbec4499b0.png&r=x",
"gravatar_id": "d68b5947524ac7869e76da923a228dcf",
"url": "https://api.github.com/users/assemble",
"html_url": "https://github.com/assemble",
@@ -722,16 +722,16 @@
"labels_url": "https://api.github.com/repos/assemble/assemble-contrib-sitemap/labels{/name}",
"releases_url": "https://api.github.com/repos/assemble/assemble-contrib-sitemap/releases{/id}",
"created_at": "2013-10-02T06:48:32Z",
- "updated_at": "2013-11-29T01:56:45Z",
+ "updated_at": "2013-12-02T06:54:01Z",
"pushed_at": "2013-11-29T01:56:45Z",
"git_url": "git://github.com/assemble/assemble-contrib-sitemap.git",
"ssh_url": "git@github.com:assemble/assemble-contrib-sitemap.git",
"clone_url": "https://github.com/assemble/assemble-contrib-sitemap.git",
"svn_url": "https://github.com/assemble/assemble-contrib-sitemap",
"homepage": null,
"size": 167,
- "stargazers_count": 2,
- "watchers_count": 2,
+ "stargazers_count": 5,
+ "watchers_count": 5,
"language": "JavaScript",
"has_issues": true,
"has_downloads": true,
@@ -741,7 +741,7 @@
"open_issues_count": 1,
"forks": 1,
"open_issues": 1,
- "watchers": 2,
+ "watchers": 5,
"default_branch": "master",
"master_branch": "master",
"permissions": {
@@ -759,7 +759,7 @@
"owner": {
"login": "assemble",
"id": 2645080,
- "avatar_url": "https://0.gravatar.com/avatar/d68b5947524ac7869e76da923a228dcf?d=https%3A%2F%2Fidenticons.github.com%2Ff6d99a3829b704a4a102b1bbec4499b0.png&r=x",
+ "avatar_url": "https://2.gravatar.com/avatar/d68b5947524ac7869e76da923a228dcf?d=https%3A%2F%2Fidenticons.github.com%2Ff6d99a3829b704a4a102b1bbec4499b0.png&r=x",
"gravatar_id": "d68b5947524ac7869e76da923a228dcf",
"url": "https://api.github.com/users/assemble",
"html_url": "https://github.com/assemble",
@@ -853,7 +853,7 @@
"owner": {
"login": "assemble",
"id": 2645080,
- "avatar_url": "https://0.gravatar.com/avatar/d68b5947524ac7869e76da923a228dcf?d=https%3A%2F%2Fidenticons.github.com%2Ff6d99a3829b704a4a102b1bbec4499b0.png&r=x",
+ "avatar_url": "https://2.gravatar.com/avatar/d68b5947524ac7869e76da923a228dcf?d=https%3A%2F%2Fidenticons.github.com%2Ff6d99a3829b704a4a102b1bbec4499b0.png&r=x",
"gravatar_id": "d68b5947524ac7869e76da923a228dcf",
"url": "https://api.github.com/users/assemble",
"html_url": "https://github.com/assemble",
@@ -0,0 +1,65 @@
+/**
+ * Assemble <http://assemble.io>
+ * Created and maintained by Jon Schlinkert and Brian Woodward
+ *
+ * Copyright (c) 2013 Upstage.
+ * Licensed under the MIT License (MIT).
+ */
+
+
+// This plugin will run before each page is rendered
+// and will loop over the pages collection, calling
+// any registered callback function passing in
+// the current page context (to be rendered) and the current
+// page (from the pages collection) allowing
+// additional relative page properties to be added.
+
+var path = require('path');
+var _ = require('lodash');
+
+var options = {
+ stage: 'render:pre:page'
+};
+
+
+var defaults = [
+ // add an isActive flag to the page if the dest matches
+ function isActive(page, context) {
+ page.isActive = (page.dest === context.page.dest ? true : false);
+ },
+
+ // add a relative link from the "current page" to the
+ // page in the collection
+ function relativeLink(page, context) {
+ var relativePath = path.relative(path.dirname(context.page.dest), path.dirname(page.dest));
+ relativePath = path.join(relativePath, path.basename(page.dest));
+ page.relativeLink = relativePath.replace(/\\/g, '/');
+ }
+];
+
+
+var plugin = function(params, next) {
+ var options = params.assemble.options;
+ var pageCollectionOpts = options.pageCollection || {};
+ var callbacks = pageCollectionOpts.preprocess || [];
+
+ if (!_.isArray(callbacks)) {
+ callbacks = [callbacks];
+ }
+
+ callbacks = _.union(callbacks, defaults);
+
+ _.map(params.context.pages, function(page) {
+ _.map(callbacks, function(callback) {
+ callback(page, params.context);
+ });
+ });
+
+ next();
+};
+
+
+// export options
+plugin.options = options;
+module.exports = plugin;
+
@@ -12,7 +12,8 @@
<section id="validate"></section>
<div class="docs-section">
<div class="docs-header" id="content">
- <h4><strong class="text-muted">test/fixtures/assets_path/assets.hbs</strong> &#x2192; <strong class="text-success">test/actual/assets_base.html</strong></h4>
+ <h4><strong class="text-muted">test/fixtures/assets_path/assets.hbs</strong> &#x2192; <strong class="text-success">test/actual/assets_base.html</strong>
+ </h4>
</div>
<hr>
</div>
@@ -100,7 +101,8 @@
<h1>Content</h1>
</div>
<ul>
- <li><a href="assets_base.html">assets_base.html</a>
+ <li>
+ <a href="assets_base.html">assets_base.html</a>
</li>
</ul>
<hr>
@@ -140,7 +142,8 @@
-->
<h1>Each pages</h1>
<ul>
- <li><a href="assets_base.html">assets</a>
+ <li>
+ <a href="assets_base.html">assets</a>
</li>
<!--
"this"
@@ -214,15 +217,17 @@
"docs-header\" id=\ "content\">\n
<h1>Content</h1>\n</div>\n
<ul>\n {{#each pages}}\n
- <li><a href=\ "{{relative ../page.dest dest}}\">{{filename}}</a>
+ <li>
+ <a href=\ "{{relative ../page.dest dest}}\">{{filename}}</a>
</li>\n {{/each}}\n</ul>\n\n
<hr>\n
<div style=\ "display: none\">\n\n
<h1>Page</h1>\n
<!--\n \"this\"\n ===============================\n this.src: {{this.src}}\n this.dest: {{this.dest}}\n this.layout: {{this.layout}}\n this.dirname: {{this.dirname}}\n this.filename: {{this.filename}}\n this.pagename: {{this.pagename}}\n this.basename: {{this.basename}}\n this.ext: {{this.ext}}\n\n \"page\"\n ===============================\n page.src: {{page.src}}\n page.dest: {{page.dest}}\n page.layout: {{page.layout}}\n page.dirname: {{page.dirname}}\n page.filename: {{page.filename}}\n page.pagename: {{page.pagename}}\n page.basename: {{page.basename}}\n page.ext: {{page.ext}}\n\n \"root\"\n ===============================\n src: {{src}}\n dest: {{dest}}\n layout: {{layout}}\n dirname: {{dirname}}\n filename: {{filename}}\n pagename: {{pagename}}\n basename: {{basename}}\n ext: {{ext}}\n -->\n
<h1>Each pages</h1>\n
<ul>\n {{#each pages}}
- <li><a href=\ "{{relative ../page.dest dest}}\">{{basename}}</a>
+ <li>
+ <a href=\ "{{relative ../page.dest dest}}\">{{basename}}</a>
</li>\n
<!--\n \"this\"\n ===============================\n this.src: {{this.src}}\n this.dest: {{this.dest}}\n this.dirname: {{this.dirname}}\n this.filename: {{this.filename}}\n this.pagename: {{this.pagename}}\n this.basename: {{this.basename}}\n this.ext: {{this.ext}}\n\n \"page\"\n ===============================\n page.src: {{../page.src}}\n page.dest: {{../page.dest}}\n page.dirname: {{../page.dirname}}\n page.filename: {{../page.filename}}\n page.pagename: {{../page.pagename}}\n page.basename: {{../page.basename}}\n page.ext: {{../page.ext}}\n\n \"root\"\n ===============================\n src: {{src}}\n dest: {{dest}}\n dirname: {{dirname}}\n filename: {{filename}}\n pagename: {{pagename}}\n basename: {{basename}}\n ext: {{ext}}\n -->\n {{/each}}\n</ul>\n\n
<h1>Debug Info</h1>\n {{{inspect page 'json'}}}\n</div>\n</div>\n</div>\n</div>\n</div>\n
Oops, something went wrong.

0 comments on commit 05d87ab

Please sign in to comment.