vim-snippets/UltiSnips/rst.snippets

189 lines
4.0 KiB
Plaintext
Raw Normal View History

2013-06-20 03:40:12 -04:00
# -*- coding: utf-8 -*-
###########################################################################
# General Stuff #
###########################################################################
2013-06-19 23:29:44 -04:00
global !p
import re
2013-06-19 23:31:41 -04:00
from collections import Counter
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
def get_popular_code_type(path):
2013-06-20 03:40:12 -04:00
"""
As function name, it will get most popular code type in given file
:param: path, to path to rst file
"""
2013-06-19 23:31:41 -04:00
with open(path) as source:
types = re.findall(r'[:|\.\.\s]code::?\s(\w+)', source.read())
try:
popular_type = Counter(types).most_common()[0][0]
except IndexError:
popular_type = "lua" # Don't break default
return popular_type
2013-06-20 04:21:04 -04:00
def complete(t, opts):
if t:
opts = [ m[len(t):] for m in opts if m.startswith(t) ]
if len(opts) == 1:
return opts[0]
return "({0})".format("|".join(opts))
2013-06-19 23:29:44 -04:00
endglobal
2013-06-20 03:40:12 -04:00
snippet part "Part" b
2013-06-19 23:29:44 -04:00
`!p snip.rv = rst_char_len(t[1])*'#'`
${1:Part name}
2013-06-19 23:29:44 -04:00
`!p snip.rv = rst_char_len(t[1])*'#'`
$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])*'='`
$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])*'-'`
$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])*'^'`
$0
endsnippet
snippet chap "Chapter" b
2013-06-19 23:29:44 -04:00
`!p snip.rv = rst_char_len(t[1])*'*'`
${1:Chapter name}
2013-06-19 23:29:44 -04:00
`!p snip.rv = rst_char_len(t[1])*'*'`
$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])*'"'`
$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
###########################################################################
# 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)`}
${2:code}
$0
endsnippet
2013-06-19 03:35:26 -04:00
snippet img "Image Block" b
.. |${2:alias}| image:: ${1:img}
endsnippet
snippet fig "Figure Block" b
.. figure:: ${1:img}
:alt: ${2:alter text}
${3}
$0
endsnippet
snippet cont "Content Block" b
2013-06-19 05:37:01 -04:00
.. contents:: ${1:Contents Title}
2013-06-19 03:35:26 -04:00
$0
endsnippet
2013-06-20 04:21:04 -04:00
#http://docutils.sourceforge.net/docs/ref/rst/directives.html#specific-admonitions
snippet sa "Specific Admonitions" b
.. $1`!p snip.rv =complete(t[1], ["attention", "caution", "danger",
"error", "hint", "important", "note",
"tip", "warning"])`::
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-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
# vim:ft=snippets: