Skip links to private notes.

This commit is contained in:
Sergio Martínez Portela 2022-10-25 23:45:30 +02:00
parent 250bcde6d5
commit d023955ee0

View File

@ -57,6 +57,18 @@ ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_PATH = os.path.join(ROOT_DIR, 'static') STATIC_PATH = os.path.join(ROOT_DIR, 'static')
class NonExistingLocalNoteError(AssertionError):
def __init__(self, note_id, src_headline):
AssertionError.__init__(self)
self.note_id = note_id
self.src_headline = src_headline
def get_message(self):
return ("Cannot follow link to '{}' on headline '{}' ({})"
.format(self.note_id,
self.src_headline.id,
self.src_headline.title.get_text().strip()))
def is_git_path(path): def is_git_path(path):
return any([chunk == ".git" for chunk in path.split(os.sep)]) return any([chunk == ".git" for chunk in path.split(os.sep)])
@ -312,10 +324,7 @@ def get_headline_with_name(target_name, doc):
def assert_id_exists(id, src_headline, graph): def assert_id_exists(id, src_headline, graph):
if id not in graph["nodes"]: if id not in graph["nodes"]:
raise AssertionError("Cannot follow link to '{}' on headline '{}' ({})" raise NonExistingLocalNoteError(id, src_headline)
.format(id,
src_headline.id,
src_headline.title.get_text()))
def print_tree(tree, indentation=0, headline=None): def print_tree(tree, indentation=0, headline=None):
# if headline and headline.id != INDEX_ID: # if headline and headline.id != INDEX_ID:
@ -447,6 +456,11 @@ def render_text_tokens(tokens, acc, headline, graph):
elif isinstance(chunk, Link): elif isinstance(chunk, Link):
link_target = chunk.value link_target = chunk.value
is_internal_link = True is_internal_link = True
description = chunk.description
if description is None:
description = chunk.value
try:
if link_target.startswith('id:'): if link_target.startswith('id:'):
assert_id_exists(link_target[3:], headline, graph) assert_id_exists(link_target[3:], headline, graph)
link_target = './' + link_target[3:] + '.node.html' link_target = './' + link_target[3:] + '.node.html'
@ -479,15 +493,15 @@ def render_text_tokens(tokens, acc, headline, graph):
): ):
raise NotImplementedError('Unknown link type: {}' raise NotImplementedError('Unknown link type: {}'
.format(link_target)) .format(link_target))
description = chunk.description
if description is None:
description = chunk.value
acc.append('<a href="{}" class="{}" >{}</a>'.format( acc.append('<a href="{}" class="{}" >{}</a>'.format(
html.escape(link_target), html.escape(link_target),
'internal' if is_internal_link else 'external', 'internal' if is_internal_link else 'external',
html.escape(description), html.escape(description),
)) ))
except NonExistingLocalNoteError as err:
logging.warning(err.get_message())
acc.append(html.escape(description))
# else: # else:
# raise NotImplementedError('TextToken: {}'.format(chunk)) # raise NotImplementedError('TextToken: {}'.format(chunk))
acc.append('</p>') acc.append('</p>')