Permalink
Browse files

Allowing for nesting of markdown comments inside of source files. Tha…

…t way not all the markdown needs to be left justified all the way (which breaks the code flow.)
  • Loading branch information...
1 parent 4a14b49 commit c1450fdc8eba4818d063305b4035223f5aab253e @iros committed Feb 29, 2012
Showing with 34 additions and 8 deletions.
  1. +34 −8 lib/jodoc-lib.js
View
@@ -10,18 +10,44 @@ var external_regex = /(\<a)\s+(href="(?:http[s]?|mailto|ftp))/g,
// input: "code"
// output: "comments"
function docker (input) {
+
var strip_code = /\057\*\*(?:.|[\r\n])*?\*\057/gm;
var strip_stars = /(\*\057|\057\*\*)/g;
var strip_extra_whitespace = /([\r\n]+)\s/g;
- var output = [];
- var a;
- while ((a = strip_code.exec(input)) !== null)
- {
- a = a[0];
- a = a.replace(strip_stars,'');
- a = a.replace(strip_extra_whitespace, '$1');
- output.push(a);
+ var beginning_whitespace = /^(\s+)(.*)/;
+ var output = [], preoutput = [];
+ var a, b, lines, line, j;
+
+ // first scan to see how many spaces we have
+ // and clear out the min amout out of every row.
+ while ((b = strip_code.exec(input)) !== null) {
+ var spaces = Infinity, curspaces;
+ b = b[0];
+ b = b.replace(strip_stars,'');
+
+ // sort out the smallest amount of space prefixing each line.
+ lines = b.split("\n");
+ for(j = 0; j < lines.length; j++) {
+ line = lines[j].trim();
+ if (line.length > 0) {
+ curspaces = beginning_whitespace.exec(lines[j])[1].length;
+ if (curspaces < spaces) {
+ spaces = curspaces;
+ }
+ }
+ }
+
+ // now remove spaces from each line above.
+ for(j = 0; j < lines.length; j++) {
+ lines[j] = lines[j].slice(spaces);
+ }
+
+ // put this back together.
+ b = lines.join("\n");
+ b = b.replace(strip_extra_whitespace, '$1');
+ output.push(b);
}
+
return output.join("\n");
}

0 comments on commit c1450fd

Please sign in to comment.