Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I was inspired inspired to write a BBCode to HTML converter as I'm currently learning functional programming. I wanted achieve functional cohesion. jsFiddle

Below is the test setup using the same rule set as the linked linked post.

I was inspired to write a BBCode to HTML converter as I'm currently learning functional programming. I wanted achieve functional cohesion. jsFiddle

Below is the test setup using the same rule set as the linked post.

I was inspired to write a BBCode to HTML converter as I'm currently learning functional programming. I wanted achieve functional cohesion. jsFiddle

Below is the test setup using the same rule set as the linked post.

edited tags
Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Updated code
Source Link
Pete
  • 2.3k
  • 2
  • 19
  • 19

I was inspired to write a BBCode to HTML converter as I'm currently learning functional programming. I wanted achieve functional cohesion. jsFiddle jsFiddle

/* Escapes a string for use with regular expressions */
function escapeString(input) {
 return input.replace(/([,円\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g,
 function(c){return "\\" + c;});
}
/* replaces all */
function replaceAll(search, input, replacement, ignore) {
 return input.replace(
 new RegExp(escapeString(search), "g"+(ignore?"i":"")), 
 replacement);
};
/* Check if an index is event */
function isIndexEven(val, index, context) {
 return (index % 2 === 0);
};
/* Sets the attributes on an HTML string given a set of attributes */
function setHtmlAttrs(htmlCode, attrs) {
 return attrs.reduce(function(htmlCode, attr, index) {
 var re = new RegExp('\\$' + (index + 1), 'g');
 return htmlCode.replace(re, attr);
 }, htmlCode);
};
/* Gets the html tag(s) for a bbCode tag */
function getHtmlTageleFragsToHtml(rulesisClosing, bbTagelementFragments) {
 return rules[bbTag]'<' + elementFragments.map(function(rulefrag) {
 return rule.filter(isIndexEven);
 });
};
/* Gets the html code for a bbCodeisClosing tag? *'/
function getHtml(rules, bbTag) {
 ' return+ rules[bbTag]frag.map(functionjoin(rule) {
 ' ') return: rulefrag.join(' '));
 });.join('><') + '>';
};
/* Converts a single bbCode Tag to Html */
function bbTagToHtmlTag(rules, tag) {
 var attrs = tag.replace(/\[\w+|\]/g, '').trim().split(' ');
 var key = tag.replace(/\[\/?| .+|\]/g, '');
 var outputisClosing = (/\//.test(tag);
 var htmlTemplate = (isClosing ? getHtmlTagrules[key].map(rules, keyfunction(rule) { .map(function(tag) { return '/' + tag; } )
 return rule.reversefilter(isIndexEven)
 }).joinreverse('><')
 : setHtmlAttrs(getHtml(rules, key : rules[key]).join;
 var output = eleFragsToHtml('><')isClosing, attrs)htmlTemplate); 
 return '<' + setHtmlAttrs(output +, '>';attrs);
};
/* Converts bbCode to HTML */
function bbCodeToHtml(rules, bbCode) {
 /* Creates the Regular Expression Search */
 var regex = new RegExp( ['\\[.?[',
 Object.keys(rules).join('|'), 
 '].*?\\]'].join(''), 'g' );
 return bbCode.match(regex).reduce(function(htmlCode, tag) {
 return replaceAll(tag, 
 htmlCode, 
 bbTagToHtmlTag(rules, tag));
 }, bbCode);
};

I was inspired to write a BBCode to HTML converter as I'm currently learning functional programming. I wanted achieve functional cohesion. jsFiddle

/* Escapes a string for use with regular expressions */
function escapeString(input) {
 return input.replace(/([,円\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g,
 function(c){return "\\" + c;});
}
/* replaces all */
function replaceAll(search, input, replacement, ignore) {
 return input.replace(
 new RegExp(escapeString(search), "g"+(ignore?"i":"")), 
 replacement);
};
/* Check if an index is event */
function isIndexEven(val, index, context) {
 return (index % 2 === 0);
};
/* Sets the attributes on an HTML string given a set of attributes */
function setHtmlAttrs(htmlCode, attrs) {
 return attrs.reduce(function(htmlCode, attr, index) {
 var re = new RegExp('\\$' + (index + 1), 'g');
 return htmlCode.replace(re, attr);
 }, htmlCode);
};
/* Gets the html tag(s) for a bbCode tag */
function getHtmlTag(rules, bbTag) {
 return rules[bbTag].map(function(rule) {
 return rule.filter(isIndexEven);
 });
};
/* Gets the html code for a bbCode tag */
function getHtml(rules, bbTag) {
  return rules[bbTag].map(function(rule) {
  return rule.join(' ');
 }); 
};
/* Converts a single bbCode Tag to Html */
function bbTagToHtmlTag(rules, tag) {
 var attrs = tag.replace(/\[\w+|\]/g, '').trim().split(' ');
 var key = tag.replace(/\[\/?| .+|\]/g, '');
 var output = (/\//.test(tag)
 ? getHtmlTag(rules, key) .map(function(tag) { return '/' + tag; } )
 .reverse()
 .join('><')
 : setHtmlAttrs(getHtml(rules, key).join('><'), attrs)); 
 return '<' + output + '>';
};
/* Converts bbCode to HTML */
function bbCodeToHtml(rules, bbCode) {
 /* Creates the Regular Expression Search */
 var regex = new RegExp( ['\\[.?[',
 Object.keys(rules).join('|'), 
 '].*?\\]'].join(''), 'g' );
 return bbCode.match(regex).reduce(function(htmlCode, tag) {
 return replaceAll(tag, 
 htmlCode, 
 bbTagToHtmlTag(rules, tag));
 }, bbCode);
};

I was inspired to write a BBCode to HTML converter as I'm currently learning functional programming. I wanted achieve functional cohesion. jsFiddle

/* Escapes a string for use with regular expressions */
function escapeString(input) {
 return input.replace(/([,円\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g,
 function(c){return "\\" + c;});
}
/* replaces all */
function replaceAll(search, input, replacement, ignore) {
 return input.replace(
 new RegExp(escapeString(search), "g"+(ignore?"i":"")), 
 replacement);
};
/* Check if an index is event */
function isIndexEven(val, index, context) {
 return (index % 2 === 0);
};
/* Sets the attributes on an HTML string given a set of attributes */
function setHtmlAttrs(htmlCode, attrs) {
 return attrs.reduce(function(htmlCode, attr, index) {
 var re = new RegExp('\\$' + (index + 1), 'g');
 return htmlCode.replace(re, attr);
 }, htmlCode);
};
/* Gets the html tag(s) for a bbCode tag */
function eleFragsToHtml(isClosing, elementFragments) {
 return '<' + elementFragments.map(function(frag) {
 return (isClosing ? '/' + frag.join(' ') : frag.join(' '));
 }).join('><') + '>';
};
/* Converts a single bbCode Tag to Html */
function bbTagToHtmlTag(rules, tag) {
 var attrs = tag.replace(/\[\w+|\]/g, '').trim().split(' ');
 var key = tag.replace(/\[\/?| .+|\]/g, '');
 var isClosing = /\//.test(tag);
 var htmlTemplate = (isClosing ? rules[key].map(function(rule) { return rule.filter(isIndexEven)
 }).reverse()
  : rules[key]);
 var output = eleFragsToHtml(isClosing, htmlTemplate); 
 return setHtmlAttrs(output, attrs);
};
/* Converts bbCode to HTML */
function bbCodeToHtml(rules, bbCode) {
 /* Creates the Regular Expression Search */
 var regex = new RegExp( ['\\[.?[',
 Object.keys(rules).join('|'), 
 '].*?\\]'].join(''), 'g' );
 return bbCode.match(regex).reduce(function(htmlCode, tag) {
 return replaceAll(tag, 
 htmlCode, 
 bbTagToHtmlTag(rules, tag));
 }, bbCode);
};
edited tags
Link
konijn
  • 34.2k
  • 5
  • 70
  • 266
Loading
Tweeted twitter.com/#!/StackCodeReview/status/454395255971151873
Updated code
Source Link
Pete
  • 2.3k
  • 2
  • 19
  • 19
Loading
Source Link
Pete
  • 2.3k
  • 2
  • 19
  • 19
Loading
default

AltStyle によって変換されたページ (->オリジナル) /