Skip to content

Commit

Permalink
docs.js
Browse files Browse the repository at this point in the history
  • Loading branch information
vladikoff committed Nov 3, 2024
1 parent 131b745 commit 7f5cc11
Showing 1 changed file with 61 additions and 78 deletions.
139 changes: 61 additions & 78 deletions tasks/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
* Licensed under the MIT license.
*/

"use strict";
'use strict';

module.exports = function (grunt) {
var fs = require("fs");
var pug = require("pug");
var highlighter = require("highlight.js");
var docs = require("./lib/docs").init(grunt);
var marked = require("marked");

var fs = require('fs');
var pug = require('pug');
var highlighter = require('highlight.js');
var docs = require('./lib/docs').init(grunt);
var marked = require('marked');

/**
* Custom task to generate grunt documentation
*/
grunt.registerTask("docs", "Compile Grunt Docs to HTML", function () {
grunt.registerTask('docs', 'Compile Grunt Docs to HTML', function () {
var done = this.async();

/**
Expand All @@ -40,44 +41,37 @@ module.exports = function (grunt) {
var items = [];

// read the Home.md of the wiki, extract the section links
var lines = fs
.readFileSync(base + "Home.md")
.toString()
.split(/\r?\n/);
var lines = fs.readFileSync(base + 'Home.md').toString().split(/\r?\n/);
for (l in lines) {
var line = lines[l];

// choose a section of the file
if (line === section) {
rMode = true;
} else if (line.substring(0, 2) === "##") {
// end of section
} else if (line.substring(0, 2) === '##') { // end of section
rMode = false;
}

if (rMode && line.length > 0) {
var item = line
.replace(/#/g, "")
.replace("]]", "")
.replace("- [[", "");
var item = line.replace(/#/g, '').replace(']]', '').replace('* [[', '');
var url = item;

if (item[0] === " ") {
if (item[0] === ' ') {
// TODO: clean this up...
if (iconClass) {
items.push({
name: item.substring(1, item.length),
icon: iconClass,
icon: iconClass
});
} else {
items.push({
name: item.substring(1, item.length),
name: item.substring(1, item.length)
});
}
} else {
items.push({
name: item,
url: url.replace(/ /g, "-").toLowerCase(),
url: url.replace(/ /g, '-').toLowerCase()
});
}
}
Expand All @@ -89,139 +83,126 @@ module.exports = function (grunt) {
* Generate grunt guides documentation
*/
function generateGuides() {
grunt.log.ok("Generating Guides...");
grunt.log.ok('Generating Guides...');

// API Docs
var sidebars = [];
var names = grunt.file.expand({ cwd: base }, [
"*.md",
"!grunt*.md",
"!README.md",
]);
var names = grunt.file.expand({cwd: base}, ['*.md', '!grunt*.md', '!README.md']);

sidebars[0] = getSidebarSection("## Documentation", "icon-file-text");
sidebars[1] = getSidebarSection("### Advanced");
sidebars[2] = getSidebarSection("### Community");
sidebars[3] = getSidebarSection("### Migration guides");
sidebars[0] = getSidebarSection('## Documentation', 'icon-file-text');
sidebars[1] = getSidebarSection('### Advanced');
sidebars[2] = getSidebarSection('### Community');
sidebars[3] = getSidebarSection('### Migration guides');

names.forEach(function (name) {
var title = name.replace(/-/g, " ").replace(".md", "");
var segment = name
.replace(/ /g, "-")
.replace(".md", "")
.toLowerCase();

var title = name.replace(/-/g, ' ').replace('.md', '');
var segment = name.replace(/ /g, '-').replace('.md', '').toLowerCase();
var src = base + name;
var dest =
"build/docs/" + name.replace(".md", "").toLowerCase() + ".html";
var dest = 'build/docs/' + name.replace('.md', '').toLowerCase() + '.html';

grunt.file.copy(src, dest, {
process: function (src) {
try {
var file = "src/tmpl/docs.pug";
var file = 'src/tmpl/docs.pug';
var templateData = {
page: "docs",
page: 'docs',
rootSidebar: true,
pageSegment: segment,
title: title,
content: docs.anchorFilter(marked(docs.wikiAnchors(src))),
sidebars: sidebars,
sidebars: sidebars
};
return pug.compile(grunt.file.read(file), { filename: file })(
templateData
);
return pug.compile(grunt.file.read(file), {filename: file})(templateData);
} catch (e) {
grunt.log.error(e);
grunt.fail.warn("Pug failed to compile.");
grunt.fail.warn('Pug failed to compile.');
}
},
}
});
});
grunt.log.ok("Created " + names.length + " files.");
grunt.log.ok('Created ' + names.length + ' files.');
}


/**
* Generate grunt API documentation
*/
function generateAPI() {
grunt.log.ok("Generating API Docs...");
grunt.log.ok('Generating API Docs...');
// API Docs
var sidebars = [];
var names = grunt.file.expand({ cwd: base }, [
"grunt.*.md",
"!*utils*",
]);
var names = grunt.file.expand({cwd: base}, ['grunt.*.md', '!*utils*']);

names = names.map(function (name) {
return name.substring(0, name.length - 3);
});

// the default api page is special
names.push("grunt");
names.push('grunt');
// TODO: temporary store for these
names.push("Inside-Tasks");
names.push("Exit-Codes");
names.push('Inside-Tasks');
names.push('Exit-Codes');

// get docs sidebars
sidebars[0] = getSidebarSection("## API", "icon-cog");
sidebars[1] = getSidebarSection("### Other");
sidebars[0] = getSidebarSection('## API', 'icon-cog');
sidebars[1] = getSidebarSection('### Other');

names.forEach(function (name) {
var src = base + name + ".md";
var dest = "build/api/" + name.toLowerCase() + ".html";
var src = base + name + '.md';
var dest = 'build/api/' + name.toLowerCase() + '.html';
grunt.file.copy(src, dest, {
process: function (src) {
try {
var file = "src/tmpl/docs.pug";
var file = 'src/tmpl/docs.pug';
var templateData = {
page: "api",
page: 'api',
pageSegment: name.toLowerCase(),
title: name.replace(/-/g, " "),
title: name.replace(/-/g, ' '),
content: docs.anchorFilter(marked(docs.wikiAnchors(src))),
sidebars: sidebars,
sidebars: sidebars
};

return pug.compile(grunt.file.read(file), { filename: file })(
templateData
);
return pug.compile(grunt.file.read(file), {filename: file})(templateData);
} catch (e) {
grunt.log.error(e);
grunt.fail.warn("Pug failed to compile.");
grunt.fail.warn('Pug failed to compile.');
}
},
}
});
});
grunt.log.ok("Created " + names.length + " files.");
grunt.log.ok('Created ' + names.length + ' files.');
}

// Set default marked options
marked.setOptions({
gfm: true,
anchors: true,
base: "/",
base: '/',
pedantic: false,
sanitize: false,
// callback for code highlighter
highlight: function (code, lang) {
highlight: function(code, lang) {
// No language specified, no syntax highlighting.
if (!lang) {
return code;
}
// Handle common abbreviations.
var langMap = {
js: "javascript",
shell: "bash",
html: "xml",
js: 'javascript',
shell: 'bash',
html: 'xml'
};
if (lang in langMap) {
lang = langMap[lang];
}
try {
return highlighter.highlight(lang, code).value;
} catch (error) {
grunt.log.error("[lang: %s] %s", lang, error.message);
return "ERROR";
grunt.log.error('[lang: %s] %s', lang, error.message);
return 'ERROR';
}
},
}
});

// grunt guides - wiki articles that are not part of the grunt api
Expand All @@ -233,6 +214,8 @@ module.exports = function (grunt) {
}

// In the future this will run multiple times for different versions.
generateDocs("node_modules/grunt-docs/");
generateDocs('node_modules/grunt-docs/');

});

};

0 comments on commit 7f5cc11

Please sign in to comment.