From 9ab873b6eaa228d3b5ed0a6641bd812a4030d18b Mon Sep 17 00:00:00 2001 From: Meng Zhuo Date: Wed, 26 Jun 2013 09:48:50 +0800 Subject: [PATCH] A docstring C change img/inc/fig snippet into id U update em/st with CJK detection --- UltiSnips/rst.snippets | 120 ++++++++++++++++++++++++++--------------- 1 file changed, 77 insertions(+), 43 deletions(-) diff --git a/UltiSnips/rst.snippets b/UltiSnips/rst.snippets index cdc2a77..f650318 100644 --- a/UltiSnips/rst.snippets +++ b/UltiSnips/rst.snippets @@ -4,6 +4,7 @@ # General Stuff # ########################################################################### global !p +import vim from os import path as ospath from string import Template import re @@ -29,17 +30,32 @@ DIRECTIVES = ['topic','sidebar','math','epigraph', 'raw'] NONE_CONTENT_DIRECTIVES = ['rubric', 'contents', 'header', - 'footer', 'date', 'include', 'title' - ] -INCLUDABLE_DIRECTIVES_INDEX = {'img':'image', - 'fig':'figure', - 'inc':'include'} + 'footer', 'date', 'include', 'title'] + +INCLUDABLE_DIRECTIVES = ['image', 'figure', 'include'] +# CJK chars +# http://stackoverflow.com/questions/2718196/find-all-chinese-text-in-a-string-using-python-and-regex +CJK_RE = re.compile(u'[⺀-⺙⺛-⻳⼀-⿕々〇〡-〩〸-〺〻㐀-䶵一-鿃豈-鶴侮-頻並-龎]', re.UNICODE) + + +def has_cjk(char): + """ + Detect char contains CJK character + + :param char: characters needs to be detect + """ + try: + CJK_RE.finditer(char).next() + except StopIteration: + return False + else: + return True def real_filename(filename): - # peal extension name off if possible + """peal extension name off if possible # i.e. "foo.bar.png will return "foo.bar" - names=filename.split('.') - return ".".join(names[:-1]) if len(names) > 1 else filename + """ + return ospath.splitext(filename)[0] def check_file_exist(rst_path, relative_path): """ @@ -53,8 +69,6 @@ def check_file_exist(rst_path, relative_path): if ospath.isfile(abs_path): return abs_path -#TODO: File preview -#TODO: File list complete def rst_char_len(char): """ @@ -91,23 +105,40 @@ def look_up_directives(regex, fpath): :return: list, empty list if nothing match """ - with open(fpath) as source: - match = re.findall(regex, source.read()) + try: + with open(fpath) as source: + match = re.findall(regex, source.read()) + except IOError: + match = [] return match -def get_popular_code_type(path): - # find most popular code type in the given rst - types = look_up_directives(r'[:|\.\.\s]code::?\s(\w+)', path) +def get_popular_code_type(): + """ + find most popular code type in the given rst + + :param path: file to detect + + :return: string, most popular code type in file + """ + buf = "".join(vim.current.buffer) + types = re.findall(r'[:|\.\.\s]code::?\s(\w+)', buf) try: popular_type = Counter(types).most_common()[0][0] except IndexError: popular_type = "lua" # Don't break default - return popular_type def complete(t, opts): + """ + get options that start with t + + :param t: query string + :param opts: list that needs to be completed + + :return: a string that start with t + """ msg = "({0})" if t: opts = [ m[len(t):] for m in opts if m.startswith(t) ] @@ -165,22 +196,26 @@ $0 endsnippet snippet em "Emphasize string" i -*${1:${VISUAL:Em}}* $0 -endsnippet - -# the CJK characters doesn't had space to sperate them, like "我强调" -# should be "我\ *强调*\ " -# Therefor we need special snippet -snippet ec "Emphasize string (CJK)" w -\ *${1:${VISUAL:Em}}*\ $0 +`!p +# dirty but works with CJK charactor detection +if has_cjk(vim.current.line): + snip.rv ="\ "`*${1:${VISUAL:Em}}*`!p +if has_cjk(vim.current.line): + snip.rv ="\ " +else: + snip.rv = " " +`$0 endsnippet snippet st "Strong string" i -**${1:${VISUAL:Strong}}** $0 -endsnippet - -snippet sc "Strong string (CJK)" w -\ **${1:${VISUAL:Strong}}**\ $0 +`!p +if has_cjk(vim.current.line): + snip.rv ="\ "`**${1:${VISUAL:Strong}}**`!p +if has_cjk(vim.current.line): + snip.rv ="\ " +else: + snip.rv = " " +`$0 endsnippet snippet "li(st)? (?P\d+)" "List" br @@ -201,7 +236,7 @@ endsnippet # More Specialized Stuff. # ########################################################################### snippet cb "Code Block" b -.. code-block:: ${1:`!p snip.rv = get_popular_code_type(path)`} +.. code-block:: ${1:`!p snip.rv = get_popular_code_type()`} ${2:code} @@ -210,29 +245,28 @@ endsnippet # match snippets : # img, inc, fig -snippet "(?P^img|inc|fig)" "Includable Block" !br +snippet id "Includable Directives" b `!p -#TODO: Import img.template in rst relative path? -real_name=real_filename(ospath.basename(t[1])) -di=INCLUDABLE_DIRECTIVES_INDEX.get(match.groupdict()['shortcut'], 'include') +real_name=real_filename(ospath.basename(t[2])) +di=t[1][:2] + link="" content="" -if di in ['image']: - link = "|{0}| ".format(real_name) +if di == 'im': + link = "|{0}|".format(real_name) -if di == 'figure': +if di == 'fi': content=""" :alt: {0} {0}""".format(real_name) - -snip.rv = ".. {link}{di}:: ".format(di=di,link=link) -`${1:file}`!p if content: +` +..`!p snip.rv = " %s" % link if link else ""` $1`!p snip.rv=complete(t[1], INCLUDABLE_DIRECTIVES)`:: ${2:file}`!p if content: snip.rv +=" "+content` `!p # Tip of whether file is exist in comment type -if not check_file_exist(path, t[1]): - snip.rv='.. FILE {0} does not exist'.format(t[1]) +if not check_file_exist(path, t[2]): + snip.rv='.. FILE {0} does not exist'.format(t[2]) else: snip.rv="" `$0 @@ -259,7 +293,7 @@ $0 endsnippet #it will be trigger at start of line or after a word -snippet ro "Text Roles" b +snippet ro "Text Roles" w \ :$1`!p snip.rv=complete(t[1], TEXT_ROLES+look_up_directives(TEXT_ROLES_REGEX, path))`:\`$2\`\