Skip to content

Commit

Permalink
Update to new ES6 text API
Browse files Browse the repository at this point in the history
  • Loading branch information
eviltrout committed Jul 13, 2016
1 parent 6f537e2 commit 032a37f
Show file tree
Hide file tree
Showing 3 changed files with 316 additions and 108 deletions.
102 changes: 102 additions & 0 deletions .eslintrc
@@ -0,0 +1,102 @@
{
"env": {
"jasmine": true,
"node": true,
"mocha": true,
"browser": true,
"builtin": true
},
ecmaVersion: 7,
"globals":
{"Ember":true,
"jQuery":true,
"$":true,
"RSVP":true,
"Discourse":true,
"Em":true,
"Handlebars":true,
"I18n":true,
"bootbox":true,
"module":true,
"moduleFor":true,
"moduleForComponent":true,
"Pretender":true,
"sandbox":true,
"controllerFor":true,
"test":true,
"ok":true,
"not":true,
"expect":true,
"equal":true,
"visit":true,
"andThen":true,
"click":true,
"currentPath":true,
"currentRouteName":true,
"currentURL":true,
"fillIn":true,
"keyEvent":true,
"triggerEvent":true,
"count":true,
"exists":true,
"visible":true,
"invisible":true,
"asyncRender":true,
"selectDropdown":true,
"asyncTestDiscourse":true,
"fixture":true,
"find":true,
"sinon":true,
"moment":true,
"start":true,
"_":true,
"alert":true,
"containsInstance":true,
"deepEqual":true,
"notEqual":true,
"define":true,
"require":true,
"requirejs":true,
"hasModule":true,
"Blob":true,
"File":true},
"rules": {
"block-scoped-var": 2,
"dot-notation": 0,
"eqeqeq": [
2,
"allow-null"
],
"guard-for-in": 2,
"no-bitwise": 2,
"no-caller": 2,
"no-cond-assign": 0,
"no-debugger": 2,
"no-empty": 0,
"no-eval": 2,
"no-extend-native": 2,
"no-extra-parens": 0,
"no-irregular-whitespace": 2,
"no-iterator": 2,
"no-loop-func": 2,
"no-multi-str": 2,
"no-new": 2,
"no-plusplus": 0,
"no-proto": 2,
"no-script-url": 2,
"no-sequences": 2,
"no-shadow": 2,
"no-undef": 2,
"no-unused-vars": 2,
"no-with": 2,
"no-this-before-super": 2,
"semi": 2,
"strict": 0,
"valid-typeof": 2,
"wrap-iife": [
2,
"inside"
]
},
"parser": "babel-eslint"
}
102 changes: 102 additions & 0 deletions assets/javascripts/lib/discourse-markdown/vbulletin-bbcode.js.es6
@@ -0,0 +1,102 @@
import { registerOption } from 'pretty-text/pretty-text';
import { builders } from 'pretty-text/engines/discourse-markdown/bbcode';

registerOption((siteSettings, opts) => opts.features["vbulletin-bbcode"] = true);

function replaceFontColor (text) {
while (text !== (text = text.replace(/\[color=([^\]]+)\]((?:(?!\[color=[^\]]+\]|\[\/color\])[\S\s])*)\[\/color\]/ig, function (match, p1, p2) {
return `<font color='${p1}'>${p2}</font>`;
})));
return text;
}

function replaceFontSize (text) {
while (text !== (text = text.replace(/\[size=([^\]]+)\]((?:(?!\[size=[^\]]+\]|\[\/size\])[\S\s])*)\[\/size\]/ig, function (match, p1, p2) {
return `<font size='${p1}'>${p2}</font>`;
})));
return text;
}

function replaceFontFace (text) {
while (text !== (text = text.replace(/\[font=([^\]]+)\]((?:(?!\[font=[^\]]+\]|\[\/font\])[\S\s])*)\[\/font\]/ig, function (match, p1, p2) {
return `<font face='${p1}'>${p2}</font>`;
})));
return text;
}

export function setup(helper) {

helper.whiteList([
'div.highlight',
'div.sepquote',
'span.smallfont',
'font[color=*]',
'font[size=*]',
'font[face=*]',
]);

helper.whiteList({
custom(tag, name, value) {
if (tag === 'span' && name === 'style') {
return /^font-size:.*$/.exec(value);
}

if (tag === 'div' && name === 'style') {
return /^text-align:(center|left|right)$/.exec(value);
}
}
});

const { register, replaceBBCode, rawBBCode, replaceBBCodeParamsRaw } = builders(helper);

replaceBBCode("small", contents => ['span', {'style': 'font-size:x-small'}].concat(contents));
replaceBBCode("highlight", contents => ['div', {'class': 'highlight'}].concat(contents));

["left", "center", "right"].forEach(direction => {
replaceBBCode(direction, contents => ['div', {'style': "text-align:" + direction}].concat(contents));
});

replaceBBCode('edit', contents => ['div', {'class': 'sepquote'}, ['span', { 'class': 'smallfont' }, "Edit:"], ['br'], ['br']].concat(contents));

replaceBBCode('ot', contents => ['div', {'class': 'sepquote'}, ['span', { 'class': 'smallfont' }, "Off Topic:"], ['br'], ['br']].concat(contents));

replaceBBCode('indent', contents => ['blockquote', ['div'].concat(contents)]);

helper.addPreProcessor(replaceFontColor);
helper.addPreProcessor(replaceFontSize);
helper.addPreProcessor(replaceFontFace);

register("aname", (contents, param) => ['a', {'name': param, 'data-bbcode': true}].concat(contents));
register("jumpto", (contents, param) => ['a', {href: "#" + param, 'data-bbcode': true}].concat(contents));
register("rule", (contents, param) => ['div', { 'style': "margin: 6px 0; height: 0; border-top: 1px solid " + contents + "; margin: auto; width: " + param }]);

rawBBCode("noparse", contents => contents);
rawBBCode('fphp', contents => ['a', {href: "http://www.php.net/manual-lookup.php?function=" + contents, 'data-bbcode': true}, contents]);
replaceBBCodeParamsRaw("fphp", (param, contents) => ['a', {href: "http://www.php.net/manual-lookup.php?function=" + param, 'data-bbcode': true}, contents]);

rawBBCode('google', contents => ['a', {href: "http://www.google.com/search?q=" + contents, 'data-bbcode': true}, contents]);

helper.replaceBlock({
start: /\[list=?(\w)?\]([\s\S]*)/igm,
stop: /\[\/list\]/igm,
emitter(blockContents, matches) {
const contents = matches[1] ? ["ol", { "type": matches[1] }] : ["ul"];

if (blockContents.length) {
blockContents.forEach(bc => {
const lines = bc.split(/\n/);
lines.forEach(line => {
if (line.indexOf("[*]") === 0) {
const li = this.processInline(line.slice(3));
if (li) {
contents.push(["li"].concat(li));
}
}
});
});
}

return contents;
}
});
}

0 comments on commit 032a37f

Please sign in to comment.