Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pattern builder #1538

Closed
wants to merge 20 commits into from
Closed
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
35 changes: 35 additions & 0 deletions components/prism-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,41 @@ var _ = _self.Prism = {
callback(env);
}
}
},

patterns: {

/**
* Replaces all placeholders of the form `<<name>>` in `basePattern` with `replacements[name]`.
*
* @param {string|RegExp} basePattern the pattern in which all placeholder are to be replaced. Placeholders are
* not allowed to be inside character sets and cannot be preceded by an unescaped backslash.
* @param {Object.<string, string|RegExp>|Array.<string|RegExp>} replacements the name-replacement-pairs.
* Replacements are not allowed to contain backreferences or capturing groups.
* @returns {RegExp} A new regular expression with the flags of `basePattern` and its source with all
* placeholders replaced.
* @example
* build(/^foo<<bar>>$/m, { bar: /b[Aa]r/ }) == /^foo(?:b[Aa]r)$/m
* build(/<<0>>foo<<0>>?/, [ /bar/ ]) == /(?:bar)foo(?:bar)?/
*/
build: function build(basePattern, replacements) {
var placeholder = /<<([\w-]+)>>/g;
var source = basePattern.source || basePattern;
var flags = basePattern.flags;
if (flags === undefined) {
flags = basePattern.exec ? basePattern.toString().match(/[igmuy]*$/)[0] : '';
}

build.test && build.test(basePattern, replacements, placeholder, source, flags);

source = source.replace(placeholder, function (m, name) {
var replacement = replacements[name];
return '(?:' + (replacement.source || replacement) + ')';
});

return RegExp(source, flags);
}

}
};

Expand Down
2 changes: 1 addition & 1 deletion components/prism-core.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions components/prism-haml.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
}
};

var filter_pattern = '((?:^|\\r?\\n|\\r)([\\t ]*)):{{filter_name}}(?:(?:\\r?\\n|\\r)(?:\\2[\\t ]+.+|\\s*?(?=\\r?\\n|\\r)))+';
var filter_pattern = /((?:^|\r?\n|\r)([\t ]*)):<<filter>>(?:(?:\r?\n|\r)(?:\2[\t ]+.+|\s*?(?=\r?\n|\r)))+/.source;

// Non exhaustive list of available filters and associated languages
var filters = [
Expand All @@ -136,7 +136,7 @@
filter = typeof filter === 'string' ? {filter: filter, language: filter} : filter;
if (Prism.languages[filter.language]) {
all_filters['filter-' + filter.filter] = {
pattern: RegExp(filter_pattern.replace('{{filter_name}}', filter.filter)),
pattern: Prism.patterns.build(filter_pattern, filter),
lookbehind: true,
inside: {
'filter-name': {
Expand All @@ -151,4 +151,4 @@

Prism.languages.insertBefore('haml', 'filter', all_filters);

}(Prism));
}(Prism));
2 changes: 1 addition & 1 deletion components/prism-haml.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/prism-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

var pattern = suffixTypes[contentType] ? getSuffixPattern(contentType) : contentType;
options[contentType] = {
pattern: RegExp('(content-type:\\s*' + pattern + '[\\s\\S]*?)(?:\\r?\\n|\\r){2}[\\s\\S]*', 'i'),
pattern: Prism.patterns.build(/(content-type:\s*<<0>>[\s\S]*?)(?:\r?\n|\r){2}[\s\S]*/i, [pattern]),
lookbehind: true,
inside: {
rest: httpLanguages[contentType]
Expand Down
Loading