Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
language: node_js
node_js:
- "0.4"
- "0.6"
- "0.8"
- "0.10"
53 changes: 32 additions & 21 deletions share/previews.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,38 @@ var bodyEl = document.getElementsByTagName('body')[0];
var pseudos = [ 'link', 'visited', 'hover', 'active', 'focus', 'target',
'enabled', 'disabled', 'checked' ];
var pseudoRe = new RegExp(":((" + pseudos.join(")|(") + "))", "gi");
var processedPseudoClasses = toArray(document.styleSheets)
.map(function(ss) {
return toArray(ss.cssRules)
.filter(function(rule) {
// Keep only rules with pseudo classes.
return rule.selectorText && rule.selectorText.match(pseudoRe);
})
.map(function(rule) {
// Replace : with . and encoded :
return rule.cssText.replace(pseudoRe, ".\\3A $1");
})
.join('');
})
.join('');
if (processedPseudoClasses.length) {
// Add a new style element with the processed pseudo class styles.
var styleEl = document.createElement('style');
styleEl.innerText = processedPseudoClasses;
var oldStyleEl = document.getElementsByTagName('style')[0];
oldStyleEl.parentNode.insertBefore(styleEl, oldStyleEl);
}
var applyPseudoStyles = function () {
var processedPseudoClasses = toArray(document.styleSheets)
.map(function(ss) {
return toArray(ss.cssRules)
.filter(function(rule) {
// Keep only rules with pseudo classes.
return rule.selectorText && rule.selectorText.match(pseudoRe);
})
.map(function(rule) {
// Replace : with . and encoded :
return rule.cssText.replace(pseudoRe, ".\\3A $1");
})
.join('');
})
.join('');
if (processedPseudoClasses.length) {
// Add a new style element with the processed pseudo class styles.
var headTag = document.head || document.getElementsByTagName('head')[0],
styleEl = document.createElement('style');
if (styleEl.styleSheet) {
styleEl.styleSheet.cssText = processedPseudoClasses;
} else {
styleEl.appendChild(document.createTextNode(processedPseudoClasses));
}
headTag.appendChild(styleEl);
}
};

// running processCSS immediately causes nothing to be done as document.styleSheets
// doesn't have all the CSS yet. This timeout is enough time for document.styleSheets
// to be updated and the pseudo styles to be added
setTimeout(applyPseudoStyles, 1);

// Resizing
// ========
Expand Down
41 changes: 38 additions & 3 deletions styledocco.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,44 @@ marked.setOptions({ sanitize: false, gfm: true });
// the beginning of lines.
var commentRegexs = {
single: /^\/\//, // Single line comments for Sass, Less and Stylus
multiStartIgnore: /^\/\*+\!/, // Multi line ignore documentation
multiStart: /^\/\*/,
multiEnd: /\*\//
};

