-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoutgoing.py
More file actions
131 lines (111 loc) · 4.06 KB
/
outgoing.py
File metadata and controls
131 lines (111 loc) · 4.06 KB
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
class Outgoing:
def line_content(self, lc):
from urllib.parse import unquote_plus, urlunparse, ParseResult
match lc:
case str():
return
case {'tag': tag}:
return
case {'cmd': cmd}:
return
case {'link': str() as link, 'linktype': 'simple-link'}:
return
case {'link': str() as note, 'linktype': 'note'}:
return
case {'link': str() as link, 'linktype': 'root-link'}:
# remarkable note link
return
case {'link': ParseResult() as url, 'linktype': 'internal-link'}:
if hasattr(self, 'curr_msg') and \
url.fragment and \
FLAT.exists(url.path.removeprefix('/disc/')):
if self.only_cross_note and self.curr_note == url.path.removeprefix('/disc/'):
return
self.result.append({'msg': self.curr_msg, 'target': {'note': url.path.removeprefix('/disc/'), 'date': unquote_plus(url.fragment)}})
return
case _:
raise NotImplementedError(lc)
def line(self, line):
assert isinstance(line, list)
for line_content in line:
self.line_content(line_content)
def node(self, node):
assert node.keys() == {'indent', 'value', 'children', 'line'}
self.line(node['line'])
for child in node['children']:
self.node(child)
def block(self, block):
if isinstance(block, dict) and {'msg', 'date', 'content'} == block.keys():
self.msg(block)
return
if block == []:
return
assert len(block) > 0
if block[0].keys() == {'line'}:
for line in block:
self.line(line['line'])
else:
for node in block:
self.node(node)
def msg(self, msg):
# {msg,date,content}
self.curr_msg = msg
for line_content in msg['msg']:
self.line_content(line_content)
del self.curr_msg
def root(self, root):
for block in root['children']:
self.block(block)
def section(self,section):
# CONSIDER maybe handling lines
# if 'lines' in section:
# section['lines']
if 'blocks' in section:
for block in section['blocks']:
self.block(block)
elif 'roots' in section:
for root in section['roots']:
self.root(root)
def page(self, page):
for section in page:
self.section(section)
def __init__(self, note, only_cross_note=True):
self.only_cross_note = only_cross_note
if isinstance(note, dict) and note.keys() == {'note', 'rewrite'}:
self.curr_note = note['note']
self.result = list()
self.page(note['rewrite'])
else:
self.curr_note = note
self.result = list()
page = REWRITE.note(note)
self.page(page)
def tagwrap(tag, o):
return f'<{tag}>{o}</{tag}>'
@app.route('/api/outgoing/<note>')
def get_outgoing(note):
o = Outgoing(note)
result = "\n".join(map(lambda p: str(p[0]) + '\n -> ' + str(p[1]), o.result))
return tagwrap('pre', result)
@app.route('/graph/inout')
def get_inout():
result = list()
inout = GRAPH.collect_inout()
from itertools import chain
result.append(f"{len(inout)} notes with {len(list(chain(*map(lambda v: v['outgoing'], inout.values()))))} references\n")
for i, (note, data) in enumerate(inout.items()):
if len(data['outgoing']) > 0 or 'incoming' in data:
result.append(f"\n<span style='color: purple'>{i:4} note '{FLAT.title(note)}'</span>\n")
# else:
if len(data['outgoing']) > 0:
result.append(' outgoing:')
for out in data['outgoing']:
result.append(DISCUSSION_RENDER.msg(out['msg'], origin_note=note))
result.append(FLASK_UTIL.ESCAPE(f" -> {FLAT.title(out['target']['note'])} @ {out['target']['date']}\n"))
if 'incoming' in data:
result.append(' incoming:')
for incoming in data['incoming']:
result.append(DISCUSSION_RENDER.msg(incoming['msg'], origin_note=incoming['source-note']))
result.append(FLASK_UTIL.ESCAPE(f" <- {FLAT.title(incoming['source-note'])} @ {incoming['msg']['date']}\n"))
result = ''.join(result)
return tagwrap('style', '.msg { display: flex; }\n .msg_timestamp {margin-right: 1em;} ') + tagwrap('pre', result)