Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
hcodes committed Apr 11, 2015
2 parents 79efdea + cf87dcc commit 34a51b1
Show file tree
Hide file tree
Showing 24 changed files with 550 additions and 328 deletions.
4 changes: 3 additions & 1 deletion .jscsrc
Expand Up @@ -8,7 +8,9 @@
"libs",
"node_modules",
"test",
"site"
"site",
"src/start.js",
"src/end.js"
],
"requireCurlyBraces": ["if", "else", "for", "while", "do"],
"requireSpaceAfterKeywords": ["do", "else"],
Expand Down
2 changes: 2 additions & 0 deletions .jshintignore
Expand Up @@ -7,5 +7,7 @@ libs/**
node_modules/**
site/**
src/main.spec.js
src/start.js
src/end.js
test/**
benchmark/**
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -133,6 +133,11 @@ var tpDigit = new Typograf({lang: 'ru', mode: 'digit'});
tpDigit.execute('...'); // …
```

### Сжатие с UglifyJS
Если `typograf.js` сжимается вместе с другими js-файлами в `UglifyJS`,
то необходимо использовать [опцию](http://lisperator.net/uglifyjs/compress) `ascii_only: false`, иначе типограф будет работать некорректно.


## Разработка
`git clone https://github.com/typograf/typograf.git ./typograf`

Expand Down
2 changes: 1 addition & 1 deletion bower.json
@@ -1,6 +1,6 @@
{
"name": "typograf",
"version": "2.9.0",
"version": "2.10.0",
"description": "Typograph in JavaScript",
"bugs": "https://github.com/typograf/typograf/issues/new",
"keywords": [
Expand Down
220 changes: 133 additions & 87 deletions dist/typograf.js
@@ -1,4 +1,18 @@
/*! Typograf | © 2014 Denis Seleznev | https://github.com/typograf/typograf/ */
/*! Typograf | © 2015 Denis Seleznev | https://github.com/typograf/typograf/ */

