Feature: a option for converting into ASCII before transforming. Patch by rgiot.

This commit is contained in:
Holger Rapp 2013-10-23 08:16:59 +02:00
commit 331eae1529
4 changed files with 58 additions and 10 deletions

View File

@ -58,7 +58,7 @@ endsnippet
snippet part "Part" b
\part{${1:part name}}
\label{prt:${2:${1/(\w+)|\W+/(?1:\L$0\E:_)/g}}}
\label{prt:${2:${1/(\w+)|\W+/(?1:\L$0\E:_)/ga}}}
${0}
@ -67,7 +67,7 @@ endsnippet
snippet cha "Chapter" b
\chapter{${1:chapter name}}
\label{cha:${2:${1/\\\w+\{(.*?)\}|\\(.)|(\w+)|([^\w\\]+)/(?4:_:\L$1$2$3\E)/g}}}
\label{cha:${2:${1/\\\w+\{(.*?)\}|\\(.)|(\w+)|([^\w\\]+)/(?4:_:\L$1$2$3\E)/ga}}}
${0}
@ -76,7 +76,7 @@ endsnippet
snippet sec "Section" b
\section{${1:section name}}
\label{sec:${2:${1/\\\w+\{(.*?)\}|\\(.)|(\w+)|([^\w\\]+)/(?4:_:\L$1$2$3\E)/g}}}
\label{sec:${2:${1/\\\w+\{(.*?)\}|\\(.)|(\w+)|([^\w\\]+)/(?4:_:\L$1$2$3\E)/ga}}}
${0}
@ -86,7 +86,7 @@ endsnippet
snippet sub "Subsection" b
\subsection{${1:subsection name}}
\label{sub:${2:${1/\\\w+\{(.*?)\}|\\(.)|(\w+)|([^\w\\]+)/(?4:_:\L$1$2$3\E)/g}}}
\label{sub:${2:${1/\\\w+\{(.*?)\}|\\(.)|(\w+)|([^\w\\]+)/(?4:_:\L$1$2$3\E)/ga}}}
${0}
@ -95,7 +95,7 @@ endsnippet
snippet ssub "Subsubsection" b
\subsubsection{${1:subsubsection name}}
\label{ssub:${2:${1/\\\w+\{(.*?)\}|\\(.)|(\w+)|([^\w\\]+)/(?4:_:\L$1$2$3\E)/g}}}
\label{ssub:${2:${1/\\\w+\{(.*?)\}|\\(.)|(\w+)|([^\w\\]+)/(?4:_:\L$1$2$3\E)/ga}}}
${0}
@ -105,7 +105,7 @@ endsnippet
snippet par "Paragraph" b
\paragraph{${1:paragraph name}}
\label{par:${2:${1/\\\w+\{(.*?)\}|\\(.)|(\w+)|([^\w\\]+)/(?4:_:\L$1$2$3\E)/g}}}
\label{par:${2:${1/\\\w+\{(.*?)\}|\\(.)|(\w+)|([^\w\\]+)/(?4:_:\L$1$2$3\E)/ga}}}
${0}
@ -115,7 +115,7 @@ endsnippet
snippet subp "Subparagraph" b
\subparagraph{${1:subparagraph name}}
\label{par:${2:${1/\\\w+\{(.*?)\}|\\(.)|(\w+)|([^\w\\]+)/(?4:_:\L$1$2$3\E)/g}}}
\label{par:${2:${1/\\\w+\{(.*?)\}|\\(.)|(\w+)|([^\w\\]+)/(?4:_:\L$1$2$3\E)/ga}}}
${0}

View File

