Permalink
Browse files

Add task for compiling docs to json and then to html.

  • Loading branch information...
1 parent b8f0565 commit edbfbf38a0375051a86ca98ba59e9b46ec6ab182 @timrwood timrwood committed Jun 6, 2014
Showing with 130 additions and 169 deletions.
  1. +1 −1 Gruntfile.js
  2. +1 −0 package.json
  3. +40 −1 pages/moment/docs.hbs
  4. +0 −71 source/data/docs.js
  5. +56 −0 tasks/docs.js
  6. +32 −96 tasks/html.js
View
@@ -7,5 +7,5 @@ module.exports = function(grunt) {
grunt.config('clean.default', 'build/*');
- grunt.registerTask('default', ['clean', 'img', 'assemble', 'js', 'css']);
+ grunt.registerTask('default', ['clean', 'img', 'html', 'js', 'css']);
};
View
@@ -7,6 +7,7 @@
"author": "Tim Wood <washwithcare@gmail.com> (http://timwoodcreates.com/)",
"devDependencies": {
"assemble": "^0.4.37",
+ "assemble-yaml": "^0.2.1",
"async": "0.2.5",
"grunt": "^0.4.5",
"grunt-autoprefixer": "^0.7.3",
View
@@ -2,4 +2,43 @@
title : Docs
layout : moment-base.hbs
intro : Documentation
----
+---
+
+<nav class="subnav">
+ {{#each docs}}
+ <div class="section">
+ <a href='#/{{ slug }}/'>{{ title }}</a>
+ <div class="dropdown">
+ {{#each items }}
+ <a href="#/{{ slug }}/">{{ title }}</a>
+ {{/each}}
+ </div>
+ </div>
+ {{/each}}
+</nav>
+
+<div class="docs">
+ {{#each docs}}
+ <article class="section">
+ <a class="target" name="/{{ slug }}/"></a>
+ <h2><a href='#/{{ slug }}/'><span class="hash">#</span>{{ title }}</a></h2>
+ </article>
+ {{#each items }}
+ <article class="method">
+ <a class="target" name="/{{ slug }}/"></a>
+ <h3>
+ <a href='#/{{ slug }}/'><span class="hash">#</span>{{ title }}</a>
+ <span>{{ version }}</span>
+ </h3>
+ <div class="prose">
+ {{#if signature}}
+ <pre>{{ signature }}</pre>
+ {{/if}}
+ {{#markdown}}
+ {{{body}}}
+ {{/markdown}}
+ </div>
+ </article>
+ {{/each}}
+ {{/each}}
+</div>
View
@@ -1,71 +0,0 @@
-var fs = require('fs'),
- highlight = require("highlight.js").highlight,
- marked = require('marked');
-
-marked.setOptions({
- highlight : function (code, lang) {
- switch(lang) {
- case "html":
- return highlight("xml", code).value;
- case "javascript":
- return highlight("javascript", code).value;
- }
- return code;
- }
-});
-
-function filename(fn) {
- fn.unshift(process.cwd());
- var output = fn.join('/');
- return output;
-}
-
-
-/*********************************************
- Docs
-*********************************************/
-
-
-function machineFriendly(str) {
- return str.replace(/[^a-z0-9]+/gi, '-').toLowerCase();
-}
-
-function normalizeDocs(folder) {
- var method, section, i, j,
- docs = require('./' + folder + '.json');
- for (i = 0; i < docs.length; i++) {
- section = docs[i];
- section.title = section.title || section.key;
- section.key = machineFriendly(section.key);
- section.body = docsAtPath(filename(['source', folder, section.key + '.md']));
-
- for (j = 0; j < section.methods.length; j++) {
- method = section.methods[j];
- method.title = method.title || method.key;
- method.key = machineFriendly(method.key);
-
- if (method.signature && method.signature.join) {
- method.signature = method.signature.join('\n');
- }
-
- method.body = docsAtPath(filename(['source', folder, section.key, method.key + '.md']));
- }
- }
- return docs;
-}
-
-function docsAtPath(p) {
- if (fs.existsSync(p)) {
- return marked(fs.readFileSync(p, 'utf8'));
- }
- return '';
-}
-
-module.exports = {
- core : function () {
- return normalizeDocs('core_docs');
- },
- timezone : function () {
- return normalizeDocs('timezone_docs');
- }
-};
View
@@ -0,0 +1,56 @@
+var path = require('path'),
+ yfm = require('assemble-yaml');
+
+module.exports = function (grunt) {
+ grunt.registerTask('docs', function (root) {
+ if (root !== 'moment' && root !== 'moment-timezone') {
+ throw new Error("Missing project name.\n\nUse `grunt docs:moment` or `grunt docs:moment-timezone`");
+ }
+
+ var files = grunt.file.expand(path.join('docs', root, '**/*.md')),
+ groups = [],
+ cache = {};
+
+ files.forEach(function (file) {
+ var data = yfm.extract(file),
+ groupPath = path.basename(path.dirname(file)),
+ itemPath = path.basename(file, '.md'),
+ groupSlug = groupPath.replace(/^\d\d-/, ''),
+ itemSlug = itemPath.replace(/^\d\d-/, ''),
+ group = cache[groupSlug],
+ item = data.context;
+
+ if (!group) {
+ group = cache[groupSlug] = {
+ slug : groupSlug,
+ items : []
+ };
+ groups.push(group);
+ }
+
+ if (itemPath === '00-intro') {
+ group.title = item.title;
+ }
+
+ group.items.push(item);
+
+ item.body = data.content;
+ item.slug = groupSlug + '/' + itemSlug;
+ item.edit = 'https://github.com/moment/momentjs.com/blob/master/docs/' + groupPath + '/' + itemPath + '.md';
+ });
+
+ grunt.file.write('.temp/docs/' + root + '.json', JSON.stringify(groups, null, 4));
+ });
+
+
+
+ grunt.config('watch.docs-moment', {
+ files: ['docs/moment/**/*.md'],
+ tasks: ['docs:moment']
+ });
+
+ grunt.config('watch.docs-moment-timezone', {
+ files: ['docs/moment-timezone/**/*.md'],
+ tasks: ['docs:moment-timezone']
+ });
+};
View
@@ -1,93 +1,3 @@
-// var fs = require('fs'),
-// library = require('../source/data/library'),
-// langs = require('../source/data/lang'),
-// docs = require('../source/data/docs'),
-// zones = require('../source/data/zones'),
-// swig = require('swig'),
-// moment = require("moment");
-//
-// swig.init({
-// allowErrors: true,
-// autoescape: true,
-// cache: false,
-// encoding: 'utf8',
-// filters: {},
-// root: filename(['source', 'templates']),
-// tags: {},
-// extensions: {},
-// tzOffset: 0
-// });
-//
-//
-// function filename(fn) {
-// fn.unshift(process.cwd());
-// var output = fn.join('/');
-// return output;
-// }
-//
-// function main(grunt, cb) {
-// var data = {
-// library : library,
-// core_docs : docs.core(),
-// timezone_docs : docs.timezone(),
-// zones : zones,
-// langs : langs,
-// cachebust : moment().format(),
-// global : 'global',
-// localhost : grunt.option('localhost') && process.cwd() || '',
-// nav : function (path) {
-// if (grunt.option('localhost')) {
-// return process.cwd() + path + "/index.html";
-// } else {
-// return path + "/";
-// }
-// }
-// };
-// library.ready(function(){
-// render(data, cb, grunt);
-// });
-// }
-//
-// function renderSingle(src, dst, data, cb, grunt) {
-// var template = swig.compileFile(src),
-// html = template.render(data);
-//
-// grunt.file.write(filename([dst]), html);
-// console.log('Wrote', dst);
-// cb();
-// }
-//
-// function render(data, cb, grunt) {
-// renderSingle('core-home.html', 'build/index.html', data, function(){
-// renderSingle('core-test.html', 'build/test/index.html', data, function(){
-// renderSingle('core-docs.html', 'build/docs/index.html', data, function(){
-// renderSingle('timezone-home.html', 'build/timezone/index.html', data, function(){
-// renderSingle('timezone-test.html', 'build/timezone/test/index.html', data, function(){
-// renderSingle('timezone-data.html', 'build/timezone/data/index.html', data, function(){
-// renderSingle('timezone-docs.html', 'build/timezone/docs/index.html', data, cb, grunt);
-// }, grunt);
-// }, grunt);
-// }, grunt);
-// }, grunt);
-// }, grunt);
-// }, grunt);
-// }
-//
-// module.exports = function(grunt) {
-// grunt.registerTask('html', 'Build HTML', function() {
-// main(grunt, this.async());
-// });
-//
-// grunt.config('watch.html', {
-// files: [
-// "source/**/*.md",
-// "source/**/*.html",
-// "source/**/*.json"
-// ],
-// tasks: ["html"]
-// });
-// };
-
var path = require('path');
function rename (dest, src) {
@@ -102,12 +12,15 @@ function rename (dest, src) {
module.exports = function(grunt) {
grunt.config('assemble', {
- options: {
- layoutdir : 'pages/layout',
- helpers : ['pages/helpers/**/*.js' ],
- marked : { sanitize: false }
+ 'options' : {
+ layoutdir : 'pages/layout',
+ helpers : ['pages/helpers/**/*.js' ],
+ marked : { sanitize: false }
},
- moment : {
+ 'moment' : {
+ options : {
+ docs : grunt.file.readJSON('.temp/docs/moment.json')
+ },
files: [{
expand : true,
rename : rename,
@@ -116,7 +29,7 @@ module.exports = function(grunt) {
src : '**/*.{hbs,md}'
}]
},
- momentTimezone : {
+ 'moment-timezone' : {
files: [{
expand : true,
rename : rename,
@@ -126,4 +39,27 @@ module.exports = function(grunt) {
}]
}
});
+
+ grunt.registerTask('html', [
+ 'docs:moment',
+ 'docs:moment-timezone',
+ 'assemble:moment',
+ 'assemble:moment-timezone'
+ ]);
+
+ grunt.config('watch.html-moment', {
+ files: [
+ '.temp/docs/moment.json',
+ 'pages/**/*.{hbs,json,md}'
+ ],
+ tasks: ['assemble:moment']
+ });
+
+ grunt.config('watch.html-moment-timezone', {
+ files: [
+ '.temp/docs/moment-timezone.json',
+ 'pages/**/*.{hbs,json,md}'
+ ],
+ tasks: ['assemble:moment-timezone']
+ });
};

0 comments on commit edbfbf3

Please sign in to comment.