Index: compile_rules_js_convert.py ================================================================== --- compile_rules_js_convert.py +++ compile_rules_js_convert.py @@ -5,13 +5,17 @@ import json def py2js (sCode): "convert Python code to JavaScript code" - # Python 2.x unicode strings - sCode = re.sub('\\b[ur]"', '"', sCode) - sCode = re.sub("\\b[ur]'", "'", sCode) + # Python strings + sCode = sCode.replace('(r"', '("') + sCode = sCode.replace("(r'", "('") + sCode = sCode.replace(' r"', ' "') + sCode = sCode.replace(" r'", " '") + sCode = sCode.replace(',r"', ',"') + sCode = sCode.replace(",r'", ",'") # operators sCode = sCode.replace(" and ", " && ") sCode = sCode.replace(" or ", " || ") sCode = re.sub("\\bnot\\b", "!", sCode) sCode = re.sub("(.+) if (.+) else (.+)", "(\\2) ? \\1 : \\3", sCode) @@ -32,15 +36,15 @@ sCode = sCode.replace(".istitle", ".gl_isTitle") sCode = sCode.replace(".capitalize", ".gl_toCapitalize") sCode = sCode.replace(".strip", ".gl_trim") sCode = sCode.replace(".lstrip", ".gl_trimLeft") sCode = sCode.replace(".rstrip", ".gl_trimRight") - sCode = sCode.replace('.replace("."', ".replace(/\./g") - sCode = sCode.replace('.replace("..."', ".replace(/\.\.\./g") - sCode = re.sub('.replace\("([^"]+)" ?,', ".replace(/\\1/g,", sCode) + sCode = sCode.replace('.replace("."', r".replace(/\./g") + sCode = sCode.replace('.replace("..."', r".replace(/\.\.\./g") + sCode = re.sub(r'.replace\("([^"]+)" ?,', ".replace(/\\1/g,", sCode) # regex - sCode = re.sub('re.search\("([^"]+)", *(m.group\(\\d\))\)', "(\\2.search(/\\1/) >= 0)", sCode) + sCode = re.sub('re.search\\("([^"]+)", *(m.group\\(\\d\\))\\)', "(\\2.search(/\\1/) >= 0)", sCode) sCode = re.sub(".search\\(/\\(\\?i\\)([^/]+)/\\) >= 0\\)", ".search(/\\1/i) >= 0)", sCode) sCode = re.sub('(look\\(sx?[][.a-z:()]*), "\\(\\?i\\)([^"]+)"', "\\1, /\\2/i", sCode) sCode = re.sub('(look\\(sx?[][.a-z:()]*), "([^"]+)"', "\\1, /\\2/", sCode) sCode = re.sub('(look_chk1\\(dDA, sx?[][.a-z:()]*, [0-9a-z.()]+), "\\(\\?i\\)([^"]+)"', "\\1, /\\2/i", sCode) sCode = re.sub('(look_chk1\\(dDA, sx?[][.a-z:()]*, [0-9a-z.()]+), "([^"]+)"', "\\1, /\\2/i", sCode) @@ -58,13 +62,13 @@ sCode = sCode.replace(".start()", ".index") sCode = sCode.replace("m.group()", "m[0]") sCode = re.sub("\\.start\\((\\d+)\\)", ".start[\\1]", sCode) sCode = re.sub("m\\.group\\((\\d+)\\)", "m[\\1]", sCode) # tuples -> lists - sCode = re.sub("\((m\.start\[\\d+\], m\[\\d+\])\)", "[\\1]", sCode) + sCode = re.sub("\\((m\\.start\\[\\d+\\], m\\[\\d+\\])\\)", "[\\1]", sCode) # regex - sCode = sCode.replace("\w[\w-]+", "[a-zA-Zà-öÀ-Ö0-9_ø-ÿØ-ßĀ-ʯfi-st][a-zA-Zà-öÀ-Ö0-9_ø-ÿØ-ßĀ-ʯfi-st-]+") + sCode = sCode.replace(r"\w[\w-]+", "[a-zA-Zà-öÀ-Ö0-9_ø-ÿØ-ßĀ-ʯfi-st][a-zA-Zà-öÀ-Ö0-9_ø-ÿØ-ßĀ-ʯfi-st-]+") sCode = sCode.replace(r"/\w/", "/[a-zA-Zà-öÀ-Ö0-9_ø-ÿØ-ßĀ-ʯfi-st]/") sCode = sCode.replace(r"[\w-]", "[a-zA-Zà-öÀ-Ö0-9_ø-ÿØ-ßĀ-ʯfi-st-]") sCode = sCode.replace(r"[\w,]", "[a-zA-Zà-öÀ-Ö0-9_ø-ÿØ-ßĀ-ʯfi-st,]") return sCode