@ -1155,6 +1155,11 @@ The options can be any combination of >
i - case insensitive
By default, regular expression matching is case sensitive. With this
option, matching is done without regard to case.
a - ascii conversion
By default, transformation are made on the raw utf-8 string. With
this option, matching is done on the corresponding ASCII string
instead, for example 'à' will become 'a'.
This option required the python package 'unidecode'.
The syntax of regular expressions is beyond the scope of this document. Python
regular expressions are used internally, so the python 're' module can be used
@ -1214,6 +1219,18 @@ this is a title
This Is A Title
Demo: ASCII transformation
------------------- SNIP -------------------
snippet ascii "Replace non ascii chars"
${1: an accentued text}
${1/.*/$0/a}
endsnippet
------------------- SNAP -------------------
ascii<tab>à la pêche aux moules
à la pêche aux moules
a la peche aux moules
Demo: Regular expression grouping
This is a clever c-like printf snippet, the second tabstop is only shown
when there is a format (%) character in the first tabstop.
@ -1372,6 +1389,7 @@ Contributors listed in chronological order:
Zhao Cai - zhaocai
John Szakmeister - jszakmeister
Jonas Diemer - diemer
Romain Giot - rgiot
8.2 Snippets *UltiSnips-contrisnippets*

View File

@ -2,9 +2,12 @@
# encoding: utf-8
import re
import sys
from UltiSnips.text_objects._mirror import Mirror
# flag used to display only one time the lack of unidecode
UNIDECODE_ALERT_RAISED = False
class _CleverReplace(object):
"""
This class mimics TextMates replace syntax
@ -102,6 +105,8 @@ class _CleverReplace(object):
class TextObjectTransformation(object):
def __init__(self, token):
self._convert_to_ascii = False
self._find = None
if token.search is None:
return
@ -113,11 +118,22 @@ class TextObjectTransformation(object):
self._match_this_many = 0
if "i" in token.options:
flags |= re.IGNORECASE
if "a" in token.options:
self._convert_to_ascii = True
self._find = re.compile(token.search, flags | re.DOTALL)
self._replace = _CleverReplace(token.replace)
def _transform(self, text):
global UNIDECODE_ALERT_RAISED
if self._convert_to_ascii:
try:
import unidecode
text = unidecode.unidecode(text)
except Exception, e:
if UNIDECODE_ALERT_RAISED == False:
UNIDECODE_ALERT_RAISED = True
sys.stderr.write("Please install unidecode python package in order to be able to make ascii conversions.\n")
if self._find is None:
return text
return self._find.subn(self._replace.replace, text, self._match_this_many)[0]
@ -129,5 +145,3 @@ class Transformation(Mirror, TextObjectTransformation):
def _get_text(self):
return self._transform(self._ts.current_text)

16
test.py
View File

@ -40,6 +40,12 @@ import subprocess
from textwrap import dedent
try:
import unidecode
UNIDECODE_IMPORTED = True
except ImportError:
UNIDECODE_IMPORTED = False
# Some constants for better reading
BS = '\x7f'
ESC = '\x1b'
@ -1473,6 +1479,16 @@ class Transformation_CleverTransformLongLower_ExceptCorrectResult(_VimTest):
keys = "test" + EX + "HALLO"
wanted = "HALLO hallo"
if UNIDECODE_IMPORTED:
class Transformation_SimpleCaseAsciiResult(_VimTest):
snippets = ("ascii", "$1 ${1/(.*)/$1/a}")
keys = "ascii" + EX + "éèàçôïÉÈÀÇÔÏ€"
wanted = "éèàçôïÉÈÀÇÔÏ€ eeacoiEEACOIEU"
class Transformation_LowerCaseAsciiResult(_VimTest):
snippets = ("ascii", "$1 ${1/(.*)/\L$1\E/a}")
keys = "ascii" + EX + "éèàçôïÉÈÀÇÔÏ€"
wanted = "éèàçôïÉÈÀÇÔÏ€ eeacoieeacoieu"
class Transformation_ConditionalInsertionSimple_ExceptCorrectResult(_VimTest):
snippets = ("test", "$1 ${1/(^a).*/(?0:began with an a)/}")
keys = "test" + EX + "a some more text"