]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx2lyx_tools.py
move README.Documentation to attic
[lyx.git] / lib / lyx2lyx / lyx2lyx_tools.py
1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2011 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18
19 '''
20 This module offers 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   Prepends a comment "% Added by lyx2lyx" to text.
31
32 insert_to_preamble(document, text[, index]):
33   Here, text can be either a single line or a list of lines. It
34   is bad practice to pass something with embedded newlines, but
35   we will handle that properly.
36   The routine inserts text at document.preamble[index], where by
37   default index is 0, so the material is inserted at the beginning.
38   Prepends a comment "% Added by lyx2lyx" to text.
39
40 put_cmd_in_ert(cmd):
41   Here cmd should be a list of strings (lines), which we want to
42   wrap in ERT. Returns a list of strings so wrapped.
43   A call to this routine will often go something like this:
44     i = find_token('\\begin_inset FunkyInset', ...)
45     j = find_end_of_inset(document.body, i)
46     content = lyx2latex(document[i:j + 1])
47     ert = put_cmd_in_ert(content)
48     document.body[i:j+1] = ert
49
50 get_ert(lines, i[, verbatim]):
51   Here, lines is a list of lines of LyX material containing an ERT inset,
52   whose content we want to convert to LaTeX. The ERT starts at index i.
53   If the optional (by default: False) bool verbatim is True, the content
54   of the ERT is returned verbatim, that is in LyX syntax (not LaTeX syntax)
55   for the use in verbatim insets.
56
57 lyx2latex(document, lines):
58   Here, lines is a list of lines of LyX material we want to convert
59   to LaTeX. We do the best we can and return a string containing
60   the translated material.
61
62 lyx2verbatim(document, lines):
63   Here, lines is a list of lines of LyX material we want to convert
64   to verbatim material (used in ERT an the like). We do the best we
65   can and return a string containing the translated material.
66
67 latex_length(slen):
68     Convert lengths (in LyX form) to their LaTeX representation. Returns
69     (bool, length), where the bool tells us if it was a percentage, and
70     the length is the LaTeX representation.
71
72 convert_info_insets(document, type, func):
73     Applies func to the argument of all info insets matching certain types
74     type : the type to match. This can be a regular expression.
75     func : function from string to string to apply to the "arg" field of
76            the info insets.
77 '''
78
79 import re
80 import string
81 from parser_tools import find_token, find_end_of_inset
82 from unicode_symbols import unicode_reps
83
84 # This will accept either a list of lines or a single line.
85 # It is bad practice to pass something with embedded newlines,
86 # though we will handle that.
87 def add_to_preamble(document, text):
88     " Add text to the preamble if it is not already there. "
89
90     if not type(text) is list:
91       # split on \n just in case
92       # it'll give us the one element list we want
93       # if there's no \n, too
94       text = text.split('\n')
95
96     i = 0
97     prelen = len(document.preamble)
98     while True:
99       i = find_token(document.preamble, text[0], i)
100       if i == -1:
101         break
102       # we need a perfect match
103       matched = True
104       for line in text:
105         if i >= prelen or line != document.preamble[i]:
106           matched = False
107           break
108         i += 1
109       if matched:
110         return
111
112     document.preamble.extend(["% Added by lyx2lyx"])
113     document.preamble.extend(text)
114
115
116 # Note that text can be either a list of lines or a single line.
117 # It should really be a list.
118 def insert_to_preamble(document, text, index = 0):
119     """ Insert text to the preamble at a given line"""
120
121     if not type(text) is list:
122       # split on \n just in case
123       # it'll give us the one element list we want
124       # if there's no \n, too
125       text = text.split('\n')
126
127     text.insert(0, "% Added by lyx2lyx")
128     document.preamble[index:index] = text
129
130
131 # A dictionary of Unicode->LICR mappings for use in a Unicode string's translate() method
132 # Created from the reversed list to keep the first of alternative definitions.
133 licr_table = dict((ord(ch), cmd) for cmd, ch in unicode_reps[::-1])
134
135 def put_cmd_in_ert(cmd):
136     """
137     Return ERT inset wrapping `cmd` as a list of strings.
138
139     `cmd` can be a string or list of lines. Non-ASCII characters are converted
140     to the respective LICR macros if defined in unicodesymbols.
141     """
142     ret = ["\\begin_inset ERT", "status collapsed", "", "\\begin_layout Plain Layout", ""]
143     # It will be faster to work with a single string internally.
144     if isinstance(cmd, list):
145         cmd = u"\n".join(cmd)
146     else:
147         cmd = u"%s" % cmd # ensure it is an unicode instance
148     cmd = cmd.translate(licr_table)
149     cmd = cmd.replace("\\", "\n\\backslash\n")
150     ret += cmd.splitlines()
151     ret += ["\\end_layout", "", "\\end_inset"]
152     return ret
153
154
155 def get_ert(lines, i, verbatim = False):
156     'Convert an ERT inset into LaTeX.'
157     if not lines[i].startswith("\\begin_inset ERT"):
158         return ""
159     j = find_end_of_inset(lines, i)
160     if j == -1:
161         return ""
162     while i < j and not lines[i].startswith("status"):
163         i = i + 1
164     i = i + 1
165     ret = ""
166     first = True
167     while i < j:
168         if lines[i] == "\\begin_layout Plain Layout":
169             if first:
170                 first = False
171             else:
172                 ret = ret + "\n"
173             while i + 1 < j and lines[i+1] == "":
174                 i = i + 1
175         elif lines[i] == "\\end_layout":
176             while i + 1 < j and lines[i+1] == "":
177                 i = i + 1
178         elif lines[i] == "\\backslash":
179             if verbatim:
180                 ret = ret + "\n" + lines[i] + "\n"
181             else:
182                 ret = ret + "\\"
183         else:
184             ret = ret + lines[i]
185         i = i + 1
186     return ret
187
188
189 def lyx2latex(document, lines):
190     'Convert some LyX stuff into corresponding LaTeX stuff, as best we can.'
191
192     content = ""
193     ert_end = 0
194     note_end = 0
195     hspace = ""
196
197     for curline in range(len(lines)):
198       line = lines[curline]
199       if line.startswith("\\begin_inset Note Note"):
200           # We want to skip LyX notes, so remember where the inset ends
201           note_end = find_end_of_inset(lines, curline + 1)
202           continue
203       elif note_end >= curline:
204           # Skip LyX notes
205           continue
206       elif line.startswith("\\begin_inset ERT"):
207           # We don't want to replace things inside ERT, so figure out
208           # where the end of the inset is.
209           ert_end = find_end_of_inset(lines, curline + 1)
210           continue
211       elif line.startswith("\\begin_inset Formula"):
212           line = line[20:]
213       elif line.startswith("\\begin_inset Quotes"):
214           # For now, we do a very basic reversion. Someone who understands
215           # quotes is welcome to fix it up.
216           qtype = line[20:].strip()
217           # lang = qtype[0]
218           side = qtype[1]
219           dbls = qtype[2]
220           if side == "l":
221               if dbls == "d":
222                   line = "``"
223               else:
224                   line = "`"
225           else:
226               if dbls == "d":
227                   line = "''"
228               else:
229                   line = "'"
230       elif line.startswith("\\begin_inset Newline newline"):
231           line = "\\\\ "
232       elif line.startswith("\\noindent"):
233           line = "\\noindent " # we need the space behind the command
234       elif line.startswith("\\begin_inset space"):
235           line = line[18:].strip()
236           if line.startswith("\\hspace"):
237               # Account for both \hspace and \hspace*
238               hspace = line[:-2]
239               continue
240           elif line == "\\space{}":
241               line = "\\ "
242           elif line == "\\thinspace{}":
243               line = "\\,"
244       elif hspace != "":
245           # The LyX length is in line[8:], after the \length keyword
246           length = latex_length(line[8:])[1]
247           line = hspace + "{" + length + "}"
248           hspace = ""
249       elif line.isspace() or \
250             line.startswith("\\begin_layout") or \
251             line.startswith("\\end_layout") or \
252             line.startswith("\\begin_inset") or \
253             line.startswith("\\end_inset") or \
254             line.startswith("\\lang") or \
255             line.strip() == "status collapsed" or \
256             line.strip() == "status open":
257           #skip all that stuff
258           continue
259
260       # this needs to be added to the preamble because of cases like
261       # \textmu, \textbackslash, etc.
262       add_to_preamble(document, ['% added by lyx2lyx for converted index entries',
263                                  '\\@ifundefined{textmu}',
264                                  ' {\\usepackage{textcomp}}{}'])
265       # a lossless reversion is not possible
266       # try at least to handle some common insets and settings
267       if ert_end >= curline:
268           line = line.replace(r'\backslash', '\\')
269       else:
270           # No need to add "{}" after single-nonletter macros
271           line = line.replace('&', '\\&')
272           line = line.replace('#', '\\#')
273           line = line.replace('^', '\\textasciicircum{}')
274           line = line.replace('%', '\\%')
275           line = line.replace('_', '\\_')
276           line = line.replace('$', '\\$')
277
278           # Do the LyX text --> LaTeX conversion
279           for rep in unicode_reps:
280               line = line.replace(rep[1], rep[0])
281           line = line.replace(r'\backslash', r'\textbackslash{}')
282           line = line.replace(r'\series bold', r'\bfseries{}').replace(r'\series default', r'\mdseries{}')
283           line = line.replace(r'\shape italic', r'\itshape{}').replace(r'\shape smallcaps', r'\scshape{}')
284           line = line.replace(r'\shape slanted', r'\slshape{}').replace(r'\shape default', r'\upshape{}')
285           line = line.replace(r'\emph on', r'\em{}').replace(r'\emph default', r'\em{}')
286           line = line.replace(r'\noun on', r'\scshape{}').replace(r'\noun default', r'\upshape{}')
287           line = line.replace(r'\bar under', r'\underbar{').replace(r'\bar default', r'}')
288           line = line.replace(r'\family sans', r'\sffamily{}').replace(r'\family default', r'\normalfont{}')
289           line = line.replace(r'\family typewriter', r'\ttfamily{}').replace(r'\family roman', r'\rmfamily{}')
290           line = line.replace(r'\InsetSpace ', r'').replace(r'\SpecialChar ', r'')
291       content += line
292     return content
293
294
295 def lyx2verbatim(document, lines):
296     'Convert some LyX stuff into corresponding verbatim stuff, as best we can.'
297
298     content = lyx2latex(document, lines)
299     content = re.sub(r'\\(?!backslash)', r'\n\\backslash\n', content)
300
301     return content
302
303
304 def latex_length(slen):
305     '''
306     Convert lengths to their LaTeX representation. Returns (bool, length),
307     where the bool tells us if it was a percentage, and the length is the
308     LaTeX representation.
309     '''
310     i = 0
311     percent = False
312     # the slen has the form
313     # ValueUnit+ValueUnit-ValueUnit or
314     # ValueUnit+-ValueUnit
315     # the + and - (glue lengths) are optional
316     # the + always precedes the -
317
318     # Convert relative lengths to LaTeX units
319     units = {"col%": "\\columnwidth",
320              "text%": "\\textwidth",
321              "page%": "\\paperwidth", 
322              "line%": "\\linewidth",
323              "theight%": "\\textheight",
324              "pheight%": "\\paperheight",
325              "baselineskip%": "\\baselineskip"
326             }
327     for unit in list(units.keys()):
328         i = slen.find(unit)
329         if i == -1:
330             continue
331         percent = True
332         minus = slen.rfind("-", 1, i)
333         plus = slen.rfind("+", 0, i)
334         latex_unit = units[unit]
335         if plus == -1 and minus == -1:
336             value = slen[:i]
337             value = str(float(value)/100)
338             end = slen[i + len(unit):]
339             slen = value + latex_unit + end
340         if plus > minus:
341             value = slen[plus + 1:i]
342             value = str(float(value)/100)
343             begin = slen[:plus + 1]
344             end = slen[i+len(unit):]
345             slen = begin + value + latex_unit + end
346         if plus < minus:
347             value = slen[minus + 1:i]
348             value = str(float(value)/100)
349             begin = slen[:minus + 1]
350             slen = begin + value + latex_unit
351
352     # replace + and -, but only if the - is not the first character
353     slen = slen[0] + slen[1:].replace("+", " plus ").replace("-", " minus ")
354     # handle the case where "+-1mm" was used, because LaTeX only understands
355     # "plus 1mm minus 1mm"
356     if slen.find("plus  minus"):
357         lastvaluepos = slen.rfind(" ")
358         lastvalue = slen[lastvaluepos:]
359         slen = slen.replace("  ", lastvalue + " ")
360     return (percent, slen)
361
362
363 def length_in_bp(length):
364     " Convert a length in LyX format to its value in bp units "
365
366     em_width = 10.0 / 72.27 # assume 10pt font size
367     text_width = 8.27 / 1.7 # assume A4 with default margins
368     # scale factors are taken from Length::inInch()
369     scales = {"bp"       : 1.0,
370               "cc"       : (72.0 / (72.27 / (12.0 * 0.376 * 2.845))),
371               "cm"       : (72.0 / 2.54),
372               "dd"       : (72.0 / (72.27 / (0.376 * 2.845))),
373               "em"       : (72.0 * em_width),
374               "ex"       : (72.0 * em_width * 0.4305),
375               "in"       : 72.0,
376               "mm"       : (72.0 / 25.4),
377               "mu"       : (72.0 * em_width / 18.0),
378               "pc"       : (72.0 / (72.27 / 12.0)),
379               "pt"       : (72.0 / (72.27)),
380               "sp"       : (72.0 / (72.27 * 65536.0)),
381               "text%"    : (72.0 * text_width / 100.0),
382               "col%"     : (72.0 * text_width / 100.0), # assume 1 column
383               "page%"    : (72.0 * text_width * 1.7 / 100.0),
384               "line%"    : (72.0 * text_width / 100.0),
385               "theight%" : (72.0 * text_width * 1.787 / 100.0),
386               "pheight%" : (72.0 * text_width * 2.2 / 100.0)}
387
388     rx = re.compile(r'^\s*([^a-zA-Z%]+)([a-zA-Z%]+)\s*$')
389     m = rx.match(length)
390     if not m:
391         document.warning("Invalid length value: " + length + ".")
392         return 0
393     value = m.group(1)
394     unit = m.group(2)
395     if not unit in scales.keys():
396         document.warning("Unknown length unit: " + unit + ".")
397         return value
398     return "%g" % (float(value) * scales[unit])
399
400
401 def revert_flex_inset(lines, name, LaTeXname):
402   " Convert flex insets to TeX code "
403   i = 0
404   while True:
405     i = find_token(lines, '\\begin_inset Flex ' + name, i)
406     if i == -1:
407       return
408     z = find_end_of_inset(lines, i)
409     if z == -1:
410       document.warning("Can't find end of Flex " + name + " inset.")
411       i += 1
412       continue
413     # remove the \end_inset
414     lines[z - 2:z + 1] = put_cmd_in_ert("}")
415     # we need to reset character layouts if necessary
416     j = find_token(lines, '\\emph on', i, z)
417     k = find_token(lines, '\\noun on', i, z)
418     l = find_token(lines, '\\series', i, z)
419     m = find_token(lines, '\\family', i, z)
420     n = find_token(lines, '\\shape', i, z)
421     o = find_token(lines, '\\color', i, z)
422     p = find_token(lines, '\\size', i, z)
423     q = find_token(lines, '\\bar under', i, z)
424     r = find_token(lines, '\\uuline on', i, z)
425     s = find_token(lines, '\\uwave on', i, z)
426     t = find_token(lines, '\\strikeout on', i, z)
427     if j != -1:
428       lines.insert(z - 2, "\\emph default")
429     if k != -1:
430       lines.insert(z - 2, "\\noun default")
431     if l != -1:
432       lines.insert(z - 2, "\\series default")
433     if m != -1:
434       lines.insert(z - 2, "\\family default")
435     if n != -1:
436       lines.insert(z - 2, "\\shape default")
437     if o != -1:
438       lines.insert(z - 2, "\\color inherit")
439     if p != -1:
440       lines.insert(z - 2, "\\size default")
441     if q != -1:
442       lines.insert(z - 2, "\\bar default")
443     if r != -1:
444       lines.insert(z - 2, "\\uuline default")
445     if s != -1:
446       lines.insert(z - 2, "\\uwave default")
447     if t != -1:
448       lines.insert(z - 2, "\\strikeout default")
449     lines[i:i + 4] = put_cmd_in_ert(LaTeXname + "{")
450     i += 1
451
452
453 def revert_font_attrs(lines, name, LaTeXname):
454   " Reverts font changes to TeX code "
455   i = 0
456   changed = False
457   while True:
458     i = find_token(lines, name + ' on', i)
459     if i == -1:
460       return changed
461     j = find_token(lines, name + ' default', i)
462     k = find_token(lines, name + ' on', i + 1)
463     # if there is no default set, the style ends with the layout
464     # assure hereby that we found the correct layout end
465     if j != -1 and (j < k or k == -1):
466       lines[j:j + 1] = put_cmd_in_ert("}")
467     else:
468       j = find_token(lines, '\\end_layout', i)
469       lines[j:j] = put_cmd_in_ert("}")
470     lines[i:i + 1] = put_cmd_in_ert(LaTeXname + "{")
471     changed = True
472     i += 1
473
474
475 def revert_layout_command(lines, name, LaTeXname):
476   " Reverts a command from a layout to TeX code "
477   i = 0
478   while True:
479     i = find_token(lines, '\\begin_layout ' + name, i)
480     if i == -1:
481       return
482     k = -1
483     # find the next layout
484     j = i + 1
485     while k == -1:
486       j = find_token(lines, '\\begin_layout', j)
487       l = len(lines)
488       # if nothing was found it was the last layout of the document
489       if j == -1:
490         lines[l - 4:l - 4] = put_cmd_in_ert("}")
491         k = 0
492       # exclude plain layout because this can be TeX code or another inset
493       elif lines[j] != '\\begin_layout Plain Layout':
494         lines[j - 2:j - 2] = put_cmd_in_ert("}")
495         k = 0
496       else:
497         j += 1
498     lines[i] = '\\begin_layout Standard'
499     lines[i + 1:i + 1] = put_cmd_in_ert(LaTeXname + "{")
500     i += 1
501
502
503 def hex2ratio(s):
504   " Converts an RRGGBB-type hexadecimal string to a float in [0.0,1.0] "
505   try:
506     val = int(s, 16)
507   except:
508     val = 0
509   if val != 0:
510     val += 1
511   return str(val / 256.0)
512
513
514 def str2bool(s):
515   "'true' goes to True, case-insensitively, and we strip whitespace."
516   s = s.strip().lower()
517   return s == "true"
518
519
520 def convert_info_insets(document, type, func):
521     "Convert info insets matching type using func."
522     i = 0
523     type_re = re.compile(r'^type\s+"(%s)"$' % type)
524     arg_re = re.compile(r'^arg\s+"(.*)"$')
525     while True:
526         i = find_token(document.body, "\\begin_inset Info", i)
527         if i == -1:
528             return
529         t = type_re.match(document.body[i + 1])
530         if t:
531             arg = arg_re.match(document.body[i + 2])
532             if arg:
533                 new_arg = func(arg.group(1))
534                 document.body[i + 2] = 'arg   "%s"' % new_arg
535         i += 3