diff --git a/vwtags.py b/vwtags.py index 2043dcf..b3e032c 100755 --- a/vwtags.py +++ b/vwtags.py @@ -1,9 +1,17 @@ #! /usr/bin/env python3 # -*- coding: utf-8 -*- +""" +source: https://github.com/vimwiki/utils/blob/master/vwtags.py +Forked from the script originally committed on 24 Jun 2014 by EinfachToll. +This script generates ctags-compatible tag information for vimwiki-tagbar +(or the like) integration. +""" from __future__ import print_function +import sys +import re -help_text = """ +help_text = r""" Extracts tags from Vimwiki files. Useful for the Tagbar plugin. Usage: @@ -11,14 +19,14 @@ anywhere and add the following to your .vimrc: let g:tagbar_type_vimwiki = { - \ 'ctagstype':'vimwiki' - \ , 'kinds':['h:header'] - \ , 'sro':'&&&' - \ , 'kind2scope':{'h':'header'} - \ , 'sort':0 - \ , 'ctagsbin':'/path/to/vwtags.py' - \ , 'ctagsargs': 'default' - \ } + \ 'ctagstype':'vimwiki' + \ , 'kinds':['h:header'] + \ , 'sro':'&&&' + \ , 'kind2scope':{'h':'header'} + \ , 'sort':0 + \ , 'ctagsbin':'/path/to/vwtags.py' + \ , 'ctagsargs': 'default' + \ } The value of ctagsargs must be one of 'default', 'markdown' or 'media', whatever syntax you use. However, if you use multiple wikis with different @@ -27,8 +35,14 @@ but there might be erroneously shown headers. """ -import sys -import re + +class Error(Exception): + """Base class for exceptions""" + + +class ReadFileIntoBufferError(Error): + """Exception raising for failed reading file into Buffer attempt""" + if len(sys.argv) < 3: print(help_text) @@ -38,24 +52,32 @@ filename = sys.argv[2] rx_default_media = r"^\s*(={1,6})([^=].*[^=])\1\s*$" rx_markdown = r"^\s*(#{1,6})([^#].*)$" +rx_fenced_code = r"^```[^\r\n]*[a-z]*$(?:\n(?!^```).*)*\n^```" +rx_header = None if syntax in ("default", "media"): rx_header = re.compile(rx_default_media) elif syntax == "markdown": + comp_rx_fcode = re.compile(rx_fenced_code, flags=re.MULTILINE) rx_header = re.compile(rx_markdown) else: rx_header = re.compile(rx_default_media + "|" + rx_markdown) -file_content = [] try: - with open(filename, "r") as vim_buffer: - file_content = vim_buffer.readlines() -except: + with open(filename, 'r') as buffer: + if syntax == "markdown": + file_content = buffer.read() + sub_rx_fcode = comp_rx_fcode.sub("", file_content) + file_content = sub_rx_fcode.split("\n") + else: + file_content = buffer.readlines() +except ReadFileIntoBufferError: + print("Failed to open file") exit() state = [""]*6 -for lnum, line in enumerate(file_content): +for lnum, line in enumerate(file_content): match_header = rx_header.match(line) if not match_header: