Created
February 6, 2016 18:46
-
-
Save falkolab/b09fa6784fe91214346b to your computer and use it in GitHub Desktop.
Android: HTML tags and localized strings for Titanium SDK.
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
/* 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); | |
}; | |
var defaultRules = { | |
'b': [ ['strong'] ], | |
'bg': [ ['span', 'style\"background-color:$1\"'] ], | |
'big': [ ['span', 'style=\"font-size: larger;\"'] ], | |
'c': [ ['span', 'style=\"color:$1\"'] ], | |
'f': [ ['span', 'style=\"font-family:$1\"'] ], | |
'i': [ ['em'] ], | |
'img': [ ['a', 'href=\"$1\"'], | |
['img', 'src=\"$1\" style=\"max-height: 200px; max-width: 200px;\"'] ], | |
'p': [ ['pre'] ], | |
'small': [ ['small'] ], | |
's': [ ['span', 'style=\"text-decoration: line-through;\"'] ] | |
}; | |
// source: http://codereview.stackexchange.com/questions/46751/bbcode-to-html-converter-using-functional-programming | |
/* Converts bbCode to HTML */ | |
exports.toHtml = function(bbCode, rules) { | |
rules = _.defaults(rules || {}, defaultRules); | |
/* 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); | |
}; |
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
var html2as = require('nl.fokkezb.html2as'), | |
bbcode2html = require('bbcode2html'); | |
html2as(bbcode2html.toHtml(L('hello_world')), function handle(err, as) { | |
if (err) { | |
Ti.API.error('html2as:', err); | |
} else { | |
$.myLabel.attributedString = as; | |
} | |
}); |
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
<resources> | |
<string name="hello_world">Hello [b]world[/b]</string> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment