Skip to content

Commit

Permalink
Новое правило en/punctuation/quot
Browse files Browse the repository at this point in the history
  • Loading branch information
hcodes committed Mar 3, 2015
1 parent 9b4d86b commit 1dfdd82
Show file tree
Hide file tree
Showing 21 changed files with 280 additions and 225 deletions.
64 changes: 64 additions & 0 deletions .gulp/json-rules.js
@@ -0,0 +1,64 @@
var through = require('through');
var path = require('path');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var File = gutil.File;
var Buffer = require('buffer').Buffer;

function getRulePath(file) {
var str = file.replace(/\.json$/, '').split(/\/|\\/);
return [str[str.length - 3], str[str.length - 2], str[str.length - 1]].join('/');
}

module.exports = function(file, opt) {
if(!file) {
throw new PluginError('gulp-json-rules', 'Missing file option for gulp-json-rules');
}
opt = opt || {};

var rules = {};
var fileName;
var firstFile;

if(typeof file === 'string') {
fileName = file;
} else if(typeof file.path === 'string') {
fileName = path.basename(file.path);
firstFile = new File(file);
} else {
throw new PluginError('gulp-json-rules', 'Missing path in file options for gulp-json-rules');
}

function bufferContents(file) {
if(file.isNull()) {
return;
}

if(file.isStream()) {
return this.emit('error', new PluginError('gulp-json-rules', 'Streaming not supported'));
}

if(!firstFile) {
firstFile = file;
}

rules[getRulePath(file.relative)] = JSON.parse(file.contents);
}

function endStream() {
var joinedFile;
if(typeof file === 'string') {
joinedFile = firstFile.clone({contents: false});
joinedFile.path = path.join(firstFile.base, file);
} else {
joinedFile = firstFile;
}

joinedFile.contents = new Buffer(JSON.stringify(rules, null, ' '), 'utf8');

this.emit('data', joinedFile);
this.emit('end');
}

return through(bufferContents, endStream);
};
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions gulp/utils.js → .gulp/utils.js
Expand Up @@ -44,7 +44,7 @@ module.exports = {
return -1;
}
}).forEach(getRow);
processTemplate('docs/RULES.md', 'templates/rules.md');
processTemplate('docs/RULES.md', '.gulp/templates/RULES.md');

text = '';
Typograf.prototype._rules.sort(function(a, b) {
Expand All @@ -64,6 +64,6 @@ module.exports = {
return -1;
}
}).forEach(getRow);
processTemplate('docs/RULES_SORTED.md', 'templates/rules_sorted.md');
processTemplate('docs/RULES_SORTED.md', '.gulp/templates/RULES_SORTED.md');
}
};
1 change: 0 additions & 1 deletion .jshintrc
Expand Up @@ -5,7 +5,6 @@
"curly": true,
"camelcase": true,
"eqeqeq": true,
"es3": true,
"immed": true,
"latedef": "nofunc",
"newcap": true,
Expand Down
2 changes: 0 additions & 2 deletions .npmignore
@@ -1,7 +1,5 @@
site/
src/
tests/
index.html
gulpfile.js
.jshintrc
.jshintignore
Expand Down
100 changes: 58 additions & 42 deletions dist/typograf.js
Expand Up @@ -88,6 +88,51 @@ Typograf._sortInnerRules = function() {
});
};

Typograf._quot = function(text, settings) {
var letters = '\\d' + this.letters() + '\u0301',
lquot = settings.lquot,
rquot = settings.rquot,
lquot2 = settings.lquot2,
rquot2 = settings.rquot2,
phrase = '[' + letters + ')!?.:;#*,' + ']*?',
reL = new RegExp('[«„“"]' + '([…' + letters + '])', 'gi'),
reR = new RegExp('(' + phrase + ')' + '[»”“"]' + '(' + phrase + ')', 'gi'),
reL1 = new RegExp(rquot2 + '([^' + lquot2 + rquot2 + ']*?)' + rquot2, 'g'),
reR1 = new RegExp(lquot2 + '([^' + lquot2 + rquot2 + ']*?)' + lquot2, 'g'),
reL2 = new RegExp(lquot2, 'g'),
reR2 = new RegExp(rquot2, 'g');

text = text
.replace(reL, lquot2 + '$1') // Opening quote
.replace(reR, '$1' + rquot2 + '$2') // Closing quote
.replace(new RegExp('(^|\\w|\\s)' + rquot2 + lquot2, 'g'),
'$1' + lquot2 + lquot2); // Fixed for the case »« at the beginning of the text

if(lquot === lquot2 && rquot === rquot2) {
text = text
.replace(reL2, lquot)
.replace(reR2, rquot)
// ««Энергия» Синергия» -> «Энергия» Синергия»
.replace(new RegExp(lquot + lquot, 'g'), lquot)
// «Энергия «Синергия»» -> «Энергия «Синергия»
.replace(new RegExp(rquot + rquot, 'g'), rquot);
} else {
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)
.replace(reR2, rquot);
}
}

return text;
};

Typograf._langs = ['en', 'ru'];

Typograf.prototype = {
constructor: Typograf,
/**
Expand Down Expand Up @@ -1094,6 +1139,18 @@ Typograf.rule({
}
});

Typograf.rule({
name: 'en/punctuation/quot',
sortIndex: 700,
func: Typograf._quot,
settings: {
lquot: '“',
rquot: '”',
lquot2: '‘',
rquot2: '’'
}
});

Typograf.data('ru/dash', {
before: '(^| |\\n)',
after: '( |,|\\.|\\?|:|!|$)'
Expand Down Expand Up @@ -1471,48 +1528,7 @@ Typograf.rule({
Typograf.rule({
name: 'ru/punctuation/quot',
sortIndex: 700,
func: function(text, settings) {
var letters = '\\d' + this.letters() + '\u0301',
lquot = settings.lquot,
rquot = settings.rquot,
lquot2 = settings.lquot2,
rquot2 = settings.rquot2,
phrase = '[' + letters + ')!?.:;#*,' + ']*?',
reL = new RegExp('[«„“"]' + '([…' + letters + '])', 'gi'),
reR = new RegExp('(' + phrase + ')' + '[»”“"]' + '(' + phrase + ')', 'gi'),
reL1 = new RegExp(rquot2 + '([^' + lquot2 + rquot2 + ']*?)' + rquot2, 'g'),
reR1 = new RegExp(lquot2 + '([^' + lquot2 + rquot2 + ']*?)' + lquot2, 'g'),
reL2 = new RegExp(lquot2, 'g'),
reR2 = new RegExp(rquot2, 'g');

text = text
.replace(reL, lquot2 + '$1') // Открывающая кавычка
.replace(reR, '$1' + rquot2 + '$2') // Закрывающая кавычка
.replace(new RegExp('(^|\\w|\\s)' + rquot2 + lquot2, 'g'),
'$1' + lquot2 + lquot2); // фикс для случая »« в начале текста

if(lquot === lquot2 && rquot === rquot2) {
text = text
.replace(reL2, lquot)
.replace(reR2, rquot)
// ««Энергия» Синергия» -> «Энергия» Синергия»
.replace(new RegExp(lquot + lquot, 'g'), lquot)
// «Энергия «Синергия»» -> «Энергия «Синергия»
.replace(new RegExp(rquot + rquot, 'g'), rquot);
} else {
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)
.replace(reR2, rquot);
}
}

return text;
},
func: Typograf._quot,
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 @@ -124,6 +124,10 @@ Typograf.prototype.titles = {
"common/sym/copy": {
"common": "(c) → ©, (tm) → ©, (r) → ™"
},
"en/punctuation/quot": {
"en": "Placement of quotation marks",
"ru": "Расстановка кавычек"
},
"ru/dash/izpod": {
"en": "Hyphen between “из-под”",
"ru": "Дефис между из-под"
Expand Down
4 changes: 4 additions & 0 deletions dist/typograf.titles.json
Expand Up @@ -124,6 +124,10 @@
"common/sym/copy": {
"common": "(c) → ©, (tm) → ©, (r) → ™"
},
"en/punctuation/quot": {
"en": "Placement of quotation marks",
"ru": "Расстановка кавычек"
},
"ru/dash/izpod": {
"en": "Hyphen between “из-под”",
"ru": "Дефис между из-под"
Expand Down
61 changes: 31 additions & 30 deletions docs/RULES.md
Expand Up @@ -35,33 +35,34 @@
| 31. | [common/sym/arrow](../src/rules/common/sym/arrow.js) | -> → →, <- → ← | 1130 | ||
| 32. | [common/sym/cf](../src/rules/common/sym/cf.js) | Добавление ° к C и F | 1020 | ||
| 33. | [common/sym/copy](../src/rules/common/sym/copy.js) | (c) → ©, (tm) → ©, (r) → ™ | 10 | ||
| 34. | [ru/dash/izpod](../src/rules/ru/dash/izpod.js) | Дефис между из-под | 35 | ||
| 35. | [ru/dash/izza](../src/rules/ru/dash/izza.js) | Дефис между из-за | 33 | ||
| 36. | [ru/dash/kade](../src/rules/ru/dash/kade.js) | Дефис перед ка, де, кась | 31 | ||
| 37. | [ru/dash/koe](../src/rules/ru/dash/koe.js) | Дефис после кое и кой | 38 | ||
| 38. | [ru/dash/main](../src/rules/ru/dash/main.js) | Дефис на тире | 620 | ||
| 39. | [ru/dash/month](../src/rules/ru/dash/month.js) | Тире между месяцами | 610 | ||
| 40. | [ru/dash/taki](../src/rules/ru/dash/taki.js) | Дефис между верно-таки и т.д. | 39 | ||
| 41. | [ru/dash/to](../src/rules/ru/dash/to.js) | Дефис перед то, либо, нибудь | 30 | ||
| 42. | [ru/dash/weekday](../src/rules/ru/dash/weekday.js) | Тире между днями недели | 600 | ||
| 43. | [ru/date/main](../src/rules/ru/date/main.js) | Преобразование дат YYYY-MM-DD к виду DD.MM.YYYY | 1300 | ||
| 44. | [ru/date/weekday](../src/rules/ru/date/weekday.js) | 2 Мая, Понедельник → 2 мая, понедельник | 1310 | ||
| 45. | [ru/money/dollar](../src/rules/ru/money/dollar.js) | $100 → 100 $ | 1140 | ||
| 46. | [ru/money/euro](../src/rules/ru/money/euro.js) | €100 → 100 € | 1140 | ||
| 47. | [ru/money/ruble](../src/rules/ru/money/ruble.js) | 1 руб. → 1 ₽ | 1145 | | |
| 48. | [ru/nbsp/afterNumberSign](../src/rules/ru/nbsp/afterNumberSign.js) | Нераз. пробел после № | 610 | ||
| 49. | [ru/nbsp/beforeParticle](../src/rules/ru/nbsp/beforeParticle.js) | Нераз. пробел перед ли, ль, же, бы, б | 570 | ||
| 50. | [ru/nbsp/but](../src/rules/ru/nbsp/but.js) | Расстановка запятых и неразрывного пробела перед а и но | 1110 | ||
| 51. | [ru/nbsp/cc](../src/rules/ru/nbsp/cc.js) | Удаление пробелов и лишних точек в вв. | 1090 | ||
| 52. | [ru/nbsp/dayMonth](../src/rules/ru/nbsp/dayMonth.js) | Нераз. пробел между числом и месяцем | 1105 | ||
| 53. | [ru/nbsp/m](../src/rules/ru/nbsp/m.js) | m2 → м², m3 → м³ и нераз. пробел | 1030 | ||
| 54. | [ru/nbsp/ooo](../src/rules/ru/nbsp/ooo.js) | Нераз. пробел после OOO, ОАО, ЗАО, НИИ и ПБОЮЛ | 1100 | ||
| 55. | [ru/nbsp/page](../src/rules/ru/nbsp/page.js) | Нераз. пробел перед стр., гл., рис., илл. | 610 | ||
| 56. | [ru/nbsp/xxxx](../src/rules/ru/nbsp/xxxx.js) | Нераз. пробел после XXXX г. (2012 г.) | 1060 | ||
| 57. | [ru/nbsp/yy](../src/rules/ru/nbsp/yy.js) | г.г. → гг. и нераз. пробел | 1080 | ||
| 58. | [ru/number/ordinals](../src/rules/ru/number/ordinals.js) | N-ый, -ой, -ая, -ое, -ые, -ым, -ом, -ых → N-й, -я, -е, -м, -х (25-й) | 1300 | ||
| 59. | [ru/optalign/bracket](../src/rules/ru/optalign/bracket.js) | для открывающей скобки | 1001 | | |
| 60. | [ru/optalign/comma](../src/rules/ru/optalign/comma.js) | для запятой | 1002 | | |
| 61. | [ru/optalign/quot](../src/rules/ru/optalign/quot.js) | для открывающей кавычки | 1000 | | |
| 62. | [ru/other/accent](../src/rules/ru/other/accent.js) | Замена заглавной буквы и добавление знака ударения | 560 | | |
| 63. | [ru/punctuation/quot](../src/rules/ru/punctuation/quot.js) | Расстановка кавычек | 700 | ||
| 34. | [en/punctuation/quot](../src/rules/en/punctuation/quot.js) | Расстановка кавычек | 700 | ||
| 35. | [ru/dash/izpod](../src/rules/ru/dash/izpod.js) | Дефис между из-под | 35 | ||
| 36. | [ru/dash/izza](../src/rules/ru/dash/izza.js) | Дефис между из-за | 33 | ||
| 37. | [ru/dash/kade](../src/rules/ru/dash/kade.js) | Дефис перед ка, де, кась | 31 | ||
| 38. | [ru/dash/koe](../src/rules/ru/dash/koe.js) | Дефис после кое и кой | 38 | ||
| 39. | [ru/dash/main](../src/rules/ru/dash/main.js) | Дефис на тире | 620 | ||
| 40. | [ru/dash/month](../src/rules/ru/dash/month.js) | Тире между месяцами | 610 | ||
| 41. | [ru/dash/taki](../src/rules/ru/dash/taki.js) | Дефис между верно-таки и т.д. | 39 | ||
| 42. | [ru/dash/to](../src/rules/ru/dash/to.js) | Дефис перед то, либо, нибудь | 30 | ||
| 43. | [ru/dash/weekday](../src/rules/ru/dash/weekday.js) | Тире между днями недели | 600 | ||
| 44. | [ru/date/main](../src/rules/ru/date/main.js) | Преобразование дат YYYY-MM-DD к виду DD.MM.YYYY | 1300 | ||
| 45. | [ru/date/weekday](../src/rules/ru/date/weekday.js) | 2 Мая, Понедельник → 2 мая, понедельник | 1310 | ||
| 46. | [ru/money/dollar](../src/rules/ru/money/dollar.js) | $100 → 100 $ | 1140 | ||
| 47. | [ru/money/euro](../src/rules/ru/money/euro.js) | €100 → 100 € | 1140 | ||
| 48. | [ru/money/ruble](../src/rules/ru/money/ruble.js) | 1 руб. → 1 ₽ | 1145 | | |
| 49. | [ru/nbsp/afterNumberSign](../src/rules/ru/nbsp/afterNumberSign.js) | Нераз. пробел после № | 610 | ||
| 50. | [ru/nbsp/beforeParticle](../src/rules/ru/nbsp/beforeParticle.js) | Нераз. пробел перед ли, ль, же, бы, б | 570 | ||
| 51. | [ru/nbsp/but](../src/rules/ru/nbsp/but.js) | Расстановка запятых и неразрывного пробела перед а и но | 1110 | ||
| 52. | [ru/nbsp/cc](../src/rules/ru/nbsp/cc.js) | Удаление пробелов и лишних точек в вв. | 1090 | ||
| 53. | [ru/nbsp/dayMonth](../src/rules/ru/nbsp/dayMonth.js) | Нераз. пробел между числом и месяцем | 1105 | ||
| 54. | [ru/nbsp/m](../src/rules/ru/nbsp/m.js) | m2 → м², m3 → м³ и нераз. пробел | 1030 | ||
| 55. | [ru/nbsp/ooo](../src/rules/ru/nbsp/ooo.js) | Нераз. пробел после OOO, ОАО, ЗАО, НИИ и ПБОЮЛ | 1100 | ||
| 56. | [ru/nbsp/page](../src/rules/ru/nbsp/page.js) | Нераз. пробел перед стр., гл., рис., илл. | 610 | ||
| 57. | [ru/nbsp/xxxx](../src/rules/ru/nbsp/xxxx.js) | Нераз. пробел после XXXX г. (2012 г.) | 1060 | ||
| 58. | [ru/nbsp/yy](../src/rules/ru/nbsp/yy.js) | г.г. → гг. и нераз. пробел | 1080 | ||
| 59. | [ru/number/ordinals](../src/rules/ru/number/ordinals.js) | N-ый, -ой, -ая, -ое, -ые, -ым, -ом, -ых → N-й, -я, -е, -м, -х (25-й) | 1300 | ||
| 60. | [ru/optalign/bracket](../src/rules/ru/optalign/bracket.js) | для открывающей скобки | 1001 | | |
| 61. | [ru/optalign/comma](../src/rules/ru/optalign/comma.js) | для запятой | 1002 | | |
| 62. | [ru/optalign/quot](../src/rules/ru/optalign/quot.js) | для открывающей кавычки | 1000 | | |
| 63. | [ru/other/accent](../src/rules/ru/other/accent.js) | Замена заглавной буквы и добавление знака ударения | 560 | | |
| 64. | [ru/punctuation/quot](../src/rules/ru/punctuation/quot.js) | Расстановка кавычек | 700 | ||

0 comments on commit 1dfdd82

Please sign in to comment.