Skip to content

Commit

Permalink
A lot of testing - different options combination, jquery and zepto, c…
Browse files Browse the repository at this point in the history
…lean up code
  • Loading branch information
marcwieland95 committed Aug 26, 2015
1 parent f564087 commit dd87767
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 112 deletions.
6 changes: 3 additions & 3 deletions @examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@

<p class="first">I call our world Flatland, not because we call it so, but to make its nature clearer to you, my happy readers, who are privileged to live in Space.</p>

<p>Imagine a vast sheet of paper on which straight Lines, Triangles, Squares, Pentagons, Hexagons, and other figures, instead of remaining fixed in their places, move freely about, on or in the surface, but without the power of rising above or sinking below it, very much like shadows only hard and with luminous edges and you will then have a pretty correct notion of my country and countrymen. Alas, a few years ago, I should have said "my universe;" but now my mind has been opened to higher views of things.</p>
<p>Imagine a vast sheet of paper on which straight Lines, Triangles, Squares, Pentagons, Hexagons, and other figures, instead of remaining fixed in their places, move freely about, on or in the surface, but without the power of rising above or sinking below it, very much like shadows - only hard and with luminous edges - and you will then have a pretty correct notion of my country and countrymen. Alas, a few years ago, I should have said "my universe;" but now my mind has been opened to higher views of things.</p>

<p>In such a country, you will perceive at once that it is impossible that there should be anything of what you call a "solid" kind; but I dare say you will suppose that we could at least distinguish by sight the Triangles, Squares, and other figures, moving about as I have described them. On the contrary, we could see nothing of the kind, not at least so as to distinguish one figure from another. Nothing was visible, nor could be visible, to us, except Straight Lines; and the necessity of this I will speedily demonstrate.</p>

<p>Place a penny on the middle of one of your tables in Space; and leaning over it, look down upon it. It will appear a circle.</p>

<p class="hypenate">But now, drawing back to the edge of the table, gradually lower your eye (thus bringing yourself more and more into the condition of the inhabitants of Flatland), and you will find the penny becoming more and more oval to your view; and at last when you have placed your eye exactly on the edge of the table (so that you are, as it were, actually a Flatlander) the penny will then have ceased to appear oval at all, and will have become, so far as you can see, a straight line.</p>

<p>The same thing would happen if you were to treat in the same way a Triangle, or Square, or any other figure cut out of pasteboard. As soon as you look at it with your eye on the edge on the table, you will find that it ceases to appear to you a figure, and that it becomes in appearance a straight line. Take for example an equilateral Triangle who represents with us a Tradesman of the respectable class. Fig. 1 represents the Tradesman as you would see him while you were bending over him from above; figs. 2 and 3 represent the Tradesman, as you would see him if your eye were close to the level, or all but on the level of the table; and if your eye were quite on the level of the table (and that is how we see him in Flatland) you would see nothing but a straight line.</p>
<p>The same thing would happen if you were to treat in the same way a Triangle, or Square, or any other figure cut out of pasteboard. As soon as you look at it with your eye on the edge on the table, you will find that it ceases to appear to you a figure, and that it becomes in appearance a straight line. Take for example an equilateral Triangle - who represents with us a Tradesman of the respectable class. Fig. 1 represents the Tradesman as you would see him while you were bending over him from above; figs. 2 and 3 represent the Tradesman, as you would see him if your eye were close to the level, or all but on the level of the table; and if your eye were quite on the level of the table (and that is how we see him in Flatland) you would see nothing but a straight line.</p>

