-
Notifications
You must be signed in to change notification settings - Fork 42
/
node.py
1344 lines (1086 loc) · 37.9 KB
/
node.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import re
import itertools
from typing import List, Iterable, Iterator, Optional, Union, Tuple, cast, Dict, Set, Sequence
try:
from collections.abc import Sequence
except ImportError:
from collections import Sequence
from .date import OrgDate, OrgDateClock, OrgDateRepeatedTask, parse_sdc
from .inline import to_plain_text
from .extra import to_rich_text, Rich
from .utils.py3compat import PY3, unicode
def lines_to_chunks(lines: Iterable[str]) -> Iterable[List[str]]:
chunk: List[str] = []
for l in lines:
if RE_NODE_HEADER.search(l):
yield chunk
chunk = []
chunk.append(l)
yield chunk
RE_NODE_HEADER = re.compile(r"^\*+ ")
def parse_heading_level(heading):
"""
Get star-stripped heading and its level
>>> parse_heading_level('* Heading')
('Heading', 1)
>>> parse_heading_level('******** Heading')
('Heading', 8)
>>> parse_heading_level('*') # None since no space after star
>>> parse_heading_level('*bold*') # None
>>> parse_heading_level('not heading') # None
"""
match = RE_HEADING_STARS.search(heading)
if match:
return (match.group(2), len(match.group(1)))
RE_HEADING_STARS = re.compile(r'^(\*+)\s+(.*?)\s*$')
def parse_heading_tags(heading: str) -> Tuple[str, List[str]]:
"""
Get first tags and heading without tags
>>> parse_heading_tags('HEADING')
('HEADING', [])
>>> parse_heading_tags('HEADING :TAG1:TAG2:')
('HEADING', ['TAG1', 'TAG2'])
>>> parse_heading_tags('HEADING: this is still heading :TAG1:TAG2:')
('HEADING: this is still heading', ['TAG1', 'TAG2'])
>>> parse_heading_tags('HEADING :@tag:_tag_:')
('HEADING', ['@tag', '_tag_'])
Here is the spec of tags from Org Mode manual:
Tags are normal words containing letters, numbers, ``_``, and
``@``. Tags must be preceded and followed by a single colon,
e.g., ``:work:``.
-- (info "(org) Tags")
"""
match = RE_HEADING_TAGS.search(heading)
if match:
heading = match.group(1)
tagstr = match.group(2)
tags = tagstr.split(':')
else:
tags = []
return (heading, tags)
# Tags are normal words containing letters, numbers, '_', and '@'. https://orgmode.org/manual/Tags.html
RE_HEADING_TAGS = re.compile(r'(.*?)\s*:([\w@:]+):\s*$')
def parse_heading_todos(heading: str, todo_candidates: List[str]) -> Tuple[str, Optional[str]]:
"""
Get TODO keyword and heading without TODO keyword.
>>> todos = ['TODO', 'DONE']
>>> parse_heading_todos('Normal heading', todos)
('Normal heading', None)
>>> parse_heading_todos('TODO Heading', todos)
('Heading', 'TODO')
"""
for todo in todo_candidates:
if heading == todo:
return ('', todo)
if heading.startswith(todo + ' '):
return (heading[len(todo) + 1:], todo)
return (heading, None)
def parse_heading_priority(heading):
"""
Get priority and heading without priority field.
>>> parse_heading_priority('HEADING')
('HEADING', None)
>>> parse_heading_priority('[#A] HEADING')
('HEADING', 'A')
>>> parse_heading_priority('[#0] HEADING')
('HEADING', '0')
>>> parse_heading_priority('[#A]')
('', 'A')
"""
match = RE_HEADING_PRIORITY.search(heading)
if match:
return (match.group(2), match.group(1))
else:
return (heading, None)
RE_HEADING_PRIORITY = re.compile(r'^\s*\[#([A-Z0-9])\] ?(.*)$')
PropertyValue = Union[str, int]
def parse_property(line: str) -> Tuple[Optional[str], Optional[PropertyValue]]:
"""
Get property from given string.
>>> parse_property(':Some_property: some value')
('Some_property', 'some value')
>>> parse_property(':Effort: 1:10')
('Effort', 70)
"""
prop_key = None
prop_val: Optional[Union[str, int]] = None
match = RE_PROP.search(line)
if match:
prop_key = match.group(1)
prop_val = match.group(2)
if prop_key == 'Effort':
(h, m) = prop_val.split(":", 2)
if h.isdigit() and m.isdigit():
prop_val = int(h) * 60 + int(m)
return (prop_key, prop_val)
RE_PROP = re.compile(r'^\s*:(.*?):\s*(.*?)\s*$')
def parse_comment(line: str): # -> Optional[Tuple[str, Sequence[str]]]: # todo wtf?? it says 'ABCMeta isn't subscriptable??'
"""
Parse special comment such as ``#+SEQ_TODO``
>>> parse_comment('#+SEQ_TODO: TODO | DONE')
('SEQ_TODO', ['TODO | DONE'])
>>> parse_comment('# not a special comment') # None
>>> parse_comment('#+FILETAGS: :tag1:tag2:')
('FILETAGS', ['tag1', 'tag2'])
"""
match = re.match(r'\s*#\+', line)
if match:
end = match.end(0)
comment = line[end:].split(':', maxsplit=1)
if len(comment) >= 2:
key = comment[0]
value = comment[1].strip()
if key.upper() == 'FILETAGS':
# just legacy behaviour; it seems like filetags is the only one that separated by ':'
# see https://orgmode.org/org.html#In_002dbuffer-Settings
return (key, [c.strip() for c in value.split(':') if len(c.strip()) > 0])
else:
return (key, [value])
return None
def parse_seq_todo(line):
"""
Parse value part of SEQ_TODO/TODO/TYP_TODO comment.
>>> parse_seq_todo('TODO | DONE')
(['TODO'], ['DONE'])
>>> parse_seq_todo(' Fred Sara Lucy Mike | DONE ')
(['Fred', 'Sara', 'Lucy', 'Mike'], ['DONE'])
>>> parse_seq_todo('| CANCELED')
([], ['CANCELED'])
>>> parse_seq_todo('REPORT(r) BUG(b) KNOWNCAUSE(k) | FIXED(f)')
(['REPORT', 'BUG', 'KNOWNCAUSE'], ['FIXED'])
See also:
* (info "(org) Per-file keywords")
* (info "(org) Fast access to TODO states")
"""
todo_done = line.split('|', 1)
if len(todo_done) == 2:
(todos, dones) = todo_done
else:
(todos, dones) = (line, '')
strip_fast_access_key = lambda x: x.split('(', 1)[0]
return (list(map(strip_fast_access_key, todos.split())),
list(map(strip_fast_access_key, dones.split())))
class OrgEnv(object):
"""
Information global to the file (e.g, TODO keywords).
"""
def __init__(self, todos=['TODO'], dones=['DONE'],
filename='<undefined>'):
self._todos = list(todos)
self._dones = list(dones)
self._todo_not_specified_in_comment = True
self._filename = filename
self._nodes = []
@property
def nodes(self):
"""
A list of org nodes.
>>> OrgEnv().nodes # default is empty (of course)
[]
>>> from orgparse import loads
>>> loads('''
... * Heading 1
... ** Heading 2
... *** Heading 3
... ''').env.nodes # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
[<orgparse.node.OrgRootNode object at 0x...>,
<orgparse.node.OrgNode object at 0x...>,
<orgparse.node.OrgNode object at 0x...>,
<orgparse.node.OrgNode object at 0x...>]
"""
return self._nodes
def add_todo_keys(self, todos, dones):
if self._todo_not_specified_in_comment:
self._todos = []
self._dones = []
self._todo_not_specified_in_comment = False
self._todos.extend(todos)
self._dones.extend(dones)
@property
def todo_keys(self):
"""
TODO keywords defined for this document (file).
>>> env = OrgEnv()
>>> env.todo_keys
['TODO']
"""
return self._todos
@property
def done_keys(self):
"""
DONE keywords defined for this document (file).
>>> env = OrgEnv()
>>> env.done_keys
['DONE']
"""
return self._dones
@property
def all_todo_keys(self):
"""
All TODO keywords (including DONEs).
>>> env = OrgEnv()
>>> env.all_todo_keys
['TODO', 'DONE']
"""
return self._todos + self._dones
@property
def filename(self):
"""
Return a path to the source file or similar information.
If the org objects are not loaded from a file, this value
will be a string of the form ``<SOME_TEXT>``.
:rtype: str
"""
return self._filename
# parser
def from_chunks(self, chunks):
yield OrgRootNode.from_chunk(self, next(chunks))
for chunk in chunks:
yield OrgNode.from_chunk(self, chunk)
class OrgBaseNode(Sequence):
"""
Base class for :class:`OrgRootNode` and :class:`OrgNode`
.. attribute:: env
An instance of :class:`OrgEnv`.
All nodes in a same file shares same instance.
:class:`OrgBaseNode` is an iterable object:
>>> from orgparse import loads
>>> root = loads('''
... * Heading 1
... ** Heading 2
... *** Heading 3
... * Heading 4
... ''')
>>> for node in root:
... print(node)
<BLANKLINE>
* Heading 1
** Heading 2
*** Heading 3
* Heading 4
Note that the first blank line is due to the root node, as
iteration contains the object itself. To skip that, use
slice access ``[1:]``:
>>> for node in root[1:]:
... print(node)
* Heading 1
** Heading 2
*** Heading 3
* Heading 4
It also supports sequence protocol.
>>> print(root[1])
* Heading 1
>>> root[0] is root # index 0 means itself
True
>>> len(root) # remember, sequence contains itself
5
Note the difference between ``root[1:]`` and ``root[1]``:
>>> for node in root[1]:
... print(node)
* Heading 1
** Heading 2
*** Heading 3
Nodes remember the line number information (1-indexed):
>>> print(root.children[1].linenumber)
5
"""
_body_lines: List[str] # set by the child classes
def __init__(self, env, index=None) -> None:
"""
Create an :class:`OrgBaseNode` object.
:type env: :class:`OrgEnv`
:arg env: This will be set to the :attr:`env` attribute.
"""
self.env = env
self.linenumber = cast(int, None) # set in parse_lines
# content
self._lines: List[str] = []
# FIXME: use `index` argument to set index. (Currently it is
# done externally in `parse_lines`.)
if index is not None:
self._index = index
"""
Index of `self` in `self.env.nodes`.
It must satisfy an equality::
self.env.nodes[self._index] is self
This value is used for quick access for iterator and
tree-like traversing.
"""
def __iter__(self):
yield self
level = self.level
for node in self.env._nodes[self._index + 1:]:
if node.level > level:
yield node
else:
break
def __len__(self):
return sum(1 for _ in self)
def __nonzero__(self):
# As self.__len__ returns non-zero value always this is not
# needed. This function is only for performance.
return True
__bool__ = __nonzero__ # PY3
def __getitem__(self, key):
if isinstance(key, slice):
return itertools.islice(self, key.start, key.stop, key.step)
elif isinstance(key, int):
if key < 0:
key += len(self)
for (i, node) in enumerate(self):
if i == key:
return node
raise IndexError("Out of range {0}".format(key))
else:
raise TypeError("Inappropriate type {0} for {1}"
.format(type(key), type(self)))
# tree structure
def _find_same_level(self, iterable):
for node in iterable:
if node.level < self.level:
return
if node.level == self.level:
return node
@property
def previous_same_level(self):
"""
Return previous node if exists or None otherwise.
>>> from orgparse import loads
>>> root = loads('''
... * Node 1
... * Node 2
... ** Node 3
... ''')
>>> (n1, n2, n3) = list(root[1:])
>>> n1.previous_same_level is None
True
>>> n2.previous_same_level is n1
True
>>> n3.previous_same_level is None # n2 is not at the same level
True
"""
return self._find_same_level(reversed(self.env._nodes[:self._index]))
@property
def next_same_level(self):
"""
Return next node if exists or None otherwise.
>>> from orgparse import loads
>>> root = loads('''
... * Node 1
... * Node 2
... ** Node 3
... ''')
>>> (n1, n2, n3) = list(root[1:])
>>> n1.next_same_level is n2
True
>>> n2.next_same_level is None # n3 is not at the same level
True
>>> n3.next_same_level is None
True
"""
return self._find_same_level(self.env._nodes[self._index + 1:])
# FIXME: cache parent node
def _find_parent(self):
for node in reversed(self.env._nodes[:self._index]):
if node.level < self.level:
return node
def get_parent(self, max_level=None):
"""
Return a parent node.
:arg int max_level:
In the normally structured org file, it is a level
of the ancestor node to return. For example,
``get_parent(max_level=0)`` returns a root node.
In the general case, it specify a maximum level of the
desired ancestor node. If there is no ancestor node
whose level is equal to ``max_level``, this function
try to find an ancestor node which level is smaller
than ``max_level``.
>>> from orgparse import loads
>>> root = loads('''
... * Node 1
... ** Node 2
... ** Node 3
... ''')
>>> (n1, n2, n3) = list(root[1:])
>>> n1.get_parent() is root
True
>>> n2.get_parent() is n1
True
>>> n3.get_parent() is n1
True
For simplicity, accessing :attr:`parent` is alias of calling
:meth:`get_parent` without argument.
>>> n1.get_parent() is n1.parent
True
>>> root.parent is None
True
This is a little bit pathological situation -- but works.
>>> root = loads('''
... * Node 1
... *** Node 2
... ** Node 3
... ''')
>>> (n1, n2, n3) = list(root[1:])
>>> n1.get_parent() is root
True
>>> n2.get_parent() is n1
True
>>> n3.get_parent() is n1
True
Now let's play with `max_level`.
>>> root = loads('''
... * Node 1 (level 1)
... ** Node 2 (level 2)
... *** Node 3 (level 3)
... ''')
>>> (n1, n2, n3) = list(root[1:])
>>> n3.get_parent() is n2
True
>>> n3.get_parent(max_level=2) is n2 # same as default
True
>>> n3.get_parent(max_level=1) is n1
True
>>> n3.get_parent(max_level=0) is root
True
"""
if max_level is None:
max_level = self.level - 1
parent = self._find_parent()
while parent.level > max_level:
parent = parent.get_parent()
return parent
@property
def parent(self):
"""
Alias of :meth:`get_parent()` (calling without argument).
"""
return self.get_parent()
# FIXME: cache children nodes
def _find_children(self):
nodeiter = iter(self.env._nodes[self._index + 1:])
try:
node = next(nodeiter)
except StopIteration:
return
if node.level <= self.level:
return
yield node
last_child_level = node.level
for node in nodeiter:
if node.level <= self.level:
return
if node.level <= last_child_level:
yield node
last_child_level = node.level
@property
def children(self):
"""
A list of child nodes.
>>> from orgparse import loads
>>> root = loads('''
... * Node 1
... ** Node 2
... *** Node 3
... ** Node 4
... ''')
>>> (n1, n2, n3, n4) = list(root[1:])
>>> (c1, c2) = n1.children
>>> c1 is n2
True
>>> c2 is n4
True
Note the difference to ``n1[1:]``, which returns the Node 3 also:
>>> (m1, m2, m3) = list(n1[1:])
>>> m2 is n3
True
"""
return list(self._find_children())
@property
def root(self):
"""
The root node.
>>> from orgparse import loads
>>> root = loads('* Node 1')
>>> n1 = root[1]
>>> n1.root is root
True
"""
root = self
while True:
parent = root.get_parent()
if not parent:
return root
root = parent
# parser
@classmethod
def from_chunk(cls, env, lines):
self = cls(env)
self._lines = lines
self._parse_comments()
return self
def _parse_comments(self):
special_comments: Dict[str, List[str]] = {}
for line in self._lines:
parsed = parse_comment(line)
if parsed:
(key, vals) = parsed
key = key.upper() # case insensitive, so keep as uppercase
special_comments.setdefault(key, []).extend(vals)
self._special_comments = special_comments
# parse TODO keys and store in OrgEnv
for todokey in ['TODO', 'SEQ_TODO', 'TYP_TODO']:
for val in special_comments.get(todokey, []):
self.env.add_todo_keys(*parse_seq_todo(val))
# misc
@property
def level(self):
"""
Level of this node.
:rtype: int
"""
raise NotImplemented
def _get_tags(self, inher=False) -> Set[str]:
"""
Return tags
:arg bool inher:
Mix with tags of all ancestor nodes if ``True``.
:rtype: set
"""
return set()
@property
def tags(self) -> Set[str]:
"""
Tags of this and parent's node.
>>> from orgparse import loads
>>> n2 = loads('''
... * Node 1 :TAG1:
... ** Node 2 :TAG2:
... ''')[2]
>>> n2.tags == set(['TAG1', 'TAG2'])
True
"""
return self._get_tags(inher=True)
@property
def shallow_tags(self) -> Set[str]:
"""
Tags defined for this node (don't look-up parent nodes).
>>> from orgparse import loads
>>> n2 = loads('''
... * Node 1 :TAG1:
... ** Node 2 :TAG2:
... ''')[2]
>>> n2.shallow_tags == set(['TAG2'])
True
"""
return self._get_tags(inher=False)
@staticmethod
def _get_text(text, format='plain'):
if format == 'plain':
return to_plain_text(text)
elif format == 'raw':
return text
elif format == 'rich':
return to_rich_text(text)
else:
raise ValueError('format={0} is not supported.'.format(format))
def get_body(self, format='plain') -> str:
"""
Return a string of body text.
See also: :meth:`get_heading`.
"""
return self._get_text(
'\n'.join(self._body_lines), format) if self._lines else ''
@property
def body(self) -> str:
"""Alias of ``.get_body(format='plain')``."""
return self.get_body()
@property
def body_rich(self) -> Iterator[Rich]:
r = self.get_body(format='rich')
return cast(Iterator[Rich], r) # meh..
@property
def heading(self) -> str:
raise NotImplementedError
def is_root(self):
"""
Return ``True`` when it is a root node.
>>> from orgparse import loads
>>> root = loads('* Node 1')
>>> root.is_root()
True
>>> n1 = root[1]
>>> n1.is_root()
False
"""
return False
def __unicode__(self):
return unicode("\n").join(self._lines)
if PY3:
__str__ = __unicode__
else:
def __str__(self):
return unicode(self).encode('utf-8')
# todo hmm, not sure if it really belongs here and not to OrgRootNode?
def get_file_property_list(self, property):
"""
Return a list of the selected property
"""
vals = self._special_comments.get(property.upper(), None)
return [] if vals is None else vals
def get_file_property(self, property):
"""
Return a single element of the selected property or None if it doesn't exist
"""
vals = self._special_comments.get(property.upper(), None)
if vals is None:
return None
elif len(vals) == 1:
return vals[0]
else:
raise RuntimeError('Multiple values for property {}: {}'.format(property, vals))
class OrgRootNode(OrgBaseNode):
"""
Node to represent a file
See :class:`OrgBaseNode` for other available functions.
"""
@property
def _body_lines(self) -> List[str]: # type: ignore[override]
# todo hacky..
# for root node, the body is whatever is before the first node
return self._lines
@property
def heading(self) -> str:
return ''
def _get_tags(self, inher=False) -> Set[str]:
filetags = self.get_file_property_list('FILETAGS')
return set(filetags)
@property
def level(self):
return 0
def get_parent(self, max_level=None):
return None
def is_root(self):
return True
class OrgNode(OrgBaseNode):
"""
Node to represent normal org node
See :class:`OrgBaseNode` for other available functions.
"""
def __init__(self, *args, **kwds) -> None:
super(OrgNode, self).__init__(*args, **kwds)
# fixme instead of casts, should organize code in such a way that they aren't necessary
self._heading = cast(str, None)
self._level = None
self._tags = cast(List[str], None)
self._todo: Optional[str] = None
self._priority = None
self._properties: Dict[str, PropertyValue] = {}
self._scheduled = OrgDate(None)
self._deadline = OrgDate(None)
self._closed = OrgDate(None)
self._timestamps: List[OrgDate] = []
self._clocklist: List[OrgDateClock] = []
self._body_lines: List[str] = []
self._repeated_tasks: List[OrgDateRepeatedTask] = []
# parser
def _parse_pre(self):
"""Call parsers which must be called before tree structuring"""
self._parse_heading()
# FIXME: make the following parsers "lazy"
ilines: Iterator[str] = iter(self._lines)
try:
next(ilines) # skip heading
except StopIteration:
return
ilines = self._iparse_sdc(ilines)
ilines = self._iparse_clock(ilines)
ilines = self._iparse_properties(ilines)
ilines = self._iparse_repeated_tasks(ilines)
ilines = self._iparse_timestamps(ilines)
self._body_lines = list(ilines)
def _parse_heading(self) -> None:
heading = self._lines[0]
(heading, self._level) = parse_heading_level(heading)
(heading, self._tags) = parse_heading_tags(heading)
(heading, self._todo) = parse_heading_todos(
heading, self.env.all_todo_keys)
(heading, self._priority) = parse_heading_priority(heading)
self._heading = heading
# The following ``_iparse_*`` methods are simple generator based
# parser. See ``_parse_pre`` for how it is used. The principle
# is simple: these methods get an iterator and returns an iterator.
# If the item returned by the input iterator must be dedicated to
# the parser, do not yield the item or yield it as-is otherwise.
def _iparse_sdc(self, ilines: Iterator[str]) -> Iterator[str]:
"""
Parse SCHEDULED, DEADLINE and CLOSED time tamps.
They are assumed be in the first line.
"""
try:
line = next(ilines)
except StopIteration:
return
(self._scheduled, self._deadline, self._closed) = parse_sdc(line)
if not (self._scheduled or
self._deadline or
self._closed):
yield line # when none of them were found
for line in ilines:
yield line
def _iparse_clock(self, ilines: Iterator[str]) -> Iterator[str]:
self._clocklist = []
for line in ilines:
cl = OrgDateClock.from_str(line)
if cl:
self._clocklist.append(cl)
else:
yield line
def _iparse_timestamps(self, ilines: Iterator[str]) -> Iterator[str]:
self._timestamps = []
self._timestamps.extend(OrgDate.list_from_str(self._heading))
for l in ilines:
self._timestamps.extend(OrgDate.list_from_str(l))
yield l
def _iparse_properties(self, ilines: Iterator[str]) -> Iterator[str]:
self._properties = {}
in_property_field = False
for line in ilines:
if in_property_field:
if line.find(":END:") >= 0:
break
else:
(key, val) = parse_property(line)
if key is not None and val is not None:
self._properties.update({key: val})
elif line.find(":PROPERTIES:") >= 0:
in_property_field = True
else:
yield line
for line in ilines:
yield line
def _iparse_repeated_tasks(self, ilines: Iterator[str]) -> Iterator[str]:
self._repeated_tasks = []
for line in ilines:
match = self._repeated_tasks_re.search(line)
if match:
# FIXME: move this parsing to OrgDateRepeatedTask.from_str
mdict = match.groupdict()
done_state = mdict['done']
todo_state = mdict['todo']
date = OrgDate.from_str(mdict['date'])
self._repeated_tasks.append(
OrgDateRepeatedTask(date.start, todo_state, done_state))
else:
yield line
_repeated_tasks_re = re.compile(
r'''
\s+ - \s+
State \s+ "(?P<done> [^"]+)" \s+
from \s+ "(?P<todo> [^"]+)" \s+
\[ (?P<date> [^\]]+) \]''',
re.VERBOSE)
def get_heading(self, format='plain'):
"""
Return a string of head text without tags and TODO keywords.
>>> from orgparse import loads
>>> node = loads('* TODO Node 1').children[0]
>>> node.get_heading()
'Node 1'
It strips off inline markup by default (``format='plain'``).
You can get the original raw string by specifying
``format='raw'``.
>>> node = loads('* [[link][Node 1]]').children[0]
>>> node.get_heading()
'Node 1'
>>> node.get_heading(format='raw')
'[[link][Node 1]]'
"""
return self._get_text(self._heading, format)
@property
def heading(self) -> str:
"""Alias of ``.get_heading(format='plain')``."""
return self.get_heading()
@property
def level(self):
return self._level