(function(root, factory) {

if(typeof define === 'function' && define.amd) {
define('typograf', [], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.Typograf = factory();
}

}(this, function() {

'use strict';

/**
* @constructor
Expand All @@ -12,7 +26,10 @@ function Typograf(prefs) {
this._settings = {};
this._enabledRules = {};

this._replaceLabel = this._replaceLabel.bind(this);
this._pasteLabel = this._pasteLabel.bind(this);
this._initSafeTags();

this._rules.forEach(this._prepareRule, this);
}

Expand Down Expand Up @@ -120,7 +137,7 @@ Typograf._quot = function(text, settings) {
text = text
.replace(reL1, rquot2 + '$1' + rquot)
.replace(reR1, lquot + '$1' + lquot2);

if(text.search(new RegExp(lquot + '|' + rquot)) === -1) {
text = text
.replace(reL2, lquot)
Expand Down Expand Up @@ -365,7 +382,7 @@ Typograf.prototype = {

this._safeTags.forEach(function(tag) {
var re = new RegExp(tag[0] + '[^]*?' + tag[1], 'gi');
text = text.replace(re, this._pasteLabel.bind(this));
text = text.replace(re, this._pasteLabel);
}, this);

return this._hideHTMLTags(text);
Expand All @@ -384,11 +401,11 @@ Typograf.prototype = {
return this._hiddenSafeTags[match];
},
_hideHTMLTags: function(text) {
return text.replace(/<[a-z\/][^]*?>/gi, this._pasteLabel.bind(this));
return text.replace(/<[a-z\/][^]*?>/gi, this._pasteLabel);
},
_showSafeTags: function(text) {
for(var i = 0; i < this._safeTags.length; i++) {
text = text.replace(/\uDBFFtf\d+\uDBFF/g, this._replaceLabel.bind(this));
text = text.replace(/\uDBFFtf\d+\uDBFF/g, this._replaceLabel);
if(text.search(/\uDBFFtf\d/) === -1) {
break;
}
Expand Down Expand Up @@ -434,11 +451,6 @@ Typograf.prototype = {
}
};

/* istanbul ignore else */
if(typeof exports === 'object') {
module.exports = Typograf;
}

Typograf.prototype.entities = [];

// http://www.w3.org/TR/html4/sgml/entities
Expand Down Expand Up @@ -844,25 +856,34 @@ Typograf.rule({
sortIndex: 2010,
func: function(text) {
var prefix = '(http|https|ftp|telnet|news|gopher|file|wais)://',
pureUrl = '([a-zA-Z0-9\/\\n+-=%&:_.~?]+[a-zA-Z0-9#+]*)',
pureUrl = '([a-zA-Z0-9\/+-=%&:_.~?]+[a-zA-Z0-9#+]*)',
re = new RegExp(prefix + pureUrl, 'g');

return text.replace(re, function($0, $1, $2) {
var url = $2,
fullUrl = $1 + '://' + $2,
return text.replace(re, function($0, protocol, path) {
path = path
.replace(/([^\/]+\/?)(\?|#)$/, '$1') // Remove ending ? and #
.replace(/^([^\/]+)\/$/, '$1'); // Remove ending /

if(protocol === 'http') {
path = path.replace(/^([^\/]+)(:80)([^\d]|\/|$)/, '$1$3'); // Remove 80 port
} else if(protocol === 'https') {
path = path.replace(/^([^\/]+)(:443)([^\d]|\/|$)/, '$1$3'); // Remove 443 port
}

var url = path,
fullUrl = protocol + '://' + path,
firstPart = '<a href="' + fullUrl + '">';

if($1 === 'http') {
url = url
.replace(/^www\./, '')
.replace(/^([^\/]+)\/$/, '$1');
if(protocol === 'http' || protocol === 'https') {
url = url.replace(/^www\./, '');

return firstPart + url + '</a>';
return firstPart + (protocol === 'http' ? url : protocol + '://' + url) + '</a>';
}

return firstPart + fullUrl + '</a>';
});
}
},
enabled: false
});

Typograf.rule({
Expand Down Expand Up @@ -1026,7 +1047,7 @@ Typograf.rule({
var re = new RegExp('(!|;|\\?)([^ \uDBFF\n\t!;?' + this.data('common/quot') + '[])', 'g');
return text
.replace(re, '$1 $2')
.replace(/(\D)(,|:)([^ \uDBFF\n\t,.?:])/g, '$1$2 $3');
.replace(/(\D)(,|:)([^ \/\uDBFF\n\t,.?:])/g, '$1$2 $3');
}
});

Expand Down Expand Up @@ -1151,6 +1172,37 @@ Typograf.rule({
}
});

Typograf.rule({
name: 'ru/date/main',
sortIndex: 1300,
func: function(text) {
var sp1 = '(-|\\.|\\/)',
sp2 = '(-|\\/)',
re1 = new RegExp('(^|\\D)(\\d{4})' + sp1 + '(\\d{2})' + sp1 + '(\\d{2})(\\D|$)', 'gi'),
re2 = new RegExp('(^|\\D)(\\d{2})' + sp2 + '(\\d{2})' + sp2 + '(\\d{4})(\\D|$)', 'gi');

return text
.replace(re1, '$1$6.$4.$2$7')
.replace(re2, '$1$4.$2.$6$7');
}
});

Typograf.rule({
name: 'ru/date/weekday',
sortIndex: 1310,
func: function(text) {
var space = '( |\u00A0)',
monthCase = this.data('ru/monthCase').join('|'),
weekday = this.data('ru/weekday').join('|'),
re = new RegExp('(\\d)' + space + '(' + monthCase + '),' + space + '(' + weekday + ')', 'gi');

return text.replace(re, function() {
var a = arguments;
return a[1] + a[2] + a[3].toLowerCase() + ',' + a[4] + a[5].toLowerCase();
});
}
});

Typograf.data('ru/dash', {
before: '(^| |\\n)',
after: '( |,|\\.|\\?|:|!|$)'
Expand Down Expand Up @@ -1273,78 +1325,27 @@ Typograf.rule({
}
});

/*jshint maxlen:1000 */
Typograf.rule({
name: 'ru/date/main',
sortIndex: 1300,
func: function(text) {
var sp1 = '(-|\\.|\\/)',
sp2 = '(-|\\/)',
re1 = new RegExp('(^|\\D)(\\d{4})' + sp1 + '(\\d{2})' + sp1 + '(\\d{2})(\\D|$)', 'gi'),
re2 = new RegExp('(^|\\D)(\\d{2})' + sp2 + '(\\d{2})' + sp2 + '(\\d{4})(\\D|$)', 'gi');

return text
.replace(re1, '$1$6.$4.$2$7')
.replace(re2, '$1$4.$2.$6$7');
}
});

Typograf.rule({
name: 'ru/date/weekday',
sortIndex: 1310,
func: function(text) {
var space = '( |\u00A0)',
monthCase = this.data('ru/monthCase').join('|'),
weekday = this.data('ru/weekday').join('|'),
re = new RegExp('(\\d)' + space + '(' + monthCase + '),' + space + '(' + weekday + ')', 'gi');

return text.replace(re, function() {
var a = arguments;
return a[1] + a[2] + a[3].toLowerCase() + ',' + a[4] + a[5].toLowerCase();
});
}
});

Typograf.rule({
name: 'ru/money/dollar',
sortIndex: 1140,
func: function(text) {
var re1 = new RegExp('(^|[\\D]{2,})\\$ ?([\\d.,]+)', 'g'),
re2 = new RegExp('(^|[\\D])([\\d.,]+) ?\\$', 'g'),
rep = '$1$2\u00A0$';

return text
.replace(re1, rep)
.replace(re2, rep);
}
});

Typograf.rule({
name: 'ru/money/euro',
sortIndex: 1140,
name: 'ru/nbsp/addr',
sortIndex: 1115,
func: function(text) {
var re1 = new RegExp('(^|[\\D]{2,})€ ?([\\d.]+)', 'g'),
re2 = new RegExp('(^|[\\D])([\\d.,]+) ?€', 'g'),
rep = '$1$2\u00A0€';

return text
.replace(re1, rep)
.replace(re2, rep);
.replace(/(\s|^)(дом|д\.|кв\.|под\.|п\-д) *(\d+)/gi, '$1$2\u00A0$3')
.replace(/(\s|^)(мкр-н|мк-н|мкр\.|мкрн)\s/gi, '$1$2\u00A0') // микрорайон
.replace(/(\s|^)(эт\.) *(-?\d+)/gi, '$1$2\u00A0$3')
.replace(/(\s|^)(\d+) +этаж([^а-яё]|$)/gi, '$1$2\u00A0этаж$3')
.replace(/(\s|^)литер\s([А-Я]|$)/gi, '$1литер\u00A0$2')
/*
область, край, город, станция, поселок, село,
деревня, улица, переулок, проезд, проспект,
бульвар, площадь, набережная, шоссе,
тупик, офис, комната, участок, владение, строение, корпус
*/
.replace(/(\s|^)(обл|кр|г|ст|пос|с|д|ул|пер|пр|пр\-т|просп|пл|бул|б\-р|наб|ш|туп|оф|комн?|уч|вл|влад|стр|кор)\. *([а-яёa-z\d]+)/gi, '$1$2.\u00A0$3');
}
});

Typograf.rule({
name: 'ru/money/ruble',
sortIndex: 1145,
func: function(text) {
var rep = '$1\u00A0₽';
return text
.replace(/^(\d+)( |\u00A0)?(р|руб)\.$/, rep)
.replace(/(\d+)( |\u00A0)?(р|руб)\.(?=[!?,:;])/g, rep)
.replace(/(\d+)( |\u00A0)?(р|руб)\.(?=\s+[A-ЯЁ])/g, rep + '.');
},
enabled: false
});

Typograf.rule({
name: 'ru/nbsp/afterNumberSign',
sortIndex: 610,
Expand Down Expand Up @@ -1435,6 +1436,47 @@ Typograf.rule({
}
});

Typograf.rule({
name: 'ru/money/dollar',
sortIndex: 1140,
func: function(text) {
var re1 = new RegExp('(^|[\\D]{2,})\\$ ?([\\d.,]+)', 'g'),
re2 = new RegExp('(^|[\\D])([\\d.,]+) ?\\$', 'g'),
rep = '$1$2\u00A0$';

return text
.replace(re1, rep)
.replace(re2, rep);
}
});

Typograf.rule({
name: 'ru/money/euro',
sortIndex: 1140,
func: function(text) {
var re1 = new RegExp('(^|[\\D]{2,})€ ?([\\d.]+)', 'g'),
re2 = new RegExp('(^|[\\D])([\\d.,]+) ?€', 'g'),
rep = '$1$2\u00A0€';

return text
.replace(re1, rep)
.replace(re2, rep);
}
});

Typograf.rule({
name: 'ru/money/ruble',
sortIndex: 1145,
func: function(text) {
var rep = '$1\u00A0₽';
return text
.replace(/^(\d+)( |\u00A0)?(р|руб)\.$/, rep)
.replace(/(\d+)( |\u00A0)?(р|руб)\.(?=[!?,:;])/g, rep)
.replace(/(\d+)( |\u00A0)?(р|руб)\.(?=\s+[A-ЯЁ])/g, rep + '.');
},
enabled: false
});

Typograf.rule({
name: 'ru/number/ordinals',
sortIndex: 1300,
Expand Down Expand Up @@ -1539,3 +1581,7 @@ Typograf.rule({

Typograf._sortRules();
Typograf._needSortRules = true;

return Typograf;

}));
4 changes: 2 additions & 2 deletions dist/typograf.min.js

Large diffs are not rendered by default.

0 comments on commit 34a51b1

Please sign in to comment.