// Hashing identification from national header
var hashCode = function(str) {
var hash = 0, i, chr, len;
if (str.length == 0) return hash;
for (i = 0, len = str.length; i < len; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};

// Make an URL slug from `str`.
var slugify = function(str) {
return encodeURIComponent(
var slug = encodeURIComponent(
str.trim().toLowerCase()
.replace(/[^\w ]+/g,'')
.replace(/ +/g,'-')
);
return slug.length <= 1 ? hashCode(str) : slug;
};

// Check if a string is code or a comment (and which type of comment).
var checkType = function(str) {
// Checking ignoring multi line comments
if (str.match(commentRegexs.multiStartIgnore) && str.match(commentRegexs.multiEnd)) {
return 'ignore';
// Treat multi start and end on same row as a single line comment.
if (str.match(commentRegexs.multiStart) && str.match(commentRegexs.multiEnd)) {
} else if (str.match(commentRegexs.multiStart) && str.match(commentRegexs.multiEnd)) {
return 'single';
// Checking for ignoring multi line comments first to avoid matching single line
} else if (str.match(commentRegexs.multiStartIgnore)) {
return 'multistartignore';
// Checking for multi line comments first to avoid matching single line
// comment symbols inside multi line blocks.
} else if (str.match(commentRegexs.multiStart)) {
Expand Down Expand Up @@ -65,6 +85,17 @@ var separate = function(css) {
var docs, code, line, blocks = [];
while (lines.length) {
docs = code = '';
// A multi line ignoring comment starts here, add lines until comment ends.
if (lines.length && checkType(lines[0]) === 'multistartignore') {
while (lines.length) {
line = lines.shift();
if (checkType(line) === 'multiend') break;
}
}
// First check for any single line ignore comments.
while (lines.length && checkType(lines[0]) === 'ignore') {
line = lines.shift();
}
// First check for any single line comments.
while (lines.length && checkType(lines[0]) === 'single') {
docs += formatDocs(lines.shift());
Expand All @@ -80,7 +111,11 @@ var separate = function(css) {
while (lines.length && (checkType(lines[0]) === 'code' || checkType(lines[0]) === 'multiend')) {
code += formatCode(lines.shift());
}
blocks.push({ docs: docs, code: code });

// Remove empty line start in document
if(trimNewLines(docs).length) {
blocks.push({ docs: docs, code: code });
}
}
return blocks;
};
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/comments-ignore.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
body {
background: white;
}

/*!
input[type="text"] {
background: white;
}
*/
3 changes: 3 additions & 0 deletions test/fixtures/comments-ignore.css.blocks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[

]
3 changes: 3 additions & 0 deletions test/fixtures/comments-ignore.css.sections.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[

]
3 changes: 0 additions & 3 deletions test/fixtures/comments.css.blocks.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
[
{
"docs": "",
"code": "body {\n background: white;\n}\n\n"
}, {
"docs": "\ninput[type=\"text\"] {\n background: white;\n}\n\n",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the rationale for removing this code?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"code": "\n"
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/comments.css.sections.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"title": "",
"slug": "",
"docs": "<p>input[type=&quot;text&quot;] {\n background: white;\n}</p>",
"code": "body {\n background: white;\n}"
"code": ""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. Why remove this?

}
]
2 changes: 2 additions & 0 deletions test/fixtures/normal.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ body {
color: black;
}

/*! Ignore comment */

/* An example of a reasonbly well written CSS file that you would find in "the wild", but without any Markdown. */

p {
Expand Down
3 changes: 0 additions & 3 deletions test/fixtures/normal.css.blocks.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
[
{
"docs": "",
"code": "body {\n background: white; /* Set background */\n /* Good contrast */\n color: black;\n}\n\n"
}, {
"docs": " An example of a reasonbly well written CSS file that you would find in \"the wild\", but without any Markdown. \n",
"code": "\np {\n margin: 0; /* Reset margin */\n}\n\n"
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/normal.css.sections.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"title": "",
"slug": "",
"docs": "<p> An example of a reasonbly well written CSS file that you would find in &quot;the wild&quot;, but without any Markdown. </p>",
"code": "body {\n background: white; /* Set background */\n /* Good contrast */\n color: black;\n}\n\n\np {\n margin: 0; /* Reset margin */\n}"
"code": "p {\n margin: 0; /* Reset margin */\n}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question as before. I'm wondering how the app will behave with a file that contains the equivalent css to generate this token.

}
]
2 changes: 1 addition & 1 deletion test/getFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if (!global.describe) { process.exit(); }
describe('getFiles', function() {
it('Runs the callback with an array of files in the 2nd arg', function(done) {
getFiles(__dirname, function(err, n) {
expect(n.length).to.equal(26);
expect(n.length).to.equal(29);
done();
})
});
Expand Down
2 changes: 1 addition & 1 deletion test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if (typeof window === 'undefined') {
fixturePath = 'fixtures/';
}
var fixtures = [ 'asterisk.css', 'code.css', 'comments.css', 'invalid.css',
'normal.css', 'sections.css', 'structured.css' ];
'normal.css', 'sections.css', 'structured.css', 'comments-ignore.css' ];

exports["Documentation and code blocks"] = function(test) {
fixtures.forEach(function(fix) {
Expand Down