1
0
Fork 0
mirror of synced 2024-11-15 21:48:57 -05:00
ultimate-vim/sources_non_forked/vim-snippets/UltiSnips/rst.snippets

312 lines
7.3 KiB
Text
Raw Normal View History

2013-07-17 19:06:05 -04:00
# -*- coding: utf-8 -*-
2012-08-16 23:41:25 -04:00
priority -50
2012-08-16 23:41:25 -04:00
###########################################################################
# General Stuff #
2012-08-16 23:41:25 -04:00
###########################################################################
2013-07-17 19:06:05 -04:00
global !p
from collections import Counter
2019-11-16 10:28:42 -05:00
from vimsnippets import complete, has_cjk, display_width
2013-07-17 19:06:05 -04:00
2019-11-16 10:28:42 -05: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']
2013-07-17 19:06:05 -04:00
TEXT_ROLES_REGEX = r'\.\.\srole::?\s(w+)'
2019-11-16 10:28:42 -05:00
# http://docutils.sourceforge.net/docs/ref/rst/directives.html#specific-admonitions
SPECIFIC_ADMONITIONS = ["attention", "caution", "danger",
"error", "hint", "important", "note",
"tip", "warning"]
2019-11-16 10:28:42 -05:00
# http://docutils.sourceforge.net/docs/ref/rst/directives.html
DIRECTIVES = ['code', 'contents', 'admonition', 'table', 'csv-table', 'list-table',
'class', 'container', 'sidebar', 'topic', 'title',
'role', 'default-role', 'raw']
2013-07-17 19:06:05 -04:00
2019-11-16 10:28:42 -05:00
# DIRECTIVES_WITHOUT_TITLE means directive arguments equal None
DIRECTIVES_WITHOUT_TITLE = ['math', 'meta', 'parsed-literal', 'line-block',
'header', 'compound', 'highlights', 'pull-quote',
'footer', 'epigraph', 'rubric', 'sectnum']
2013-07-17 19:06:05 -04:00
INCLUDABLE_DIRECTIVES = ['image', 'figure', 'include']
2019-11-16 10:28:42 -05:00
# Directives for Subsubsection Definition
DIRECTIVES_FOR_SUBSTITUTION = ['replace', 'unicode', 'date']
2013-07-17 19:06:05 -04:00
2019-11-16 10:28:42 -05:00
# http://www.pygal.org/en/stable/documentation/types/index.html
CHART_TYPES = ["Line", "StackedLine", "HorizontalLine", "Bar", "StackedBar", "HorizontalBar", "Histogram", "XY", "DateLine", "TimeLine", "TimeDeltaLine", "DateTimeLine", "Pie", "Radar", "Box", "Dot", "Funnel", "Gauge", "SolidGauge", "Pyramid", "Treemap"]
2013-07-17 19:06:05 -04:00
def real_filename(filename):
"""pealeextension name off if possible
# i.e. "foo.bar.png will return "foo.bar"
2013-07-17 19:06:05 -04:00
"""
2019-11-16 10:28:42 -05:00
return os.path.splitext(filename)[0]
2013-07-17 19:06:05 -04:00
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
"""
2019-11-16 10:28:42 -05:00
abs_path = os.path.join(os.path.dirname(rst_path), relative_path)
if os.path.isfile(abs_path):
return abs_path
2013-07-17 19:06:05 -04:00
def make_items(times, leading='+'):
"""
make lines with leading char multitimes
:param: times, how many times you need
:param: leading, leading character
"""
times = int(times)
if leading == 1:
msg = ""
2014-08-03 18:02:51 -04:00
for x in range(1, times+1):
msg += "%s. Item\n" % x
return msg
else:
return ("%s Item\n" % leading) * times
2013-07-17 19:06:05 -04:00
def look_up_directives(regex, fpath):
"""
find all directive args in given file
:param: regex, the regex that needs to match
:param: path, to path to rst file
2013-07-17 19:06:05 -04: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-07-17 19:06:05 -04:00
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
2013-07-17 19:06:05 -04:00
endglobal
2012-08-16 23:41:25 -04:00
snippet part "Part" b
2019-11-16 10:28:42 -05:00
`!p snip.rv = display_width(t[1])*'#'`
2014-08-03 18:02:51 -04:00
${1:${VISUAL:Part name}}
2019-11-16 10:28:42 -05:00
`!p snip.rv = display_width(t[1])*'#'`
2012-08-16 23:41:25 -04:00
$0
endsnippet
2014-08-03 18:02:51 -04:00
snippet chap "Chapter" b
2019-11-16 10:28:42 -05:00
`!p snip.rv = display_width(t[1])*'*'`
2014-08-03 18:02:51 -04:00
${1:${VISUAL:Chapter name}}
2019-11-16 10:28:42 -05:00
`!p snip.rv = display_width(t[1])*'*'`
2014-08-03 18:02:51 -04:00
$0
endsnippet
2012-08-16 23:41:25 -04:00
snippet sec "Section" b
2014-08-03 18:02:51 -04:00
${1:${VISUAL:Section name}}
2019-11-16 10:28:42 -05:00
`!p snip.rv = display_width(t[1])*'='`
2012-08-16 23:41:25 -04:00
$0
endsnippet
snippet ssec "Subsection" b
2014-08-03 18:02:51 -04:00
${1:${VISUAL:Subsection name}}
2019-11-16 10:28:42 -05:00
`!p snip.rv = display_width(t[1])*'-'`
2012-08-16 23:41:25 -04:00
$0
endsnippet
snippet sssec "Subsubsection" b
2014-08-03 18:02:51 -04:00
${1:${VISUAL:Subsubsection name}}
2019-11-16 10:28:42 -05:00
`!p snip.rv = display_width(t[1])*'^'`
2012-08-16 23:41:25 -04:00
$0
endsnippet
snippet para "Paragraph" b
2014-08-03 18:02:51 -04:00
${1:${VISUAL:Paragraph name}}
2019-11-16 10:28:42 -05:00
`!p snip.rv = display_width(t[1])*'"'`
2013-07-17 19:06:05 -04:00
$0
endsnippet
snippet em "Emphasize string" i
`!p
2014-08-03 18:02:51 -04:00
# dirty but works with CJK character detection
2013-07-17 19:06:05 -04:00
if has_cjk(vim.current.line):
snip.rv ="\ "`*${1:${VISUAL:Em}}*`!p
if has_cjk(vim.current.line):
snip.rv ="\ "
2013-07-17 19:06:05 -04:00
else:
snip.rv = " "
2013-07-17 19:06:05 -04:00
`$0
endsnippet
2012-08-16 23:41:25 -04:00
2013-07-17 19:06:05 -04:00
snippet st "Strong string" i
`!p
2013-07-17 19:06:05 -04:00
if has_cjk(vim.current.line):
snip.rv ="\ "`**${1:${VISUAL:Strong}}**`!p
if has_cjk(vim.current.line):
snip.rv ="\ "
2013-07-17 19:06:05 -04:00
else:
snip.rv = " "
2013-07-17 19:06:05 -04:00
`$0
endsnippet
snippet "li(st)? (?P<num>\d+)" "List" br
2012-08-16 23:41:25 -04:00
$0
2013-07-17 19:06:05 -04:00
`!p
# usage: li 4<tab>
# which will extand into a unordered list contains 4 items
snip.rv = make_items(match.groupdict()['num'])
`
2012-08-16 23:41:25 -04:00
endsnippet
2013-07-17 19:06:05 -04:00
snippet "ol(st)? (?P<num>\d+)" "Order List" br
$0
`!p
# usage: ol 4<tab>
# which will extand into a ordered list contains 4 items
snip.rv = make_items(match.groupdict()['num'], 1)
`
endsnippet
2012-08-16 23:41:25 -04:00
###########################################################################
# More Specialized Stuff. #
2012-08-16 23:41:25 -04:00
###########################################################################
snippet cb "Code Block" b
2013-07-17 19:06:05 -04:00
.. code-block:: ${1:`!p snip.rv = get_popular_code_type()`}
2012-08-16 23:41:25 -04:00
2014-08-03 18:02:51 -04:00
${2:${VISUAL:code}}
2012-08-16 23:41:25 -04:00
$0
endsnippet
2013-07-17 19:06:05 -04:00
# match snippets :
# img, inc, fig
snippet id "Includable Directives" b
`!p
2019-11-16 10:28:42 -05:00
real_name=real_filename(os.path.basename(t[2]))
2013-07-17 19:06:05 -04:00
di=t[1][:2]
link=""
content=""
if di == 'im':
link = "|{0}|".format(real_name)
2013-07-17 19:06:05 -04:00
if di == 'fi':
content="""
:alt: {0}
2017-02-11 08:01:38 -05:00
{0}""".format(real_name)
2013-07-17 19:06:05 -04:00
`
2014-08-03 18:02:51 -04:00
..`!p snip.rv = " %s" % link if link else ""` $1`!p
snip.rv=complete(t[1], INCLUDABLE_DIRECTIVES)
`:: ${2:${VISUAL:file}}`!p
if content:
snip.rv +=" "+content`
2013-07-17 19:06:05 -04:00
`!p
# Tip of whether file is exist in comment type
if not check_file_exist(path, t[2]):
snip.rv='.. FILE {0} does not exist'.format(t[2])
2013-07-17 19:06:05 -04:00
else:
snip.rv=""
2013-07-17 19:06:05 -04:00
`$0
endsnippet
snippet di "Directives" b
.. $1`!p snip.rv=complete(t[1], DIRECTIVES)`:: $2
2014-08-03 18:02:51 -04:00
${3:${VISUAL:Content}}
2013-07-17 19:06:05 -04:00
$0
endsnippet
2019-11-16 10:28:42 -05:00
snippet dt "Directives without title" b
.. $1`!p snip.rv=complete(t[1], DIRECTIVES_WITHOUT_TITLE)`::
${2:${VISUAL:Content}}
$0
endsnippet
snippet ds "Directives for subscription" b
.. |$1| $2`!p snip.rv=complete(t[2], DIRECTIVES_FOR_SUBSTITUTION)`:: ${3:Content}
2013-07-17 19:06:05 -04:00
$0
endsnippet
snippet sa "Specific Admonitions" b
2019-11-16 10:28:42 -05:00
.. $1`!p snip.rv =complete(t[1], SPECIFIC_ADMONITIONS)`:: $2
2019-11-16 10:28:42 -05:00
${3:${VISUAL:Content}}
2013-07-17 19:06:05 -04:00
$0
endsnippet
2019-11-16 10:28:42 -05:00
# it will be trigger at start of line or after a word
2013-07-17 19:06:05 -04:00
snippet ro "Text Roles" w
\ :$1`!p snip.rv=complete(t[1],
2019-11-16 10:28:42 -05:00
TEXT_ROLES+look_up_directives(TEXT_ROLES_REGEX,
path))`:\`$2\`\
2013-07-17 19:06:05 -04:00
endsnippet
2017-02-11 08:01:38 -05:00
snippet eu "Embedded URI" i
`!p
if has_cjk(vim.current.line):
snip.rv = "\ "`\`${1:${VISUAL:Text}} <${2:URI}>\`_`!p
if has_cjk(vim.current.line):
snip.rv ="\ "
else:
snip.rv = ""
`$0
endsnippet
snippet fnt "Footnote or Citation" i
[${1:Label}]_ $0
.. [$1] ${2:Reference}
endsnippet
# Only for Nikola — Static Site Generator
snippet chart "Pygal chart for Nikola" b
.. chart:: $1`!p snip.rv=complete(t[1], CHART_TYPES)`
:title: '${2:Browser usage evolution (in %)}'
:x_labels: [${3:"2002", "2003", "2004", "2005", "2006", "2007"}]
2019-11-16 10:28:42 -05:00
'Firefox', [None, None, 0, 16.6, 25, 31]
'Chrome', [None, None, None, None, None, None]
'IE', [85.8, 84.6, 84.7, 74.5, 66, 58.6]
'Others', [14.2, 15.4, 15.3, 8.9, 9, 10.4]
$0
endsnippet
2013-07-17 19:06:05 -04:00
############
# Sphinx #
############
snippet sid "SideBar" b
.. sidebar:: ${1:SideBar Title}
2014-08-03 18:02:51 -04:00
${2:${VISUAL:SideBar Content}}
2013-07-17 19:06:05 -04:00
endsnippet
2019-11-16 10:28:42 -05:00
# vim:set list noet sts=0 sw=4 ts=4: