Skip to content

Commit

Permalink
Типографика на лету
Browse files Browse the repository at this point in the history
  • Loading branch information
hcodes committed Sep 13, 2015
1 parent 8ab2dda commit bbb1f50
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 11 deletions.
29 changes: 25 additions & 4 deletions src/main.js
Expand Up @@ -3,11 +3,13 @@
* @param {Object} [prefs]
* @param {string} [prefs.lang] Language rules
* @param {string} [prefs.mode] HTML entities as: 'default' - UTF-8, 'digit' -  , 'name' -  
* @param {boolean} [prefs.live] Live mode
* @param {string|string[]} [prefs.enable] Enable rules
* @param {string|string[]} [prefs.disable] Disable rules
*/
function Typograf(prefs) {
this._prefs = typeof prefs === 'object' ? prefs : {};
this._prefs.live = this._prefs.live || false;

this._settings = {};
this._enabledRules = {};
Expand All @@ -31,6 +33,7 @@ function Typograf(prefs) {
* @param {Function} rule.handler Processing function
* @param {number} [rule.index] Sorting index for rule
* @param {boolean} [rule.disabled] Rule is disabled by default
* @param {boolean} [rule.live] Live mode
* @param {Object} [rule.settings] Settings for rule
* @return {Typograf} this
*/
Expand Down Expand Up @@ -223,7 +226,12 @@ Typograf.prototype = {
innerRulesForQueue = {},
mode = typeof prefs.mode === 'undefined' ? this._prefs.mode : prefs.mode,
iterator = function(rule) {
var rlang = rule._lang;
var rlang = rule._lang,
live = this._prefs.live;

if((live === true && rule.live === false) || (live === false && rule.live === true)) {
return;
}

if((rlang === 'common' || rlang === lang) && this.enabled(rule.name)) {
this._onBeforeRule && this._onBeforeRule(text);
Expand Down Expand Up @@ -267,6 +275,8 @@ Typograf.prototype = {
}

text = this._utfication(text);
executeRulesForQueue('utf');

executeRulesForQueue();
text = this._modification(text, mode);

Expand Down Expand Up @@ -358,9 +368,7 @@ Typograf.prototype = {
return commonLetter === langLetter || !lang ? commonLetter : commonLetter + langLetter;
},
_fixLineEnd: function(text) {
return text
.replace(/\r\n/g, '\n') // Windows
.replace(/\r/g, '\n'); // MacOS
return text.replace(/\r\n/g, '\n'); // Windows
},
_prepareRule: function(rule) {
var name = rule.name,
Expand Down Expand Up @@ -405,6 +413,19 @@ Typograf.prototype = {
},
_rules: [],
_innerRules: [],
_getRule: function(name) {
var rule = null;
this._rules.some(function(item) {
if(item.name === name) {
rule = item;
return true;
}

return false;
});

return rule;
},
_initSafeTags: function() {
this._safeTags = [
['<!--', '-->'],
Expand Down
8 changes: 8 additions & 0 deletions src/rules/common/nbsp/replaceNbsp.js
@@ -0,0 +1,8 @@
Typograf.rule({
name: 'common/nbsp/replaceNbsp',
queue: 'utf',
live: true,
handler: function(text) {
return text.replace(/\u00A0/g, ' ');
}
});
4 changes: 4 additions & 0 deletions src/rules/common/nbsp/replaceNbsp.json
@@ -0,0 +1,4 @@
{
"en": "Replacing non-breaking space in the ordinary",
"ru": "Замена неразрывного пробела на обычный"
}
9 changes: 9 additions & 0 deletions src/rules/common/nbsp/replaceNbsp.spec.js
@@ -0,0 +1,9 @@
tests.push(['common/nbsp/replaceNbsp', [
[
'Флойд\u00A0Мэйуэзер\u00A0одержал\u00A049-ю\u00A0победу\u00A0и\u00A0объявил\u00A0о\u00A0завершении карьеры',
'Флойд Мэйуэзер одержал 49-ю победу и объявил о завершении карьеры',
{
live: true
}
]
]]);
1 change: 1 addition & 0 deletions src/rules/common/punctuation/exclamation.js
@@ -1,6 +1,7 @@
Typograf.rule({
name: 'common/punctuation/exclamation',
index: 1150,
live: false,
handler: function(text) {
return text
.replace(/(^|[^!])!{2}($|[^!])/, '$1!$2')
Expand Down
1 change: 1 addition & 0 deletions src/rules/common/space/trimRight.js
@@ -1,6 +1,7 @@
Typograf.rule({
name: 'common/space/trimRight',
index: 535,
live: false,
handler: String.prototype.trimRight ? function(text) {
return text.trimRight();
} : /* istanbul ignore next */ function(text) {
Expand Down
16 changes: 10 additions & 6 deletions test/test.rules.js
Expand Up @@ -62,13 +62,12 @@ describe('inner rules', function() {
});

describe('rules', function() {
var itTypograf = new Typograf();
tests.forEach(function(elem) {
var name = elem[0];
it(name, function() {
itTypograf.disable('*').enable(name);

elem[1].forEach(function(as) {
var itTypograf = new Typograf(as[2]);
itTypograf.disable('*').enable(name);
var result = itTypograf.execute(as[0], {lang: getLang(name, as)});
assert.equal(result, as[1], as[0] + ' → ' + as[1]);
});
Expand All @@ -77,15 +76,20 @@ describe('rules', function() {
});

describe('rules, double execute', function() {
var itTypograf = new Typograf();
tests.forEach(function(elem) {
var name = elem[0];
it(name, function() {
itTypograf.disable('*').enable(name);

elem[1].forEach(function(as) {
var itTypograf = new Typograf(as[2]);
itTypograf.disable('*').enable(name);

var result = itTypograf.execute(as[0], {lang: getLang(name, as)});
assert.equal(result, as[1], as[0] + ' → ' + as[1]);

if(!itTypograf._getRule(name).disabled) {
result = itTypograf.execute(result, {lang: getLang(name, as)});
assert.equal(result, as[1], as[0] + ' → ' + as[1]);
}
});
});
});
Expand Down
1 change: 0 additions & 1 deletion test/test.smoke.common.js
Expand Up @@ -19,7 +19,6 @@ var assert = require('chai').assert,
[' <pre \n class="red"> Hello world!! </pre> ', '<pre \n class="red"> Hello world!! </pre>'],
[' <pre2> Hello world!! <pre> Hello world!! </pre> </pre2> ', '<pre2> Hello world! <pre> Hello world!! </pre> </pre2>'],
['1\r\n2\r\n3', '1\n2\n3'], // Windows
['1\r\r2\r3', '1\n\n2\n3'], // MacOS
[0, '0'],
[null, 'null'],
['', ''],
Expand Down

0 comments on commit bbb1f50

Please sign in to comment.