Patch from Jose.
https://www.mail-archive.com/lyx-devel@lists.lyx.org/msg217770.html
# A dictionary of Unicode->LICR mappings for use in a Unicode string's translate() method
# Created from the reversed list to keep the first of alternative definitions.
-licr_table = dict((ord(ch), cmd) for cmd, ch in unicode_reps[::-1])
+licr_table = {ord(ch): cmd for cmd, ch in unicode_reps[::-1]}
def put_cmd_in_ert(cmd, is_open=False, as_paragraph=False):
"""
i = find_token(lines, '\\begin_inset Label', i)
if i == -1:
return
- lines[i] = '\\begin_inset LatexCommand \label{' + lines[i][19:] + '}'
+ lines[i] = '\\begin_inset LatexCommand \\label{' + lines[i][19:] + '}'
i = i + 1
def find_end_of_inset(lines, i):
- " Finds the matching \end_inset"
+ r" Finds the matching \end_inset"
return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
def remove_oldfloat(document):
- " Change \begin_float .. \end_float into \begin_inset Float .. \end_inset"
+ r" Change \begin_float .. \end_float into \begin_inset Float .. \end_inset"
lines = document.body
i = 0
while True:
if flag:
flag = 0
if hfill:
- start = ["","\hfill",""]+start
+ start = ["",r"\hfill",""]+start
else:
start = ['\\layout %s' % document.default_layout,''] + start
new = []
new2 = []
if check_token(lines[i], "\\layout LaTeX"):
- new = ['\layout %s' % document.default_layout, "", ""]
+ new = [r'\layout %s' % document.default_layout, "", ""]
k = i+1
while True:
note_lines = [txt]+note_lines
for line in note_lines:
- new = new + ['\layout %s' % document.default_layout, ""]
+ new = new + [r'\layout %s' % document.default_layout, ""]
tmp = line.split('\\')
new = new + [tmp[0]]
for x in tmp[1:]:
# Private helper functions
def find_end_of_inset(lines, i):
- "Finds the matching \end_inset"
+ r"Finds the matching \end_inset"
return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
def find_end_of_inset(lines, i):
- "Finds the matching \end_inset"
+ r"Finds the matching \end_inset"
return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
def del_token(lines, token, start, end):
####################################################################
def remove_color_default(document):
- " Remove \color default"
+ r" Remove \color default"
i = 0
while True:
i = find_token(document.body, "\\color default", i)
def add_end_header(document):
- " Add \end_header"
+ r" Add \end_header"
document.header.append("\\end_header");
def rm_end_header(document):
- " Remove \end_header"
+ r" Remove \end_header"
i = find_token(document.header, "\\end_header", 0)
if i == -1:
return
def convert_spaces(document):
- " \SpecialChar ~ -> \InsetSpace ~"
+ r" \SpecialChar ~ -> \InsetSpace ~"
for i in range(len(document.body)):
document.body[i] = document.body[i].replace("\\SpecialChar ~",
"\\InsetSpace ~")
def revert_spaces(document):
- " \InsetSpace ~ -> \SpecialChar ~"
+ r" \InsetSpace ~ -> \SpecialChar ~"
regexp = re.compile(r'(.*)(\\InsetSpace\s+)(\S+)')
i = 0
while True:
def rename_spaces(document):
- """ \InsetSpace \, -> \InsetSpace \thinspace{}
- \InsetSpace \space -> \InsetSpace \space{}"""
+ """ \\InsetSpace \\, -> \\InsetSpace \thinspace{}
+ \\InsetSpace \\space -> \\InsetSpace \\space{}"""
for i in range(len(document.body)):
document.body[i] = document.body[i].replace("\\InsetSpace \\space",
"\\InsetSpace \\space{}")
- document.body[i] = document.body[i].replace("\\InsetSpace \,",
+ document.body[i] = document.body[i].replace("\\InsetSpace \\,",
"\\InsetSpace \\thinspace{}")
def revert_space_names(document):
- """ \InsetSpace \thinspace{} -> \InsetSpace \,
- \InsetSpace \space{} -> \InsetSpace \space"""
+ """ \\InsetSpace \thinspace{} -> \\InsetSpace \\,
+ \\InsetSpace \\space{} -> \\InsetSpace \\space"""
for i in range(len(document.body)):
document.body[i] = document.body[i].replace("\\InsetSpace \\space{}",
"\\InsetSpace \\space")
def remove_insetparent(document):
- " Remove \lyxparent"
+ r" Remove \lyxparent"
i = 0
while True:
i = find_token(document.body, "\\begin_inset LatexCommand \\lyxparent", i)
def add_end_layout(document):
- " Add \end_layout"
+ r" Add \end_layout"
i = find_token(document.body, '\\layout', 0)
if i == -1:
def rm_end_layout(document):
- " Remove \end_layout"
+ r" Remove \end_layout"
i = 0
while True:
i = find_token(document.body, '\\end_layout', i)
def layout2begin_layout(document):
- " \layout -> \begin_layout "
+ r" \layout -> \begin_layout "
i = 0
while True:
i = find_token(document.body, '\\layout', i)
def begin_layout2layout(document):
- " \begin_layout -> \layout "
+ r" \begin_layout -> \layout "
i = 0
while True:
i = find_token(document.body, '\\begin_layout', i)
# convert the inner_position
if document.body[i][:14] == "inner_position":
innerpos = inner_pos[int(document.body[i][15])]
- del document.body[i]
+ del document.body[i]
else:
innerpos = inner_pos[0]
'\\begin_layout %s' % document.default_layout,
"",
"%s" % firstname,
- "\end_layout",
+ r"\end_layout",
"",
- "\end_inset",
+ r"\end_inset",
"",
"",
"\\begin_inset CharStyle Surname",
def normalize_paragraph_params(document):
" Place all the paragraph parameters in their own line. "
body = document.body
-
+
allowed_parameters = '\\paragraph_spacing', '\\noindent', \
'\\align', '\\labelwidthstring', "\\start_of_appendix", \
"\\leftindent"
else:
if insets and insets[-1] == "Formula":
# avoid putting an ERT in a math; instead put command as text
- command = command.replace('\\\\', '\mathrm{')
+ command = command.replace('\\\\', r'\mathrm{')
command = command + '}'
elif not insets or insets[-1] != "ERT":
# add an ERT inset with the replacement character
def convert_bibitem(document):
- """ Convert
+ r""" Convert
\bibitem [option]{argument}
to
def convert_commandparams(document):
""" Convert
- \begin_inset LatexCommand \cmdname[opt1][opt2]{arg}
- \end_inset
+ \\begin_inset LatexCommand \\cmdname[opt1][opt2]{arg}
+ \\end_inset
to
- \begin_inset LatexCommand cmdname
+ \\begin_inset LatexCommand cmdname
name1 "opt1"
name2 "opt2"
name3 "arg"
- \end_inset
+ \\end_inset
name1, name2 and name3 can be different for each command.
"""
def convert_lyxline(document):
- " remove fontsize commands for \lyxline "
+ r" remove fontsize commands for \lyxline "
# The problematic is: The old \lyxline definition doesn't handle the fontsize
# to change the line thickness. The new definiton does this so that imported
# \lyxlines would have a different line thickness. The eventual fontsize command
def revert_preamble_listings_params(document):
- " Revert preamble option \listings_params "
+ r" Revert preamble option \listings_params "
i = find_token(document.header, "\\listings_params", 0)
if i != -1:
document.preamble.append('\\usepackage{listings}')
document.header[i] = "\\language arabic_arabtex"
i = 0
while i < len(document.body):
- h = document.body[i].find("\lang arabic", 0, len(document.body[i]))
+ h = document.body[i].find(r"\lang arabic", 0, len(document.body[i]))
if (h != -1):
# change the language name
- document.body[i] = '\lang arabic_arabtex'
+ document.body[i] = r'\lang arabic_arabtex'
i = i + 1
document.header[i] = "\\language arabic"
i = 0
while i < len(document.body):
- h = document.body[i].find("\lang arabic_arabtex", 0, len(document.body[i]))
+ h = document.body[i].find(r"\lang arabic_arabtex", 0, len(document.body[i]))
if (h != -1):
# change the language name
- document.body[i] = '\lang arabic'
+ document.body[i] = r'\lang arabic'
i = i + 1
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)
data = data.replace('\\\\', '\\')
# Math:
- mathre = re.compile('^(.*?)(\$.*?\$)(.*)')
+ mathre = re.compile(r'^(.*?)(\$.*?\$)(.*)')
lines = data.split('\n')
for line in lines:
#document.warning("LINE: " + line)
def convert_inset_command(document):
- """
+ r"""
Convert:
\begin_inset LatexCommand cmd
to
def revert_inset_command(document):
- """
+ r"""
Convert:
\begin_inset CommandInset InsetType
LatexCommand cmd
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 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 convert_math_output(document):
- " Convert \html_use_mathml to \html_math_output "
+ r" Convert \html_use_mathml to \html_math_output "
i = find_token(document.header, "\\html_use_mathml", 0)
if i == -1:
return
def revert_math_output(document):
- " Revert \html_math_output to \html_use_mathml "
+ r" Revert \html_math_output to \html_use_mathml "
i = find_token(document.header, "\\html_math_output", 0)
if i == -1:
return
def convert_prettyref(document):
" Converts prettyref references to neutral formatted refs "
- re_ref = re.compile("^\s*reference\s+\"(\w+):(\S+)\"")
- nm_ref = re.compile("^\s*name\s+\"(\w+):(\S+)\"")
+ re_ref = re.compile("^\\s*reference\\s+\"(\\w+):(\\S+)\"")
+ nm_ref = re.compile("^\\s*name\\s+\"(\\w+):(\\S+)\"")
i = 0
while True:
def revert_refstyle(document):
" Reverts neutral formatted refs to prettyref "
- re_ref = re.compile("^reference\s+\"(\w+):(\S+)\"")
- nm_ref = re.compile("^\s*name\s+\"(\w+):(\S+)\"")
+ re_ref = re.compile("^reference\\s+\"(\\w+):(\\S+)\"")
+ nm_ref = re.compile("^\\s*name\\s+\"(\\w+):(\\S+)\"")
i = 0
while True:
def revert_mathrsfs(document):
- " Load mathrsfs if \mathrsfs us use in the document "
+ r" Load mathrsfs if \mathrsfs us use in the document "
i = 0
for line in document.body:
if line.find("\\mathscr{") != -1:
if not check_passthru:
return
- rx = re.compile("\\\\begin_layout \s*(\w+)")
+ rx = re.compile("\\\\begin_layout \\s*(\\w+)")
beg = 0
for lay in ["Chunk", "Scrap"]:
while True:
" http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg161298.html "
if not check_passthru:
return
- rx = re.compile("\\\\begin_layout \s*(\w+)")
+ rx = re.compile("\\\\begin_layout \\s*(\\w+)")
beg = 0
for lay in ["Chunk", "Scrap"]:
while True:
def convert_langpack(document):
" Add \\language_package parameter "
- i = find_token(document.header, "\language" , 0)
+ i = find_token(document.header, r"\language" , 0)
if i == -1:
document.warning("Malformed document. No \\language defined!")
return
usage:
revert_Argument_to_TeX_brace(document, LineOfBegin, LineOfEnd, StartArgument, EndArgument, isEnvironment, isOpt)
LineOfBegin is the line of the \\begin_layout or \\begin_inset statement
- LineOfEnd is the line of the \end_layout or \end_inset statement, if "0" is given, the end of the file is used instead
+ LineOfEnd is the line of the \\end_layout or \\end_inset statement, if "0" is given, the end of the file is used instead
StartArgument is the number of the first argument that needs to be converted
EndArgument is the number of the last argument that needs to be converted or the last defined one
isEnvironment must be true, if the layout is for a LaTeX environment
def convert_listoflistings(document):
- 'Convert ERT \lstlistoflistings to TOC lstlistoflistings inset'
+ r'Convert ERT \lstlistoflistings to TOC lstlistoflistings inset'
# We can support roundtrip because the command is so simple
i = 0
while True:
# Private helper functions
def revert_Argument_to_TeX_brace(document, line, endline, n, nmax, environment, opt, nolastopt):
- """
+ r"""
Reverts an InsetArgument to TeX-code
usage:
revert_Argument_to_TeX_brace(document, LineOfBegin, LineOfEnd, StartArgument, EndArgument, isEnvironment, isOpt, notLastOpt)
document.body[i : i + 1] = ["\\begin_layout Standard", ""] + begcmd
- add_to_preamble(document, "\\providecommand{\questionname}{Question}")
+ add_to_preamble(document, "\\providecommand{\\questionname}{Question}")
if starred:
add_to_preamble(document, "\\theoremstyle{plain}\n" \
if document.textclass != "jss":
return
- # at first revert the inset layouts because
+ # at first revert the inset layouts because
# they can be part of the In_Preamble layouts
il_dict = {
"Pkg" : "pkg",
i += 1
continue
content = lyx2latex(document, document.body[i:j + 1])
- add_to_preamble(document, ["\\setlength{\hintscolumnwidth}{" + content + "}"])
+ add_to_preamble(document, ["\\setlength{\\hintscolumnwidth}{" + content + "}"])
del document.body[i:j + 1]
# now change the new styles to the obsolete ones
# \name
add_to_preamble(document, "\\%s{%s}[thm]{\\protect\\solutionname}" % \
(theoremName, LaTeXName))
- add_to_preamble(document, "\\providecommand{\solutionname}{Solution}")
+ add_to_preamble(document, "\\providecommand{\\solutionname}{Solution}")
i = j
"Preamble",
" \\usepackage{beamerarticle,pgf}",
" % this default might be overridden by plain title style",
- " \\newcommand\makebeamertitle{\\frame{\\maketitle}}%",
+ " \\newcommand\\makebeamertitle{\\frame{\\maketitle}}%",
" \\AtBeginDocument{",
" \\let\\origtableofcontents=\\tableofcontents",
" \\def\\tableofcontents{\\@ifnextchar[{\\origtableofcontents}{\\gobbletableofcontents}}",
continue
# literal dash followed by a non-white-character or no-break space:
- if re.search(u"[\u2013\u2014]([\S\u00A0\u202F\u2060]|$)",
+ if re.search(u"[\u2013\u2014]([\\S\u00A0\u202F\u2060]|$)",
line, flags=re.UNICODE):
has_literal_dashes = True
# ligature dash followed by non-white-char or no-break space on next line:
if (re.search(r"(\\twohyphens|\\threehyphens)", line) and
- re.match(u"[\S\u00A0\u202F\u2060]", lines[i+1], flags=re.UNICODE)):
+ re.match(u"[\\S\u00A0\u202F\u2060]", lines[i+1], flags=re.UNICODE)):
has_ligature_dashes = True
if has_literal_dashes and has_ligature_dashes:
# TODO: insert a warning note in the document?
r""]
def convert_allowbreak(document):
- " Zero widths Space-inset -> \SpecialChar allowbreak. "
+ r" Zero widths Space-inset -> \SpecialChar allowbreak. "
lines = document.body
i = find_complete_lines(lines, allowbreak_emulation, 2)
while i != -1:
def revert_allowbreak(document):
- " \SpecialChar allowbreak -> Zero widths Space-inset. "
+ r" \SpecialChar allowbreak -> Zero widths Space-inset. "
i = 1
lines = document.body
while i < len(lines):
i += 1
def revert_tuftecite(document):
- """Revert \cite commands in tufte classes"""
+ r"""Revert \cite commands in tufte classes"""
tufte = ["tufte-book", "tufte-handout"]
if document.textclass not in tufte:
if len(datecomps) > 1:
argv = datecomps[0]
isodate = datecomps[1]
- m = re.search('(\d\d\d\d)-(\d\d)-(\d\d)', isodate)
+ m = re.search(r'(\d\d\d\d)-(\d\d)-(\d\d)', isodate)
if m:
dte = date(int(m.group(1)), int(m.group(2)), int(m.group(3)))
# FIXME if we had the path to the original document (not the one in the tmp dir),
if len(timecomps) > 1:
argv = timecomps[0]
isotime = timecomps[1]
- m = re.search('(\d\d):(\d\d):(\d\d)', isotime)
+ m = re.search(r'(\d\d):(\d\d):(\d\d)', isotime)
if m:
tme = time(int(m.group(1)), int(m.group(2)), int(m.group(3)))
else:
- m = re.search('(\d\d):(\d\d)', isotime)
+ m = re.search(r'(\d\d):(\d\d)', isotime)
if m:
tme = time(int(m.group(1)), int(m.group(2)))
# FIXME if we had the path to the original document (not the one in the tmp dir),
"korean": ("", "korean"),
}
if document.language in new_languages:
- used_languages = set((document.language, ))
+ used_languages = {document.language}
else:
used_languages = set()
i = 0
break
k = find_token(document.header, "\\color", i, j)
if k != -1:
- m = re.search('\\\\color (\S+) (\S+)', document.header[k])
+ m = re.search('\\\\color (\\S+) (\\S+)', document.header[k])
if m:
document.header[k] = "\\color " + m.group(1)
i += 1
el = find_token(document.body, '\\strikeout on', flt, elt)
if el != -1:
extralines.append("\\strikeout default")
- document.body[elt:elt+1] = extralines + put_cmd_in_ert("\\end{cellvarwidth}") + ["\end_layout"]
+ document.body[elt:elt+1] = extralines + put_cmd_in_ert("\\end{cellvarwidth}") + [r"\end_layout"]
parlang = -1
for q in range(flt, elt):
if document.body[q] != "" and document.body[q][0] != "\\":
find_end_of_sequence(lines, i):
Find the end of the sequence of layouts of the same kind.
Considers nesting. If the last paragraph in sequence is nested,
- the position of the last \end_deeper is returned, else
- the position of the last \end_layout.
+ the position of the last \\end_deeper is returned, else
+ the position of the last \\end_layout.
is_in_inset(lines, i, inset, default=(-1,-1)):
Check if line i is in an inset of the given type.
get_containing_inset(lines, i):
Finds out what kind of inset line i is within. Returns a
- list containing what follows \begin_inset on the line
+ list containing what follows \\begin_inset on the line
on which the inset begins, plus the starting and ending line.
Returns False on any kind of error or if it isn't in an inset.
So get_containing_inset(document.body, i) might return:
def get_option_value(line, option):
- rx = option + '\s*=\s*"([^"]+)"'
+ rx = option + r'\s*=\s*"([^"]+)"'
rx = re.compile(rx)
m = rx.search(line)
if not m:
def set_option_value(line, option, value):
- rx = '(' + option + '\s*=\s*")[^"]+"'
+ rx = '(' + option + r'\s*=\s*")[^"]+"'
rx = re.compile(rx)
m = rx.search(line)
if not m:
return line
- return re.sub(rx, '\g<1>' + value + '"', line)
+ return re.sub(rx, r'\g<1>' + value + '"', line)
def del_token(lines, token, start=0, end=0):
def get_containing_inset(lines, i):
'''
Finds out what kind of inset line i is within. Returns a
- list containing (i) what follows \begin_inset on the line
+ list containing (i) what follows \\begin_inset on the line
on which the inset begins, plus the starting and ending line.
Returns False on any kind of error or if it isn't in an inset.
'''