'Do \text{} replacements inside of javascript
This takes a \\text{} string from some javascript and does the proper
i18nization of its inner contents. Sometimes, if javascript variables
are inside the contents, it will pull them out and do a string
interpolation using i18n._.
Note that this doesn\'t currently handle pure string concatenation very
well, so something like \\text{" + "hello " + ", " + "world" + "}
will produce something weird, and needs to be fixed manually.
Some examples of replacements:
\\text{Hello, world}  =>  \\text{" + i18n._("Hello, world") + "}
\\text{\' + BLAH + \' is happy}  =>
\\text{\' + i18n._("%(BLAH)s is happy", {BLAH: BLAH}) + \'}'

@staticmethod
def _do_javascript_text_replace(match):

    (text_start, text_content, text_end) = match.groups()
    if MathJaxTextFilter._nonalpha_re.match(text_content):
        return match.group(0)
    search_re = re.compile('["\']\\s*\\+\\s*(.*?)\\s*\\+\\s*["\']', re.DOTALL)
    if ((not re.match('["\']\\s*\\+', text_content)) or (not re.search('\\+\\s*["\']$', text_content)) or (len(search_re.findall(text_content)) != 1)):
        if search_re.search(text_content):
            start_quote = re.search('^[^\'"]*([\'"])', text_content)
            end_quote = re.search('([\'"])[^\'"]*$', text_content)
            var_dict = collections.OrderedDict()
            interp_string = search_re.sub(MathJaxTextFilter.var_replace_builder(var_dict), text_content)
            text_content = ('%s    +    i18n._("%s",    %s)    +    %s' % (start_quote.group(1), interp_string, MathJaxTextFilter._javascript_dumps(var_dict), end_quote.group(1)))
        else:
            text_content = ('"    +    i18n._("%s")    +    "' % text_content)
    return ((text_start + text_content) + text_end)
