Skip to content

Instantly share code, notes, and snippets.

@mikesherov
Forked from 140bytes/LICENSE.txt
Created June 6, 2011 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mikesherov/1011230 to your computer and use it in GitHub Desktop.
Save mikesherov/1011230 to your computer and use it in GitHub Desktop.
140byt.es -- bbCodifier
function (
a, //text to BBCodified
b //placeholder
){
return ( //return the results of...
b = function( //and also assign this function to a var to be called recursively
c //partial text to be re-BBCodified
){
return c.replace( //return the replaced version of the partial text
/\[ //begin regex with open bracket
(\w+) //any word character (function name captured as first group
\s? //a space zero or 1 times
([^\]]*) //any non close-bracket character zero or more time (function param captured as second group)
\] //close bracket
(.+) //any character one or more times (the text to modify captured as third group)
\[ //an open bracket
\/ //a forward slash
\1 //the same string as the first matched group, which is function name
\] //close bracket
/gm, //end regex, matching all occurences and multiline
function(
d, //the whole matched string
e, //the captured function name
f, //the captured param
g //the captured text to run the function on
){ //if we're in the body of the function, means we had more BBCode to parse, otherwise the string is returned as is
return b( //run the replace again on..
g[e] ? //if the function exists... (this checks for bad tag names
g[e](f) //the result of the function executed on the string with the params
: // or
g // just the string itself without any function applied
)
})
})(a) //invoke the partial text matcher immediately
}
function(a,b){return(b=function(c){return c.replace(/\[(\w+)\s?([^\]]*)\](.+)\[\/\1\]/gm,function(d,e,f,g){return b(g[e]?g[e](f):g)})})(a)}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Mike Sherov <http://mike.sherov.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "bbCodifier",
"description": "Parses provided text as if it was a bbCode-esque block of text. Uses regex and some funny built in string functions",
"keywords": [
"parsing",
"forums",
"bbCode",
"markdown",
"regex"
]
}
<!DOCTYPE html>
<title>Foo</title>
<textarea id="theinput" rows=10 cols=40>[link http://mike.sherov.com][big]mike [bold]sherov's[/bold] website[/big][/link]</textarea>
<button onclick="display();">bbCodify</button>
<div id="result"></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var bbCodifier = function(a,b){return(b=function(c){return c.replace(/\[(\w+)\s?([^\]]*)\](.+)\[\/\1\]/gm,function(d,e,f,g){return b(g[e]?g[e](f):g)})})(a)};
function display(){
document.getElementById('result').innerHTML = bbCodifier(document.getElementById('theinput').value);
}
/*supported tags:
[big]
[small]
[bold]
[italics]
[fixed]
[strike]
[fontcolor green]
[fontsize 6]
[sub]
[sup]
[link url]
*/
</script>
@atk
Copy link

atk commented Jun 30, 2011

You could still shorten the regexp for 3 bytes with an ungreedy multiplier: \s?([^\]]*)\] => \s?(.*?)\]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment