Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def thefilter(x):
if (self.fix_swedish_a):
x = re.sub(r'\\AA\s+', r'\\AA{}', x)
if (self.encode_utf8_to_latex):
x = latexencode.utf8tolatex(x, non_ascii_only=True)
if (self.encode_latex_to_utf8):
x = latex2text.latex2text(x)
return x
input = "\"\N{LATIN CAPITAL LETTER A WITH GRAVE} votre sant\N{LATIN SMALL LETTER E WITH ACUTE}!\" s'exclama le ma\N{LATIN SMALL LETTER I WITH CIRCUMFLEX}tre de maison \N{LATIN SMALL LETTER A WITH GRAVE} 100%."
self.assertEqual(utf8tolatex(input),
"''{\\`A} votre sant{\\'e}!'' s'exclama le ma{\\^\\i}tre de maison {\\`a} 100{\\%}.")
self.assertEqual(utf8tolatex(input, non_ascii_only=True),
"\"{\\`A} votre sant{\\'e}!\" s'exclama le ma{\\^\\i}tre de maison {\\`a} 100%.")
# TODO: in the future, be clever and avoid breaking macros like this ("\itre"):
self.assertEqual(utf8tolatex(input, brackets=False),
"''\\`A votre sant\\'e!'' s'exclama le ma\\^\\itre de maison \\`a 100\\%.")
ascii_chars_convert = " \" # $ % & \\ _ { } ~ "
self.assertEqual(utf8tolatex(ascii_chars_convert, non_ascii_only=True),
ascii_chars_convert)
self.assertEqual(utf8tolatex(ascii_chars_convert, non_ascii_only=False),
" '' {\\#} {\\$} {\\%} {\\&} {\\textbackslash} {\\_} {\\{} {\\}} {\\textasciitilde} ")
test_bad_chars = "A unicode character: \N{THAI CHARACTER THO THONG}"
# generates warnings -- that's good
with self.assertLogs(logger='pylatexenc.latexencode', level='WARNING') as cm:
self.assertEqual(utf8tolatex(test_bad_chars, substitute_bad_chars=False),
test_bad_chars) # unchanged
with self.assertLogs(logger='pylatexenc.latexencode', level='WARNING') as cm:
self.assertEqual(utf8tolatex(test_bad_chars, substitute_bad_chars=True),
"A unicode character: {\\bfseries ?}")
def test_basic(self):
input = "\"\N{LATIN CAPITAL LETTER A WITH GRAVE} votre sant\N{LATIN SMALL LETTER E WITH ACUTE}!\" s'exclama le ma\N{LATIN SMALL LETTER I WITH CIRCUMFLEX}tre de maison \N{LATIN SMALL LETTER A WITH GRAVE} 100%."
self.assertEqual(utf8tolatex(input),
"''{\\`A} votre sant{\\'e}!'' s'exclama le ma{\\^\\i}tre de maison {\\`a} 100{\\%}.")
self.assertEqual(utf8tolatex(input, non_ascii_only=True),
"\"{\\`A} votre sant{\\'e}!\" s'exclama le ma{\\^\\i}tre de maison {\\`a} 100%.")
# TODO: in the future, be clever and avoid breaking macros like this ("\itre"):
self.assertEqual(utf8tolatex(input, brackets=False),
"''\\`A votre sant\\'e!'' s'exclama le ma\\^\\itre de maison \\`a 100\\%.")
ascii_chars_convert = " \" # $ % & \\ _ { } ~ "
self.assertEqual(utf8tolatex(ascii_chars_convert, non_ascii_only=True),
ascii_chars_convert)
self.assertEqual(utf8tolatex(ascii_chars_convert, non_ascii_only=False),
" '' {\\#} {\\$} {\\%} {\\&} {\\textbackslash} {\\_} {\\{} {\\}} {\\textasciitilde} ")
test_bad_chars = "A unicode character: \N{THAI CHARACTER THO THONG}"
# generates warnings -- that's good
with self.assertLogs(logger='pylatexenc.latexencode', level='WARNING') as cm:
return os.path.join(bdir, x)
with codecs.open(fn('_tmp_uni_chars_test.temp.txt'), 'w', encoding='utf-8') as testf:
for i in range(0x10FFFF):
# iter over all valid unicode characters
try:
chrname = unicodedata.name(unichr(i)) # test if valid, i.e., it has a UNICODE NAME
except ValueError:
continue
line = "0x%04X %-50s |%s|\n"%(i, '['+chrname+']', unichr(i))
# try to encode it using utf8tolatex
try:
enc = utf8tolatex(line, fail_bad_chars=True)
except ValueError:
continue
testf.write(enc)
with codecs.open(fn('uni_chars_test_previous.txt'), 'r', encoding='utf-8') as reff, \
codecs.open(fn('_tmp_uni_chars_test.temp.txt'), 'r', encoding='utf-8') as testf:
a = reff.readlines()
b = testf.readlines()
logging.getLogger().setLevel(loglevel)
logger = logging.getLogger(__name__)
# only check up to the supported unicode range
if sys.maxunicode < 0x10FFFF:
logger.warning("Only checking up to unicode U+%X, you python build doesn't support higher",
sys.maxunicode)
def latex(self, prec=15, nested_scope=None):
"""Return the corresponding math mode latex string."""
del prec # TODO prec ignored
try:
from pylatexenc.latexencode import utf8tolatex
except ImportError:
raise ImportError("To export latex from qasm "
"pylatexenc needs to be installed. Run "
"'pip install pylatexenc' before using this "
"method.")
return utf8tolatex(self.sym(nested_scope))
'pylatexenc installed. Run "pip install '
'pylatexenc" before using the latex or '
'latex_source drawers.')
regex = re.compile(r"(?
vv=[vv,]
vv=[parseFilePath(ii,basedir,folder,iszotero) for\
ii in vv]
# proper way of joining multiple paths?
#vv='}, {'.join(vv)
vv='; '.join(vv)
vv=u'%s' %vv
if vv=='':
continue
else:
kk='file'
#--------------Parse unicode to latex--------------
if type(vv) is list:
fieldvv=[latexencode.utf8tolatex(ii) for ii in vv]
else:
# Leave file path alone
if kk!='file':
fieldvv=latexencode.utf8tolatex(vv)
else:
fieldvv=vv
#----------Add tags to keywords if iszotero----------
if iszotero and (kk=='tags' or kk=='keywords') and not gotkeywords:
if iskeyword:
keywords=getField(metadict,'keywords',[])
else:
keywords=[]
if not isinstance(keywords, list):
keywords=[keywords,]
tags=getField(metadict,'tags',[])
#vv='}, {'.join(vv)
vv='; '.join(vv)
vv=u'%s' %vv
if vv=='':
continue
else:
kk='file'
#--------------Parse unicode to latex--------------
if type(vv) is list:
fieldvv=[latexencode.utf8tolatex(ii) for ii in vv]
else:
# Leave file path alone
if kk!='file':
fieldvv=latexencode.utf8tolatex(vv)
else:
fieldvv=vv
#----------Add tags to keywords if iszotero----------
if iszotero and (kk=='tags' or kk=='keywords') and not gotkeywords:
if iskeyword:
keywords=getField(metadict,'keywords',[])
else:
keywords=[]
if not isinstance(keywords, list):
keywords=[keywords,]
tags=getField(metadict,'tags',[])
if not isinstance(tags, list):
tags=[tags,]
keywords.extend(tags)
keywords=list(set(keywords))
doctype=getField(metadict,'type','article')
if doctype==u'JournalArticle':
doctype='article' #Necessary?
citekey=getField(metadict,'citationkey','citationkey')
#-------------------Get authors-------------------
first=metadict['firstnames']
last=metadict['lastname']
if first is None or last is None:
authors=''
if type(first) is not list and type(last) is not list:
authors='%s, %s' %(last, first)
else:
authors=['%s, %s' %(ii[0],ii[1]) for ii in zip(last,first)]
authors=' and '.join(authors)
authors=latexencode.utf8tolatex(authors)
string='@%s{%s,\n' %(doctype,citekey)
entries=['author = {%s}' %authors,]
#--------------Populate other fields--------------
gotkeywords=False
for kk,vv in metadict.items():
if vv is None:
continue
if kk in ['type','firstnames','lastname','docid']:
continue
if kk in ['year','month','day']:
# Convert float to int to str
try:
vv=str(int(vv))
def generate_latex_label(label):
"""Convert a label to a valid latex string."""
if not HAS_PYLATEX:
raise ImportError('The latex and latex_source drawers need '
'pylatexenc installed. Run "pip install '
'pylatexenc" before using the latex or '
'latex_source drawers.')
regex = re.compile(r"(?