Skip to content

Commit

Permalink
v3.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hcodes committed Sep 13, 2015
1 parent 0464bbf commit c55f429
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 157 deletions.
24 changes: 24 additions & 0 deletions CHANGELOG.ru.md
@@ -1,5 +1,29 @@
# Changelog

## v3.3.0
### Типографирование на лету
```JavaScript
var tp = new Typograf({lang: 'ru', live: true});`
```

У правил появилось дополнительное свойство `live` #133, #139.
```JavaScript
// Добавляем правило
Typograf.rule({
name: 'common/other/emoji',
live: false,
handler: function (text) {
return text.replace(/:-\)/g, '\uD83D\uDE0A');
}
});
```
`live: true` — правило работает только в режиме типографирования на лету.
`live: false` — правило не работает на лету.

### Доработки
- Исправлены сложные случаи с кавычками и HTML-тегами #131
- Не типографировать экранированные HTML-теги #138

## v3.2.0
Доработки:
- Расстановка тире в месяцах `в апреле-мае`
Expand Down
26 changes: 16 additions & 10 deletions README.md
Expand Up @@ -8,14 +8,16 @@

[![Dependency Status](https://img.shields.io/david/typograf/typograf.svg?style=flat)](https://david-dm.org/typograf/typograf) [![devDependency Status](https://img.shields.io/david/dev/typograf/typograf.svg?style=flat)](https://david-dm.org/typograf/typograf#info=devDependencies)

## Типограф
Помогает автоматически расставлять неразрывные пробелы, исправлять мелкие опечатки, приводить кавычки к правильному виду, заменять дефисы на тире в нужных местах и многое другое.

[Типограф в действии](https://typograf.github.io) с [мобильной версией](https://typograf.github.io/mobile.html)

## Черты:
+ гибкость и расширяемость;
+ UTF-8;
+ кроссплатформенность;
+ кроссбраузерность;
+ мультиязычность;
+ UTF-8;
+ поддержка Node.js;
+ [типографирование на лету](https://github.com/typograf/jquery-typograf);
+ TDD.
Expand Down Expand Up @@ -108,16 +110,12 @@ var tp = new Typograf({lang: 'ru'});
tp.setting('common/nbsp/beforeShortLast', 'lengthLastWord', 5);
```

