import sys, os
from parser_tools import find_token, find_end_of, find_tokens, get_value
-
-# Provide support for both python 2 and 3
-PY2 = sys.version_info[0] == 2
-if not PY2:
- text_type = str
- unichr = chr
-else:
- text_type = unicode
- unichr = unichr
-# End of code to support for both python 2 and 3
+from unicode_symbols import unicode_reps
####################################################################
# Private helper functions
return l
-# FIXME: Use the version in unicode_symbols.py which has some bug fixes
-def read_unicodesymbols():
- " Read the unicodesymbols list of unicode characters and corresponding commands."
- pathname = os.path.abspath(os.path.dirname(sys.argv[0]))
- fp = open(os.path.join(pathname.strip('lyx2lyx'), 'unicodesymbols'))
- spec_chars = []
- # Two backslashes, followed by some non-word character, and then a character
- # in brackets. The idea is to check for constructs like: \"{u}, which is how
- # they are written in the unicodesymbols file; but they can also be written
- # as: \"u or even \" u.
- r = re.compile(r'\\\\(\W)\{(\w)\}')
- for line in fp.readlines():
- if line[0] != '#' and line.strip() != "":
- line=line.replace(' "',' ') # remove all quotation marks with spaces before
- line=line.replace('" ',' ') # remove all quotation marks with spaces after
- line=line.replace(r'\"','"') # replace \" by " (for characters with diaeresis)
- try:
- [ucs4,command,dead] = line.split(None,2)
- if command[0:1] != "\\":
- continue
- spec_chars.append([command, unichr(eval(ucs4))])
- except:
- continue
- m = r.match(command)
- if m != None:
- command = "\\\\"
- # If the character is a double-quote, then we need to escape it, too,
- # since it is done that way in the LyX file.
- if m.group(1) == "\"":
- command += "\\"
- commandbl = command
- command += m.group(1) + m.group(2)
- commandbl += m.group(1) + ' ' + m.group(2)
- spec_chars.append([command, unichr(eval(ucs4))])
- spec_chars.append([commandbl, unichr(eval(ucs4))])
- fp.close()
- return spec_chars
-
-
def extract_argument(line):
'Extracts a LaTeX argument from the start of line. Returns (arg, rest).'
if not line:
return (None, "")
- bracere = re.compile("(\s*)(.*)")
+ bracere = re.compile(r"(\s*)(.*)")
n = bracere.match(line)
whitespace = n.group(1)
stuff = n.group(2)
return retval
-unicode_reps = read_unicodesymbols()
-
#Bug 5022....
#Might should do latex2ert first, then deal with stuff that DOESN'T
#end up inside ERT. That routine could be modified so that it returned
data = data.replace('\\\\', '\\')
# Math:
- mathre = re.compile('^(.*?)(\$.*?\$)(.*)')
+ mathre = re.compile(r'^(.*?)(\$.*?\$)(.*)')
lines = data.split('\n')
for line in lines:
#document.warning("LINE: " + line)
continue
inert = ert_end >= curline
content += lyxline2latex(document, lines[curline], inert)
-
+
return content
def remove_inzip_options(document):
"Remove inzipName and embed options from the Graphics inset"
i = 0
- while 1:
+ while True:
i = find_token(document.body, "\\begin_inset Graphics", i)
if i == -1:
return
def convert_inset_command(document):
- """
+ r"""
Convert:
\begin_inset LatexCommand cmd
to
LatexCommand cmd
"""
i = 0
- while 1:
+ while True:
i = find_token(document.body, "\\begin_inset LatexCommand", i)
if i == -1:
return
def revert_inset_command(document):
- """
+ r"""
Convert:
\begin_inset CommandInset InsetType
LatexCommand cmd
will not be able to recognize. Not sure what to do about that.
"""
i = 0
- while 1:
+ while True:
i = find_token(document.body, "\\begin_inset CommandInset", i)
if i == -1:
return
def convert_latexcommand_index(document):
- "Convert from LatexCommand form to collapsable form."
+ "Convert from LatexCommand form to collapsible form."
i = 0
r1 = re.compile('name "(.*)"')
while True:
def revert_latexcommand_index(document):
- "Revert from collapsable form to LatexCommand form."
+ "Revert from collapsible form to LatexCommand form."
i = 0
while True:
i = find_token(document.body, "\\begin_inset Index", i)
def revert_inset_info(document):
'Replace info inset with its content'
i = 0
- while 1:
+ while True:
i = find_token(document.body, '\\begin_inset Info', i)
if i == -1:
return
def convert_macro_global(document):
- "Remove TeX code command \global when it is in front of a macro"
+ r"Remove TeX code command \global when it is in front of a macro"
# math macros are nowadays already defined \global, so that an additional
# \global would make the document uncompilable, see
# http://www.lyx.org/trac/ticket/5371
def convert_framed_notes(document):
"Convert framed notes to boxes. "
i = 0
- while 1:
+ while True:
i = find_tokens(document.body, ["\\begin_inset Note Framed", "\\begin_inset Note Shaded"], i)
if i == -1:
return
def revert_framed_notes(document):
"Revert framed boxes to notes. "
i = 0
- while 1:
+ while True:
i = find_tokens(document.body, ["\\begin_inset Box Framed", "\\begin_inset Box Shaded"], i)
if i == -1:
def revert_nocite(document):
"Revert LatexCommand nocite to ERT"
i = 0
- while 1:
+ while True:
i = find_token(document.body, "\\begin_inset CommandInset citation", i)
if i == -1:
return
def revert_rotfloat(document):
" Revert sideways custom floats. "
i = 0
- while 1:
+ while True:
# whitespace intended (exclude \\begin_inset FloatList)
i = find_token(document.body, "\\begin_inset Float ", i)
if i == -1:
def revert_widesideways(document):
" Revert wide sideways floats. "
i = 0
- while 1:
+ while True:
# whitespace intended (exclude \\begin_inset FloatList)
i = find_token(document.body, '\\begin_inset Float ', i)
if i == -1:
def revert_inset_embedding(document, type):
' Remove embed tag from certain type of insets'
i = 0
- while 1:
+ while True:
i = find_token(document.body, "\\begin_inset %s" % type, i)
if i == -1:
return
def convert_subfig(document):
" Convert subfigures to subfloats. "
i = 0
- while 1:
+ while True:
addedLines = 0
i = find_token(document.body, '\\begin_inset Graphics', i)
if i == -1:
def revert_subfig(document):
" Revert subfloats. "
i = 0
- while 1:
+ while True:
# whitespace intended (exclude \\begin_inset FloatList)
i = find_tokens(document.body, ['\\begin_inset Float ', '\\begin_inset Wrap'], i)
if i == -1:
def remove_extra_embedded_files(document):
- " Remove \extra_embedded_files from buffer params "
+ r" Remove \extra_embedded_files from buffer params "
i = find_token(document.header, '\\extra_embedded_files', 0)
if i == -1:
return
def revert_pdfpages(document):
' Revert pdfpages external inset to ERT '
i = 0
- while 1:
+ while True:
i = find_token(document.body, "\\begin_inset External", i)
if i == -1:
return
def revert_graphics_group(document):
' Revert group information from graphics insets '
i = 0
- while 1:
+ while True:
i = find_token(document.body, "\\begin_inset Graphics", i)
if i == -1:
return
"Paragraph*": "Paragraph",
"Subparagraph*": "Subparagraph"}
i = 0
- while 1:
+ while True:
i = find_token(document.body, "\\begin_layout", i)
if i == -1:
return