Beispiel #1
0
    def build_commands(self, block: Block) -> List[Command]:
        """
        Build all of the manipulation commands for a given block.
        - One pair to set the text.
        - Multiple pairs for styles.
        - Multiple pairs for entities.
        """
        style_commands = Command.from_style_ranges(block)
        entity_commands = Command.from_entity_ranges(block)
        styles_and_entities = style_commands + entity_commands
        styles_and_entities.sort(key=attrgetter("index"))

        return ([Command("start_text", 0)] + styles_and_entities +
                [Command("stop_text", len(block["text"]))])
Beispiel #2
0
 def test_from_entity_ranges_multiple(self):
     self.assertEqual(str(Command.from_entity_ranges({
         'entityRanges': [
             {
                 'offset': 0,
                 'length': 4,
                 'key': 3,
             },
             {
                 'offset': 9,
                 'length': 3,
                 'key': 10,
             },
         ],
     })), str([
         Command('start_entity', 0, '3'),
         Command('stop_entity', 4, '3'),
         Command('start_entity', 9, '10'),
         Command('stop_entity', 12, '10'),
     ]))
 def test_from_entity_ranges_multiple(self):
     self.assertEqual(
         str(
             Command.from_entity_ranges({
                 "entityRanges": [
                     {
                         "offset": 0,
                         "length": 4,
                         "key": 3
                     },
                     {
                         "offset": 9,
                         "length": 3,
                         "key": 10
                     },
                 ]
             })),
         str([
             Command("start_entity", 0, "3"),
             Command("stop_entity", 4, "3"),
             Command("start_entity", 9, "10"),
             Command("stop_entity", 12, "10"),
         ]),
     )