Пример #1
0
    def parse(self,
              raw_container: CharacterContainer) -> List[CharacterContainer]:
        delimiter = self._delimiter
        words = []
        index = 0
        container = CharacterContainer()
        while True:
            if index == len(raw_container.characters):
                break
            character: Character = raw_container.characters[index]
            previous_character = self.get_previous_character(
                raw_container, index)
            next_character = self.get_next_character(raw_container, index)
            if self._is_delimiter(character) and len(container.characters) > 0 and \
                    not self._is_delimiter(previous_character) and not self._is_delimiter(next_character):

                index += 1
                character = raw_container.characters[index]
                words.append(container)
                container = CharacterContainer()

            elif self._is_delimiter(previous_character) and not self._is_delimiter(character) \
                    and not self._is_delimiter(self.get_previous_character(raw_container, index - 1)):

                words.append(container)
                container = CharacterContainer()

            container.append_str(
                character.character.lower() if character.case ==
                CharacterType.UPPERCASE else character.character)
            index += 1

        words.append(container)

        return words
Пример #2
0
    def parse(self,
              raw_container: CharacterContainer) -> List[CharacterContainer]:
        words = []
        index = 0
        self._have_words_started = False
        container = CharacterContainer()
        while True:
            if index == len(raw_container.characters):
                break
            character: Character = raw_container.characters[index]
            is_literal = False
            valid_literal = None
            for literal in self.literals:
                for i, c in enumerate(literal):
                    if i + index < len(
                            raw_container.characters
                    ) and c == raw_container.characters[i + index].character:
                        is_literal = True

                    else:
                        is_literal = False
                        break

                if is_literal:
                    valid_literal = literal
                    break

            if self._has_seen_first_character(raw_container, index) \
                    and (self._is_new_word(raw_container, index) or
                         self._is_series_of_delimiters(raw_container, index)) \
                    and len(container.characters) > 0:

                words.append(container)
                container = CharacterContainer()

            if valid_literal:
                for c in valid_literal:
                    container.append_str(c.lower())
                    index += 1

                self._have_words_started = True
                if index < len(raw_container.characters):
                    words.append(container)
                    container = CharacterContainer()
                    character: Character = raw_container.characters[index]

                else:
                    break

            container.append_str(
                character.character.lower() if character.case ==
                CharacterType.UPPERCASE else character.character)
            index += 1

        words.append(container)

        return words
Пример #3
0
    def perform(self, namespace: Namespace):
        formatter = self._formatter
        parsed_words = namespace.parsed_words
        formatted_strings = []

        for words in parsed_words:
            formatted_strings.append(formatter.format([CharacterContainer(str(word).upper()) for word in words]))

        namespace.formatted_strings = formatted_strings
Пример #4
0
    def perform(self, namespace: Namespace):
        parser = self._parser
        parsed_words = []
        file_object: IO = namespace.file_object

        with file_object as f:
            for line in f.readlines():
                parsed_words.append(parser.parse(CharacterContainer(line.strip())))

        namespace.parsed_words = parsed_words
Пример #5
0
def camel_case_to_constant_case(string: str, literals: List[str] = None):
    words = [
        CharacterContainer(str(word).upper())
        for word in parse_camel_case(string, literals)
    ]
    return CharacterDelimitedFormatter('_').format(words)
Пример #6
0
def parse_snake_case(string: str):
    return CharacterDelimitedParser(CharacterType.UNDERSCORE).parse(
        CharacterContainer(string))
Пример #7
0
def snake_case_to_constant_case(string: str):
    words = [
        CharacterContainer(str(word).upper())
        for word in parse_snake_case(string)
    ]
    return CharacterDelimitedFormatter('_').format(words)
Пример #8
0
def parse_path(string: str):
    return CharacterDelimitedParser(CharacterType.SLASH).parse(
        CharacterContainer(string))
Пример #9
0
def parse_domain(string: str):
    return CharacterDelimitedParser(CharacterType.DOT).parse(
        CharacterContainer(string))
Пример #10
0
def parse_kebab_case(string: str):
    return CharacterDelimitedParser(CharacterType.DASH).parse(
        CharacterContainer(string))
Пример #11
0
def parse_pascal_case(string: str, literals: List[str] = None):
    return CaseDelimitedParser(CharacterType.UPPERCASE,
                               literals).parse(CharacterContainer(string))
Пример #12
0
 def format(self, words: List[CharacterContainer]) -> str:
     return str(
         CharacterContainer(self._delimiter.join([str(w) for w in words])))
Пример #13
0
    def format(self, words: List[CharacterContainer]) -> str:
        format_literals = [
            acronym.lower() for acronym in self._format_literals
        ]
        case_delimited = CharacterContainer()
        first_character_casing = self._first_character_casing
        valid_first_characters = [
            CharacterType.UPPERCASE, CharacterType.LOWERCASE,
            CharacterType.OTHER
        ]
        should_capitalize = False
        is_first_word = True

        for word in words:
            if str(word) in format_literals:
                case_delimited.append_str(
                    self._format_literals[format_literals.index(str(word))])
                is_first_word = False
                should_capitalize = True
            else:
                for character in word:
                    if is_first_word and character.case in valid_first_characters:
                        is_first_word = False
                        if first_character_casing == CharacterType.UPPERCASE:
                            case_delimited.append_str(
                                character.character.upper())

                        elif first_character_casing == CharacterType.LOWERCASE:
                            case_delimited.append_str(
                                character.character.lower())

                    elif should_capitalize:
                        case_delimited.append_str(character.character.upper())
                        should_capitalize = False

                    else:
                        case_delimited.append_str(character.character)

                should_capitalize = True and not is_first_word

        return str(case_delimited)