Skip to content

Instantly share code, notes, and snippets.

@niccolomineo
Last active August 23, 2023 14:32
Show Gist options
  • Save niccolomineo/0bc0f45e86520fd508609a49e91a6251 to your computer and use it in GitHub Desktop.
Save niccolomineo/0bc0f45e86520fd508609a49e91a6251 to your computer and use it in GitHub Desktop.
BBCode-to-HTML JS tokenizer + parser
var input = '[section][lang]Web writing (EN / IT / FR)[y]A lot[/y][/lang][/g][g Tools][lang]Git, AngularJS, GruntJS, Bower, Bash[y]A lot[/y][/lang][lang]Photoshop, Illustrator, InDesign[y]A lot[/y][/lang][/g][/section][p][i]Head in the clouds and feet on the ground web technologies enthusiast, with 6+ years across the web product value chain, aiming at 0 pending emails a day.[/i][/p][p]Hands-on practice across all web dev layers and in all stages of the web product Agile lifecycle, in Euro/regional institutions and private media agencies. Specific strengths:[/p] [*]API-first thinker[/*] [*]mixed technical/design skills[/*] [*]coolheadedness[/*] [*]stakeholder convergence and allocation[/*] [*]brand oriented/redundancy allergic view.[/*][p]Check out my full [a docs/niccolo_mineo_cv.pdf|résumé].[/p][q]The personal qualities and technical skills of Niccolò, as well as his ability to adapt to a multinational and multilingual environment, have made it very easy to integrate him in a well established team. We are sincerely convinced that he would be a very good asset for any audiovisual media operation as well as any IT and communications department. [qs https://linkedin.com/in/niccolomineo]F. Carbajo - Head of Audiovisual Unit in DG-COMM, European Parliament[/qs][/q][q]It was a real pleasure to work with Niccolò. Always enthusiastic with every project, having excellent ideas for improving efficiency and quality. He has strong design and development skills that were improving every day. He is always willing to learn and experiment with new technologies and techniques. I am sure he will be an excellent asset for any team having him.[qs http://www.linkedin.com/in/nikosdesign]N. Sourmelakis - Web Manager and User Experience Expert, Europol[/qs][/q]',
BBCodeRegExMap = {
'tagClass': {
"(\\[[^\\]]+?)(\\§\\s*)(.+?)([\\§\\|\\#].+?)?(\\])": "$1 class='$3'$4$5"
},
'tagId': {
"(\\[[^\\]]+?)(\\#\\s*)(.+?)([\\§\\|\\#].+?)?(\\])": "$1 id='$3'$4$5"
},
'tagImgAlt': {
"(\\[img.+?)(\\|)(\\s*)(.+?)(\\s*\\w+\\=\\'.+?)?(\\s*)([\\§\\|\\#].+?)?(\\])": "$1$3 alt='$4'$5$6$7$8"
},
'tagATitle': {
"(\\[a.+?)(\\|)(\\s*)(.+?)(\\s*\\w+\\=\\'.+?)?(\\s*)([\\§\\|\\#].+?)?(\\])": "$1$3 title='$4'$5$6$7$8$4</a>"
},
'tagImgSrc': {
"(\\[img\\s*)(.+?)(\\s*\\w+\\=\\'.+?)?(\\s*)([\\s\\§\\|\\#].+?)?(\\])": "$1src='$2'$3$4$5$6"
},
'tagAHref': {
"(\\[a\\s*)(.+?)(\\s*\\w+\\=\\'.+?)?(\\s*)([\\s\\§\\|\\#].+?)?(\\])": "$1href='$2' target='_blank'$3$4$5$6"
},
'tagUl': {
"(\\[\\*\\])(.*)(\\[\\/\\*\\])": "<ul><li>$2</li></ul>"
},
'tagLi': {
"(\\[\\/\\*\\])(\\s*)(\\[\\*\\])": "</li><li>"
},
'tagFieldsetLegend': {
"(\\[g\\s*)(.+?)(\\s*\\w+\\=\\'.+?)?(\\s*)([\\§\\|\\#].+?)?(\\])(.+?)(\\[\\/g\\])": "<fieldset$4$5><legend class='sans-serif-400-italic'>$2</legend>$7</fieldset>"
},
'tagSmall': {
"(\\[)(s)(\\s*)(.*?)(\\])(.+?)(\\[)(\\/)(s)(\\])": "<small $4>$6</small>"
},
'tagEm': {
"(\\[)(i)(\\s*)(.*?)(\\])(.+?)(\\[)(\\/)(i)(\\])": "<em $4>$6</em>"
},
'tagDiv': {
"(\\[)(section)(\\s*)(.*?)(\\])(.+?)(\\[)(\\/)(section)(\\])": "<div $4>$6</div>"
},
'tagQuote': {
"(\\[)(q)(\\s*)(.*?)(\\])(.+?)(\\[)(\\/)(q)(\\])": "<p class='quote'$4><span class='serif-900'>“</span> $6</p>"
},
'tagQuoteSource': {
"(\\[qs\\s*)(.+?)(\\s*\\w+\\=\\'.+?)?(\\s*)([\\s\\§\\|\\#].+?)?(\\])(.+?)(\\[)(\\/)(qs)(\\])": "<br><a href='$2' class='quote-source'$5>$7</a>"
},
'tagLanguage': {
"(\\[)(lang)(\\s*.*?)(\\])(.+?)(\\[)(\\/)(lang)(\\])": "<p class='sans-serif-600'>$5</p>"
},
'tagYear': {
"(\\[)(y)(\\s*.*?)(\\])(.+?)(\\[)(\\/)(y)(\\])": "<div class='lang-$5-years' id='skills-bar'><p class='sans-serif-600'>$5</p></div>"
},
'tagAngleBrackets': {
"\\[(\\/?)(.+?)[\\]]\\]?": "<$1$2>"
}
};
for (var i in BBCodeRegExMap) {
var BBCodeKey = Object.keys(BBCodeRegExMap[i])[0],
BBCodeVal = BBCodeRegExMap[i][BBCodeKey];
input.match(BBCodeKey) ? input = input.replace(new RegExp(BBCodeKey, 'g'), BBCodeVal) : input
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment