def _replace_conditional(match, string): """Replaces a conditional match in a transformation.""" conditional_match = _CONDITIONAL.search(string) while conditional_match: start = conditional_match.start() end = _find_closing_brace(string, start + 4) args = _split_conditional(string[start + 4:end - 1]) rv = "" if match.group(int(conditional_match.group(1))): rv = unescape(_replace_conditional(match, args[0])) elif len(args) > 1: rv = unescape(_replace_conditional(match, args[1])) string = string[:start] + rv + string[end:] conditional_match = _CONDITIONAL.search(string) return string
def _replace_conditional(match, string): """Replaces a conditional match in a transformation.""" conditional_match = _CONDITIONAL.search(string) while conditional_match: start = conditional_match.start() end = _find_closing_brace(string, start+4) args = _split_conditional(string[start+4:end-1]) rv = "" if match.group(int(conditional_match.group(1))): rv = unescape(_replace_conditional(match, args[0])) elif len(args) > 1: rv = unescape(_replace_conditional(match, args[1])) string = string[:start] + rv + string[end:] conditional_match = _CONDITIONAL.search(string) return string
def replace(self, match): """Replaces 'match' through the correct replacement string.""" transformed = self._expression # Replace all $? with capture groups transformed = _DOLLAR.subn(lambda m: match.group(int(m.group(1))), transformed)[0] # Replace Case switches def _one_char_case_change(match): """Replaces one character case changes.""" if match.group(1)[0] == "u": return match.group(1)[-1].upper() else: return match.group(1)[-1].lower() transformed = _ONE_CHAR_CASE_SWITCH.subn(_one_char_case_change, transformed)[0] def _multi_char_case_change(match): """Replaces multi character case changes.""" if match.group(1)[0] == "U": return match.group(1)[1:].upper() else: return match.group(1)[1:].lower() transformed = _LONG_CASEFOLDINGS.subn(_multi_char_case_change, transformed)[0] transformed = _replace_conditional(match, transformed) return unescape(fill_in_whitespace(transformed))
def replace(self, match): """Replaces 'match' through the correct replacement string.""" transformed = self._expression # Replace all $? with capture groups transformed = _DOLLAR.subn( lambda m: match.group(int(m.group(1))), transformed)[0] # Replace Case switches def _one_char_case_change(match): """Replaces one character case changes.""" if match.group(1)[0] == 'u': return match.group(1)[-1].upper() else: return match.group(1)[-1].lower() transformed = _ONE_CHAR_CASE_SWITCH.subn( _one_char_case_change, transformed)[0] def _multi_char_case_change(match): """Replaces multi character case changes.""" if match.group(1)[0] == 'U': return match.group(1)[1:].upper() else: return match.group(1)[1:].lower() transformed = _LONG_CASEFOLDINGS.subn( _multi_char_case_change, transformed)[0] transformed = _replace_conditional(match, transformed) return unescape(fill_in_whitespace(transformed))
def _parse(self, stream, indent): for _ in range(8): # ${VISUAL next(stream) if stream.peek() == ":": next(stream) self.alternative_text, char = _parse_till_unescaped_char(stream, '/}') self.alternative_text = unescape(self.alternative_text) if char == '/': # Transformation going on try: self.search = _parse_till_unescaped_char(stream, '/')[0] self.replace = _parse_till_unescaped_char(stream, '/')[0] self.options = _parse_till_closing_brace(stream) except StopIteration: raise RuntimeError( "Invalid ${VISUAL} transformation! Forgot to escape a '/'?") else: self.search = None self.replace = None self.options = None
def _parse(self, stream, indent): for _ in range(8): # ${VISUAL next(stream) if stream.peek() == ":": next(stream) self.alternative_text, char = _parse_till_unescaped_char(stream, "/}") self.alternative_text = unescape(self.alternative_text) if char == "/": # Transformation going on try: self.search = _parse_till_unescaped_char(stream, "/")[0] self.replace = _parse_till_unescaped_char(stream, "/")[0] self.options = _parse_till_closing_brace(stream) except StopIteration: raise RuntimeError( "Invalid ${VISUAL} transformation! Forgot to escape a '/'?" ) else: self.search = None self.replace = None self.options = None