2 * Bootstrap Grunt task for parsing Less docstrings
3 * http://getbootstrap.com
4 * Copyright 2014-2015 Twitter, Inc.
5 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
10 var Markdown = require('markdown-it');
12 function markdown2html(markdownString) {
13 var md = new Markdown();
15 // the slice removes the <p>...</p> wrapper output by Markdown processor
16 return md.render(markdownString.trim()).slice(3, -5);
22 //== This is a normal heading, which starts a section. Sections group variables together.
23 //## Optional description for the heading
25 //=== This is a subheading.
27 //** Optional description for the following variable. You **can** use Markdown in descriptions to discuss `<html>` stuff.
30 //-- This is a heading for a section whose variables shouldn't be customizable
32 All other lines are ignored completely.
36 var CUSTOMIZABLE_HEADING = /^[/]{2}={2}(.*)$/;
37 var UNCUSTOMIZABLE_HEADING = /^[/]{2}-{2}(.*)$/;
38 var SUBSECTION_HEADING = /^[/]{2}={3}(.*)$/;
39 var SECTION_DOCSTRING = /^[/]{2}#{2}(.+)$/;
40 var VAR_ASSIGNMENT = /^(@[a-zA-Z0-9_-]+):[ ]*([^ ;][^;]*);[ ]*$/;
41 var VAR_DOCSTRING = /^[/]{2}[*]{2}(.+)$/;
43 function Section(heading, customizable) {
44 this.heading = heading.trim();
45 this.id = this.heading.replace(/\s+/g, '-').toLowerCase();
46 this.customizable = customizable;
47 this.docstring = null;
48 this.subsections = [];
51 Section.prototype.addSubSection = function (subsection) {
52 this.subsections.push(subsection);
55 function SubSection(heading) {
56 this.heading = heading.trim();
57 this.id = this.heading.replace(/\s+/g, '-').toLowerCase();
61 SubSection.prototype.addVar = function (variable) {
62 this.variables.push(variable);
65 function VarDocstring(markdownString) {
66 this.html = markdown2html(markdownString);
69 function SectionDocstring(markdownString) {
70 this.html = markdown2html(markdownString);
73 function Variable(name, defaultValue) {
75 this.defaultValue = defaultValue;
76 this.docstring = null;
79 function Tokenizer(fileContent) {
80 this._lines = fileContent.split('\n');
81 this._next = undefined;
84 Tokenizer.prototype.unshift = function (token) {
85 if (this._next !== undefined) {
86 throw new Error('Attempted to unshift twice!');
91 Tokenizer.prototype._shift = function () {
92 // returning null signals EOF
93 // returning undefined means the line was ignored
94 if (this._next !== undefined) {
95 var result = this._next;
96 this._next = undefined;
99 if (this._lines.length <= 0) {
102 var line = this._lines.shift();
104 match = SUBSECTION_HEADING.exec(line);
105 if (match !== null) {
106 return new SubSection(match[1]);
108 match = CUSTOMIZABLE_HEADING.exec(line);
109 if (match !== null) {
110 return new Section(match[1], true);
112 match = UNCUSTOMIZABLE_HEADING.exec(line);
113 if (match !== null) {
114 return new Section(match[1], false);
116 match = SECTION_DOCSTRING.exec(line);
117 if (match !== null) {
118 return new SectionDocstring(match[1]);
120 match = VAR_DOCSTRING.exec(line);
121 if (match !== null) {
122 return new VarDocstring(match[1]);
124 var commentStart = line.lastIndexOf('//');
125 var varLine = commentStart === -1 ? line : line.slice(0, commentStart);
126 match = VAR_ASSIGNMENT.exec(varLine);
127 if (match !== null) {
128 return new Variable(match[1], match[2]);
133 Tokenizer.prototype.shift = function () {
135 var result = this._shift();
136 if (result === undefined) {
143 function Parser(fileContent) {
144 this._tokenizer = new Tokenizer(fileContent);
147 Parser.prototype.parseFile = function () {
150 var section = this.parseSection();
151 if (section === null) {
152 if (this._tokenizer.shift() !== null) {
153 throw new Error('Unexpected unparsed section of file remains!');
157 sections.push(section);
161 Parser.prototype.parseSection = function () {
162 var section = this._tokenizer.shift();
163 if (section === null) {
166 if (!(section instanceof Section)) {
167 throw new Error('Expected section heading; got: ' + JSON.stringify(section));
169 var docstring = this._tokenizer.shift();
170 if (docstring instanceof SectionDocstring) {
171 section.docstring = docstring;
173 this._tokenizer.unshift(docstring);
175 this.parseSubSections(section);
180 Parser.prototype.parseSubSections = function (section) {
182 var subsection = this.parseSubSection();
183 if (subsection === null) {
184 if (section.subsections.length === 0) {
185 // Presume an implicit initial subsection
186 subsection = new SubSection('');
187 this.parseVars(subsection);
192 section.addSubSection(subsection);
195 if (section.subsections.length === 1 && !section.subsections[0].heading && section.subsections[0].variables.length === 0) {
196 // Ignore lone empty implicit subsection
197 section.subsections = [];
201 Parser.prototype.parseSubSection = function () {
202 var subsection = this._tokenizer.shift();
203 if (subsection instanceof SubSection) {
204 this.parseVars(subsection);
207 this._tokenizer.unshift(subsection);
211 Parser.prototype.parseVars = function (subsection) {
213 var variable = this.parseVar();
214 if (variable === null) {
217 subsection.addVar(variable);
221 Parser.prototype.parseVar = function () {
222 var docstring = this._tokenizer.shift();
223 if (!(docstring instanceof VarDocstring)) {
224 this._tokenizer.unshift(docstring);
227 var variable = this._tokenizer.shift();
228 if (variable instanceof Variable) {
229 variable.docstring = docstring;
232 this._tokenizer.unshift(variable);
237 module.exports = Parser;