### Добавить правило
### Добавить простое правило
```JavaScript
Typograf.rule({
// Язык/группа/правило
name: 'common/other/parampampam',
// Очередность выполнения правил, чем меньше индекс, тем раньше выполнится правило
index: 2000,
// Функция обработки правила
handler: function(text) {
return text.replace(/parampampam/g, 'tryam');
name: 'common/other/emoji',
handler: function (text) {
return text.replace(/:-\)/g, '\uD83D\uDE0A');
}
});
```
Expand All @@ -137,6 +135,14 @@ var tpDigit = new Typograf({lang: 'ru', mode: 'digit'});
tpDigit.execute('...'); // …
```

### Типографика на лету
```JavaScript
var tp = new Typograf({lang: 'ru', live: true});
tp.execute('"Мир"');
```
[Подробнее](https://github.com/typograf/jquery-typograf)


### Сжатие с UglifyJS
Если `typograf.js` сжимается вместе с другими js-файлами в `UglifyJS`,
то необходимо использовать [опцию](http://lisperator.net/uglifyjs/compress) `ascii_only: false`, иначе типограф будет работать некорректно.
Expand Down
2 changes: 1 addition & 1 deletion bower.json
@@ -1,6 +1,6 @@
{
"name": "typograf",
"version": "3.2.0",
"version": "3.3.0",
"description": "Typograph in JavaScript",
"ignore": [
"**/.*",
Expand Down
101 changes: 73 additions & 28 deletions dist/typograf.js
Expand Up @@ -19,11 +19,13 @@ if(typeof define === 'function' && define.amd) {
* @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 @@ -47,6 +49,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 @@ -146,28 +149,24 @@ Typograf._quot = function(text, settings) {
reQuotes = new RegExp(quotes, 'g'),
reFirstQuot = new RegExp('^(\s)?(' + quotes + ')', 'g'),
reOpeningTag = new RegExp('(^|\\s)' + quotes + privateLabel, 'g'),
reClosingTag = new RegExp(privateLabel + quotes + '([\s!?.:;#*,]|$)', 'g');
reClosingTag = new RegExp(privateLabel + quotes + '([\s!?.:;#*,]|$)', 'g'),
count = 0;

text = text
.replace(reQuotes, '"')
.replace(reQuotes, function() {
count++;
return '"';
})
.replace(reL, lquot + '$1') // Opening quote
.replace(reR, '$1' + rquot + '$2') // Closing quote
.replace(reOpeningTag, '$1' + lquot + privateLabel)
.replace(reClosingTag, privateLabel + rquot + '$1')
.replace(reOpeningTag, '$1' + lquot + privateLabel) // Opening quote and tag
.replace(reClosingTag, privateLabel + rquot + '$1') // Tag and closing quote
.replace(reFirstQuot, '$1' + lquot)
.replace(new RegExp('(^|\\w|\\s)' + rquot + lquot, 'g'),
'$1' + lquot + lquot); // Fixed for the case »« at the beginning of the text

if(lquot2 && rquot2) {
if(lquot === lquot2 && rquot === rquot2) {
return text
// ««Энергия» Синергия» -> «Энергия» Синергия»
.replace(new RegExp(lquot + lquot, 'g'), lquot)
// «Энергия «Синергия»» -> «Энергия «Синергия»
.replace(new RegExp(rquot + rquot, 'g'), rquot);
} else {
return Typograf._innerQuot(text, settings);
}
if(lquot2 && rquot2 && count % 2 === 0) {
return Typograf._innerQuot(text, settings);
}

return text;
Expand Down Expand Up @@ -204,13 +203,13 @@ Typograf._innerQuot = function(text, settings) {
} else if(letter === rquot) {
if(level <= -1) {
level = 0;
}

bufText.push(closingQuotes[level]);

level--;
if(level < -1) {
level = -1;
bufText.push(openingQuotes[level]);
} else {
bufText.push(closingQuotes[level]);
level--;
if(level < -1) {
level = -1;
}
}
} else {
bufText.push(letter);
Expand Down Expand Up @@ -243,7 +242,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 @@ -278,7 +282,7 @@ Typograf.prototype = {
rulesForQueue[q].push(rule);
}, this);

this._isHTML = text.search(/<[a-z!]/i) !== -1;
this._isHTML = text.search(/(<\/?[a-z]|<!|&[lg]t;)/i) !== -1;

executeRulesForQueue('start');

Expand All @@ -287,6 +291,8 @@ Typograf.prototype = {
}

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

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

Expand Down Expand Up @@ -378,9 +384,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 @@ -425,6 +429,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 Expand Up @@ -474,7 +491,10 @@ Typograf.prototype = {
return this._hiddenSafeTags[match];
},
_hideHTMLTags: function(text) {
return text.replace(/<[a-z\/][^]*?>/gi, this._pasteLabel);
return text
.replace(/<\/?[a-z][^]*?>/gi, this._pasteLabel) // Tags
.replace(/&lt;\/?[a-z][^]*?&gt;/gi, this._pasteLabel) // Escaping tags
.replace(/&[gl]t;/gi, this._pasteLabel);
},
_showSafeTags: function(text) {
var label = Typograf._privateLabel,
Expand Down Expand Up @@ -1066,6 +1086,15 @@ Typograf.rule({

})();

Typograf.rule({
name: 'common/nbsp/replaceNbsp',
queue: 'utf',
live: true,
handler: function(text) {
return text.replace(/\u00A0/g, ' ');
}
});

Typograf.rule({
name: 'common/number/fraction',
index: 1120,
Expand Down Expand Up @@ -1147,6 +1176,7 @@ Typograf.rule({
Typograf.rule({
name: 'common/punctuation/exclamation',
index: 1150,
live: false,
handler: function(text) {
return text
.replace(/(^|[^!])!{2}($|[^!])/, '$1!$2')
Expand Down Expand Up @@ -1251,6 +1281,7 @@ Typograf.rule({
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 Expand Up @@ -1744,7 +1775,21 @@ Typograf.rule({
Typograf.rule({
name: 'ru/punctuation/quot',
index: 700,
handler: Typograf._quot,
handler: function(text, settings) {
var lquot = settings.lquot,
rquot = settings.rquot;

text = Typograf._quot.call(this, text, settings);
if(lquot === settings.lquot2 && rquot === settings.rquot2) {
return text
// ««Энергия» Синергия» -> «Энергия» Синергия»
.replace(new RegExp(lquot + lquot, 'g'), lquot)
// «Энергия «Синергия»» -> «Энергия «Синергия»
.replace(new RegExp(rquot + rquot, 'g'), rquot);
}

return text;
},
settings: {
lquot: '«',
rquot: '»',
Expand Down
2 changes: 1 addition & 1 deletion dist/typograf.min.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions dist/typograf.titles.js
Expand Up @@ -47,6 +47,10 @@ Typograf.titles = {
"en": "Replace non-breaking space to normal space in tags nowrap and nobr",
"ru": "Заменять нераз. пробел на обычный пробел в тегах nowrap и nobr"
},
"common/nbsp/replaceNbsp": {
"en": "Replacing non-breaking space in the ordinary",
"ru": "Замена неразрывного пробела на обычный"
},
"common/number/fraction": {
"common": "1/2 → ½, 1/4 → ¼, 3/3 → ¾"
},
Expand Down
4 changes: 4 additions & 0 deletions dist/typograf.titles.json
Expand Up @@ -47,6 +47,10 @@
"en": "Replace non-breaking space to normal space in tags nowrap and nobr",
"ru": "Заменять нераз. пробел на обычный пробел в тегах nowrap и nobr"
},
"common/nbsp/replaceNbsp": {
"en": "Replacing non-breaking space in the ordinary",
"ru": "Замена неразрывного пробела на обычный"
},
"common/number/fraction": {
"common": "1/2 → ½, 1/4 → ¼, 3/3 → ¾"
},
Expand Down

0 comments on commit c55f429

Please sign in to comment.