]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx2lyx_tools.py
These commands should just take some lines.
[lyx.git] / lib / lyx2lyx / lyx2lyx_tools.py
1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2010 The LyX team
4 #
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.
9 #
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.
14 #
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.
18
19 '''
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.
23
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.
30
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].
36
37 put_cmd_in_ert(arg):
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
46
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.
51
52 latex_length(slen):
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.
56
57 '''
58
59 import string
60 from parser_tools import find_token
61 from unicode_symbols import unicode_reps
62
63
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. "
69
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')
75
76     i = 0
77     prelen = len(document.preamble)
78     while True:
79       i = find_token(document.preamble, text[0], i)
80       if i == -1:
81         break
82       # we need a perfect match
83       matched = True
84       for line in text:
85         if i >= prelen or line != document.preamble[i]:
86           matched = False
87           break
88         i += 1
89       if matched:
90         return
91
92     document.preamble.extend(text)
93
94
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"""
99     
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')
105
106     document.preamble[index:index] = text
107
108
109 def put_cmd_in_ert(arg):
110     '''
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.
113     '''
114     
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:
119       s = "\n".join(arg)
120     else:
121       s = arg
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"]
127     return ret
128
129             
130 def lyx2latex(document, lines):
131     'Convert some LyX stuff into corresponding LaTeX stuff, as best we can.'
132
133     content = ""
134     ert_end = 0
135     note_end = 0
136     hspace = ""
137
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)
143           continue
144       elif note_end >= curline:
145           # Skip LyX notes
146           continue
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)
151           continue
152       elif line.startswith("\\begin_inset Formula"):
153           line = line[20:]
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()
158           # lang = qtype[0]
159           side = qtype[1]
160           dbls = qtype[2]
161           if side == "l":
162               if dbls == "d":
163                   line = "``"
164               else:
165                   line = "`"
166           else:
167               if dbls == "d":
168                   line = "''"
169               else:
170                   line = "'"
171       elif line.startswith("\\begin_inset space"):
172           line = line[18:].strip()
173           if line.startswith("\\hspace"):
174               # Account for both \hspace and \hspace*
175               hspace = line[:-2]
176               continue
177           elif line == "\\space{}":
178               line = "\\ "
179           elif line == "\\thinspace{}":
180               line = "\\,"
181       elif hspace != "":
182           # The LyX length is in line[8:], after the \length keyword
183           length = latex_length(line[8:])[1]
184           line = hspace + "{" + length + "}"
185           hspace = ""
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":
194           #skip all that stuff
195           continue
196
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', '\\')
206       else:
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('$', '\\$')
214
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'')
228       content += line
229     return content
230
231
232 def latex_length(slen):
233     ''' 
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.
237     '''
238     i = 0
239     percent = False
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 -
245
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():
251         i = slen.find(unit)
252         if i == -1:
253             continue
254         percent = True
255         minus = slen.rfind("-", 1, i)
256         plus = slen.rfind("+", 0, i)
257         latex_unit = units[unit]
258         if plus == -1 and minus == -1:
259             value = slen[:i]
260             value = str(float(value)/100)
261             end = slen[i + len(unit):]
262             slen = value + latex_unit + end
263         if plus > minus:
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
269         if plus < minus:
270             value = slen[minus + 1:i]
271             value = str(float(value)/100)
272             begin = slen[:minus + 1]
273             slen = begin + value + latex_unit
274
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)
284
285
286 def revert_flex_inset(lines, name, LaTeXname):
287   " Convert flex insets to TeX code "
288   i = 0
289   while True:
290     i = find_token(lines, '\\begin_inset Flex ' + name, i)
291     if i == -1:
292       return
293     z = find_end_of_inset(lines, i)
294     if z == -1:
295       document.warning("Can't find end of Flex " + name + " inset.")
296       i += 1
297       continue
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)
312     if j != -1:
313       lines.insert(z - 2, "\\emph default")
314     if k != -1:
315       lines.insert(z - 2, "\\noun default")
316     if l != -1:
317       lines.insert(z - 2, "\\series default")
318     if m != -1:
319       lines.insert(z - 2, "\\family default")
320     if n != -1:
321       lines.insert(z - 2, "\\shape default")
322     if o != -1:
323       lines.insert(z - 2, "\\color inherit")
324     if p != -1:
325       lines.insert(z - 2, "\\size default")
326     if q != -1:
327       lines.insert(z - 2, "\\bar default")
328     if r != -1:
329       lines.insert(z - 2, "\\uuline default")
330     if s != -1:
331       lines.insert(z - 2, "\\uwave default")
332     if t != -1:
333       lines.insert(z - 2, "\\strikeout default")
334     lines[i:i + 4] = put_cmd_in_ert(LaTeXname + "{")
335     i += 1
336
337
338 def revert_font_attrs(lines, name, LaTeXname):
339   " Reverts font changes to TeX code "
340   i = 0
341   changed = False
342   while True:
343     i = find_token(lines, name + ' on', i)
344     if i == -1:
345       return changed
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("}")
352     else:
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 + "{")
356     changed = True
357     i += 1
358
359
360 def revert_layout_command(lines, name, LaTeXname):
361   " Reverts a command from a layout to TeX code "
362   i = 0
363   while True:
364     i = find_token(lines, '\\begin_layout ' + name, i)
365     if i == -1:
366       return
367     k = -1
368     # find the next layout
369     j = i + 1
370     while k == -1:
371       j = find_token(lines, '\\begin_layout', j)
372       l = len(lines)
373       # if nothing was found it was the last layout of the document
374       if j == -1:
375         lines[l - 4:l - 4] = put_cmd_in_ert("}")
376         k = 0
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("}")
380         k = 0
381       else:
382         j += 1
383     lines[i] = '\\begin_layout Standard'
384     lines[i + 1:i + 1] = put_cmd_in_ert(LaTeXname + "{")
385     i += 1
386
387
388 def hex2ratio(s):
389   " Converts an RRGGBB-type hexadecimal string to a float in [0.0,1.0] "
390   try:
391     val = int(s, 16)
392   except:
393     val = 0
394   if val != 0:
395     val += 1
396   return str(val / 256.0)
397
398
399 def str2bool(s):
400   "'true' goes to True, case-insensitively, and we strip whitespace."
401   s = s.strip().lower()
402   return s == "true"