2013-06-20 03:40:12 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-03-17 15:13:47 -04:00
|
|
|
|
2014-02-26 03:31:33 -05:00
|
|
|
priority -50
|
|
|
|
|
2013-03-17 15:13:47 -04:00
|
|
|
###########################################################################
|
2014-02-26 03:31:33 -05:00
|
|
|
# General Stuff #
|
2013-03-17 15:13:47 -04:00
|
|
|
###########################################################################
|
2013-06-19 23:29:44 -04:00
|
|
|
global !p
|
2013-06-25 21:48:50 -04:00
|
|
|
import vim
|
2013-06-21 02:55:09 -04:00
|
|
|
from os import path as ospath
|
|
|
|
from string import Template
|
2013-06-19 23:29:44 -04:00
|
|
|
import re
|
2013-06-19 23:31:41 -04:00
|
|
|
from collections import Counter
|
|
|
|
|
2014-04-13 04:24:56 -04:00
|
|
|
from vimsnippets import complete
|
|
|
|
|
2013-06-21 02:55:09 -04:00
|
|
|
#http://docutils.sourceforge.net/docs/ref/rst/roles.html
|
|
|
|
TEXT_ROLES = ['emphasis','literal','code','math',
|
2014-02-26 03:31:33 -05:00
|
|
|
'pep-reference','rfc-reference',
|
|
|
|
'strong','subscript','superscript',
|
|
|
|
'title-reference','raw']
|
2013-06-21 02:55:09 -04:00
|
|
|
TEXT_ROLES_REGEX = r'\.\.\srole::?\s(w+)'
|
|
|
|
|
|
|
|
#http://docutils.sourceforge.net/docs/ref/rst/directives.html#specific-admonitions
|
2014-02-26 03:31:33 -05:00
|
|
|
SPECIFIC_ADMONITIONS = ["attention", "caution", "danger",
|
|
|
|
"error", "hint", "important", "note",
|
|
|
|
"tip", "warning"]
|
2013-06-21 02:55:09 -04:00
|
|
|
#http://docutils.sourceforge.net/docs/ref/rst/directives.html
|
|
|
|
DIRECTIVES = ['topic','sidebar','math','epigraph',
|
2014-02-26 03:31:33 -05:00
|
|
|
'parsed-literal','code','highlights',
|
|
|
|
'pull-quote','compound','container',
|
|
|
|
'list-table','class','sectnum',
|
|
|
|
'role','default-role','unicode',
|
|
|
|
'raw']
|
2013-06-21 02:55:09 -04:00
|
|
|
|
|
|
|
NONE_CONTENT_DIRECTIVES = ['rubric', 'contents', 'header',
|
2014-02-26 03:31:33 -05:00
|
|
|
'footer', 'date', 'include', 'title']
|
2013-06-25 21:48:50 -04:00
|
|
|
|
|
|
|
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):
|
2014-02-26 03:31:33 -05:00
|
|
|
"""
|
|
|
|
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
|
2013-06-21 02:55:09 -04:00
|
|
|
|
|
|
|
def real_filename(filename):
|
2014-02-26 03:31:33 -05:00
|
|
|
"""pealeextension name off if possible
|
|
|
|
# i.e. "foo.bar.png will return "foo.bar"
|
2013-06-25 21:48:50 -04:00
|
|
|
"""
|
2014-02-26 03:31:33 -05:00
|
|
|
return ospath.splitext(filename)[0]
|
2013-06-21 02:55:09 -04:00
|
|
|
|
|
|
|
def check_file_exist(rst_path, relative_path):
|
2014-02-26 03:31:33 -05:00
|
|
|
"""
|
|
|
|
For RST file, it can just include files as relative path.
|
|
|
|
|
|
|
|
:param rst_path: absolute path to rst file
|
|
|
|
:param relative_path: path related to rst file
|
|
|
|
:return: relative file's absolute path if file exist
|
|
|
|
"""
|
|
|
|
abs_path = ospath.join(ospath.dirname(rst_path), relative_path)
|
|
|
|
if ospath.isfile(abs_path):
|
|
|
|
return abs_path
|
2013-06-21 02:55:09 -04:00
|
|
|
|
|
|
|
|
2014-07-03 06:52:40 -04:00
|
|
|
try:
|
|
|
|
rst_char_len = vim.strwidth # Requires Vim 7.3+
|
|
|
|
except AttributeError:
|
|
|
|
from unicodedata import east_asian_width # Requires Python 2.4+
|
|
|
|
def rst_char_len(s):
|
|
|
|
"""Return the required over-/underline length for s."""
|
|
|
|
result = 0
|
|
|
|
for c in s:
|
|
|
|
result += 2 if east_asian_width(c) in ('W', 'F') else 1
|
|
|
|
return result
|
2013-06-19 23:29:44 -04:00
|
|
|
|
2013-06-19 23:31:41 -04:00
|
|
|
|
|
|
|
def make_items(times, leading='+'):
|
2014-02-26 03:31:33 -05:00
|
|
|
"""
|
|
|
|
make lines with leading char multitimes
|
|
|
|
|
|
|
|
:param: times, how many times you need
|
|
|
|
:param: leading, leading character
|
|
|
|
"""
|
|
|
|
times = int(times)
|
|
|
|
if leading == 1:
|
|
|
|
msg = ""
|
|
|
|
for x in xrange(1, times+1):
|
|
|
|
msg += "%s. Item\n" % x
|
|
|
|
return msg
|
|
|
|
else:
|
|
|
|
return ("%s Item\n" % leading) * times
|
2013-06-19 23:31:41 -04:00
|
|
|
|
|
|
|
|
2013-06-21 02:55:09 -04:00
|
|
|
def look_up_directives(regex, fpath):
|
2014-02-26 03:31:33 -05:00
|
|
|
"""
|
|
|
|
find all directive args in given file
|
|
|
|
:param: regex, the regex that needs to match
|
|
|
|
:param: path, to path to rst file
|
2013-06-21 02:55:09 -04:00
|
|
|
|
2014-02-26 03:31:33 -05:00
|
|
|
:return: list, empty list if nothing match
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
with open(fpath) as source:
|
|
|
|
match = re.findall(regex, source.read())
|
|
|
|
except IOError:
|
|
|
|
match = []
|
|
|
|
return match
|
2013-06-21 02:55:09 -04:00
|
|
|
|
|
|
|
|
2013-06-25 21:48:50 -04:00
|
|
|
def get_popular_code_type():
|
2014-02-26 03:31:33 -05:00
|
|
|
"""
|
|
|
|
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
|
2013-06-19 23:29:44 -04:00
|
|
|
endglobal
|
2013-06-20 03:40:12 -04:00
|
|
|
|
2013-03-17 15:13:47 -04:00
|
|
|
snippet part "Part" b
|
2013-06-19 23:29:44 -04:00
|
|
|
`!p snip.rv = rst_char_len(t[1])*'#'`
|
2013-03-17 15:13:47 -04:00
|
|
|
${1:Part name}
|
2013-06-19 23:29:44 -04:00
|
|
|
`!p snip.rv = rst_char_len(t[1])*'#'`
|
2013-03-17 15:13:47 -04:00
|
|
|
|
|
|
|
$0
|
|
|
|
endsnippet
|
|
|
|
|
|
|
|
snippet sec "Section" b
|
|
|
|
${1:Section name}
|
2013-06-19 23:29:44 -04:00
|
|
|
`!p snip.rv = rst_char_len(t[1])*'='`
|
2013-03-17 15:13:47 -04:00
|
|
|
|
|
|
|
$0
|
|
|
|
endsnippet
|
|
|
|
|
|
|
|
snippet ssec "Subsection" b
|
|
|
|
${1:Section name}
|
2013-06-19 23:29:44 -04:00
|
|
|
`!p snip.rv = rst_char_len(t[1])*'-'`
|
2013-03-17 15:13:47 -04:00
|
|
|
|
|
|
|
$0
|
|
|
|
endsnippet
|
|
|
|
|
|
|
|
snippet sssec "Subsubsection" b
|
|
|
|
${1:Section name}
|
2013-06-19 23:29:44 -04:00
|
|
|
`!p snip.rv = rst_char_len(t[1])*'^'`
|
2013-03-17 15:13:47 -04:00
|
|
|
|
|
|
|
$0
|
|
|
|
endsnippet
|
|
|
|
|
|
|
|
snippet chap "Chapter" b
|
2013-06-19 23:29:44 -04:00
|
|
|
`!p snip.rv = rst_char_len(t[1])*'*'`
|
2013-03-17 15:13:47 -04:00
|
|
|
${1:Chapter name}
|
2013-06-19 23:29:44 -04:00
|
|
|
`!p snip.rv = rst_char_len(t[1])*'*'`
|
2013-03-17 15:13:47 -04:00
|
|
|
|
|
|
|
$0
|
|
|
|
endsnippet
|
|
|
|
|
|
|
|
snippet para "Paragraph" b
|
|
|
|
${1:Paragraph name}
|
2013-06-19 23:29:44 -04:00
|
|
|
`!p snip.rv = rst_char_len(t[1])*'"'`
|
2013-03-17 15:13:47 -04:00
|
|
|
|
|
|
|
$0
|
|
|
|
endsnippet
|
|
|
|
|
2013-06-19 05:37:01 -04:00
|
|
|
snippet em "Emphasize string" i
|
2013-06-25 21:48:50 -04:00
|
|
|
`!p
|
|
|
|
# dirty but works with CJK charactor detection
|
|
|
|
if has_cjk(vim.current.line):
|
2014-02-26 03:31:33 -05:00
|
|
|
snip.rv ="\ "`*${1:${VISUAL:Em}}*`!p
|
|
|
|
if has_cjk(vim.current.line):
|
|
|
|
snip.rv ="\ "
|
2013-06-25 21:48:50 -04:00
|
|
|
else:
|
2014-02-26 03:31:33 -05:00
|
|
|
snip.rv = " "
|
2013-06-25 21:48:50 -04:00
|
|
|
`$0
|
2013-06-19 05:37:01 -04:00
|
|
|
endsnippet
|
|
|
|
|
|
|
|
snippet st "Strong string" i
|
2014-02-26 03:31:33 -05:00
|
|
|
`!p
|
2013-06-25 21:48:50 -04:00
|
|
|
if has_cjk(vim.current.line):
|
2014-02-26 03:31:33 -05:00
|
|
|
snip.rv ="\ "`**${1:${VISUAL:Strong}}**`!p
|
|
|
|
if has_cjk(vim.current.line):
|
|
|
|
snip.rv ="\ "
|
2013-06-25 21:48:50 -04:00
|
|
|
else:
|
2014-02-26 03:31:33 -05:00
|
|
|
snip.rv = " "
|
2013-06-25 21:48:50 -04:00
|
|
|
`$0
|
2013-06-19 05:37:01 -04:00
|
|
|
endsnippet
|
|
|
|
|
|
|
|
snippet "li(st)? (?P<num>\d+)" "List" br
|
|
|
|
$0
|
|
|
|
`!p
|
2013-06-26 01:57:08 -04:00
|
|
|
# usage: li 4<tab>
|
|
|
|
# which will extand into a unordered list contains 4 items
|
2013-06-19 05:37:01 -04:00
|
|
|
snip.rv = make_items(match.groupdict()['num'])
|
|
|
|
`
|
|
|
|
endsnippet
|
|
|
|
|
|
|
|
snippet "ol(st)? (?P<num>\d+)" "Order List" br
|
|
|
|
$0
|
|
|
|
`!p
|
2013-06-26 01:57:08 -04:00
|
|
|
# usage: ol 4<tab>
|
|
|
|
# which will extand into a ordered list contains 4 items
|
2013-06-19 05:37:01 -04:00
|
|
|
snip.rv = make_items(match.groupdict()['num'], 1)
|
|
|
|
`
|
|
|
|
endsnippet
|
2013-03-17 15:13:47 -04:00
|
|
|
###########################################################################
|
2014-02-26 03:31:33 -05:00
|
|
|
# More Specialized Stuff. #
|
2013-03-17 15:13:47 -04:00
|
|
|
###########################################################################
|
|
|
|
snippet cb "Code Block" b
|
2013-06-25 21:48:50 -04:00
|
|
|
.. code-block:: ${1:`!p snip.rv = get_popular_code_type()`}
|
2013-03-17 15:13:47 -04:00
|
|
|
|
2014-02-26 03:31:33 -05:00
|
|
|
${2:code}
|
2013-03-17 15:13:47 -04:00
|
|
|
|
|
|
|
$0
|
|
|
|
endsnippet
|
|
|
|
|
2013-06-21 02:55:09 -04:00
|
|
|
# match snippets :
|
|
|
|
# img, inc, fig
|
2013-06-25 21:48:50 -04:00
|
|
|
snippet id "Includable Directives" b
|
2014-02-26 03:31:33 -05:00
|
|
|
`!p
|
2013-06-25 21:48:50 -04:00
|
|
|
real_name=real_filename(ospath.basename(t[2]))
|
|
|
|
di=t[1][:2]
|
|
|
|
|
2013-06-21 02:55:09 -04:00
|
|
|
link=""
|
|
|
|
content=""
|
|
|
|
|
2013-06-25 21:48:50 -04:00
|
|
|
if di == 'im':
|
2014-02-26 03:31:33 -05:00
|
|
|
link = "|{0}|".format(real_name)
|
2013-06-21 02:55:09 -04:00
|
|
|
|
2013-06-25 21:48:50 -04:00
|
|
|
if di == 'fi':
|
2014-02-26 03:31:33 -05:00
|
|
|
content="""
|
|
|
|
:alt: {0}
|
|
|
|
{0}""".format(real_name)
|
2013-06-25 21:48:50 -04:00
|
|
|
`
|
|
|
|
..`!p snip.rv = " %s" % link if link else ""` $1`!p snip.rv=complete(t[1], INCLUDABLE_DIRECTIVES)`:: ${2:file}`!p if content:
|
2014-02-26 03:31:33 -05:00
|
|
|
snip.rv +=" "+content`
|
2013-06-21 02:55:09 -04:00
|
|
|
`!p
|
|
|
|
# Tip of whether file is exist in comment type
|
2013-06-25 21:48:50 -04:00
|
|
|
if not check_file_exist(path, t[2]):
|
2014-02-26 03:31:33 -05:00
|
|
|
snip.rv='.. FILE {0} does not exist'.format(t[2])
|
2013-06-21 02:55:09 -04:00
|
|
|
else:
|
2014-02-26 03:31:33 -05:00
|
|
|
snip.rv=""
|
2013-06-21 02:55:09 -04:00
|
|
|
`$0
|
2013-06-19 03:35:26 -04:00
|
|
|
endsnippet
|
|
|
|
|
2013-06-21 02:55:09 -04:00
|
|
|
snippet di "Directives" b
|
|
|
|
.. $1`!p snip.rv=complete(t[1], DIRECTIVES)`:: $2
|
2013-06-19 03:35:26 -04:00
|
|
|
|
2014-02-26 03:31:33 -05:00
|
|
|
${3:Content}
|
2013-06-19 03:35:26 -04:00
|
|
|
$0
|
|
|
|
endsnippet
|
|
|
|
|
2013-06-21 02:55:09 -04:00
|
|
|
snippet nd "None Content Directives" b
|
|
|
|
.. $1`!p snip.rv=complete(t[1], NONE_CONTENT_DIRECTIVES)`:: $2
|
2013-06-19 03:35:26 -04:00
|
|
|
$0
|
|
|
|
endsnippet
|
|
|
|
|
2013-06-20 04:21:04 -04:00
|
|
|
snippet sa "Specific Admonitions" b
|
2013-06-21 02:55:09 -04:00
|
|
|
.. $1`!p snip.rv =complete(t[1], SPECIFIC_ADMONITIONS)`::
|
2014-02-26 03:31:33 -05:00
|
|
|
|
|
|
|
${2:Content}
|
2013-06-19 03:35:26 -04:00
|
|
|
|
|
|
|
$0
|
|
|
|
endsnippet
|
|
|
|
|
2013-06-21 02:55:09 -04:00
|
|
|
#it will be trigger at start of line or after a word
|
2013-06-25 21:48:50 -04:00
|
|
|
snippet ro "Text Roles" w
|
2013-06-21 02:55:09 -04:00
|
|
|
\ :$1`!p snip.rv=complete(t[1],
|
2014-02-26 03:31:33 -05:00
|
|
|
TEXT_ROLES+look_up_directives(TEXT_ROLES_REGEX,
|
|
|
|
path))`:\`$2\`\
|
2013-06-21 02:55:09 -04:00
|
|
|
endsnippet
|
|
|
|
|
2013-06-19 05:37:01 -04:00
|
|
|
############
|
|
|
|
# Sphinx #
|
|
|
|
############
|
|
|
|
|
2013-06-19 03:35:26 -04:00
|
|
|
snippet sid "SideBar" b
|
|
|
|
.. sidebar:: ${1:SideBar Title}
|
|
|
|
|
2014-02-26 03:31:33 -05:00
|
|
|
${2:SideBar Content}
|
2013-06-19 03:35:26 -04:00
|
|
|
endsnippet
|
2013-03-17 15:13:47 -04:00
|
|
|
# vim:ft=snippets:
|