2013-06-20 03:40:12 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-03-17 15:13:47 -04:00
|
|
|
|
|
|
|
###########################################################################
|
|
|
|
# General Stuff #
|
|
|
|
###########################################################################
|
2013-06-19 23:29:44 -04:00
|
|
|
global !p
|
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
|
|
|
|
|
2013-06-21 02:55:09 -04:00
|
|
|
#http://docutils.sourceforge.net/docs/ref/rst/roles.html
|
|
|
|
TEXT_ROLES = ['emphasis','literal','code','math',
|
|
|
|
'pep-reference','rfc-reference',
|
|
|
|
'strong','subscript','superscript',
|
|
|
|
'title-reference','raw']
|
|
|
|
TEXT_ROLES_REGEX = r'\.\.\srole::?\s(w+)'
|
|
|
|
|
|
|
|
#http://docutils.sourceforge.net/docs/ref/rst/directives.html#specific-admonitions
|
|
|
|
SPECIFIC_ADMONITIONS = ["attention", "caution", "danger",
|
|
|
|
"error", "hint", "important", "note",
|
|
|
|
"tip", "warning"]
|
|
|
|
#http://docutils.sourceforge.net/docs/ref/rst/directives.html
|
|
|
|
DIRECTIVES = ['topic','sidebar','math','epigraph',
|
|
|
|
'parsed-literal','code','highlights',
|
|
|
|
'pull-quote','compound','container',
|
|
|
|
'list-table','class','sectnum',
|
|
|
|
'role','default-role','unicode',
|
|
|
|
'raw']
|
|
|
|
|
|
|
|
NONE_CONTENT_DIRECTIVES = ['rubric', 'contents', 'header',
|
|
|
|
'footer', 'date', 'include', 'title'
|
|
|
|
]
|
|
|
|
INCLUDABLE_DIRECTIVES_INDEX = {'img':'image',
|
|
|
|
'fig':'figure',
|
|
|
|
'inc':'include'}
|
|
|
|
|
|
|
|
def real_filename(filename):
|
|
|
|
# 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
|
|
|
|
|
|
|
|
def check_file_exist(rst_path, relative_path):
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
|
|
|
#TODO: File preview
|
|
|
|
#TODO: File list complete
|
|
|
|
|
2013-06-19 23:29:44 -04:00
|
|
|
def rst_char_len(char):
|
2013-06-20 03:40:12 -04:00
|
|
|
"""
|
|
|
|
return len of string which fit in rst
|
|
|
|
For instance:chinese "我" decode as only one character,
|
|
|
|
However, the rst interpreter needs 2 "=" instead of 1.
|
2013-06-19 23:29:44 -04:00
|
|
|
|
2013-06-20 03:40:12 -04:00
|
|
|
:param: char needs to be count
|
|
|
|
"""
|
|
|
|
return len(re.findall(r'[^\u4e00-\u9fff\s]', char))+len(char)
|
2013-06-19 23:31:41 -04:00
|
|
|
|
|
|
|
def make_items(times, leading='+'):
|
2013-06-20 03:40:12 -04:00
|
|
|
"""
|
|
|
|
make lines with leading char multitimes
|
|
|
|
|
|
|
|
:param: times, how many times you need
|
|
|
|
:param: leading, leading character
|
|
|
|
"""
|
2013-06-19 23:31:41 -04:00
|
|
|
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-21 02:55:09 -04:00
|
|
|
def look_up_directives(regex, fpath):
|
2013-06-20 03:40:12 -04:00
|
|
|
"""
|
2013-06-21 02:55:09 -04:00
|
|
|
find all directive args in given file
|
|
|
|
:param: regex, the regex that needs to match
|
2013-06-20 03:40:12 -04:00
|
|
|
:param: path, to path to rst file
|
2013-06-21 02:55:09 -04:00
|
|
|
|
|
|
|
:return: list, empty list if nothing match
|
2013-06-20 03:40:12 -04:00
|
|
|
"""
|
2013-06-21 02:55:09 -04:00
|
|
|
with open(fpath) as source:
|
|
|
|
match = re.findall(regex, source.read())
|
|
|
|
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)
|
2013-06-19 23:31:41 -04:00
|
|
|
try:
|
|
|
|
popular_type = Counter(types).most_common()[0][0]
|
|
|
|
except IndexError:
|
|
|
|
popular_type = "lua" # Don't break default
|
|
|
|
|
|
|
|
return popular_type
|
2013-06-21 02:55:09 -04:00
|
|
|
|
2013-06-20 04:21:04 -04:00
|
|
|
|
|
|
|
def complete(t, opts):
|
2013-06-21 02:55:09 -04:00
|
|
|
msg = "({0})"
|
2013-06-20 04:21:04 -04:00
|
|
|
if t:
|
|
|
|
opts = [ m[len(t):] for m in opts if m.startswith(t) ]
|
|
|
|
if len(opts) == 1:
|
|
|
|
return opts[0]
|
2013-06-21 02:55:09 -04:00
|
|
|
|
|
|
|
if not len(opts):
|
|
|
|
msg = "{0}"
|
|
|
|
return msg.format("|".join(opts))
|
2013-06-20 04:21:04 -04:00
|
|
|
|
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-20 03:40:12 -04:00
|
|
|
*${1:${VISUAL:Em}}* $0
|
2013-06-19 05:37:01 -04:00
|
|
|
endsnippet
|
|
|
|
|
2013-06-20 03:40:12 -04:00
|
|
|
# the CJK characters doesn't had space to sperate them, like "我强调"
|
|
|
|
# should be "我\ *强调*\ "
|
|
|
|
# Therefor we need special snippet
|
2013-06-19 05:37:01 -04:00
|
|
|
snippet ec "Emphasize string (CJK)" w
|
2013-06-20 03:40:12 -04:00
|
|
|
\ *${1:${VISUAL:Em}}*\ $0
|
2013-06-19 05:37:01 -04:00
|
|
|
endsnippet
|
|
|
|
|
|
|
|
snippet st "Strong string" i
|
2013-06-20 03:40:12 -04:00
|
|
|
**${1:${VISUAL:Strong}}** $0
|
2013-06-19 05:37:01 -04:00
|
|
|
endsnippet
|
|
|
|
|
|
|
|
snippet sc "Strong string (CJK)" w
|
2013-06-20 03:40:12 -04:00
|
|
|
\ **${1:${VISUAL:Strong}}**\ $0
|
2013-06-19 05:37:01 -04:00
|
|
|
endsnippet
|
|
|
|
|
|
|
|
snippet "li(st)? (?P<num>\d+)" "List" br
|
|
|
|
$0
|
|
|
|
`!p
|
|
|
|
snip.rv = make_items(match.groupdict()['num'])
|
|
|
|
`
|
|
|
|
endsnippet
|
|
|
|
|
2013-06-19 22:27:05 -04:00
|
|
|
# usage: ol 3<tab>
|
2013-06-19 05:37:01 -04:00
|
|
|
snippet "ol(st)? (?P<num>\d+)" "Order List" br
|
|
|
|
$0
|
|
|
|
`!p
|
|
|
|
snip.rv = make_items(match.groupdict()['num'], 1)
|
|
|
|
`
|
|
|
|
endsnippet
|
2013-03-17 15:13:47 -04:00
|
|
|
###########################################################################
|
|
|
|
# More Specialized Stuff. #
|
|
|
|
###########################################################################
|
|
|
|
snippet cb "Code Block" b
|
2013-06-19 22:15:00 -04:00
|
|
|
.. code-block:: ${1:`!p snip.rv = get_popular_code_type(path)`}
|
2013-03-17 15:13:47 -04:00
|
|
|
|
|
|
|
${2:code}
|
|
|
|
|
|
|
|
$0
|
|
|
|
endsnippet
|
|
|
|
|
2013-06-21 02:55:09 -04:00
|
|
|
# match snippets :
|
|
|
|
# img, inc, fig
|
|
|
|
snippet "(?P<shortcut>^img|inc|fig)" "Includable Block" !br
|
|
|
|
`!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')
|
|
|
|
link=""
|
|
|
|
content=""
|
|
|
|
|
|
|
|
if di in ['image']:
|
|
|
|
link = "|{0}| ".format(real_name)
|
|
|
|
|
|
|
|
if di == 'figure':
|
|
|
|
content="""
|
|
|
|
:alt: {0}
|
|
|
|
{0}""".format(real_name)
|
|
|
|
|
|
|
|
snip.rv = ".. {link}{di}:: ".format(di=di,link=link)
|
|
|
|
`${1: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])
|
|
|
|
else:
|
|
|
|
snip.rv=""
|
|
|
|
`$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
|
|
|
|
2013-06-21 02:55:09 -04: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)`::
|
2013-06-19 03:35:26 -04:00
|
|
|
|
2013-06-20 04:21:04 -04: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
|
|
|
|
snippet ro "Text Roles" b
|
|
|
|
\ :$1`!p snip.rv=complete(t[1],
|
|
|
|
TEXT_ROLES+look_up_directives(TEXT_ROLES_REGEX,
|
|
|
|
path))`:\`$2\`\
|
|
|
|
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}
|
|
|
|
|
|
|
|
${2:SideBar Content}
|
|
|
|
endsnippet
|
2013-03-17 15:13:47 -04:00
|
|
|
# vim:ft=snippets:
|