From 07dc2b8ceb8d4e9c1dbcb67beef13a34ac00c50a Mon Sep 17 00:00:00 2001 From: Tomas Janousek Date: Wed, 29 Jul 2020 22:37:03 +0200 Subject: [PATCH] vwtags: Viewports/presets shown using their name only In other words hide the parts behind `|`. Handle vimwiki links correctly. --- extra/vwtags.py | 48 ++++++++++++++++++++++++++------------------ taskwiki/regexp.py | 12 +++++------ tests/test_vwtags.py | 28 ++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 26 deletions(-) diff --git a/extra/vwtags.py b/extra/vwtags.py index 178c7c1..41ce172 100755 --- a/extra/vwtags.py +++ b/extra/vwtags.py @@ -1,35 +1,43 @@ #! /usr/bin/env python3 # -*- coding: utf-8 -*- -import sys +import os import re +import sys -rx_default_media = r"^\s*(={1,6})([^=].*[^=])\1\s*$" -rx_markdown = r"^\s*(#{1,6})([^#].*)$" +if __name__ == '__main__': + path = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + sys.path.insert(0, path) + +from taskwiki import regexp + + +def match_header(line, syntax): + m = re.search(regexp.VIEWPORT[syntax], line) + if m: + return m + + m = re.search(regexp.PRESET[syntax], line) + if m: + return m + + m = re.search(regexp.HEADER[syntax], line) + if m: + return m + + return None def process(file_content, filename, syntax): - if syntax in ("default", "media"): - rx_header = re.compile(rx_default_media) - elif syntax == "markdown": - rx_header = re.compile(rx_markdown) - else: - rx_header = re.compile(rx_default_media + "|" + rx_markdown) - state = [""]*6 for lnum, line in enumerate(file_content): - - match_header = rx_header.match(line) - - if not match_header: + m = match_header(line, syntax) + if not m: continue - match_lvl = match_header.group(1) or match_header.group(3) - match_tag = match_header.group(2) or match_header.group(4) - - cur_lvl = len(match_lvl) - cur_tag = match_tag.strip() - cur_searchterm = "^" + match_header.group(0).rstrip("\r\n") + "$" + cur_lvl = len(m.group('header_start')) + cur_tag = m.group('name').strip() + cur_searchterm = "^" + m.group(0).rstrip("\r\n") + "$" cur_kind = "h" state[cur_lvl-1] = cur_tag diff --git a/taskwiki/regexp.py b/taskwiki/regexp.py index b98e479..4951467 100644 --- a/taskwiki/regexp.py +++ b/taskwiki/regexp.py @@ -45,7 +45,7 @@ VIEWPORT = { 'default': re.compile( r'^' # Starts at the begging of the line - r'[=]+' # Heading begging + r'(?P[=]+)' # Heading begging r'(?P[^=\|\[\{]*)' # Name of the viewport, all before the | sign # Cannot include '[', '=', '|', and '{' r'\|' # Bar @@ -65,7 +65,7 @@ VIEWPORT = { 'markdown': re.compile( r'^' # Starts at the begging of the line - r'[#]+' # Heading begging + r'(?P[#]+)' # Heading begging r'(?P[^#\|\[\{]*)' # Name of the viewport, all before the | sign # Cannot include '[', '#', '|', and '{' r'\|' # Bar @@ -89,7 +89,7 @@ HEADER = { re.compile( r'^' # Starts at the beginning of the line r'(?P[=]+)' # With a positive number of = - r'[^=]+' # Character other than = + r'(?P[^=]+)' # Character other than = r'[=]+' # Positive number of =, closing the header r'\s*' # Allow trailing whitespace ), @@ -97,7 +97,7 @@ HEADER = { re.compile( r'^' # Starts at the beginning of the line r'(?P[#]+)' # With a positive number of # - r'[^#]+' # Character other than # + r'(?P[^#]+)' # Character other than # ) } @@ -106,7 +106,7 @@ PRESET = { re.compile( r'^' # Starts at the beginning of the line r'(?P[=]+)' # With a positive number of = - r'([^=\|\[\{]*)' # Heading caption, everything up to || + r'(?P[^=\|\[\{]*)' # Heading caption, everything up to || # Cannot include '[', '=', '|, and '{' r'\|\|' # Delimiter r'(?P[^=\|]+?)' # Filter preset @@ -121,7 +121,7 @@ PRESET = { re.compile( r'^' # Starts at the beginning of the line r'(?P[#]+)' # With a positive number of # - r'([^#\|\[\{]*)' # Heading caption, everything up to || + r'(?P[^#\|\[\{]*)' # Heading caption, everything up to || # Cannot include '[', '#', '|, and '{' r'\|\|' # Delimiter r'(?P[^#\|]+?)' # Filter preset diff --git a/tests/test_vwtags.py b/tests/test_vwtags.py index 6bb116d..5620eca 100644 --- a/tests/test_vwtags.py +++ b/tests/test_vwtags.py @@ -65,3 +65,31 @@ class TestTagsBasic(MultiSyntaxTagsTest): re.compile(r"^g file.wiki /.*/;\" h line:7$"), re.compile(r"^h file.wiki /.*/;\" h line:8 header:g$"), ] + + +class TestTagsViewportsPresets(MultiSyntaxTagsTest): + wiki_input = """\ + HEADER1(a) + HEADER2(b | +DUETODAY) + HEADER2(c || +home) + HEADER3(d | +OVERDUE | due:today) + """ + + expected_output = [ + re.compile(r"^a file.wiki /.*/;\" h line:1$"), + re.compile(r"^b file.wiki /.*/;\" h line:2 header:a$"), + re.compile(r"^c file.wiki /.*/;\" h line:3 header:a$"), + re.compile(r"^d file.wiki /.*/;\" h line:4 header:a&&&c$"), + ] + + +class TestTagsVimwikiLinks(TagsTest): + wiki_input = """\ + = [[link]] = + == [[li|nk]] == + """ + + expected_output = """\ + [[link]] file.wiki /^= [[link]] =$/;" h line:1 + [[li|nk]] file.wiki /^== [[li|nk]] ==$/;" h line:2 header:[[link]] + """