Skip to content

Commit

Permalink
Boilerplate'd , added node/bower/grunt for development
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Frasser committed Dec 14, 2013
1 parent bceb388 commit bdb847b
Show file tree
Hide file tree
Showing 13 changed files with 635 additions and 148 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
.DS_Store?
._*
.Trashes
Thumbs.DB
Thumbs.DB
node_modules
bower_components
16 changes: 16 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"boss": true,
"curly": true,
"eqeqeq": true,
"eqnull": true,
"expr": true,
"immed": false,
"noarg": true,
"onevar": true,
"smarttabs": true,
"trailing": true,
"unused": true,
"strict": false,
"node": true,
"jquery": true
}
61 changes: 61 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module.exports = function (grunt) {

"use strict";

grunt.initConfig({

// Import package manifest
pkg: grunt.file.readJSON("package.json"),

// Banner definitions
meta: {
banner: "/*\n" +
" * <%= pkg.title || pkg.name %> - v<%= pkg.version %>\n" +
" * <%= pkg.description %>\n" +
" * <%= pkg.homepage %>\n" +
" *\n" +
" * Made by <%= pkg.author %>\n" +
" * Under <%= pkg.license %> License\n" +
" */\n"
},

// Concat definitions
concat: {
dist: {
src: ["src/jquery.linkify.js"],
dest: "dist/jquery.linkify.js"
},
options: {
banner: "<%= meta.banner %>"
}
},

// Lint definitions
jshint: {
files: ["src/jquery.linkify.js"],
options: {
jshintrc: ".jshintrc"
}
},

// Minify definitions
uglify: {
my_target: {
src: ["dist/jquery.linkify.js"],
dest: "dist/jquery.linkify.min.js"
},
options: {
banner: "<%= meta.banner %>"
}
}

});

grunt.loadNpmTasks("grunt-contrib-concat");
grunt.loadNpmTasks("grunt-contrib-jshint");
grunt.loadNpmTasks("grunt-contrib-uglify");

grunt.registerTask("default", ["jshint", "concat", "uglify"]);
grunt.registerTask("travis", ["jshint"]);

};
25 changes: 25 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "jQuery-linkify",
"version": "1.0.0",
"homepage": "https://github.com/HitSend/jQuery-linkify/",
"authors": [
"SoapBox Innovations, Inc. <[email protected]>"
],
"description": "Find URLs in plain text and return HTML for discovered links.",
"main": "src/jquery.linkify.js",
"keywords": [
"jquery",
"plugin",
"boilerplate",
"jquery-plugin",
"jquery-boilerplate",
"link",
"url",
"match",
"html"
],
"license": "MIT",
"dependencies": {
"jquery": "1.10.2"
}
}
7 changes: 4 additions & 3 deletions test.html → demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
<title>jQuery.linkify</title>
</head>
<body>

