jashkenas / docco

Literate Programming can be Quick and Dirty.
http://ashkenas.com/docco/
Other
3.55k stars 571 forks source link

support paragraphs with empty comment #392

Open cancerberoSgx opened 6 years ago

cancerberoSgx commented 6 years ago

Hello, This comments for generating paragraphs work OK:

// paragraph1

// paragraph2

But this code won't work OK:

// paragraph1
//
// paragraph2

And the result will be that in parallel or classic layouts the source code won't be aligned with the text.

Why this is important? because if you are writing multiple paragraphs, most editors allow you to select lots of text and comment it all automatically and editors use the second style (adding a comment for empty lines.

I'm short of time now for a PR, but evertheless I was able to fix the problem in docco.js. Basically I added a group at the end of the lines in the regex and changing a little the condition to detect code lines from text lines in the parser:

@@ -155,7 +155,8 @@
     }
     for (k = 0, len1 = lines.length; k < len1; k++) {
       line = lines[k];
-      if (line.match(lang.commentMatcher) && !line.match(lang.commentFilter)) {
+      var commentMatch = lang.commentMatcher.exec(line)
+      if (commentMatch && commentMatch[1] && !line.match(lang.commentFilter)) {
         if (hasCode) {
           save();
         }
@@ -164,8 +165,10 @@
           save();
         }
       } else {
-        hasCode = true;
-        codeText += line + '\n';
+        if(!commentMatch || commentMatch[1]){
+          hasCode = true;
+          codeText += line + '\n';
+        }  
       }
     }
     save();
@@ -332,7 +335,7 @@
     for (ext in languages) {
       l = languages[ext];
       // Does the line begin with a comment?
-      l.commentMatcher = RegExp(`^\\s*${l.symbol}\\s?`);
+      l.commentMatcher = RegExp(`^\\s*${l.symbol}\\s?([^a.*z]?)`);
       // Ignore [hashbangs](http://en.wikipedia.org/wiki/Shebang_%28Unix%29) and interpolations...
       l.commentFilter = /(^#![\/]|^\s*#\{)/;
     }