Commit e98a5e3c authored by Jean-Benoist Leger's avatar Jean-Benoist Leger

PEP8

parent f938d037
# Copyright (c) 2016, Jean-Benoist Leger <jb@leger.tf>
# All rights reserved.
#
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
......@@ -61,7 +61,7 @@ __title__ = 'flatlatex'
__author__ = 'Jean-Benoist Leger'
__licence__ = 'BSD-2'
version_info = (0,2)
__version__ = '.'.join(map(str,version_info))
version_info = (0, 2)
__version__ = '.'.join(map(str, version_info))
from .conv import converter
# Copyright (c) 2016, Jean-Benoist Leger <jb@leger.tf>
# All rights reserved.
#
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
......@@ -23,13 +23,14 @@
# POSSIBILITY OF SUCH DAMAGE.
from . import data
from .transliteration import transliterator,transliterate
from .transliteration import transliterator, transliterate
import regex
from . import latexfuntypes
from . import parser
from .latexfuntypes import LatexSyntaxError
class converter:
"""flatlatex converter class
......@@ -57,20 +58,20 @@ class converter:
return lambda x: symb
for cmd in data.symbols:
self.__cmds[cmd] = latexfuntypes.latexfun(
makefun(data.symbols[cmd]),
0)
makefun(data.symbols[cmd]),
0)
# combinings
def makefun(comb):
return lambda x: self.__latexfun_comb(comb,x)
return lambda x: self.__latexfun_comb(comb, x)
for cmd in data.combinings:
self.__cmds[cmd] = latexfuntypes.latexfun(
makefun(data.combinings[cmd]),
1)
makefun(data.combinings[cmd]),
1)
# others
self.__cmds[r'\frac'] = latexfuntypes.latexfun(self.__latexfun_frac,2)
self.__cmds[r'\sqrt'] = latexfuntypes.latexfun(self.__latexfun_sqrt,1)
self.__cmds[r'\frac'] = latexfuntypes.latexfun(self.__latexfun_frac, 2)
self.__cmds[r'\sqrt'] = latexfuntypes.latexfun(self.__latexfun_sqrt, 1)
# newcommands
for nc in data.newcommands:
......@@ -91,125 +92,125 @@ class converter:
while(idx < len(parsed)):
element = parsed[idx]
if element[0] == 'oper':
outvec.append(('oper',element[1]))
idx+=1
outvec.append(('oper', element[1]))
idx += 1
continue
if element[0] == 'char':
outvec.append(('char',element[1]))
idx+=1
outvec.append(('char', element[1]))
idx += 1
continue
if element[0] == 'cmd':
try:
pycmd = self.__cmds[element[1]]
except KeyError:
outvec.append(('char',element[1]))
idx+=1
outvec.append(('char', element[1]))
idx += 1
continue
if len(parsed) <= idx+pycmd.nargs:
if len(parsed) <= idx + pycmd.nargs:
raise LatexSyntaxError
args = [
self.convert(parsed[idx+k+1][1])
self.convert(parsed[idx + k + 1][1])
for k in range(pycmd.nargs)
]
outvec.append(('char',pycmd.fun(args)))
idx+=1+pycmd.nargs
outvec.append(('char', pycmd.fun(args)))
idx += 1 + pycmd.nargs
continue
if element[0] == 'subexpr':
outvec.append(('char',self.convert(element[1])))
idx+=1
outvec.append(('char', self.convert(element[1])))
idx += 1
continue
raise Exception
#subandsuperscript
# subandsuperscript
for idx in range(len(outvec)):
if outvec[idx][0] == 'oper':
if len(outvec) <= idx+1:
if len(outvec) <= idx + 1:
raise LatexSyntaxError
if outvec[idx+1][0] == 'oper':
if outvec[idx + 1][0] == 'oper':
raise LatexSyntaxError
for idx in range(len(outvec)):
if outvec[idx][0] == 'oper' and outvec[idx][1] =='^':
if idx+2 < len(outvec):
if outvec[idx+2][0] == 'oper' and outvec[idx+2][1]=='_':
if outvec[idx][0] == 'oper' and outvec[idx][1] == '^':
if idx + 2 < len(outvec):
if outvec[idx + 2][0] == 'oper' and outvec[idx + 2][1] == '_':
# we invert ^ and _
(
outvec[idx],
outvec[idx+1],
outvec[idx+2],
outvec[idx+3],
outvec[idx + 1],
outvec[idx + 2],
outvec[idx + 3],
) = (
outvec[idx+2],
outvec[idx+3],
outvec[idx + 2],
outvec[idx + 3],
outvec[idx],
outvec[idx+1],
outvec[idx + 1],
)
# sub
newoutvec = []
idx = 0
while idx<len(outvec):
if idx+1<len(outvec):
if outvec[idx+1][0] == 'oper' and outvec[idx+1][1] == '_':
while idx < len(outvec):
if idx + 1 < len(outvec):
if outvec[idx + 1][0] == 'oper' and outvec[idx + 1][1] == '_':
newoutvec.append(
(
'char',
self.__indexed(
outvec[idx][1],
outvec[idx+2][1]
outvec[idx + 2][1]
),
)
)
idx+=3
idx += 3
continue
newoutvec.append(outvec[idx])
idx+=1
idx += 1
outvec = newoutvec
# super
newoutvec = []
idx = 0
while idx<len(outvec):
if idx+1<len(outvec):
if outvec[idx+1][0] == 'oper' and outvec[idx+1][1] == '^':
while idx < len(outvec):
if idx + 1 < len(outvec):
if outvec[idx + 1][0] == 'oper' and outvec[idx + 1][1] == '^':
newoutvec.append(
(
'char',
self.__exponent(
outvec[idx][1],
outvec[idx+2][1]
outvec[idx + 2][1]
),
)
)
idx+=3
idx += 3
continue
newoutvec.append(outvec[idx])
idx+=1
idx += 1
outvec = newoutvec
return ''.join([x[1] for x in outvec])
def __indexed(self,a,b):
def __indexed(self, a, b):
f_sub = transliterate(data.subscript)
bsub,ok = f_sub(b)
bsub, ok = f_sub(b)
if self.__is_complex_expr(a):
a = '('+a+')'
a = '(' + a + ')'
if ok:
return a+bsub
return a+'['+b+']'
def __exponent(self,a,b):
return a + bsub
return a + '[' + b + ']'
def __exponent(self, a, b):
f_sup = transliterate(data.superscript)
bsup,ok = f_sup(b)
bsup, ok = f_sup(b)
if self.__is_complex_expr(a):
a = '('+a+')'
a = '(' + a + ')'
if ok:
return a+bsup
return a + bsup
if self.__is_complex_expr(b):
b = '('+b+')'
return a+'^'+b
b = '(' + b + ')'
return a + '^' + b
def __is_complex_expr(self, expr):
return (len(expr)>1)
return (len(expr) > 1)
def __latexfun_comb(self, comb, inputs):
expr = inputs[0]
if(len(expr)==1):
if(len(expr) == 1):
if self.allow_combinings:
return expr + comb[0]
return comb[1] + '(' + expr + ')'
......@@ -225,20 +226,20 @@ class converter:
"""
parsed = parser.parse(one_newcommand)
if not (len(parsed) in (3,6)):
if not (len(parsed) in (3, 6)):
raise LatexSyntaxError
ok=False
ok = False
if parsed[0][0] == 'cmd':
if parsed[0][1] in (
r'\newcommand',
r'\renewcommand',
r'\def'):
ok=True
ok = True
if not ok:
raise LatexSyntaxError
nargs = 0
if len(parsed)==6:
if parsed[2] == ('char','[') and parsed[4] == ('char',']'):
if len(parsed) == 6:
if parsed[2] == ('char', '[') and parsed[4] == ('char', ']'):
nargs = int(parsed[3][1])
else:
raise LatexSyntaxError
......@@ -248,47 +249,43 @@ class converter:
def thefun(args):
expr = cmdexpr
for i in range(len(args)):
expr = regex.sub('#%i'%(i+1),args[i],expr)
expr = regex.sub('#%i' % (i + 1), args[i], expr)
return self.convert(expr)
self.__cmds[cmdname] = latexfuntypes.latexfun(
lambda x: thefun(x),
nargs)
lambda x: thefun(x),
nargs)
return None
def __latexfun_frac(self,inputs):
def __latexfun_frac(self, inputs):
a = inputs[0]
b = inputs[1]
try:
ret = data.known_fracts[(a,b)]
ret = data.known_fracts[(a, b)]
return ret
except KeyError:
pass
if self.allow_zw:
f_sub = transliterate(data.subscript)
f_sup = transliterate(data.superscript)
a_sup,ok_sup = f_sup(a)
b_sub,ok_sub = f_sub(b)
a_sup, ok_sup = f_sup(a)
b_sub, ok_sub = f_sub(b)
if ok_sup and ok_sub:
return a_sup+'⁄'+b_sub
return a_sup + '⁄' + b_sub
if self.__is_complex_expr(a):
a = '('+a+')'
a = '(' + a + ')'
if self.__is_complex_expr(b):
b = '('+b+')'
return a+'/'+b
b = '(' + b + ')'
return a + '/' + b
def __latexfun_sqrt(self,inputs):
def __latexfun_sqrt(self, inputs):
a = inputs[0]
if self.__is_complex_expr(a):
a = '(' + a + ')'
return '√'+a
return '√' + a
# Copyright (c) 2016, Jean-Benoist Leger <jb@leger.tf>
# All rights reserved.
#
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
......@@ -23,23 +23,23 @@
# POSSIBILITY OF SUCH DAMAGE.
known_fracts = {
('1','2'): '½',
('1','3'): '⅓',
('2','3'): '⅔',
('1','4'): '¼',
('3','4'): '¾',
('1','5'): '⅕',
('2','5'): '⅖',
('3','5'): '⅗',
('4','5'): '⅘',
('1','6'): '⅙',
('5','6'): '⅚',
('1','7'): '⅐',
('1','8'): '⅛',
('3','8'): '⅜',
('5','8'): '⅝',
('7','8'): '⅞',
('1','9'): '⅑',
('1', '2'): '½',
('1', '3'): '⅓',
('2', '3'): '⅔',
('1', '4'): '¼',
('3', '4'): '¾',
('1', '5'): '⅕',
('2', '5'): '⅖',
('3', '5'): '⅗',
('4', '5'): '⅘',
('1', '6'): '⅙',
('5', '6'): '⅚',
('1', '7'): '⅐',
('1', '8'): '⅛',
('3', '8'): '⅜',
('5', '8'): '⅝',
('7', '8'): '⅞',
('1', '9'): '⅑',
}
subscript = {
......@@ -140,8 +140,8 @@ superscript = {
'∊': 'ᵋ',
'θ': 'ᶿ',
'ι': 'ᶥ',
'Φ': 'ᶲ',
'φ': 'ᵠ',
'Φ': 'ᶲ',
'φ': 'ᵠ',
'χ': 'ᵡ',
}
......@@ -622,8 +622,6 @@ mono = {
}
symbols = {
r'\_': '_',
r'\\': '\\',
......@@ -967,20 +965,20 @@ symbols = {
}
combinings = {
r'\hat': ('\u0302','hat'),
r'\grave': ('\u0301','grave'),
r'\dot': ('\u0307','dot'),
r'\not': ('\u0338','not'),
r'\overrightarrow': ('\u20d7','overrightarrow'),
r'\overline': ('\u0305','overline'),
r'\tilde': ('\u0303','tilde'),
r'\bar': ('\u0304','bar'),
r'\acute': ('\u0300','acute'),
r'\ddot': ('\u0308','ddot'),
r'\overleftarrow': ('\u20d6','overleftarrow'),
r'\check': ('\u030c','check'),
r'\vec': ('\u20d7','vec'),
r'\underline': ('\u0332','underline'),
r'\hat': ('\u0302', 'hat'),
r'\grave': ('\u0301', 'grave'),
r'\dot': ('\u0307', 'dot'),
r'\not': ('\u0338', 'not'),
r'\overrightarrow': ('\u20d7', 'overrightarrow'),
r'\overline': ('\u0305', 'overline'),
r'\tilde': ('\u0303', 'tilde'),
r'\bar': ('\u0304', 'bar'),
r'\acute': ('\u0300', 'acute'),
r'\ddot': ('\u0308', 'ddot'),
r'\overleftarrow': ('\u20d6', 'overleftarrow'),
r'\check': ('\u030c', 'check'),
r'\vec': ('\u20d7', 'vec'),
r'\underline': ('\u0332', 'underline'),
}
newcommands = (
......
# Copyright (c) 2016, Jean-Benoist Leger <jb@leger.tf>
# All rights reserved.
#
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
......@@ -22,11 +22,13 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
class latexfun:
def __init__(self, fun, nargs):
self.fun = fun
self.nargs = nargs
class LatexSyntaxError(SyntaxError):
pass
# Copyright (c) 2016, Jean-Benoist Leger <jb@leger.tf>
# All rights reserved.
#
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
......@@ -25,25 +25,27 @@
import regex
from .latexfuntypes import LatexSyntaxError
def parse_one_element(s):
R = r'((?>\\(?:[^A-Za-z]|[A-Za-z]+))|(?>[^\{\}\\])|\{(?1)*\})'
r = regex.match(R,s)
r = regex.match(R, s)
if not r:
raise LatexSyntaxError
s = s[r.span()[1]:]
c = r.captures()[0]
if c[0] == '\\':
return ( ('cmd', c), s)
return (('cmd', c), s)
if c[0] == '{':
return ( ('subexpr', c[1:-1]), s)
if c in ('_','^'):
return ( ('oper', c), s)
return ( ('char', c), s)
return (('subexpr', c[1:-1]), s)
if c in ('_', '^'):
return (('oper', c), s)
return (('char', c), s)
def parse(s):
l = []
while len(s)>0:
m,s = parse_one_element(s)
while len(s) > 0:
m, s = parse_one_element(s)
if not (m[1] == ' '):
l.append(m)
return l
# Copyright (c) 2016, Jean-Benoist Leger <jb@leger.tf>
# All rights reserved.
#
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
......@@ -24,8 +24,10 @@
from . import latexfuntypes
def transliterate(alphabet):
alphabet_keys = alphabet.keys()
def fun(flat_input):
flat_output = ''
success = True
......@@ -38,11 +40,6 @@ def transliterate(alphabet):
return (flat_output, success)
return fun
def transliterator(alphabet):
return latexfuntypes.latexfun(lambda x: transliterate(alphabet)(x[0])[0],1)
def transliterator(alphabet):
return latexfuntypes.latexfun(lambda x: transliterate(alphabet)(x[0])[0], 1)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment