Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🛑 fenced comments clashing with markdown headers #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 38 additions & 16 deletions vwtags.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
#! /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:
Install Tagbar (https://github.com/preservim/tagbar/). Then, put this file
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
Expand All @@ -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)
Expand All @@ -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:
Expand Down