|
|
from src.domain.paragraph import Paragraph |
|
|
from src.domain.block import Block |
|
|
|
|
|
INFINITE = 10000 |
|
|
|
|
|
|
|
|
class Container_requirements: |
|
|
|
|
|
def __init__(self, paragraphs: [Paragraph], title: Paragraph = None, level: int = 0, index: [int] = None, |
|
|
father=None, id_=0): |
|
|
if index is None: |
|
|
index = [] |
|
|
self.level = level |
|
|
if not self.level: |
|
|
pass |
|
|
self.title = title |
|
|
self.paragraphs = [] |
|
|
self.all_paragraphs = paragraphs |
|
|
self.children = [] |
|
|
self.index = index |
|
|
self.father = father |
|
|
self.id_ = int(str(1) + str(father.id_) + str(id_)) |
|
|
if paragraphs: |
|
|
self.paragraphs, self.children = self.create_children(paragraphs.copy(), level, index) |
|
|
self.containers = [self] |
|
|
for child in self.children: |
|
|
self.containers += child.containers |
|
|
self.blocks = self.get_blocks_requirements() |
|
|
|
|
|
|
|
|
@property |
|
|
def text(self): |
|
|
text = "" |
|
|
if self.title: |
|
|
text = "Titre " + str(self.level) + " : " + self.title.text + '\n' |
|
|
for p in self.paragraphs: |
|
|
text += p.text + '\n' |
|
|
for child in self.children: |
|
|
text += child.text |
|
|
return text |
|
|
|
|
|
|
|
|
def move(self, position: int, new_father=None): |
|
|
current_father = self.father |
|
|
current_father.children.remove(self) |
|
|
|
|
|
self.rank = new_father.rank + 1 if new_father else 0 |
|
|
self.father = new_father |
|
|
if position < len(new_father.children): |
|
|
new_father.children.insert(position, self) |
|
|
else: |
|
|
new_father.children.append(self) |
|
|
|
|
|
def create_children(self, paragraphs, level, rank) -> ([], []): |
|
|
""" |
|
|
creates children containers or directly attached content |
|
|
and returns the list of containers and contents of level+1 |
|
|
:return: |
|
|
[Content or Container] |
|
|
""" |
|
|
attached_paragraphs = [] |
|
|
container_paragraphs = [] |
|
|
container_title = None |
|
|
children = [] |
|
|
in_children = False |
|
|
level = INFINITE |
|
|
child_id = 0 |
|
|
|
|
|
while paragraphs: |
|
|
p = paragraphs.pop(0) |
|
|
if not in_children and not p.is_structure: |
|
|
attached_paragraphs.append(p) |
|
|
else: |
|
|
in_children = True |
|
|
if p.is_structure and p.level <= level: |
|
|
if container_paragraphs or container_title: |
|
|
children.append(Container_requirements(container_paragraphs, container_title, level, rank, self, child_id)) |
|
|
child_id += 1 |
|
|
container_paragraphs = [] |
|
|
container_title = p |
|
|
level = p.level |
|
|
|
|
|
else: |
|
|
container_paragraphs.append(p) |
|
|
|
|
|
if container_paragraphs or container_title: |
|
|
children.append(Container_requirements(container_paragraphs, container_title, level, rank, self, child_id)) |
|
|
child_id += 1 |
|
|
|
|
|
return attached_paragraphs, children |
|
|
|
|
|
@property |
|
|
def structure(self): |
|
|
|
|
|
self_structure = {str(self.id_): { |
|
|
'index': str(self.id_), |
|
|
'canMove': True, |
|
|
'isFolder': True, |
|
|
'children': [p.id_ for p in self.paragraphs] + [child.id_ for child in self.children], |
|
|
'canRename': True, |
|
|
'data': {}, |
|
|
'level': self.level, |
|
|
'title': self.title.text if self.title else 'root' |
|
|
}} |
|
|
paragraphs_structure = [p.structure for p in self.paragraphs] |
|
|
structure = [self_structure] + paragraphs_structure |
|
|
for child in self.children: |
|
|
structure += child.structure |
|
|
return structure |
|
|
|
|
|
def get_blocks_requirements(self): |
|
|
block = Block(level=self.level, index=self.index) |
|
|
if self.title: |
|
|
self.title.text = self.title.text.replace('\r', '').replace('\n', '') |
|
|
block.title = self.title.text |
|
|
block.content = self.title.text + '/' |
|
|
temp_father = self.father |
|
|
while temp_father and type(temp_father) == Container_requirements: |
|
|
if temp_father.title: |
|
|
temp_father.title.text = temp_father.title.text.replace('\r', '').replace('\n', '') |
|
|
block.content = temp_father.title.text + '/' + block.content |
|
|
temp_father = temp_father.father |
|
|
block.content += " :\n\n" |
|
|
i = 0 |
|
|
for p in self.paragraphs: |
|
|
if not p.blank: |
|
|
i = 1 |
|
|
if p.text.startswith('##### '): |
|
|
special_action = p.text.lstrip('##### ') |
|
|
block.specials.append(special_action) |
|
|
else: |
|
|
block.content += p.text |
|
|
if i == 0: |
|
|
blocks = [] |
|
|
else: |
|
|
blocks = [block] |
|
|
for child in self.children: |
|
|
blocks += child.blocks |
|
|
return blocks |
|
|
|
|
|
|