<script>
/* Some scripts here */
$('.first').hyphenate({
$('p').hyphenate({
lang: 'de',
hypenChar: true,
exceptions: 'Flatla|nd, bec|ause',
Expand Down
26 changes: 13 additions & 13 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ module.exports = function(grunt) {
banner: '/*!\n' +
' * <%= pkg.name %>\n' +
' * Version: <%= pkg.version %>\n' +
' * <%= pkg.description %>\n' +
' * Author: <%= pkg.author %>\n' +
' * Site: http://<%= pkg.homepage %>/\n' +
' * Licensed under the BSD license\n' +
' */\n' +
''
' * <%= pkg.description %>\n' +
' * Author: <%= pkg.author %>\n' +
' * Site: http://<%= pkg.homepage %>/\n' +
' * Licensed under the BSD license\n' +
' */\n' +
''
},
src: 'src/jquery.hypher.js',
dest: 'dist/jquery.hypher.js',
Expand All @@ -36,13 +36,13 @@ module.exports = function(grunt) {
banner: '/*!\n' +
' * <%= pkg.name %>\n' +
' * Version: <%= pkg.version %>\n' +
' * <%= pkg.description %>\n' +
' * Author: <%= pkg.author %>\n' +
' * Site: http://<%= pkg.homepage %>/\n' +
' * Licensed under the BSD license\n' +
' */\n' +
'',
//report: 'gzip'
' * <%= pkg.description %>\n' +
' * Author: <%= pkg.author %>\n' +
' * Site: http://<%= pkg.homepage %>/\n' +
' * Licensed under the BSD license\n' +
' */\n' +
'',
//report: 'gzip'
},
files: {
'dist/jquery.hypher.min.js': ['src/jquery.hypher.js']
Expand Down
48 changes: 14 additions & 34 deletions dist/jquery.hypher.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* hypher-for-jquery
* Version: 0.2.0
* A fast and small JavaScript hyphenation engine for jQuery
* Author: @bramstein, @xxcram
* Author: @bramstein, @marcwieland95
* Site: http://http://www.bramstein.com//
* Licensed under the BSD license
*/
Expand All @@ -19,9 +19,7 @@
var defaults = {

// {String} Set the correct language for your content
//lang: $('html').attr('lang'),
lang: language,
// lang: jsonSettings.language,

// {Int} The minimum amount of characters on the left of the word
leftMin: false,
Expand All @@ -43,7 +41,6 @@

// {String} Add exceptions as a comma-separated string - add your custom hypenation with | (vertical bar)
exceptions: null,
//exceptions: jsonSettings.exceptions,

};

Expand All @@ -65,10 +62,6 @@
async: false, // all data have to be available to run
dataType: 'script',
cache: true,
success: function(data) {
//pattern_settings = data;
//console.log(pattern_settings);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log('Pattern file can\'t be loaded - check path');
}
Expand All @@ -79,11 +72,14 @@
// Error message and exit
if (typeof Hyphenator === 'undefined') {
console.log('Pattern isn\'t included - autoload is turned off');
// return false;
}

}

// Autoload is on, but pattern still isn't available
if (typeof Hyphenator === 'undefined') {
console.log('Pattern isn\'t included - maybe pattern file doesn\'t exists or path linkes to wrong directory wrong');
}

// Set variables
i = 0;
Expand All @@ -92,29 +88,26 @@
* @type {!number}
* @const
*/
//this.leftMin = this.options.leftMin;
// Load content from patterns when noting set in settings
if (!this.leftMin) this.leftMin = Hyphenator.languages[language].leftmin;
if (!this.options.leftMin) this.options.leftMin = Hyphenator.languages[language].leftmin;

/**
* @type {!number}
* @const
*/
//this.rightMin = this.options.rightMin;
// Load content from patterns when noting set in settings
if (!this.rightMin) this.rightMin = Hyphenator.languages[language].rightmin;
if (!this.options.rightMin) this.options.rightMin = Hyphenator.languages[language].rightmin;

/**
* @type {!Object.<string, !Array.<string>>}
*/
//this.exceptions = this.options.exceptions;
// Load content from patterns when noting set in settings
if (!this.exceptions) this.exceptions = Hyphenator.languages[language].exceptions;
if (!this.options.exceptions) this.options.exceptions = Hyphenator.languages[language].exceptions;

this.exceptionsObject = {};

if (this.exceptions) {
exceptions = this.exceptions.split(/,\s?/g);
if (this.options.exceptions) {
exceptions = this.options.exceptions.split(/,\s?/g);

for (; i < exceptions.length; i += 1) {
this.exceptionsObject[exceptions[i].replace(/\|/g, '').toLowerCase()] = new RegExp('(' + exceptions[i].split('|').join(')(') + ')', 'i'); // \u2027
Expand All @@ -124,19 +117,13 @@
/**
* @type {!Hypher.TrieNode}
*/
this.trie = this.trie = this.createTrie(Hyphenator.languages[language].patterns); // this.createTrie(jsonSettings.patterns[1]);
//this.trie = this.createTrie(jsonSettings.patterns);
this.trie = this.createTrie(Hyphenator.languages[language].patterns);

// Return hypenation to replace TextNode
this.hypens = this.hyphenateText(nodeValue);

}

/**
* @typedef {{_points: !Array.<number>}}
*/
//Hypher.TrieNode;

/**
* Creates a trie from a language pattern.
* @private
Expand Down Expand Up @@ -184,7 +171,6 @@
}
}


return tree;
};

Expand All @@ -194,9 +180,7 @@
* @param {!string} str The text to hyphenate.
* @return {!string} The same text with soft hyphens inserted in the right positions.
*/
Hypher.prototype.hyphenateText = function (str, minLength) {
minLength = minLength || this.options.minLength;

Hypher.prototype.hyphenateText = function (str) {

// Regexp("\b", "g") splits on word boundaries,
// compound separators and ZWNJ so we don't need
Expand All @@ -205,15 +189,14 @@
// we implement it manually.
var words = str.split(/([a-zA-Z0-9_\u0027\u00DF-\u00EA\u00EC-\u00EF\u00F1-\u00F6\u00F8-\u00FD\u0101\u0103\u0105\u0107\u0109\u010D\u010F\u0111\u0113\u0117\u0119\u011B\u011D\u011F\u0123\u0125\u012B\u012F\u0131\u0135\u0137\u013C\u013E\u0142\u0144\u0146\u0148\u0151\u0153\u0155\u0159\u015B\u015D\u015F\u0161\u0165\u016B\u016D\u016F\u0171\u0173\u017A\u017C\u017E\u017F\u0219\u021B\u02BC\u0390\u03AC-\u03CE\u03F2\u0401\u0410-\u044F\u0451\u0454\u0456\u0457\u045E\u0491\u0531-\u0556\u0561-\u0587\u0902\u0903\u0905-\u090B\u090E-\u0910\u0912\u0914-\u0928\u092A-\u0939\u093E-\u0943\u0946-\u0948\u094A-\u094D\u0982\u0983\u0985-\u098B\u098F\u0990\u0994-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BE-\u09C3\u09C7\u09C8\u09CB-\u09CD\u09D7\u0A02\u0A03\u0A05-\u0A0A\u0A0F\u0A10\u0A14-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A3E-\u0A42\u0A47\u0A48\u0A4B-\u0A4D\u0A82\u0A83\u0A85-\u0A8B\u0A8F\u0A90\u0A94-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABE-\u0AC3\u0AC7\u0AC8\u0ACB-\u0ACD\u0B02\u0B03\u0B05-\u0B0B\u0B0F\u0B10\u0B14-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3E-\u0B43\u0B47\u0B48\u0B4B-\u0B4D\u0B57\u0B82\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB5\u0BB7-\u0BB9\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD7\u0C02\u0C03\u0C05-\u0C0B\u0C0E-\u0C10\u0C12\u0C14-\u0C28\u0C2A-\u0C33\u0C35-\u0C39\u0C3E-\u0C43\u0C46-\u0C48\u0C4A-\u0C4D\u0C82\u0C83\u0C85-\u0C8B\u0C8E-\u0C90\u0C92\u0C94-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBE-\u0CC3\u0CC6-\u0CC8\u0CCA-\u0CCD\u0D02\u0D03\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D28\u0D2A-\u0D39\u0D3E-\u0D43\u0D46-\u0D48\u0D4A-\u0D4D\u0D57\u0D60\u0D61\u0D7A-\u0D7F\u1F00-\u1F07\u1F10-\u1F15\u1F20-\u1F27\u1F30-\u1F37\u1F40-\u1F45\u1F50-\u1F57\u1F60-\u1F67\u1F70-\u1F7D\u1F80-\u1F87\u1F90-\u1F97\u1FA0-\u1FA7\u1FB2-\u1FB4\u1FB6\u1FB7\u1FBD\u1FBF\u1FC2-\u1FC4\u1FC6\u1FC7\u1FD2\u1FD3\u1FD6\u1FD7\u1FE2-\u1FE7\u1FF2-\u1FF4\u1FF6\u1FF7\u200D\u2019]+)/g);


for (var i = 0; i < words.length; i += 1) {
if (words[i].indexOf('/') !== -1) {
// Don't insert a zero width space if the slash is at the beginning or end
// of the text, or right after or before a space.
if (i !== 0 && i !== words.length - 1 && !(/\s+\/|\/\s+/.test(words[i]))) {
words[i] += '\u200B';
}
} else if (words[i].length > minLength) {
} else if (words[i].length > this.options.minLength) {
// Hyphernate
if(!this.options.hypenChar) {
words[i] = this.hyphenate(words[i]).join('\u00AD');
Expand All @@ -232,7 +215,6 @@
return words.join('');
};


/**
* Hyphenates a word.
*
Expand Down Expand Up @@ -294,7 +276,7 @@
}

for (i = 1; i < wordLength - 1; i += 1) {
if (i > this.leftMin && i < (wordLength - this.rightMin) && points[i] % 2) {
if (i > this.options.leftMin && i < (wordLength - this.options.rightMin) && points[i] % 2) {
result.push(originalCharacters[i]);
} else {
result[result.length - 1] += originalCharacters[i];
Expand All @@ -304,8 +286,6 @@
return result;
};



$.fn.hyphenate = function(options) {

if (options === undefined) {
Expand Down
Loading

0 comments on commit dd87767

Please sign in to comment.