|
1
|
1 |
|
import re |
|
2
|
1 |
|
from typing import Dict, List |
|
3
|
|
|
|
|
4
|
1 |
|
from cleo.styles import OutputStyle |
|
5
|
|
|
|
|
6
|
1 |
|
import sdoc |
|
7
|
1 |
|
from sdoc.sdoc2.node.Node import Node |
|
8
|
1 |
|
from sdoc.sdoc2.NodeStore import NodeStore |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
1 |
View Code Duplication |
class TextNode(Node): |
|
|
|
|
|
|
12
|
|
|
""" |
|
13
|
|
|
SDoc2 node for items. |
|
14
|
|
|
""" |
|
15
|
|
|
|
|
16
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
17
|
1 |
|
def __init__(self, io: OutputStyle, options: Dict[str, str], argument: str): |
|
18
|
|
|
""" |
|
19
|
|
|
Object constructor. |
|
20
|
|
|
|
|
21
|
|
|
:param OutputStyle io: The IO object. |
|
22
|
|
|
:param dict[str,str] options: Not used. |
|
23
|
|
|
:param str argument: The actual text. |
|
24
|
|
|
""" |
|
25
|
1 |
|
super().__init__(io, 'TEXT', options, argument) |
|
26
|
|
|
|
|
27
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
28
|
1 |
|
def print_info(self, level: int) -> None: |
|
29
|
|
|
""" |
|
30
|
|
|
Temp function for development. |
|
31
|
|
|
|
|
32
|
|
|
:param int level: the level of block commands. |
|
33
|
|
|
""" |
|
34
|
|
|
self.io.writeln("{0!s}{1:4d} {2!s} {3!s}".format(' ' * 4 * level, self.id, self.name, '')) |
|
35
|
|
|
|
|
36
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
37
|
1 |
|
def get_command(self) -> str: |
|
38
|
|
|
""" |
|
39
|
|
|
Returns the command of this node, i.e. TEXT. |
|
40
|
|
|
""" |
|
41
|
1 |
|
return 'TEXT' |
|
42
|
|
|
|
|
43
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
44
|
1 |
|
def is_block_command(self) -> bool: |
|
45
|
|
|
""" |
|
46
|
|
|
Returns False. |
|
47
|
|
|
""" |
|
48
|
1 |
|
return False |
|
49
|
|
|
|
|
50
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
51
|
1 |
|
def is_inline_command(self) -> bool: |
|
52
|
|
|
""" |
|
53
|
|
|
Returns False. |
|
54
|
|
|
""" |
|
55
|
|
|
return False |
|
56
|
|
|
|
|
57
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
58
|
1 |
|
def is_phrasing(self) -> bool: |
|
59
|
|
|
""" |
|
60
|
|
|
Returns True. |
|
61
|
|
|
""" |
|
62
|
1 |
|
return True |
|
63
|
|
|
|
|
64
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
65
|
1 |
|
def split_by_paragraph(self) -> List[int]: |
|
66
|
|
|
""" |
|
67
|
|
|
Splits this text node into text nodes without a paragraph separator (i.e. a double new line) in to a list of |
|
68
|
|
|
text nodes without paragraph separator each paragraph separator is replace with a end paragraph node. Each |
|
69
|
|
|
paragraph separator is replaced wth a end paragraph node. |
|
70
|
|
|
|
|
71
|
|
|
Returns a list of node IDs. |
|
72
|
|
|
|
|
73
|
|
|
:rtype: list[int] |
|
74
|
|
|
""" |
|
75
|
1 |
|
text_ids = [] |
|
76
|
1 |
|
list_of_texts = re.split("\n\n", self._argument) |
|
77
|
|
|
|
|
78
|
|
|
# Cleaning the text parts. |
|
79
|
1 |
|
if "\n" in list_of_texts: |
|
80
|
1 |
|
list_of_texts[list_of_texts.index("\n")] = ' ' |
|
81
|
1 |
|
if list_of_texts[0] == '': |
|
82
|
1 |
|
list_of_texts.remove('') |
|
83
|
|
|
|
|
84
|
|
|
# Checking the range. |
|
85
|
1 |
|
if list_of_texts: |
|
86
|
1 |
|
if not list_of_texts[-1]: |
|
87
|
1 |
|
to = len(list_of_texts) - 1 |
|
88
|
|
|
else: |
|
89
|
1 |
|
to = len(list_of_texts) |
|
90
|
|
|
|
|
91
|
|
|
# Creating text and paragraph end nodes and put id's in list. |
|
92
|
1 |
|
for text in list_of_texts[:to]: |
|
93
|
1 |
|
text_node = TextNode(self.io, {}, text) |
|
94
|
1 |
|
sdoc.sdoc2.node_store.store_node(text_node) |
|
95
|
1 |
|
text_ids.append(text_node.id) |
|
96
|
|
|
|
|
97
|
1 |
|
end_paragraph_node = sdoc.sdoc2.node_store.create_inline_node('end_paragraph') |
|
98
|
1 |
|
text_ids.append(end_paragraph_node.id) |
|
99
|
|
|
|
|
100
|
|
|
# Checking where we need to add paragraph. |
|
101
|
1 |
|
if text_ids: |
|
102
|
1 |
|
if list_of_texts[-1]: |
|
103
|
1 |
|
text_ids.pop() |
|
104
|
|
|
|
|
105
|
1 |
|
return text_ids |
|
106
|
|
|
|
|
107
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
|
108
|
1 |
|
def prune_whitespace(self, leading: bool = False, trailing: bool = False): |
|
109
|
|
|
""" |
|
110
|
|
|
Method for removing whitespace in text. |
|
111
|
|
|
|
|
112
|
|
|
:param bool leading: if True, remove whitespaces from start. |
|
113
|
|
|
:param bool trailing: if True, remove whitespaces from end. |
|
114
|
|
|
""" |
|
115
|
1 |
|
if leading: |
|
116
|
1 |
|
self._argument = self._argument.lstrip() |
|
117
|
1 |
|
if trailing: |
|
118
|
1 |
|
self._argument = self._argument.rstrip() |
|
119
|
1 |
|
self._argument = re.sub(r'\s+', ' ', self._argument) |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
|
123
|
|
|
NodeStore.register_inline_command('TEXT', TextNode) |
|
124
|
|
|
|