Created
September 26, 2014 11:40
-
-
Save morgondag/9b0671baf9478a1e2a5b to your computer and use it in GitHub Desktop.
encode4HTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
encode4HTML:function(str) { | |
return str | |
//.replace(/\r\n?/g,'\n') | |
// normalize newlines - I'm not sure how these | |
// are parsed in PC's. In Mac's they're \n's | |
//.replace(/(^((?!\n)\s)+|((?!\n)\s)+$)/gm,'') | |
// trim each line | |
//.replace(/(?!\n)\s+/g,' ') | |
// reduce multiple spaces to 2 (like in "a b") | |
//.replace(/^\n+|\n+$/g,'') | |
// trim the whole string | |
/* | |
.replace(/[<>&"']/g,function(a) { | |
// replace these signs with encoded versions | |
switch (a) { | |
case '<' : return '<'; | |
case '>' : return '>'; | |
case '&' : return '&'; | |
case '"' : return '"'; | |
case '\'' : return '''; | |
} | |
})*/ | |
.replace(/\n{2,}/g, '</p><p><br>') | |
// replace 2 or more consecutive empty lines with these | |
.replace(/\n/g, '<br />') | |
// replace single newline symbols with the <br /> entity | |
.replace(/^(.+?)$/, '<p>$1</p>') | |
.replace(/</g, '<') | |
.replace(/>/g, '>') | |
// wrap all the string into <p> tags | |
// if there's at least 1 non-empty character | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment