Skip to content

Commit

Permalink
Improve handling of literals and add language-specific options.
Browse files Browse the repository at this point in the history
Now any language can specify what may count as a literal (e.g. strings, regexes etc),
whose contents therefore should not be counted when matching for comments.

Closes jbt#73
  • Loading branch information
jbt committed Oct 12, 2014
1 parent 89799a3 commit 2a20ffa
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,12 @@ Docker.prototype.parseSections = function(data, language){
var line = codeLines[i];

// Only match against parts of the line that don't appear in strings
var matchable = line.replace(/(["'])((?:[^\\\1]|(?:\\\\)*?\\[^\\])*?)\1/g,'');
var matchable = line.replace(/(["'])((?:[^\\\1]|(?:\\\\)*?\\[^\\])*?)\1/g,'$1$1');
if(params.literals) {
params.literals.forEach(function(replace){
matchable = matchable.replace(replace[0], replace[1]);
});
}

if(params.multiLine){
// If we are currently in a multiline comment, behave differently
Expand Down Expand Up @@ -839,6 +844,7 @@ Docker.prototype.languageParams = function(filename, filedata){
// * `commentsIgnore`: Regex of comments to strip completely (don't even doc)
// * `jsDoc`: Whether to parse multiline comments as jsDoc
// * `type`: Either `'code'` (default) or `'markdown'` - format of page to render
// * `literals`: Array of match/replace pairs for literals to ignore for comment matching
//
// I'm not even going to pretend that this is an exhaustive list of
// languages that Pygments can understand. This is just a list of the most
Expand All @@ -849,13 +855,19 @@ Docker.prototype.languages = {
javascript: {
extensions: [ 'js' ],
executables: [ 'node' ],
comment: '//', multiLine: [ /\/\*\*?/, /\*\// ], commentsIgnore: /^\s*\/\/=/, jsDoc: true
comment: '//', multiLine: [ /\/\*\*?/, /\*\// ], commentsIgnore: /^\s*\/\/=/, jsDoc: true,
literals: [
[ /\/(?![\*\/])((?:[^\\\/]|(?:\\\\)*?\\[^\\])*?)\//g, '/./' ]
]
},
coffeescript: {
extensions: [ 'coffee' ],
names: [ 'cakefile' ],
executables: [ 'coffee' ],
comment: '#', multiLine: [ /^\s*#{3}\s*$/m, /^\s*#{3}\s*$/m ], jsDoc: true
comment: '#', multiLine: [ /^\s*#{3}\s*$/m, /^\s*#{3}\s*$/m ], jsDoc: true,
literals: [
[ /\/(?![\*\/])((?:[^\\\/]|(?:\\\\)*?\\[^\\])*?)\//g, '/./' ]
]
},
livescript: {
extensions: [ 'ls' ],
Expand Down

0 comments on commit 2a20ffa

Please sign in to comment.