<p class="test-paragraph">This is a test http://www.google.com and also facebook.ca</p>
<p class="test-paragraph">Please visit http://www.amazon.ca/?query=fail%20amazingand have the time of your life</p>
<script src="libraries/jquery-1.9.1.min.js"></script>
<script src="source/jquery.linkify.js"></script>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="../src/jquery.linkify.js"></script>
<script>
$(document).ready(function() {
$('.test-paragraph').linkify();
console.log(jQuery.linkify("Email [email protected] and then go to www.hitsend.com"));
});
</script>
</body>
Expand Down
244 changes: 244 additions & 0 deletions dist/jquery.linkify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
/*
* Linkify - v1.0.0
* Find URLs in plain text and return HTML for discovered links.
* https://github.com/HitSend/jQuery-linkify/
*
* Made by SoapBox Innovations, Inc.
* Under MIT License
*/
;(function ($, window, document, undefined) {

// Create the defaults once
var pluginName = 'linkify',
defaults = {
tagName: 'a',
newLine: '<br>\n',
target: '_blank',
linkClass: null,
linkClasses: ['linkified'],
linkAttributes: null
};

/**
A Linkified object contains a DOM node (or just plain text) whose
inner text is replaced by HTML containing `<a>` links to URLs
discovered in that text. Call with
new Linkified(element, options)
Here are some the available options and their defaults
{
tagName: 'a',
newLine: '<br>\n',
target: '_blank',
linkClass: null,
linkClasses: ['linkified'],
linkAttributes: null
}
@class Linkified
*/
function Linkified(element, options) {

this.element = element;

// Setup settings
this.settings = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;

this.init();
}

Linkified.prototype = {

/**
Initialized
@method init
*/
init: function () {
this.settings.linkClasses = this.settings.linkClasses || [];
this.linkify();
},

/**
Linkify the contained element
@method linkify
@return {String} html
*/
linkify: function () {
var attr,
linkClass = this.settings.linkClass,
linkClasses = this.settings.linkClasses || [],
linkReplace = [],
text = typeof this.element === 'object' && this.element.textContent ?
this.element.textContent :
this.element.toString() || '';

// Normalize class names
if (linkClass && $.inArray(linkClass, linkClasses) < 0) {
linkClasses.push(linkClass);
this.settings.linkClass = undefined;
}

// Get rid of tags and HTML-structure,
// Duplicate whitespace in preparation for linking
text = text
.replace(/</g, '&lt;')
.replace(/(\s)/g, '$1$1');

// Build up the replacement string

linkReplace.push(
'$1<' + this.settings.tagName,
'href="http://$2$4$5$6"'
);

// Add classes
if (linkClasses.length > 0) {
linkReplace.push('class="' + linkClasses.join(' ') + '"');
}

// Add target
if (this.settings.target) {
linkReplace.push('target="' + this.settings.target + '"');
}

// Add other (normalized) attributes
for (attr in this.settings.linkAttributes) {
linkReplace.push([
attr,
'="',
this.settings.linkAttributes[attr]
.replace(/"/g, '&quot;')
.replace(/\$/g, '&#36;'),
'"'
].join(''));
}

// Finish off
linkReplace.push('>$2$3$4$5$6</' + this.settings.tagName + '>$7');

// Create the link
text = text.replace(this.constructor.linkMatch, linkReplace.join(' '));

// The previous line added `http://` to emails. Replace that with `mailto:`
text = text.replace(this.constructor.emailLinkMatch, '$1mailto:$3');

// Revert whitespace characters back to a single character
text = text.replace(/(\s){2}/g, '$1');

// Trim and account for new lines
text = text
.replace(/^\s+|\s+$/gm, '')
.replace(/\n/gi, this.settings.newLine);

if (typeof this.element === 'object') {

// Set the HTML on the element to the newly linkified text
this.element.innerHTML = text;
}

this.html = text;

return text;
},

/**
Returns the HTML of the linkified element.
@method toString
@return {String} html
*/
toString: function () {

// Returned the linkified HTML
return this.html || '';
}

};

/**
The url-matching regular expression for double-spaced text
@property linkMatch
@static
@type RegExp
*/
Linkified.linkMatch = new RegExp([

// The groups
'(', // 1. Character before the link
'\\s|[^a-zA-Z0-9.\\+_\\/"\\>\\-]|^',
')(?:', //Main group
'(', // 2. Email address (optional)
'[a-zA-Z0-9\\+_\\-]+',
'(?:',
'\\.[a-zA-Z0-9\\+_\\-]+',
')*@',
')?(', // 3. Protocol (optional)
'http:\\/\\/|https:\\/\\/|ftp:\\/\\/',
')?(', // 4. Domain & Subdomains
'(?:(?:[a-z0-9][a-z0-9_%\\-_+]*\\.)+)',
')(', // 5. Top-level domain - http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains
'(?:com|ca|co|edu|gov|net|org|dev|biz|cat|int|pro|tel|mil|aero|asia|coop|info|jobs|mobi|museum|name|post|travel|local|[a-z]{2})',
')(', // 6. Query string (optional)
'(?:',
'[\\/|\\?]',
'(?:',
'[\\-a-zA-Z0-9_%#*&+=~!?,;:.\\/]*',
')*',
')',
'[\\-\\/a-zA-Z0-9_%#*&+=~]',
'|',
'\\/?',
')?',
')(', // 7. Character after the link
'[^a-zA-Z0-9\\+_\\/"\\<\\-]|$',
')'
].join(''), 'g');

/**
The regular expression of matching email links after the
application of the initial link matcher.
@property emailLinkMatch
@static
@type RegExp
*/
Linkified.emailLinkMatch = /(<[a-z]+ href=")(http:\/\/)([a-zA-Z0-9\+_\-]+(?:\.[a-zA-Z0-9\+_\-]+)*@)/g;


// Plugin definition
$.fn[pluginName] = function (options) {
return this.each(function () {
if (!$.data(this, 'plugin-' + pluginName)) {
$.data(this, 'plugin-' + pluginName, new Linkified(this, options));
} else {
$.data(this, 'plugin-' + pluginName).linkify();
}
});
};

// Maintain access to the constructor from the plugin
$.fn[pluginName].Constructor = Linkified;

// Setup click events for linkified elements
$('body').on('click', '.linkified', function () {
var $link = $(this),
url = $link.attr('href'),
isEmail = url.substr(0, 7) === 'mailto:',
target = $link.attr('target');

if (isEmail) {

// mailto links ignore the target
window.location.href = url;

} else {
window.open(url, target);
}

return false;
});

})(jQuery, window, document);
Loading

0 comments on commit bdb847b

Please sign in to comment.