UNPKG

4.96 kBJavaScriptView Raw
1var _ = require('underscore'),
2 path = require('path');
3
4module.exports = function(static) {
5 var outputPluginIndex = _.debounce(function() {
6 pluginIndex.update();
7 }, 1000);
8
9 var plugins = {},
10 pluginIndex = static.createFile('plugins/index.html');
11
12 //process markdown files with handlebars then markdown
13 static.file(/plugins\/.+\.md$/, function(file) {
14 file.transform(function(buffer, next) {
15 var content = buffer.toString(),
16 title = /^#\s*(.*?)\s*#*$/m.exec(content);
17 plugins[file.target] = {
18 title: title && title[1],
19 summary: ''
20 };
21 next(buffer);
22 });
23 });
24 static.file(/\.(md|markdown)$/, function(file) {
25 file.transform('markdown');
26 file.changeExtensionTo('html');
27 });
28
29 //process handlebars files with handlebars
30 static.file(/\.handlebars$/, function(file) {
31 file.transform('handlebars');
32 file.changeExtensionTo('html');
33 });
34
35 //process stylus files with stylus
36 static.file(/\.styl$/, function(file) {
37 file.transform('stylus');
38 file.changeExtensionTo('css');
39 });
40
41 //copy assets to assets folder in target
42 static.file(/^assets\//, function(file) {
43 file.write('assets');
44 });
45
46 //copy scripts to scripts folder in target
47 static.file(/^scripts\//, function(file) {
48 file.write('scripts');
49 });
50
51 //copy styles to styles folder in target
52 static.file(/^styles\//, function(file) {
53 file.write('styles');
54 });
55
56 //copy pages to root
57 static.file(/\.(?:html?|md)$/, function(file) {
58 //add package.json values to scope of file
59 for (var key in static.package) {
60 file.set(key, static.package[key]);
61 }
62
63 //save to root of target directory
64 file.write('.', '');
65
66 //wrap pages in template
67 file.transform(function(buffer, next) {
68 next(file.render('templates/index.handlebars', {
69 yield: buffer
70 }));
71 });
72 });
73
74 static.file(/(plugins\/)?.+\.md$/, function(file) {
75 file.$(function(window) {
76 //set the title of the page to be the first h1 in the body if present
77 var title, title_element = window.$('.container h1:first')[0];
78 if (title_element) {
79 title = title_element.innerHTML;
80 window.$('title').html(title)
81 }
82
83 // Update all markdown links to point to the html equivalent
84 // NOTE: forEach is not supported by the return from $ in this context
85 var anchors = window.$('a[href$=".md"]');
86 for (var i = 0; i < anchors.length; i++) {
87 var anchor = anchors[i];
88 anchor.href = anchor.getAttribute('href').replace(/\.md$/, '.html');
89 }
90 });
91 });
92
93 static.file(/[^\/]+\.md$/, function(file) {
94 file.$(function(window) {
95 //assign ids
96 window.$('h2, h3, h4').each(function() {
97 this.id = this.innerHTML.replace(/[\s\.]+/g,'-').toLowerCase();
98 var len = window.$('[id^="' + this.id + '"]').length;
99 if (len > 1) {
100 this.id += '-' + len;
101 }
102 });
103
104 //build toc
105 var toc_html = '<ul>';
106 window.$('h2').each(function() {
107 toc_html += '<li class="header"><a href="#' + this.id + '">' + this.innerHTML + '</a>';
108 var signatures = window.$(this).nextUntil('h2').filter('h3');
109 if (signatures.length) {
110 toc_html += '<ul class="sub">';
111 signatures.each(function(){
112 toc_html += '<li><a href="#' + this.id + '">' + this.innerHTML.split(/\</).shift() + '</a></li>'
113 });
114 toc_html += '</ul></li>';
115 }
116 });
117 toc_html += '</ul>';
118
119 //append toc
120 window.$('#sidebar').html(toc_html);
121 });
122 });
123
124 static.file(/plugins\/.+$/, function(file) {
125 file.$(function(window) {
126 var toc_html = '<ul>'
127 + '<li class="header"><a href="index.html">Plugins</a>'
128 + '<ul class="sub">';
129 _.chain(plugins).keys().sort().each(function(name) {
130 toc_html += '<li><a href="' + file.get('root') + name + '">' + plugins[name].title + '</a></li>';
131 });
132 toc_html += '</ul></li>'
133 + '<li class="header"><a href="' + file.get('root') + '">Home</a></li>'
134 + '</ul>';
135 window.$('#sidebar').html(toc_html);
136
137 // Collect the intro info (ignoring the index file)
138 var plugin = plugins[file.target];
139 if (plugin) {
140 plugin.summary = '';
141 window.$('#introduction').nextUntil('h2').each(function() {
142 plugin.summary += this.outerHTML;
143 });
144 }
145 });
146 });
147
148 static.file(/plugins\/index\.html$/, function(file) {
149 file.$(function(window) {
150 var pluginList = '';
151 for (var name in plugins) {
152 pluginList += '<h2><a href="' + file.get('root') + name + '">' + plugins[name].title + '</a></h2>' + plugins[name].summary;
153 }
154
155 window.$('.container').html(pluginList);
156 });
157 });
158
159 static.file(/plugins\/.+\.md$/, function(file) {
160 file.on('write', function(file, next) {
161 outputPluginIndex()
162 next();
163 });
164 });
165};