1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2010 The LyX team
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 This modules offer several free functions to help with lyx2lyx'ing.
21 More documentaton is below, but here is a quick guide to what
22 they do. Optional arguments are marked by brackets.
24 add_to_preamble(document, text):
25 Here, text can be either a single line or a list of lines. It
26 is bad practice to pass something with embedded newlines, but
27 we will handle that properly.
28 The routine checks to see whether the provided material is
29 already in the preamble. If not, it adds it.
31 insert_to_preamble(index, document, text):
32 Here, text can be either a single line or a list of lines. It
33 is bad practice to pass something with embedded newlines, but
34 we will handle that properly.
35 The routine inserts text at document.preamble[index].
38 Here arg should be a list of strings (lines), which we want to
39 wrap in ERT. Returns a list of strings so wrapped.
40 A call to this routine will often go something like this:
41 i = find_token('\\begin_inset FunkyInset', ...)
42 j = find_end_of_inset(document.body, i)
43 content = lyx2latex(document[i:j + 1])
44 ert = put_cmd_in_ert(content)
45 document.body[i:j+1] = ert
47 lyx2latex(document, lines):
48 Here, lines is a list of lines of LyX material we want to convert
49 to LaTeX. We do the best we can and return a string containing
50 the translated material.
53 Convert lengths (in LyX form) to their LaTeX representation. Returns
54 (bool, length), where the bool tells us if it was a percentage, and
55 the length is the LaTeX representation.
60 from parser_tools import find_token
61 from unicode_symbols import unicode_reps
64 # This will accept either a list of lines or a single line.
65 # It is bad practice to pass something with embedded newlines,
66 # though we will handle that.
67 def add_to_preamble(document, text):
68 " Add text to the preamble if it is not already there. "
70 if not type(text) is list:
71 # split on \n just in case
72 # it'll give us the one element list we want
73 # if there's no \n, too
74 text = text.split('\n')
77 prelen = len(document.preamble)
79 i = find_token(document.preamble, text[0], i)
82 # we need a perfect match
85 if i >= prelen or line != document.preamble[i]:
92 document.preamble.extend(text)
95 # Note that text can be either a list of lines or a single line.
96 # It should really be a list.
97 def insert_to_preamble(index, document, text):
98 """ Insert text to the preamble at a given line"""
100 if not type(text) is list:
101 # split on \n just in case
102 # it'll give us the one element list we want
103 # if there's no \n, too
104 text = text.split('\n')
106 document.preamble[index:index] = text
109 def put_cmd_in_ert(arg):
111 arg should be a list of lines we want to wrap in ERT.
112 Returns a list of strings, with the lines so wrapped.
115 ret = ["\\begin_inset ERT", "status collapsed", "\\begin_layout Plain Layout", ""]
116 # It will be faster for us to work with a single string internally.
117 # That way, we only go through the unicode_reps loop once.
118 if type(arg) is list:
122 for rep in unicode_reps:
123 s = s.replace(rep[1], rep[0].replace('\\\\', '\\'))
124 s = s.replace('\\', "\\backslash\n")
125 ret += s.splitlines()
126 ret += ["\\end_layout", "\\end_inset"]
130 def lyx2latex(document, lines):
131 'Convert some LyX stuff into corresponding LaTeX stuff, as best we can.'
138 for curline in range(len(lines)):
139 line = lines[curline]
140 if line.startswith("\\begin_inset Note Note"):
141 # We want to skip LyX notes, so remember where the inset ends
142 note_end = find_end_of_inset(lines, curline + 1)
144 elif note_end >= curline:
147 elif line.startswith("\\begin_inset ERT"):
148 # We don't want to replace things inside ERT, so figure out
149 # where the end of the inset is.
150 ert_end = find_end_of_inset(lines, curline + 1)
152 elif line.startswith("\\begin_inset Formula"):
154 elif line.startswith("\\begin_inset Quotes"):
155 # For now, we do a very basic reversion. Someone who understands
156 # quotes is welcome to fix it up.
157 qtype = line[20:].strip()
171 elif line.startswith("\\begin_inset space"):
172 line = line[18:].strip()
173 if line.startswith("\\hspace"):
174 # Account for both \hspace and \hspace*
177 elif line == "\\space{}":
179 elif line == "\\thinspace{}":
182 # The LyX length is in line[8:], after the \length keyword
183 length = latex_length(line[8:])[1]
184 line = hspace + "{" + length + "}"
186 elif line.isspace() or \
187 line.startswith("\\begin_layout") or \
188 line.startswith("\\end_layout") or \
189 line.startswith("\\begin_inset") or \
190 line.startswith("\\end_inset") or \
191 line.startswith("\\lang") or \
192 line.strip() == "status collapsed" or \
193 line.strip() == "status open":
197 # this needs to be added to the preamble because of cases like
198 # \textmu, \textbackslash, etc.
199 add_to_preamble(document, ['% added by lyx2lyx for converted index entries',
200 '\\@ifundefined{textmu}',
201 ' {\\usepackage{textcomp}}{}'])
202 # a lossless reversion is not possible
203 # try at least to handle some common insets and settings
204 if ert_end >= curline:
205 line = line.replace(r'\backslash', '\\')
207 # No need to add "{}" after single-nonletter macros
208 line = line.replace('&', '\\&')
209 line = line.replace('#', '\\#')
210 line = line.replace('^', '\\textasciicircum{}')
211 line = line.replace('%', '\\%')
212 line = line.replace('_', '\\_')
213 line = line.replace('$', '\\$')
215 # Do the LyX text --> LaTeX conversion
216 for rep in unicode_reps:
217 line = line.replace(rep[1], rep[0] + "{}")
218 line = line.replace(r'\backslash', r'\textbackslash{}')
219 line = line.replace(r'\series bold', r'\bfseries{}').replace(r'\series default', r'\mdseries{}')
220 line = line.replace(r'\shape italic', r'\itshape{}').replace(r'\shape smallcaps', r'\scshape{}')
221 line = line.replace(r'\shape slanted', r'\slshape{}').replace(r'\shape default', r'\upshape{}')
222 line = line.replace(r'\emph on', r'\em{}').replace(r'\emph default', r'\em{}')
223 line = line.replace(r'\noun on', r'\scshape{}').replace(r'\noun default', r'\upshape{}')
224 line = line.replace(r'\bar under', r'\underbar{').replace(r'\bar default', r'}')
225 line = line.replace(r'\family sans', r'\sffamily{}').replace(r'\family default', r'\normalfont{}')
226 line = line.replace(r'\family typewriter', r'\ttfamily{}').replace(r'\family roman', r'\rmfamily{}')
227 line = line.replace(r'\InsetSpace ', r'').replace(r'\SpecialChar ', r'')
232 def latex_length(slen):
234 Convert lengths to their LaTeX representation. Returns (bool, length),
235 where the bool tells us if it was a percentage, and the length is the
236 LaTeX representation.
240 # the slen has the form
241 # ValueUnit+ValueUnit-ValueUnit or
242 # ValueUnit+-ValueUnit
243 # the + and - (glue lengths) are optional
244 # the + always precedes the -
246 # Convert relative lengths to LaTeX units
247 units = {"text%":"\\textwidth", "col%":"\\columnwidth",
248 "page%":"\\paperwidth", "line%":"\\linewidth",
249 "theight%":"\\textheight", "pheight%":"\\paperheight"}
250 for unit in units.keys():
255 minus = slen.rfind("-", 1, i)
256 plus = slen.rfind("+", 0, i)
257 latex_unit = units[unit]
258 if plus == -1 and minus == -1:
260 value = str(float(value)/100)
261 end = slen[i + len(unit):]
262 slen = value + latex_unit + end
264 value = slen[plus + 1:i]
265 value = str(float(value)/100)
266 begin = slen[:plus + 1]
267 end = slen[i+len(unit):]
268 slen = begin + value + latex_unit + end
270 value = slen[minus + 1:i]
271 value = str(float(value)/100)
272 begin = slen[:minus + 1]
273 slen = begin + value + latex_unit
275 # replace + and -, but only if the - is not the first character
276 slen = slen[0] + slen[1:].replace("+", " plus ").replace("-", " minus ")
277 # handle the case where "+-1mm" was used, because LaTeX only understands
278 # "plus 1mm minus 1mm"
279 if slen.find("plus minus"):
280 lastvaluepos = slen.rfind(" ")
281 lastvalue = slen[lastvaluepos:]
282 slen = slen.replace(" ", lastvalue + " ")
283 return (percent, slen)
286 def revert_flex_inset(lines, name, LaTeXname):
287 " Convert flex insets to TeX code "
290 i = find_token(lines, '\\begin_inset Flex ' + name, i)
293 z = find_end_of_inset(lines, i)
295 document.warning("Can't find end of Flex " + name + " inset.")
298 # remove the \end_inset
299 lines[z - 2:z + 1] = put_cmd_in_ert("}")
300 # we need to reset character layouts if necessary
301 j = find_token(lines, '\\emph on', i, z)
302 k = find_token(lines, '\\noun on', i, z)
303 l = find_token(lines, '\\series', i, z)
304 m = find_token(lines, '\\family', i, z)
305 n = find_token(lines, '\\shape', i, z)
306 o = find_token(lines, '\\color', i, z)
307 p = find_token(lines, '\\size', i, z)
308 q = find_token(lines, '\\bar under', i, z)
309 r = find_token(lines, '\\uuline on', i, z)
310 s = find_token(lines, '\\uwave on', i, z)
311 t = find_token(lines, '\\strikeout on', i, z)
313 lines.insert(z - 2, "\\emph default")
315 lines.insert(z - 2, "\\noun default")
317 lines.insert(z - 2, "\\series default")
319 lines.insert(z - 2, "\\family default")
321 lines.insert(z - 2, "\\shape default")
323 lines.insert(z - 2, "\\color inherit")
325 lines.insert(z - 2, "\\size default")
327 lines.insert(z - 2, "\\bar default")
329 lines.insert(z - 2, "\\uuline default")
331 lines.insert(z - 2, "\\uwave default")
333 lines.insert(z - 2, "\\strikeout default")
334 lines[i:i + 4] = put_cmd_in_ert(LaTeXname + "{")
338 def revert_font_attrs(lines, name, LaTeXname):
339 " Reverts font changes to TeX code "
343 i = find_token(lines, name + ' on', i)
346 j = find_token(lines, name + ' default', i)
347 k = find_token(lines, name + ' on', i + 1)
348 # if there is no default set, the style ends with the layout
349 # assure hereby that we found the correct layout end
350 if j != -1 and (j < k or k == -1):
351 lines[j:j + 1] = put_cmd_in_ert("}")
353 j = find_token(lines, '\\end_layout', i)
354 lines[j:j] = put_cmd_in_ert("}")
355 lines[i:i + 1] = put_cmd_in_ert(LaTeXname + "{")
360 def revert_layout_command(lines, name, LaTeXname):
361 " Reverts a command from a layout to TeX code "
364 i = find_token(lines, '\\begin_layout ' + name, i)
368 # find the next layout
371 j = find_token(lines, '\\begin_layout', j)
373 # if nothing was found it was the last layout of the document
375 lines[l - 4:l - 4] = put_cmd_in_ert("}")
377 # exclude plain layout because this can be TeX code or another inset
378 elif lines[j] != '\\begin_layout Plain Layout':
379 lines[j - 2:j - 2] = put_cmd_in_ert("}")
383 lines[i] = '\\begin_layout Standard'
384 lines[i + 1:i + 1] = put_cmd_in_ert(LaTeXname + "{")
389 " Converts an RRGGBB-type hexadecimal string to a float in [0.0,1.0] "
396 return str(val / 256.0)
400 "'true' goes to True, case-insensitively, and we strip whitespace."
401 s = s.strip().lower()