1
|
|
|
""" |
2
|
|
|
SDoc |
3
|
|
|
|
4
|
|
|
Copyright 2016 Set Based IT Consultancy |
5
|
|
|
|
6
|
|
|
Licence MIT |
7
|
|
|
""" |
8
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class Enumerable: |
12
|
|
|
""" |
13
|
|
|
Class for storing information about numeration of heading nodes. |
14
|
|
|
""" |
15
|
|
|
|
16
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
17
|
|
|
def __init__(self): |
18
|
|
|
""" |
19
|
|
|
Object constructor. |
20
|
|
|
""" |
21
|
|
|
|
22
|
|
|
self._numerate_data = {} |
23
|
|
|
|
24
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
25
|
|
|
def get_level(self, level): |
26
|
|
|
""" |
27
|
|
|
Gets the level from numerate data. |
28
|
|
|
|
29
|
|
|
:param: int level: The level. |
30
|
|
|
|
31
|
|
|
:rtype: int |
32
|
|
|
""" |
33
|
|
|
if level in self._numerate_data: |
34
|
|
|
return self._numerate_data[level] |
35
|
|
|
else: |
36
|
|
|
return None |
37
|
|
|
|
38
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
39
|
|
|
def generate_numeration(self, level): |
40
|
|
|
""" |
41
|
|
|
Sets current enumeration of headings at a heading level. |
42
|
|
|
|
43
|
|
|
:param int level: The level of nested heading. |
44
|
|
|
""" |
45
|
|
|
for num in range(level+1): |
46
|
|
|
if num not in self._numerate_data: |
47
|
|
|
self._numerate_data[num] = 0 |
48
|
|
|
|
49
|
|
|
# Truncate levels. |
50
|
|
|
new_numerate_data = {} |
51
|
|
|
for key in self._numerate_data: |
52
|
|
|
if key <= level: |
53
|
|
|
new_numerate_data[key] = self._numerate_data[key] |
54
|
|
|
|
55
|
|
|
self._numerate_data = new_numerate_data |
56
|
|
|
|
57
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
58
|
|
|
def increment_last_level(self): |
59
|
|
|
""" |
60
|
|
|
Increments the last level in number of a heading number. |
61
|
|
|
""" |
62
|
|
|
last_level = max(self._numerate_data) |
63
|
|
|
self._numerate_data[last_level] += 1 |
64
|
|
|
|
65
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
66
|
|
|
def remove_starting_zeros(self): |
67
|
|
|
""" |
68
|
|
|
Removes starting multiple zero symbols. And lefts one if we have omitted levels. |
69
|
|
|
""" |
70
|
|
|
# Check if we have level 'bigger' than part. |
71
|
|
|
if 1 in self._numerate_data: |
72
|
|
|
# If first encountered level is equal 0, we start passing from level '2' to max level. |
73
|
|
|
# If we have '0' we remove them until we not encountered '0', otherwise we go out the loop. |
74
|
|
|
if self._numerate_data[1] == 0: |
75
|
|
|
for key in range(2, max(self._numerate_data)): |
76
|
|
|
if self._numerate_data[key] == 0: |
77
|
|
|
del self._numerate_data[key] |
78
|
|
|
else: |
79
|
|
|
break |
80
|
|
|
|
81
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
82
|
|
|
def get_string(self): |
83
|
|
|
""" |
84
|
|
|
Returns the string equivalent of levels for future output. |
85
|
|
|
|
86
|
|
|
:rtype: str |
87
|
|
|
""" |
88
|
|
|
numbering = [] |
89
|
|
|
|
90
|
|
|
if max(self._numerate_data) == 0: |
91
|
|
|
return self._numerate_data[0] |
92
|
|
|
else: |
93
|
|
|
for key in self._numerate_data: |
94
|
|
|
# If we need to generate chapter, subsection ... etc. we omit outputting part number. |
95
|
|
|
if key != 0: |
96
|
|
|
numbering.append(self._numerate_data[key]) |
97
|
|
|
|
98
|
|
|
return '.'.join(map(str, numbering)) |
|
|
|
|
99
|
|
|
|
100
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
101
|
|
|
|