(function(root){var typogr=function(obj){return new Wrapper(obj)};typogr.version="0.6.8";if(typeof module!=="undefined"&&module.exports){module.exports=typogr}else if(typeof define==="function"&&define.amd){define(function(){return typogr})}else{root.typogr=typogr}var re=function(regexp,flag){return new RegExp(regexp,flag)};var re_skip_tags=/<(\/)?(style|pre|code|kbd|script|math|title)[^>]*>/i;var amp=typogr.amp=function(text){var re_amp=/(\s| )(&|&|&\#38;)(\s| )/g,re_intra_tag=/(<[^<]*>)?([^<]*)(<\/[^<]*>)?/g;if(!text&&typeof text!=="string"){return}return text.replace(re_intra_tag,function(str,prefix,text,suffix){prefix=prefix||"";suffix=suffix||"";if(prefix.match(re_skip_tags))return prefix+text+suffix;text=text.replace(re_amp,'$1&$3');return prefix+text+suffix})};var ord=typogr.ord=function(text){if(!text&&typeof text!=="string"){return}var tokens=tokenize(text),result=[],in_skipped_tag=false,close_match,re_suffix=/(\d+)(st|nd|rd|th)/g;tokens.forEach(function(token){if(token.type==="tag"){result.push(token.txt);close_match=re_skip_tags.exec(token.txt);if(close_match&&close_match[1]===undefined){in_skipped_tag=true}else{in_skipped_tag=false}}else{if(in_skipped_tag){result.push(token.txt)}else{result.push(token.txt.replace(re_suffix,'$1$2'))}}});return result.join("")};var initQuotes=typogr.initQuotes=function(text){var re_quote=re("(?:(?:<(?:p|h[1-6]|li|dt|dd)[^>]*>|^)"+"\\s*"+"(?:<(?:a|em|span|strong|i|b)[^>]*>\\s*)*)"+'(?:("|“|“)|'+"('|‘|‘))","i");if(!text&&typeof text!=="string"){return}return text.replace(re_quote,function(matched_str,dquo,squo){var classname=dquo?"dquo":"quo",quote=dquo?dquo:squo;return[matched_str.slice(0,matched_str.lastIndexOf(quote)),'',quote,""].join("")})};var widont=typogr.widont=function(text){var inline_tags="a|em|span|strong|i|b";var word="(?:<(?:"+inline_tags+")[^>]*?>)*?[^\\s<>]+(?:]*?>)*?";var re_widont=re("("+"\\s+"+word+"\\s+"+word+")"+"(?:\\s+)"+"("+"[^<>\\s]+"+"(?:\\s*]*?>\\s*\\.*)*?"+"(?:\\s*?|$)"+")","gi");return text.replace(re_widont,'$1 $2')};var caps=typogr.caps=function(text){var tokens=tokenize(text),result=[],in_skipped_tag=false,close_match,re_cap=re("("+"(\\b[A-Z\\d]*"+"[A-Z]\\d*[A-Z]"+"[A-Z\\d']*\\b)"+"|(\\b[A-Z]+\\.\\s?"+"(?:[A-Z]+\\.\\s?)+)"+"(?:\\s|\\b|$)"+")","g");tokens.forEach(function(token){if(token.type==="tag"){result.push(token.txt);close_match=re_skip_tags.exec(token.txt);if(close_match&&close_match[1]===undefined){in_skipped_tag=true}else{in_skipped_tag=false}}else{if(in_skipped_tag){result.push(token.txt)}else{result.push(token.txt.replace(re_cap,function(matched_str,g1,g2,g3){var caps,tail;if(g2){return'%s'.replace("%s",g2)}else{if(g3.slice(-1)===" "){caps=g3.slice(0,-1);tail=" "}else{caps=g3;tail=""}return'%s1%s2'.replace("%s1",caps).replace("%s2",tail)}}))}}});return result.join("")};typogr.typogrify=function(src){var text=src;if(src.jquery&&src.html){text=src.html()}text=amp(text);text=widont(text);text=smartypants(text);text=caps(text);text=initQuotes(text);text=ord(text);return text};var smartypants=typogr.smartypants=function(text){var tokens=tokenize(text),result=[],skipped_tag_stack=[],skipped_tag="",skip_match="",in_pre=false,prev_token_last_char="",last_char,t;tokens.forEach(function(token){if(token.type==="tag"){result.push(token.txt);if((skip_match=re_skip_tags.exec(token.txt))!==null){skipped_tag=skip_match[2].toLowerCase();if(skip_match[1]){if(skipped_tag_stack.length>0){if(skipped_tag===skipped_tag_stack[skipped_tag_stack.length-1]){skipped_tag_stack.pop()}}if(skipped_tag_stack.length===0){in_pre=false}}else{skipped_tag_stack.push(skipped_tag);in_pre=true}}}else{t=token.txt;t=t.replace(/(rock )'n'( roll)/gi,"$1’n’$2");last_char=t.slice(-1);if(!in_pre){t=smartEscapes(t);t=smartDashes(t);t=smartEllipses(t);t=smartBackticks(t);switch(t){case"'":if(/\S/.test(prev_token_last_char)){t="’"}else{t="‘"}break;case'"':if(/\S/.test(prev_token_last_char)){t="”"}else{t="“"}break;default:t=smartQuotes(t)}}prev_token_last_char=last_char;result.push(t)}});return result.join("")};var tokenize=typogr.tokenize=function(text){var tokens=[],lastIndex=0,re_tag=/([^<]*)(<[^>]*>)/gi,curr_token;while((curr_token=re_tag.exec(text))!==null){var pre_text=curr_token[1],tag_text=curr_token[2];if(pre_text){tokens.push({type:"text",txt:pre_text})}tokens.push({type:"tag",txt:tag_text});lastIndex=re_tag.lastIndex}if(re_tag.lastIndex<=text.length){if(text.slice(-1)=="."&&text.slice(-2)!=".."){tokens.push({type:"text",txt:text.slice(lastIndex,text.length-1)});tokens.push({type:"text",txt:text.slice(-1)})}else{tokens.push({type:"text",txt:text.slice(lastIndex)})}}return tokens};var smartEscapes=typogr.smartEscapes=function(text){return text.replace(/\\"/g,""").replace(/\\'/g,"'").replace(/\\-/g,"-").replace(/\\\./g,".").replace(/\\\\/g,"\").replace(/\\`/g,"`")};var smartDashes=typogr.smartDashes=function(text){return text.replace(/---/g,"—").replace(/([^<][^!]|[^!]|^)--(?!>)/g,"$1–")};var smartEllipses=typogr.smartEllipses=function(text){return text.replace(/\.\.\./g,"…").replace(/\. \. \./g,"…")};var smartBackticks=typogr.smartBackticks=function(text){return text.replace(/``/g,"“").replace(/''/g,"”")};var smartQuotes=typogr.smartQuotes=function(text){var punct_cls="[!\"#\\$\\%\\'()*+,-.\\/:;<=>?\\@\\[\\\\]\\^_`{|}~]",re_punct_str="(?=%s\\B)".replace("%s",punct_cls),close_cls="[^\\ \\t\\r\\n\\[\\{\\(\\-]",dec_dashes="–|—",re_opening_single_quotes=re("("+"\\s|"+" |"+"--|"+"&[mn]dash;|"+dec_dashes+"|"+"ȁ[34];"+")"+"'"+"(?=\\w)","g"),re_closing_single_quotes=re("("+close_cls+")"+"'"+"(?!\\s | s\\b | \\d)","g"),re_closing_single_quotes2=re("("+close_cls+")"+"'"+"(?!\\s | s\\b)","g"),re_opening_double_quotes=re("("+"\\s|"+" |"+"--|"+"&[mn]dash;|"+dec_dashes+"|"+"ȁ[34];"+")"+'"'+"(?=\\w)","g"),re_closing_double_quotes=re('"(?=\\s)',"g"),re_closing_double_quotes2=re("("+close_cls+')"',"g");return text.replace(re("^'%s".replace("%s",re_punct_str),"g"),"’").replace(re('^"%s'.replace("%s",re_punct_str),"g"),"”").replace(/"'(?=\w)/g,"“‘").replace(/'"(?=\w)/g,"‘“").replace(/\b'(?=\d{2}s)/g,"’").replace(re_opening_single_quotes,"$1‘").replace(re_closing_single_quotes,"$1’").replace(re_closing_single_quotes2,"$1’$2").replace("'","’").replace(re_opening_double_quotes,"$1“").replace(re_closing_double_quotes,"”").replace(re_closing_double_quotes2,"$1”").replace('"',"“")};var Wrapper=function(obj){this._wrapped=obj};var result=function(obj,chain){return chain?typogr(obj).chain():obj};var addToWrapper=function(name,func){Wrapper.prototype[name]=function(){return result(func.call(typogr,this._wrapped),this._chain)}};var isFunction=function(obj){return!!(obj&&obj.constructor&&obj.call&&obj.apply)};var name;for(name in typogr){if(typogr.hasOwnProperty(name)&&isFunction(typogr[name])){addToWrapper(name,typogr[name])}}Wrapper.prototype.chain=function(){this._chain=true;return this};Wrapper.prototype.value=function(){return this._wrapped}})(this);