]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx_2_4.py
use revert_language in more cases
[lyx.git] / lib / lyx2lyx / lyx_2_4.py
index 13a95cf17dfaeed42fdfbd6181ac7b60e6fbf2a0..cf68d70a3470f8e0d645a8f37baa2465e2ec3d9f 100644 (file)
@@ -36,7 +36,7 @@ from parser_tools import (count_pars_in_inset, find_end_of_inset, find_end_of_la
 #    is_in_inset, set_bool_value
 #    find_tokens, find_token_exact, check_token
 
-from lyx2lyx_tools import (put_cmd_in_ert, add_to_preamble)
+from lyx2lyx_tools import (put_cmd_in_ert, add_to_preamble, revert_language)
 #  revert_font_attrs, insert_to_preamble, latex_length
 #  get_ert, lyx2latex, lyx2verbatim, length_in_bp, convert_info_insets
 #  revert_flex_inset, hex2ratio, str2bool
@@ -126,7 +126,7 @@ class fontmapping:
             return fontname
         return None
 
-def createFontMapping():
+def createFontMapping(fontlist):
     # Create info for known fonts for the use in
     #   convert_latexFonts() and
     #   revert_latexFonts()
@@ -136,21 +136,28 @@ def createFontMapping():
     # * For now, add DejaVu and IBMPlex only.
     # * Expand, if desired
     fm = fontmapping()
-    fm.expandFontMapping(['DejaVuSerif', 'DejaVuSerifCondensed'], "roman", None, None)
-    fm.expandFontMapping(['DejaVuSans','DejaVuSansCondensed'], "sans", "sf", None, "scaled")
-    fm.expandFontMapping(['DejaVuSansMono'], "typewriter", "tt", None, "scaled")
-    fm.expandFontMapping(['IBMPlexSerif', 'IBMPlexSerifThin,thin',
-                          'IBMPlexSerifExtraLight,extralight', 'IBMPlexSerifLight,light',
-                          'IBMPlexSerifSemibold,semibold'],
-                         "roman", None, "plex-serif")
-    fm.expandFontMapping(['IBMPlexSans','IBMPlexSansCondensed,condensed',
-                          'IBMPlexSansThin,thin', 'IBMPlexSansExtraLight,extralight',
-                          'IBMPlexSansLight,light', 'IBMPlexSansSemibold,semibold'],
-                         "sans", "sf", "plex-sans", "scale")
-    fm.expandFontMapping(['IBMPlexMono', 'IBMPlexMonoThin,thin',
-                          'IBMPlexMonoExtraLight,extralight', 'IBMPlexMonoLight,light',
-                          'IBMPlexMonoSemibold,semibold'],
-                         "typewriter", "tt", "plex-mono", "scale")
+    for font in fontlist:
+        if font == 'DejaVu':
+            fm.expandFontMapping(['DejaVuSerif', 'DejaVuSerifCondensed'], "roman", None, None)
+            fm.expandFontMapping(['DejaVuSans','DejaVuSansCondensed'], "sans", "sf", None, "scaled")
+            fm.expandFontMapping(['DejaVuSansMono'], "typewriter", "tt", None, "scaled")
+        elif font == 'IBM':
+            fm.expandFontMapping(['IBMPlexSerif', 'IBMPlexSerifThin,thin',
+                                  'IBMPlexSerifExtraLight,extralight', 'IBMPlexSerifLight,light',
+                                  'IBMPlexSerifSemibold,semibold'],
+                                 "roman", None, "plex-serif")
+            fm.expandFontMapping(['IBMPlexSans','IBMPlexSansCondensed,condensed',
+                                  'IBMPlexSansThin,thin', 'IBMPlexSansExtraLight,extralight',
+                                  'IBMPlexSansLight,light', 'IBMPlexSansSemibold,semibold'],
+                                 "sans", "sf", "plex-sans", "scale")
+            fm.expandFontMapping(['IBMPlexMono', 'IBMPlexMonoThin,thin',
+                                  'IBMPlexMonoExtraLight,extralight', 'IBMPlexMonoLight,light',
+                                  'IBMPlexMonoSemibold,semibold'],
+                                 "typewriter", "tt", "plex-mono", "scale")
+        elif font == 'Adobe':
+            fm.expandFontMapping(['ADOBESourceSerifPro'], "roman", None, "sourceserifpro")
+            fm.expandFontMapping(['ADOBESourceSansPro'], "sans", "sf", "sourcesanspro", "scaled")
+            fm.expandFontMapping(['ADOBESourceCodePro'], "typewriter", "tt", "sourcecodepro", "scaled")
     return fm
 
 def convert_fonts(document, fm):
@@ -212,8 +219,9 @@ def convert_fonts(document, fm):
         j = find_token(document.header, ft, 0)
         if j != -1:
             val = get_value(document.header, ft, j)
-            vals = val.split()
-            document.header[j] = ft + ' "' + fn + '" ' + vals[1]
+            words = val.split() # ! splits also values like '"DejaVu Sans"'
+            words[0] = '"' + fn + '"'
+            document.header[j] = ft + ' ' + ' '.join(words)
 
 def revert_fonts(document, fm, fontmap):
     " Revert native font definition to LaTeX "
@@ -235,8 +243,8 @@ def revert_fonts(document, fm, fontmap):
             continue
         ft = mo.group(1)    # 'roman', 'sans', 'typewriter', 'math'
         val = get_value(document.header, ft, i)
-        words = val.split()
-        font = words[0].replace('"', '')
+        words = val.split(' ')     # ! splits also values like '"DejaVu Sans"'
+        font = words[0].strip('"') # TeX font name has no whitespace
         if not font in fm.font2pkgmap:
             i += 1
             continue
@@ -244,7 +252,8 @@ def revert_fonts(document, fm, fontmap):
         val = fontinfo.package
         if not val in fontmap:
             fontmap[val] = []
-        document.header[i] = ft + ' "default" ' + words[1]
+        words[0] = '"default"'
+        document.header[i] = ft + ' ' + ' '.join(words)
         if fontinfo.scaleopt != None:
             xval =  get_value(document.header, "\\font_" + fontinfo.scaletype + "_scale", 0)
             mo = rscales.search(xval)
@@ -268,7 +277,7 @@ def convert_latexFonts(document):
     " Handle DejaVu and IBMPlex fonts definition to LaTeX "
 
     if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
-        fm = createFontMapping()
+        fm = createFontMapping(['DejaVu', 'IBM'])
         convert_fonts(document, fm)
 
 def revert_latexFonts(document):
@@ -276,7 +285,23 @@ def revert_latexFonts(document):
 
     if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
         fontmap = dict()
-        fm = createFontMapping()
+        fm = createFontMapping(['DejaVu', 'IBM'])
+        revert_fonts(document, fm, fontmap)
+        add_preamble_fonts(document, fontmap)
+
+def convert_AdobeFonts(document):
+    " Handle DejaVu and IBMPlex fonts definition to LaTeX "
+
+    if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
+        fm = createFontMapping(['Adobe'])
+        convert_fonts(document, fm)
+
+def revert_AdobeFonts(document):
+    " Revert native DejaVu font definition to LaTeX "
+
+    if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
+        fontmap = dict()
+        fm = createFontMapping(['Adobe'])
         revert_fonts(document, fm, fontmap)
         add_preamble_fonts(document, fontmap)
 
@@ -529,7 +554,7 @@ def revert_floatpclass(document):
             k = find_token(document.body, 'placement document', i, i + 2)
             if k != -1:
                 del document.body[k]
-            i = j
+            i += 1
             continue
         del document.body[k]
 
@@ -564,7 +589,7 @@ def revert_floatalignment(document):
         l = find_token(document.body, "\\begin_layout Plain Layout", i, j)
         if l == -1:
             document.warning("Can't find float layout!")
-            i = j
+            i += 1
             continue
         alcmd = []
         if alignment == "left":
@@ -575,7 +600,7 @@ def revert_floatalignment(document):
             alcmd = put_cmd_in_ert("\\raggedleft{}")
         if len(alcmd) > 0:
             document.body[l+1:l+1] = alcmd
-        i = j 
+        i += 1
 
 
 def revert_tuftecite(document):
@@ -705,7 +730,7 @@ def revert_vcolumns(document):
                             if vval != "":
                                 needarray = True
                             vval += "V{\\linewidth}"
-                
+
                             document.body[col_line] = document.body[col_line][:-1] + " special=\"" + vval + "\">"
                             # ERT newlines and linebreaks (since LyX < 2.4 automatically inserts parboxes
                             # with newlines, and we do not want that)
@@ -751,7 +776,7 @@ def revert_bibencoding(document):
     if engine in ["biblatex", "biblatex-natbib"]:
         biblatex = True
 
-    # Map lyx to latex encoding names 
+    # Map lyx to latex encoding names
     encodings = {
         "utf8" : "utf8",
         "utf8x" : "utf8x",
@@ -972,6 +997,7 @@ def revert_dateinfo(document):
         "lowersorbian" : ["%A, %d. %B %Y", "%d.%m.%y", "%d %B %Y", "%d %b %Y", "%d.%m.%Y"],
         "macedonian" : ["%A, %d %B %Y", "%d.%m.%y", "%d %B %Y", "%d %b %Y", "%d.%m.%Y"],
         "magyar" : ["%Y. %B %d., %A", "%Y. %m. %d.", "%Y. %B %d.", "%Y. %b %d.", "%Y.%m.%d."],
+        "malayalam" : ["%A, %d %B, %Y", "%d/%m/%y", "%d %B %Y", "%d %b %Y", "%d-%m-%Y"],
         "marathi" : ["%A, %d %B, %Y", "%d/%m/%y", "%d %B %Y", "%d %b %Y", "%d-%m-%Y"],
         "mongolian" : ["%A, %Y оны %m сарын %d", "%Y-%m-%d", "%Y оны %m сарын %d", "%d-%m-%Y", "%d-%m-%Y"],
         "naustrian" : ["%A, %d. %B %Y", "%d.%m.%y", "%d. %B %Y", "%d. %b %Y", "%d.%m.%Y"],
@@ -1071,6 +1097,10 @@ def revert_dateinfo(document):
             fmt = re.sub('[^\'%]d', '%d', fmt)
             fmt = fmt.replace("'", "")
             result = dte.strftime(fmt)
+        if sys.version_info < (3,0):
+            # In Python 2, datetime module works with binary strings,
+            # our dateformat strings are utf8-encoded:
+            result = result.decode('utf-8')
         document.body[i : j+1] = result
         i = i + 1
 
@@ -1150,6 +1180,7 @@ def revert_timeinfo(document):
         "lowersorbian" : ["%H:%M:%S %Z", "%H:%M"],
         "macedonian" : ["%H:%M:%S %Z", "%H:%M"],
         "magyar" : ["%H:%M:%S %Z", "%H:%M"],
+        "malayalam" : ["%p %I:%M:%S %Z", "%p %I:%M"],
         "marathi" : ["%I:%M:%S %p %Z", "%I:%M %p"],
         "mongolian" : ["%H:%M:%S %Z", "%H:%M"],
         "naustrian" : ["%H:%M:%S %Z", "%H:%M"],
@@ -1299,7 +1330,7 @@ def revert_l7ninfo(document):
             continue
         arg = find_token(document.body, 'arg', i, j)
         argv = get_quoted_value(document.body, "arg", arg)
-        # remove trailing colons, menu accelerator (|...) and qt accelerator (&), while keeping literal " & " 
+        # remove trailing colons, menu accelerator (|...) and qt accelerator (&), while keeping literal " & "
         argv = argv.rstrip(':').split('|')[0].replace(" & ", "</amp;>").replace("&", "").replace("</amp;>", " & ")
         document.body[i : j+1] = argv
         i = i + 1
@@ -1358,6 +1389,30 @@ def revert_lformatinfo(document):
         i = i + 1
 
 
+def convert_hebrew_parentheses(document):
+    " Don't reverse parentheses in Hebrew text"
+    current_language = document.language
+    for i, line in enumerate(document.body):
+        if line.startswith('\\lang '):
+            current_language = line[len('\\lang '):]
+        elif line.startswith('\\end_layout'):
+            current_language = document.language
+        elif current_language == 'hebrew' and not line.startswith('\\'):
+            document.body[i] = line.replace('(','\x00').replace(')','(').replace('\x00',')')
+
+
+def revert_hebrew_parentheses(document):
+    " Store parentheses in Hebrew text reversed"
+    # This only exists to keep the convert/revert naming convention
+    convert_hebrew_parentheses(document)
+
+
+def revert_malayalam(document):
+    " Set the document language to English but assure Malayalam output "
+
+    revert_language(document, "malayalam", "", "malayalam")
+
+
 ##
 # Conversion hub
 #
@@ -1383,10 +1438,16 @@ convert = [
            [561, [convert_latexFonts]], # Handle dejavu, ibmplex fonts in GUI
            [562, []],
            [563, []],
-           [564, []]
+           [564, []],
+           [565, [convert_AdobeFonts]], # Handle adobe fonts in GUI
+           [566, [convert_hebrew_parentheses]],
+           [567, []],
           ]
 
 revert =  [
+           [566, [revert_malayalam]],
+           [565, [revert_hebrew_parentheses]],
+           [564, [revert_AdobeFonts]],
            [563, [revert_lformatinfo]],
            [562, [revert_listpargs]],
            [561, [revert_l7ninfo]],