mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
[CS2] Literate CoffeeScript without dependencies (#4509)
* Reimplement `invertLiterate` without any dependency, tracking indentation levels (including inside lists); update literate test files to also check that no tests are skipped * Drop Literate CoffeeScript’s support for executable code blocks inside list items (and also for second paragraphs or blockquotes in list items) * Update Literate CoffeeScript docs to reflect current supported syntax
This commit is contained in:
parent
ae096a331a
commit
0e8feb7ee9
8 changed files with 110 additions and 131 deletions
|
@ -1,8 +1,6 @@
|
|||
// Generated by CoffeeScript 2.0.0-beta1
|
||||
(function() {
|
||||
var buildLocationData, extend, flatten, md, ref, repeat, syntaxErrorToString;
|
||||
|
||||
md = require('markdown-it')();
|
||||
var buildLocationData, extend, flatten, ref, repeat, syntaxErrorToString;
|
||||
|
||||
exports.starts = function(string, literal, start) {
|
||||
return literal === string.substr(start, literal.length);
|
||||
|
@ -28,10 +26,10 @@
|
|||
};
|
||||
|
||||
exports.compact = function(array) {
|
||||
var item, j, len1, results;
|
||||
var i, item, len1, results;
|
||||
results = [];
|
||||
for (j = 0, len1 = array.length; j < len1; j++) {
|
||||
item = array[j];
|
||||
for (i = 0, len1 = array.length; i < len1; i++) {
|
||||
item = array[i];
|
||||
if (item) {
|
||||
results.push(item);
|
||||
}
|
||||
|
@ -65,10 +63,10 @@
|
|||
};
|
||||
|
||||
exports.flatten = flatten = function(array) {
|
||||
var element, flattened, j, len1;
|
||||
var element, flattened, i, len1;
|
||||
flattened = [];
|
||||
for (j = 0, len1 = array.length; j < len1; j++) {
|
||||
element = array[j];
|
||||
for (i = 0, len1 = array.length; i < len1; i++) {
|
||||
element = array[i];
|
||||
if ('[object Array]' === Object.prototype.toString.call(element)) {
|
||||
flattened = flattened.concat(flatten(element));
|
||||
} else {
|
||||
|
@ -86,10 +84,10 @@
|
|||
};
|
||||
|
||||
exports.some = (ref = Array.prototype.some) != null ? ref : function(fn) {
|
||||
var e, j, len1, ref1;
|
||||
var e, i, len1, ref1;
|
||||
ref1 = this;
|
||||
for (j = 0, len1 = ref1.length; j < len1; j++) {
|
||||
e = ref1[j];
|
||||
for (i = 0, len1 = ref1.length; i < len1; i++) {
|
||||
e = ref1[i];
|
||||
if (fn(e)) {
|
||||
return true;
|
||||
}
|
||||
|
@ -98,22 +96,28 @@
|
|||
};
|
||||
|
||||
exports.invertLiterate = function(code) {
|
||||
var out;
|
||||
var blankLine, i, indented, insideComment, len1, line, listItemStart, out, ref1;
|
||||
out = [];
|
||||
md.renderer.rules = {
|
||||
code_block: function(tokens, idx, options, env, slf) {
|
||||
var i, j, len1, line, lines, results, startLine;
|
||||
startLine = tokens[idx].map[0];
|
||||
lines = tokens[idx].content.split('\n');
|
||||
results = [];
|
||||
for (i = j = 0, len1 = lines.length; j < len1; i = ++j) {
|
||||
line = lines[i];
|
||||
results.push(out[startLine + i] = line);
|
||||
}
|
||||
return results;
|
||||
blankLine = /^\s*$/;
|
||||
indented = /^[\t ]/;
|
||||
listItemStart = /^(?:\t?| {0,3})(?:[\*\-\+]|[0-9]{1,9}\.)[ \t]/;
|
||||
insideComment = false;
|
||||
ref1 = code.split('\n');
|
||||
for (i = 0, len1 = ref1.length; i < len1; i++) {
|
||||
line = ref1[i];
|
||||
if (blankLine.test(line)) {
|
||||
insideComment = false;
|
||||
out.push(line);
|
||||
} else if (insideComment || listItemStart.test(line)) {
|
||||
insideComment = true;
|
||||
out.push(`# ${line}`);
|
||||
} else if (!insideComment && indented.test(line)) {
|
||||
out.push(line);
|
||||
} else {
|
||||
insideComment = true;
|
||||
out.push(`# ${line}`);
|
||||
}
|
||||
};
|
||||
md.render(code);
|
||||
}
|
||||
return out.join('\n');
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue