Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sapegin committed May 13, 2017
0 parents commit 5acf18d
Show file tree
Hide file tree
Showing 14 changed files with 4,514 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# editorconfig.org

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{json,yml,md,babelrc,eslintrc,lintstagedrc,remarkrc}]
indent_style = space
indent_size = 2
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
coverage/*
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "tamia"
}
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
node_modules
.DS_Store
Thumbs.db
.idea
.vscode
*.sublime-project
*.sublime-workspace
*.log
Changelog.md
coverage/
node_modules/
.idea/
.vscode/
.eslintcache
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__tests__/
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: node_js
cache:
directories:
- node_modules
yarn: true
node_js:
- 6
- 7
after_success:
- npm run semantic-release
branches:
except:
- /^v\d+\.\d+\.\d+$/
38 changes: 38 additions & 0 deletions Contributing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# How to contribute

I love pull requests. And following this simple guidelines will make your pull request easier to merge.

## Submitting pull requests

1. Create a new branch, please don’t work in master directly.
2. Add failing tests (if there’re any tests in project) for the change you want to make. Run tests (see below) to see the tests fail.
3. Hack on.
4. Run tests to see if the tests pass. Repeat steps 2–4 until done.
5. Update the documentation to reflect any changes.
6. Push to your fork and submit a pull request.

## JavaScript code style

[See here](https://github.com/tamiadev/eslint-config-tamia#code-style-at-a-glance).

## Other notes

- If you have commit access to repo and want to make big change or not sure about something, make a new branch and open pull request.
- Don’t commit generated files: compiled from Stylus CSS, minified JavaScript, etc.
- Don’t change version number and changelog.
- Install [EditorConfig](http://editorconfig.org/) plugin for your code editor.
- Feel free to [ask me](http://sapegin.me) anything you need.

## Building and running tests

Install dependencies:

```bash
npm install
```

Run tests:

```bash
npm test
```
23 changes: 23 additions & 0 deletions License.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
The MIT License
===============

Copyright 2017 Artem Sapegin (http://sapegin.me), contributors

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
71 changes: 71 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# textlint-rule-diacritics

[![textlint fixable rule](https://img.shields.io/badge/textlint-fixable-green.svg?style=social)](https://textlint.github.io/)
[![Build Status](https://travis-ci.org/sapegin/textlint-rule-diacritics.svg)](https://travis-ci.org/sapegin/textlint-rule-diacritics)
[![npm](https://img.shields.io/npm/v/textlint-rule-diacritics.svg)](https://www.npmjs.com/package/textlint-rule-diacritics)

[Textlint](https://github.com/textlint/textlint) rule to check correct usage of diacritics.

For example:

* creme brulee → crème brûlée
* deja vu → déjà vu
* senorita → señorita
* doppelganger → doppelgänger

(You can customize the rules as you wish.)

![](https://d3vv6lp55qjaqc.cloudfront.net/items/1p0s3e2p1U1m1r3N2Q41/diacritics.png)

## Installation

```shell
npm install textlint-rule-diacritics
```

## Usage

```shell
textlint --fix --rule diacritics Readme.md
```

## Configuration

By default the rule will check against my personal [diacritics](./words.json). You can change it in your `.textlintrc`:

```js
{
"rules": {
"diacritics": {
// List of additional words
"words": [
"tâmia",
],
// OR load words from a file
"words": "~/words.json",
// OR load words from npm
"words": "@johnsmith/words"
}
}
}
```

Read more about [configuring textlint](https://github.com/textlint/textlint/blob/master/docs/configuring.md).

## Changelog

The changelog can be found on the [Releases page](https://github.com/sapegin/textlint-rule-diacritics/releases).

## Contributing

Everyone is welcome to contribute. Please take a moment to review the [contributing guidelines](Contributing.md).

## Authors and license

[Artem Sapegin](http://sapegin.me) and [contributors](https://github.com/sapegin/textlint-rule-diacritics/graphs/contributors).

MIT License, see the included [License.md](License.md) file.

Inspired by [retext-diacritics](https://github.com/wooorm/retext-diacritics).

Dictionary source: [Wiktionary](https://en.wiktionary.org/wiki/Appendix:English_words_with_diacritics).
150 changes: 150 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
const fs = require('fs');
const stripJsonComments = require('strip-json-comments');
const matchCasing = require('match-casing');

const DEFAULT_OPTIONS = {
words: [],
};

const MARK_GROUPS = ["’'", 'àâäåa', 'éèêëe', 'çc', 'îíi', 'ñn', 'öo', 'šs', 'ûüu', 'ÿy'];

function reporter(context, options = {}) {
const opts = Object.assign({}, DEFAULT_OPTIONS, options);
const words = getWords(opts.words);

// Regexp for all possible mistakes
const regExp = getRegExp(getPatterns(words));

const { Syntax, RuleError, report, fixer, getSource } = context;
return {
[Syntax.Str](node) {
return new Promise(resolve => {
const text = getSource(node);

let match;
// eslint-disable-next-line no-cond-assign
while ((match = regExp.exec(text))) {
const matched = match[0];
const replacement = getCorrection(words, matched);

// Skip correct spelling
if (matched === replacement) {
continue;
}

const index = match.index;
const range = [index, index + matched.length];
const fix = fixer.replaceTextRange(range, replacement);
const message = `Incorrect usage of the word: “${matched}”, use “${replacement}” instead`;
report(node, new RuleError(message, { index, fix }));
}

resolve();
});
},
};
}

/**
* Load all default words joined with additional worsd from a config file
*
* @param {string|string[]} words
* @return {string[]}
*/
function getWords(words) {
const defaults = loadJson('./words.json');
const extras = typeof words === 'string' ? loadJson(words) : words;
return defaults.concat(extras);
}

/**
* Load JSON file, strip comments.
*
* @param {string} filepath
* @return {object}
*/
function loadJson(filepath) {
const json = fs.readFileSync(require.resolve(filepath), 'utf8');
return JSON.parse(stripJsonComments(json));
}

/**
* Replace all diacritics with RegExp patterns that match that character with or without a diacritic mark:
* décor → d[éèêëe]cor
*
* @param {string} word
* @return {string}
*/
function getPattern(word) {
return MARK_GROUPS.reduce((pattern, group) => {
// Strip the last character which is a character without diacritic mark
const groupPattern = `[${group.substring(0, group.length - 1)}]`;
return pattern.replace(new RegExp(groupPattern, 'ig'), `[${group}]`);
}, word);
}

/**
* Get patterns from words list
*
* @param {string[]} words
* @return {string[]}
*/
function getPatterns(words) {
return words.map(getPattern);
}

/**
* RegExp from list of patterns.
*
* For "résumé" it should match:
* - resume
* - résume
* - résumé
* - resumes
* - Resume
*
* For "raison d'etre" it should match:
* - raison d'etre
* - raison d’etre
* - raison d'être
* - Raison d'être
*
* @param {string[]} patterns
* @return {RegExp}
*/
function getRegExp(patterns) {
return new RegExp(`(\\b(?:${patterns.join('|')})[\\w-]*\\b)`, 'ig');
}

/**
* Return a correct word based on found incorrect word.
* Keeps case and suffix of an original word.
*
* @param {string[]} words
* @param {string} match
* @return {string|boolean}
*/
function getCorrection(words, match) {
for (const word of words) {
const pattern = getPattern(word);
if (!getRegExp([pattern]).test(match)) {
continue;
}

const corrected = match.replace(new RegExp(`\\b${pattern}`, 'i'), word);
return matchCasing(corrected, match);
}

return false;
}

module.exports = {
linter: reporter,
fixer: reporter,
test: {
getPattern,
getPatterns,
getRegExp,
getCorrection,
},
};
Loading

0 comments on commit 5acf18d

Please sign in to comment.