From aea4efb4c342052ac04c01ba500674e0084f4435 Mon Sep 17 00:00:00 2001 From: jason libsch Date: Mon, 26 Jan 2015 13:46:21 -0800 Subject: [PATCH] add orphan rule --- js/ragadjust.js | 108 ++++++++++++++++++++++++------------------------ 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/js/ragadjust.js b/js/ragadjust.js index 16144a8..97467a8 100644 --- a/js/ragadjust.js +++ b/js/ragadjust.js @@ -1,55 +1,55 @@ ragadjust = function (s, method) { - - if (document.querySelectorAll) { - - var eles = document.querySelectorAll(s), - elescount = eles.length, - - preps = /(\s|^|>)((aboard|about|above|across|after|against|along|amid|among|anti|around|before|behind|below|beneath|beside|besides|between|beyond|concerning|considering|despite|down|during|except|excepting|excluding|following|from|inside|into|like|minus|near|onto|opposite|outside|over|past|plus|regarding|round|save|since|than|that|this|through|toward|towards|under|underneath|unlike|until|upon|versus|with|within|without)\s)+/gi, - - smallwords = /(\s|^)(([a-zA-Z-_(]{1,2}('|’)*[a-zA-Z-_,;]{0,1}?\s)+)/gi, // words with 3 or less characters - - dashes = /([-–—])\s/gi, - - emphasis = /(<(strong|em|b|i)>)(([^\s]+\s*){2,3})?(<\/(strong|em|b|i)>)/gi; - - while (elescount-- > 0) { - - var ele = eles[elescount], - elehtml = ele.innerHTML; - - if (method == 'prepositions' || method == 'all') - - // replace prepositions (greater than 3 characters) - elehtml = elehtml.replace(preps, function(contents, p1, p2) { - return p1 + p2.replace(/\s/gi, ' '); - }); - - if (method == 'small-words' || method == 'all') - - // replace small words - elehtml = elehtml.replace(smallwords, function(contents, p1, p2) { - return p1 + p2.replace(/\s/g, ' '); - }); - - if (method == 'dashes' || method == 'all') - - // replace small words - elehtml = elehtml.replace(dashes, function(contents) { - return contents.replace(/\s/g, ' '); - }); - - if (method == 'emphasis' || method == 'all') - - // emphasized text - elehtml = elehtml.replace(emphasis, function(contents, p1, p2, p3, p4, p5) { - return p1 + p3.replace(/\s/gi, ' ') + p5; - }); - - ele.innerHTML = elehtml; - - } - - } - -}; \ No newline at end of file + + if (document.querySelectorAll) { + + var eles = document.querySelectorAll(s), + elescount = eles.length, + preps = /(\s|^|>)((aboard|about|above|across|after|against|along|amid|among|anti|around|before|behind|below|beneath|beside|besides|between|beyond|concerning|considering|despite|down|during|except|excepting|excluding|following|from|inside|into|like|minus|near|onto|opposite|outside|over|past|plus|regarding|round|save|since|than|that|this|through|toward|towards|under|underneath|unlike|until|upon|versus|with|within|without)\s)+/gi, + smallwords = /(\s|^)(([a-zA-Z-_(]{1,2}('|’)*[a-zA-Z-_,;]{0,1}?\s)+)/gi, // words with 3 or less characters + dashes = /([-–—])\s/gi, + emphasis = /(<(strong|em|b|i)>)(([^\s]+\s*){2,3})?(<\/(strong|em|b|i)>)/gi; + orphans = /(.| )[^\s|^ ]+$/ + ele = null; + + while (elescount-- > 0) { + ele = eles[elescount], + elehtml = ele.innerHTML; + + if (method == 'prepositions' || method == 'all') { + // replace prepositions (greater than 3 characters) + elehtml = elehtml.replace(preps, function(contents, p1, p2) { + return p1 + p2.replace(/\s/gi, ' '); + }); + + } if (method == 'small-words' || method == 'all') { + // replace small words + elehtml = elehtml.replace(smallwords, function(contents, p1, p2) { + return p1 + p2.replace(/\s/g, ' '); + }); + + } if (method == 'dashes' || method == 'all') { + // replace small words + elehtml = elehtml.replace(dashes, function(contents) { + return contents.replace(/\s/g, ' '); + }); + + } if (method == 'emphasis' || method == 'all') { + // emphasized text + elehtml = elehtml.replace(emphasis, function(contents, p1, p2, p3, p4, p5) { + return p1 + p3.replace(/\s/gi, ' ') + p5; + }); + + } if (method == 'orphans' || method == 'all') { + // no single words on there own last line + elehtml = elehtml.replace(orphans, function(contents, p1, p2) { + return contents.replace(/(\s|^ )/, ' ') + }); + + } + + ele.innerHTML = elehtml; + } + + } + +};