Merge pull request #1059 from glowing-bear/codify-greediness

Improve codify filter
This commit is contained in:
Lorenz Hübschle-Schneider 2019-08-20 14:57:44 +02:00 committed by GitHub
commit c0fb485b14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 4 deletions

View File

@ -2114,6 +2114,11 @@ button.close:hover {
font-weight: bold; font-weight: bold;
} }
code {
background-color: #444;
color: #fff;
}
/* */ /* */
/* Mobile layout */ /* Mobile layout */
/* */ /* */

View File

@ -336,7 +336,7 @@ npm run build-electron-{windows, darwin, linux}</pre>
<td class="prefix"><span ng-class="::{'repeated-prefix': bufferline.prefixtext==bufferlines[$index-1].prefixtext}"><a ng-click="addMention(bufferline)"><span class="hidden-bracket" ng-if="::(bufferline.showHiddenBrackets)">&lt;</span><span ng-repeat="part in ::bufferline.prefix" ng-class="::part.classes" ng-bind="::part.text|prefixlimit:25"></span><span class="hidden-bracket" ng-if="::(bufferline.showHiddenBrackets)">&gt;</span></a></span></td><!-- <td class="prefix"><span ng-class="::{'repeated-prefix': bufferline.prefixtext==bufferlines[$index-1].prefixtext}"><a ng-click="addMention(bufferline)"><span class="hidden-bracket" ng-if="::(bufferline.showHiddenBrackets)">&lt;</span><span ng-repeat="part in ::bufferline.prefix" ng-class="::part.classes" ng-bind="::part.text|prefixlimit:25"></span><span class="hidden-bracket" ng-if="::(bufferline.showHiddenBrackets)">&gt;</span></a></span></td><!--
--><td class="message"><!-- --><td class="message"><!--
--><div ng-repeat="metadata in ::bufferline.metadata" plugin data="::metadata"></div><!-- --><div ng-repeat="metadata in ::bufferline.metadata" plugin data="::metadata"></div><!--
--><span ng-repeat="part in ::bufferline.content" class="text" ng-class="::part.classes.concat(['line-' + part.$$hashKey.replace(':','_')])" ng-bind-html="::part.text | conditionalLinkify:part.classes.includes('cof-chat_host') | codify | DOMfilter:'irclinky' | DOMfilter:'emojify':settings.enableJSEmoji | DOMfilter:'inlinecolour' | DOMfilter:'latexmath':('.line-' + part.$$hashKey.replace(':','_')):settings.enableMathjax"></span> --><span ng-repeat="part in ::bufferline.content" class="text" ng-class="::part.classes.concat(['line-' + part.$$hashKey.replace(':','_')])" ng-bind-html="::part.text | conditionalLinkify:part.classes.includes('cof-chat_host') | DOMfilter:'codify' | DOMfilter:'irclinky' | DOMfilter:'emojify':settings.enableJSEmoji | DOMfilter:'inlinecolour' | DOMfilter:'latexmath':('.line-' + part.$$hashKey.replace(':','_')):settings.enableMathjax"></span>
</td> </td>
</tr> </tr>
<tr class="readmarker" ng-if="activeBuffer().lastSeen==$index"> <tr class="readmarker" ng-if="activeBuffer().lastSeen==$index">

View File

@ -239,9 +239,14 @@ weechat.filter('prefixlimit', function() {
weechat.filter('codify', function() { weechat.filter('codify', function() {
return function(text) { return function(text) {
var re = /`(.+?)`/g; // The groups of this regex are:
return text.replace(re, function(match, code) { // 1. Start of line or space, to prevent codifying weird`stuff` like this
var rr = '<span class="hidden-bracket">`</span><code>' + code + '</code><span class="hidden-bracket">`</span>'; // 2. Opening single or triple backticks (not 2, not more than 3)
// 3. The code block, does not start with another backtick, non-greedy expansion
// 4. The closing backticks, identical to group 2
var re = /(^|\s)(```|`)([^`].*?)\2/g;
return text.replace(re, function(match, ws, open, code) {
var rr = ws + '<span class="hidden-bracket">' + open + '</span><code>' + code + '</code><span class="hidden-bracket">' + open + '</span>';
return rr; return rr;
}); });
}; };

View File

@ -100,6 +100,8 @@ describe('Filters', function() {
it('should codify single snippets', inject(function(codifyFilter) { it('should codify single snippets', inject(function(codifyFilter) {
expect(codifyFilter('z `foo` z')).toEqual('z <span class="hidden-bracket">`</span><code>foo</code><span class="hidden-bracket">`</span> z'); expect(codifyFilter('z `foo` z')).toEqual('z <span class="hidden-bracket">`</span><code>foo</code><span class="hidden-bracket">`</span> z');
expect(codifyFilter('z `a` z')).toEqual('z <span class="hidden-bracket">`</span><code>a</code><span class="hidden-bracket">`</span> z');
expect(codifyFilter('z ```foo``` z')).toEqual('z <span class="hidden-bracket">```</span><code>foo</code><span class="hidden-bracket">```</span> z');
})); }));
it('should codify multiple snippets', inject(function(codifyFilter) { it('should codify multiple snippets', inject(function(codifyFilter) {
@ -115,5 +117,20 @@ describe('Filters', function() {
})); }));
it('should not codify double backticks', inject(function(codifyFilter) {
expect(codifyFilter('some ``non-code``')).toEqual('some ``non-code``');
}));
it('should not codify pseudo-fancy quotes', inject(function(codifyFilter) {
expect(codifyFilter('some ``fancy qoutes\'\'')).toEqual('some ``fancy qoutes\'\'');
}));
it('should not codify stuff in the middle of a word or URL', inject(function(codifyFilter) {
expect(codifyFilter('https://foo.bar/`wat`')).toEqual('https://foo.bar/`wat`');
expect(codifyFilter('Weird`ness`')).toEqual('Weird`ness`');
}));
}); });
}); });