Skip to content
This repository has been archived by the owner on Dec 4, 2017. It is now read-only.

Commit

Permalink
0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 16, 2014
0 parents commit 20c0afe
Show file tree
Hide file tree
Showing 12 changed files with 702 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.DS_Store
coverage
136 changes: 136 additions & 0 deletions .jscs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
{
"requireCurlyBraces": [
"if",
"else",
"for",
"while",
"do",
"try",
"catch"
],
"requireSpaceAfterKeywords": [
"if",
"else",
"for",
"while",
"do",
"switch",
"return",
"try",
"catch"
],
"requireSpaceBeforeBlockStatements": true,
"requireParenthesesAroundIIFE": true,
"requireSpacesInConditionalExpression": true,
"requireSpacesInFunctionExpression": {
"beforeOpeningCurlyBrace": true
},
"requireSpacesInAnonymousFunctionExpression": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
},
"requireSpacesInNamedFunctionExpression": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
},
"requireSpacesInFunctionExpression": {
"beforeOpeningCurlyBrace": true
},
"requireMultipleVarDecl": true,
"requireBlocksOnNewline": true,
"disallowPaddingNewlinesInBlocks": true,
"disallowEmptyBlocks": true,
"disallowSpacesInsideObjectBrackets": true,
"disallowSpacesInsideArrayBrackets": true,
"disallowSpacesInsideParentheses": true,
"requireSpacesInsideObjectBrackets": "all",
"disallowDanglingUnderscores": true,
"requireSpaceAfterObjectKeys": true,
"requireCommaBeforeLineBreak": true,
"requireOperatorBeforeLineBreak": [
"?",
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!==",
">",
">=",
"<",
"<="
],
"requireSpaceBeforeBinaryOperators": [
"=",
",",
"+",
"-",
"/",
"*",
"==",
"===",
"!=",
"!=="
],
"requireSpaceAfterBinaryOperators": [
"=",
",",
"+",
"-",
"/",
"*",
"==",
"===",
"!=",
"!=="
],
"disallowSpaceAfterPrefixUnaryOperators": ["++", "--", "+", "-", "~", "!"],
"disallowSpaceBeforePostfixUnaryOperators": ["++", "--"],
"requireSpaceBeforeBinaryOperators": [
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!=="
],
"requireSpaceAfterBinaryOperators": [
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!=="
],
"disallowImplicitTypeConversion": ["numeric", "boolean", "binary", "string"],
"requireCamelCaseOrUpperCaseIdentifiers": true,
"disallowKeywords": ["with"],
"disallowMultipleLineStrings": true,
"disallowMultipleLineBreaks": true,
"validateLineBreaks": "LF",
"validateQuoteMarks": "'",
"disallowMixedSpacesAndTabs": true,
"disallowTrailingWhitespace": true,
"disallowTrailingComma": true,
"disallowKeywordsOnNewLine": ["else"],
"requireLineFeedAtFileEnd": true,
"maximumLineLength": 78,
"requireCapitalizedConstructors": true,
"safeContextKeyword": "self",
"requireDotNotation": true,
"disallowYodaConditions": true,
"validateJSDoc": {
"checkParamNames": true,
"checkRedundantParams": true,
"requireParamTypes": true
}
}
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
.travis.yml
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: "node_js"
before_install:
- "npm update -g npm"
script: "npm run-script test-travis"
node_js:
- "0.10"
- "0.11"
after_script: "npm install coveralls@2.10.0 && cat ./coverage/lcov.info | coveralls"
Empty file added History.md
Empty file.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(The MIT License)

Copyright (c) 2014 Titus Wormer <tituswormer@gmail.com>

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.
55 changes: 55 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# retext-link [![Build Status](https://travis-ci.org/wooorm/retext-link.svg?branch=master)](https://travis-ci.org/wooorm/retext-link) [![Coverage Status](https://img.shields.io/coveralls/wooorm/retext-link.svg)](https://coveralls.io/r/wooorm/retext-link?branch=master)

[![browser support](https://ci.testling.com/wooorm/retext-link.png) ](https://ci.testling.com/wooorm/retext-link)

See [Browser Support](#browser-support) for more information (a.k.a. don’t worry about those grey icons above).

---

**[retext](https://github.com/wooorm/retext "Retext")** link support.

## Installation

NPM:
```sh
$ npm install retext-link
```

Component.js:
```sh
$ component install wooorm/retext-link
```

## Usage

```js
var Retext = require('retext'),
link = require('retext-link'),
visit = require('retext-visit');

new Retext()
.use(visit)
.use(link)
.parse(
'Have you seen http://foo.com/blah_blah_(wikipedia)_(again)? ' +
'Its pretty cool. Send me an email at test+foo@bar.com'
)
.visit(function (node) {
if (node.data.dataType === 'link') {
console.log([node.toString(), node.type]);
}
});
/*
* >> ['http://foo.com/blah_blah_(wikipedia)_(again)', 'SourceNode']
* >> ['test+foo@bar.com', 'SourceNode']
*/

```

## API

None, the plugin automatically detects links inside text and stores them in a SourceNode, whereas without retext-link parsers would tokenize `'http://www.test.com'` as something like: WordNode, PunctuationNode, PunctuationNode, WordNode, PunctuationNode, WordNode, PunctuationNode, WordNode.

## License

MIT
17 changes: 17 additions & 0 deletions component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "retext-link",
"version": "0.0.1",
"description": "Retext link support",
"keywords": [
"retext",
"link",
"url",
"uri"
],
"repository": "wooorm/retext-link",
"dependencies": {},
"scripts" : [
"index.js"
],
"license": "MIT"
}
138 changes: 138 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
'use strict';

function astToString(node) {
if ('value' in node) {
return node.value;
}

if (node.children.length === 1) {
return astToString(node.children[0]);
}

var values = [],
children = node.children,
iterator = -1;

while (children[++iterator]) {
values.push(astToString(children[iterator]));
}

return values.join('');
}

var EXPRESSION_SLASH = /^\/{1,3}$/;

function mergeLinkExceptions(child, index, parent) {
var siblings = parent.children,
nodes = [child],
start, end, currentIndex, value, type;

if (
child.type !== 'PunctuationNode' ||
astToString(child) !== '.'
) {
return;
}

currentIndex = end = start = index;

while (siblings[start - 1]) {
type = siblings[start - 1].type;

if (type !== 'WordNode' && type !== 'PunctuationNode') {
break;
}

if (
type === 'PunctuationNode' &&
EXPRESSION_SLASH.test(astToString(siblings[start - 1]))
) {
break;
}

nodes.unshift(siblings[--start]);

if (
type === 'WordNode' &&
astToString(siblings[start]) === 'www'
) {
break;
}
}

while (siblings[end + 1]) {
type = siblings[end + 1].type;

if (type !== 'WordNode' && type !== 'PunctuationNode') {
break;
}

nodes.push(siblings[++end]);
}

if (currentIndex === start || currentIndex === end) {
return;
}

/* 1-3 slashes */
if (
start > 0 &&
siblings[start - 1].type === 'PunctuationNode' &&
EXPRESSION_SLASH.test(astToString(siblings[start - 1]))
) {
nodes.unshift(siblings[--start]);
}

/* URL protocol and colon. */
if (
start > 2 &&
siblings[start - 1].type === 'PunctuationNode' &&
astToString(siblings[start - 1]) === ':' &&
siblings[start - 2].type === 'WordNode'
) {
nodes.unshift(siblings[start - 2], siblings[start - 1]);
start -= 2;
}

value = astToString(siblings[end]);

if (
siblings[end].type === 'PunctuationNode' &&
value !== '/' && value !== ')'
) {
end--;
nodes.pop();
}

siblings.splice(start, end - start + 1, {
'type' : 'SourceNode',
'data' : {
'dataType' : 'link'
},
'value' : astToString({
'children' : nodes
})
});

index++;

if (value === '.') {
return index++;
}

return index;
}

function attach(retext) {
var parser = retext.parser;

parser.tokenizeSentenceModifiers = [
mergeLinkExceptions
].concat(parser.tokenizeSentenceModifiers);
}

function link() {}

exports = module.exports = link;

link.attach = attach;
Loading

0 comments on commit 20c0afe

Please sign in to comment.