'Convert lengths to their LaTeX representation.'
i = 0
percent = False
- # the string has always the form
- # ValueUnit+ValueUnit-ValueUnit
- # the + and - lengths are optional
- # the + is always before the -
- i = string.find("text%")
- if i > -1:
- percent = True
- minus = string.rfind("-", 0, i)
- plus = string.rfind("+", 0, i)
- if plus == -1 and minus == -1:
- value = string[:i]
- value = str(float(value)/100)
- end = string[i+5:]
- string = value + "\\textwidth" + end
- if plus > minus:
- value = string[plus+1:i]
- value = str(float(value)/100)
- begin = string[:plus+1]
- end = string[i+5:]
- string = begin + value + "\\textwidth" + end
- if plus < minus:
- value = string[minus+1:i]
- value = str(float(value)/100)
- begin = string[:minus+1]
- string = begin + value + "\\textwidth"
- i = string.find("col%")
- if i > -1:
- percent = True
- minus = string.rfind("-", 0, i)
- plus = string.rfind("+", 0, i)
- if plus == -1 and minus == -1:
- value = string[:i]
- value = str(float(value)/100)
- end = string[i+4:]
- string = value + "\\columnwidth" + end
- if plus > minus:
- value = string[plus+1:i]
- value = str(float(value)/100)
- begin = string[:plus+1]
- end = string[i+4:]
- string = begin + value + "\\columnwidth" + end
- if plus < minus:
- value = string[minus+1:i]
- value = str(float(value)/100)
- begin = string[:minus+1]
- string = begin + value + "\\columnwidth"
- i = string.find("page%")
- if i > -1:
- percent = True
- minus = string.rfind("-", 0, i)
- plus = string.rfind("+", 0, i)
- if plus == -1 and minus == -1:
- value = string[:i]
- value = str(float(value)/100)
- end = string[i+5:]
- string = value + "\\paperwidth" + end
- if plus > minus:
- value = string[plus+1:i]
- value = str(float(value)/100)
- begin = string[:plus+1]
- end = string[i+5:]
- string = begin + value + "\\paperwidth" + end
- if plus < minus:
- value = string[minus+1:i]
- value = str(float(value)/100)
- begin = string[:minus+1]
- string = begin + value + "\\paperwidth"
- i = string.find("line%")
- if i > -1:
- percent = True
- minus = string.rfind("-", 0, i)
- plus = string.rfind("+", 0, i)
- if plus == -1 and minus == -1:
- value = string[:i]
- value = str(float(value)/100)
- end = string[i+5:]
- string = value + "\\linewidth" + end
- if plus > minus:
- value = string[plus+1:i]
- value = str(float(value)/100)
- begin = string[:plus+1]
- end = string[i+5:]
- string = begin + value + "\\linewidth" + end
- if plus < minus:
- value = string[minus+1:i]
- value = str(float(value)/100)
- begin = string[:minus+1]
- string = begin + value + "\\linewidth"
- i = string.find("theight%")
- if i > -1:
- percent = True
- minus = string.rfind("-", 0, i)
- plus = string.rfind("+", 0, i)
- if plus == -1 and minus == -1:
- value = string[:i]
- value = str(float(value)/100)
- end = string[i+8:]
- string = value + "\\textheight" + end
- if plus > minus:
- value = string[plus+1:i]
- value = str(float(value)/100)
- begin = string[:plus+1]
- end = string[i+8:]
- string = begin + value + "\\textheight" + end
- if plus < minus:
- value = string[minus+1:i]
- value = str(float(value)/100)
- begin = string[:minus+1]
- string = begin + value + "\\textheight"
- i = string.find("pheight%")
- if i > -1:
- percent = True
- minus = string.rfind("-", 0, i)
- plus = string.rfind("+", 0, i)
- if plus == -1 and minus == -1:
- value = string[:i]
- value = str(float(value)/100)
- end = string[i+8:]
- string = value + "\\paperheight" + end
- if plus > minus:
- value = string[plus+1:i]
- value = str(float(value)/100)
- begin = string[:plus+1]
- end = string[i+8:]
- string = begin + value + "\\paperheight" + end
- if plus < minus:
- value = string[minus+1:i]
- value = str(float(value)/100)
- begin = string[:minus+1]
- string = begin + value + "\\paperheight"
- # replace + and -, but only when the - is not the first character
- string = string.replace("+", " plus ")
- if string.find("-") == 0:
- minusstring = string[1:]
- minusstring = minusstring.replace("-", " minus ")
- string = "-" + minusstring
- else:
- string = string.replace("-", " minus ")
- # handle the case that "+-1mm" was used because LaTeX only understands
+ # the string has the form
+ # ValueUnit+ValueUnit-ValueUnit or
+ # ValueUnit+-ValueUnit
+ # the + and - (glue lengths) are optional
+ # the + always precedes the -
+
+ # Convert relative lengths to LaTeX units
+ units = {"text%":"\\textwidth", "col%":"\\columnwidth",
+ "page%":"\\pagewidth", "line%":"\\linewidth",
+ "theight%":"\\textheight", "pheight%":"\\pageheight"}
+ for unit in units.keys():
+ i = string.find(unit)
+ if i != -1:
+ percent = True
+ minus = string.rfind("-", 1, i)
+ plus = string.rfind("+", 0, i)
+ latex_unit = units[unit]
+ if plus == -1 and minus == -1:
+ value = string[:i]
+ value = str(float(value)/100)
+ end = string[i + len(unit):]
+ string = value + latex_unit + end
+ if plus > minus:
+ value = string[plus+1:i]
+ value = str(float(value)/100)
+ begin = string[:plus+1]
+ end = string[i+len(unit):]
+ string = begin + value + latex_unit + end
+ if plus < minus:
+ value = string[minus+1:i]
+ value = str(float(value)/100)
+ begin = string[:minus+1]
+ string = begin + value + latex_unit
+
+ # replace + and -, but only if the - is not the first character
+ string = string[0] + string[1:].replace("+", " plus ").replace("-", " minus ")
+ # handle the case where "+-1mm" was used, because LaTeX only understands
# "plus 1mm minus 1mm"
if string.find("plus minus"):
lastvaluepos = string.rfind(" ")
document.body[i:i+3] = subst
i = i + 2
+def convert_author_id(document):
+ " Add the author_id to the \\author definition and make sure 0 is not used"
+ i = 0
+ j = 1
+ while True:
+ i = find_token(document.header, "\\author", i)
+ if i == -1:
+ break
+
+ r = re.compile(r'(\\author) (\".*\")\s?(.*)$')
+ m = r.match(document.header[i])
+ if m != None:
+ name = m.group(2)
+
+ email = ''
+ if m.lastindex == 3:
+ email = m.group(3)
+ document.header[i] = "\\author %i %s %s" % (j, name, email)
+ j = j + 1
+ i = i + 1
+
+ k = 0
+ while True:
+ k = find_token(document.body, "\\change_", k)
+ if k == -1:
+ break
+
+ change = document.body[k].split(' ');
+ if len(change) == 3:
+ type = change[0]
+ author_id = int(change[1])
+ time = change[2]
+ document.body[k] = "%s %i %s" % (type, author_id + 1, time)
+ k = k + 1
+
+def revert_author_id(document):
+ " Remove the author_id from the \\author definition "
+ i = 0
+ j = 0
+ idmap = dict()
+ while True:
+ i = find_token(document.header, "\\author", i)
+ if i == -1:
+ break
+
+ r = re.compile(r'(\\author) (\d+) (\".*\")\s?(.*)$')
+ m = r.match(document.header[i])
+ if m != None:
+ author_id = int(m.group(2))
+ idmap[author_id] = j
+ name = m.group(3)
+
+ email = ''
+ if m.lastindex == 4:
+ email = m.group(4)
+ document.header[i] = "\\author %s %s" % (name, email)
+ i = i + 1
+ j = j + 1
+
+ k = 0
+ while True:
+ k = find_token(document.body, "\\change_", k)
+ if k == -1:
+ break
+
+ change = document.body[k].split(' ');
+ if len(change) == 3:
+ type = change[0]
+ author_id = int(change[1])
+ time = change[2]
+ document.body[k] = "%s %i %s" % (type, idmap[author_id], time)
+ k = k + 1
+
+
+def revert_suppress_date(document):
+ " Revert suppressing of default document date to preamble code "
+ i = 0
+ while True:
+ i = find_token(document.header, "\\suppress_date", i)
+ if i == -1:
+ break
+ # remove the preamble line and write to the preamble
+ # when suppress_date was true
+ date = get_value(document.header, "\\suppress_date", i)
+ if date == "true":
+ add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
+ add_to_preamble(document, ["\\date{}"])
+ del document.header[i]
+ i = i + 1
+
+
+def revert_mhchem(document):
+ "Revert mhchem loading to preamble code"
+ i = 0
+ j = 0
+ k = 0
+ i = find_token(document.header, "\\use_mhchem 1", 0)
+ if i != -1:
+ mhchem = "auto"
+ else:
+ i = find_token(document.header, "\\use_mhchem 2", 0)
+ if i != -1:
+ mhchem = "on"
+ if mhchem == "auto":
+ j = find_token(document.body, "\\cf{", 0)
+ if j != -1:
+ mhchem = "on"
+ else:
+ j = find_token(document.body, "\\ce{", 0)
+ if j != -1:
+ mhchem = "on"
+ if mhchem == "on":
+ add_to_preamble(document, ["% this command was inserted by lyx2lyx"])
+ add_to_preamble(document, ["\\PassOptionsToPackage{version=3}{mhchem}"])
+ add_to_preamble(document, ["\\usepackage{mhchem}"])
+ k = find_token(document.header, "\\use_mhchem", 0)
+ if k == -1:
+ document.warning("Malformed LyX document: Could not find mhchem setting.")
+ return
+ del document.header[k]
+
##
# Conversion hub
[365, []],
[366, []],
[367, []],
- [368, []]
+ [368, []],
+ [369, [convert_author_id]],
+ [370, []],
+ [371, []]
]
-revert = [[367, [revert_hspace_glue_lengths]],
+revert = [[370, [revert_mhchem]],
+ [369, [revert_suppress_date]],
+ [368, [revert_author_id]],
+ [367, [revert_hspace_glue_lengths]],
[366, [revert_percent_vspace_lengths, revert_percent_hspace_lengths]],
[365, [revert_percent_skip_lengths]],
[364, [revert_paragraph_indentation]],