Passed
Branch master (0442a2)
by P.R.
02:00
created

TextNode.split_by_paragraph()   D

Complexity

Conditions 8

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

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