]> 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 c4b007e056db3130d79c512b71479b9690339a2d..cf68d70a3470f8e0d645a8f37baa2465e2ec3d9f 100644 (file)
@@ -22,20 +22,21 @@ import re, string
 import unicodedata
 import sys, os
 
-from datetime import date
+from datetime import (datetime, date, time)
 
 # Uncomment only what you need to import, please.
 
 from parser_tools import (count_pars_in_inset, find_end_of_inset, find_end_of_layout,
-find_token, get_bool_value, get_option_value, get_value, get_quoted_value)
+                          find_token, find_re, get_bool_value, get_containing_layout,
+                          get_option_value, get_value, get_quoted_value)
 #    del_token, del_value, del_complete_lines,
 #    find_complete_lines, find_end_of,
 #    find_re, find_substring, find_token_backwards,
-#    get_containing_inset, get_containing_layout,
+#    get_containing_inset,
 #    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
@@ -43,7 +44,228 @@ from lyx2lyx_tools import (put_cmd_in_ert, add_to_preamble)
 ####################################################################
 # Private helper functions
 
+def add_preamble_fonts(document, fontmap):
+    " Add collected font-packages with their option to user-preamble"
 
+    for pkg in fontmap:
+        if len(fontmap[pkg]) > 0:
+            xoption = "[" + ",".join(fontmap[pkg]) + "]"
+        else:
+            xoption = ""
+        preamble = "\\usepackage" + xoption + "{%s}" % pkg
+        add_to_preamble(document, [preamble])
+
+
+def createkey(pkg, options):
+    options.sort()
+    return pkg + ':' + "-".join(options)
+
+class fontinfo:
+    def __init__(self):
+        self.fontname = None    # key into font2pkgmap
+        self.fonttype = None    # roman,sans,typewriter,math
+        self.scaletype = None   # None,sf,tt
+        self.scaleopt = None    # None, 'scaled', 'scale'
+        self.scaleval = 1
+        self.package = None
+        self.options = []
+        self.pkgkey = None      # key into pkg2fontmap
+
+    def addkey(self):
+        self.pkgkey = createkey(self.package, self.options)
+
+class fontmapping:
+    def __init__(self):
+        self.font2pkgmap = dict()
+        self.pkg2fontmap = dict()
+        self.pkginmap = dict()  # defines, if a map for package exists
+
+    def expandFontMapping(self, font_list, font_type, scale_type, pkg, scaleopt = None):
+        " Expand fontinfo mapping"
+        #
+        # fontlist:    list of fontnames, each element
+        #              may contain a ','-separated list of needed options
+        #              like e.g. 'IBMPlexSansCondensed,condensed'
+        # font_type:   one of 'roman', 'sans', 'typewriter', 'math'
+        # scale_type:  one of None, 'sf', 'tt'
+        # pkg:         package defining the font. Defaults to fontname if None
+        # scaleopt:    one of None, 'scale', 'scaled', or some other string
+        #              to be used in scale option (e.g. scaled=0.7)
+        for fl in font_list:
+            fe = fontinfo()
+            fe.fonttype = font_type
+            fe.scaletype = scale_type
+            flt = fl.split(",")
+            font_name = flt[0]
+            fe.fontname = font_name
+            fe.options = flt[1:]
+            fe.scaleopt = scaleopt
+            if pkg == None:
+                fe.package = font_name
+            else:
+                fe.package = pkg
+            fe.addkey()
+            self.font2pkgmap[font_name] = fe
+            if fe.pkgkey in self.pkg2fontmap:
+                # Repeated the same entry? Check content
+                if self.pkg2fontmap[fe.pkgkey] != font_name:
+                    document.error("Something is wrong in pkgname+options <-> fontname mapping")
+            self.pkg2fontmap[fe.pkgkey] = font_name
+            self.pkginmap[fe.package] = 1
+
+    def getfontname(self, pkg, options):
+        options.sort()
+        pkgkey = createkey(pkg, options)
+        if not pkgkey in self.pkg2fontmap:
+            return None
+        fontname = self.pkg2fontmap[pkgkey]
+        if not fontname in self.font2pkgmap:
+            document.error("Something is wrong in pkgname+options <-> fontname mapping")
+            return None
+        if pkgkey == self.font2pkgmap[fontname].pkgkey:
+            return fontname
+        return None
+
+def createFontMapping(fontlist):
+    # Create info for known fonts for the use in
+    #   convert_latexFonts() and
+    #   revert_latexFonts()
+    #
+    # * Would be more handy to parse latexFonts file,
+    #   but the path to this file is unknown
+    # * For now, add DejaVu and IBMPlex only.
+    # * Expand, if desired
+    fm = fontmapping()
+    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):
+    " Handle font definition to LaTeX "
+
+    rpkg = re.compile(r'^\\usepackage(\[([^\]]*)\])?\{([^\}]+)\}')
+    rscaleopt = re.compile(r'^scaled?=(.*)')
+
+    i = 0
+    while i < len(document.preamble):
+        i = find_re(document.preamble, rpkg, i)
+        if i == -1:
+            return
+        mo = rpkg.search(document.preamble[i])
+        if mo == None or mo.group(2) == None:
+            options = []
+        else:
+            options = mo.group(2).replace(' ', '').split(",")
+        pkg = mo.group(3)
+        o = 0
+        oscale = 1
+        while o < len(options):
+            mo = rscaleopt.search(options[o])
+            if mo == None:
+                o += 1
+                continue
+            oscale = mo.group(1)
+            del options[o]
+            break
+
+        if not pkg in fm.pkginmap:
+            i += 1
+            continue
+        # determine fontname
+        fn = fm.getfontname(pkg, options)
+        if fn == None:
+            i += 1
+            continue
+        del document.preamble[i]
+        fontinfo = fm.font2pkgmap[fn]
+        if fontinfo.scaletype == None:
+            fontscale = None
+        else:
+            fontscale = "\\font_" + fontinfo.scaletype + "_scale"
+            fontinfo.scaleval = oscale
+
+        if i > 0 and document.preamble[i-1] == "% Added by lyx2lyx":
+            del document.preamble[i-1]
+        if fontscale != None:
+            j = find_token(document.header, fontscale, 0)
+            if j != -1:
+                val = get_value(document.header, fontscale, j)
+                vals = val.split()
+                scale = "100"
+                if oscale != None:
+                    scale = "%03d" % int(float(oscale) * 100)
+                document.header[j] = fontscale + " " + scale + " " + vals[1]
+        ft = "\\font_" + fontinfo.fonttype
+        j = find_token(document.header, ft, 0)
+        if j != -1:
+            val = get_value(document.header, ft, j)
+            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 "
+    # fonlist := list of fonts created from the same package
+    # Empty package means that the font-name is the same as the package-name
+    # fontmap (key = package, val += found options) will be filled
+    # and used later in add_preamble_fonts() to be added to user-preamble
+
+    rfontscale = re.compile(r'^\s*(\\font_(roman|sans|typewriter|math))\s+')
+    rscales = re.compile(r'^\s*(\d+)\s+(\d+)')
+    i = 0
+    while i < len(document.header):
+        i = find_re(document.header, rfontscale, i)
+        if (i == -1):
+            break
+        mo = rfontscale.search(document.header[i])
+        if mo == None:
+            i += 1
+            continue
+        ft = mo.group(1)    # 'roman', 'sans', 'typewriter', 'math'
+        val = get_value(document.header, ft, i)
+        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
+        fontinfo = fm.font2pkgmap[font]
+        val = fontinfo.package
+        if not val in fontmap:
+            fontmap[val] = []
+        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)
+            if mo != None:
+                xval1 = mo.group(1)
+                xval2 = mo.group(2)
+                if xval1 != "100":
+                    # set correct scale option
+                    fontmap[val].extend([fontinfo.scaleopt + "=" + format(float(xval1) / 100, '.2f')])
+        if len(fontinfo.options) > 0:
+            fontmap[val].extend(fontinfo.options)
+        i += 1
 
 ###############################################################################
 ###
@@ -51,8 +273,40 @@ from lyx2lyx_tools import (put_cmd_in_ert, add_to_preamble)
 ###
 ###############################################################################
 
+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(['DejaVu', 'IBM'])
+        convert_fonts(document, fm)
+
+def revert_latexFonts(document):
+    " Revert native DejaVu font definition to LaTeX "
+
+    if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
+        fontmap = dict()
+        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)
+
 def removeFrontMatterStyles(document):
-    " Remove styles Begin/EndFromatter"
+    " Remove styles Begin/EndFrontmatter"
 
     layouts = ['BeginFrontmatter', 'EndFrontmatter']
     for layout in layouts:
@@ -300,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]
 
@@ -335,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":
@@ -346,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):
@@ -476,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)
@@ -522,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",
@@ -743,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"],
@@ -842,10 +1097,322 @@ 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
+
+
+def revert_timeinfo(document):
+    " Revert time info insets to static text. "
+
+# FIXME This currently only considers the main language and uses the system locale
+# Ideally, it should honor context languages and switch the locale accordingly.
+# Also, the time object is "naive", i.e., it does not know of timezones (%Z will
+# be empty).
+
+    # The time formats for each language using strftime syntax:
+    # long, short
+    timeformats = {
+        "afrikaans" : ["%H:%M:%S %Z", "%H:%M"],
+        "albanian" : ["%I:%M:%S %p, %Z", "%I:%M %p"],
+        "american" : ["%I:%M:%S %p %Z", "%I:%M %p"],
+        "amharic" : ["%I:%M:%S %p %Z", "%I:%M %p"],
+        "ancientgreek" : ["%H:%M:%S %Z", "%H:%M:%S"],
+        "arabic_arabi" : ["%I:%M:%S %p %Z", "%I:%M %p"],
+        "arabic_arabtex" : ["%I:%M:%S %p %Z", "%I:%M %p"],
+        "armenian" : ["%H:%M:%S %Z", "%H:%M"],
+        "asturian" : ["%H:%M:%S %Z", "%H:%M"],
+        "australian" : ["%I:%M:%S %p %Z", "%I:%M %p"],
+        "austrian" : ["%H:%M:%S %Z", "%H:%M"],
+        "bahasa" : ["%H.%M.%S %Z", "%H.%M"],
+        "bahasam" : ["%I:%M:%S %p %Z", "%I:%M %p"],
+        "basque" : ["%H:%M:%S (%Z)", "%H:%M"],
+        "belarusian" : ["%H:%M:%S, %Z", "%H:%M"],
+        "bosnian" : ["%H:%M:%S %Z", "%H:%M"],
+        "brazilian" : ["%H:%M:%S %Z", "%H:%M"],
+        "breton" : ["%H:%M:%S %Z", "%H:%M"],
+        "british" : ["%H:%M:%S %Z", "%H:%M"],
+        "bulgarian" : ["%H:%M:%S %Z", "%H:%M"],
+        "canadian" : ["%I:%M:%S %p %Z", "%I:%M %p"],
+        "canadien" : ["%H:%M:%S %Z", "%H h %M"],
+        "catalan" : ["%H:%M:%S %Z", "%H:%M"],
+        "chinese-simplified" : ["%Z %p%I:%M:%S", "%p%I:%M"],
+        "chinese-traditional" : ["%p%I:%M:%S [%Z]", "%p%I:%M"],
+        "coptic" : ["%H:%M:%S %Z", "%H:%M:%S"],
+        "croatian" : ["%H:%M:%S (%Z)", "%H:%M"],
+        "czech" : ["%H:%M:%S %Z", "%H:%M"],
+        "danish" : ["%H.%M.%S %Z", "%H.%M"],
+        "divehi" : ["%H:%M:%S %Z", "%H:%M"],
+        "dutch" : ["%H:%M:%S %Z", "%H:%M"],
+        "english" : ["%I:%M:%S %p %Z", "%I:%M %p"],
+        "esperanto" : ["%H:%M:%S %Z", "%H:%M:%S"],
+        "estonian" : ["%H:%M:%S %Z", "%H:%M"],
+        "farsi" : ["%H:%M:%S (%Z)", "%H:%M"],
+        "finnish" : ["%H.%M.%S %Z", "%H.%M"],
+        "french" : ["%H:%M:%S %Z", "%H:%M"],
+        "friulan" : ["%H:%M:%S %Z", "%H:%M"],
+        "galician" : ["%H:%M:%S %Z", "%H:%M"],
+        "georgian" : ["%H:%M:%S %Z", "%H:%M"],
+        "german" : ["%H:%M:%S %Z", "%H:%M"],
+        "german-ch" : ["%H:%M:%S %Z", "%H:%M"],
+        "german-ch-old" : ["%H:%M:%S %Z", "%H:%M"],
+        "greek" : ["%I:%M:%S %p %Z", "%I:%M %p"],
+        "hebrew" : ["%H:%M:%S %Z", "%H:%M"],
+        "hindi" : ["%I:%M:%S %p %Z", "%I:%M %p"],
+        "icelandic" : ["%H:%M:%S %Z", "%H:%M"],
+        "interlingua" : ["%H:%M:%S %Z", "%H:%M"],
+        "irish" : ["%H:%M:%S %Z", "%H:%M"],
+        "italian" : ["%H:%M:%S %Z", "%H:%M"],
+        "japanese" : ["%H時%M分%S秒 %Z", "%H:%M"],
+        "japanese-cjk" : ["%H時%M分%S秒 %Z", "%H:%M"],
+        "kannada" : ["%I:%M:%S %p %Z", "%I:%M %p"],
+        "kazakh" : ["%H:%M:%S %Z", "%H:%M"],
+        "khmer" : ["%I:%M:%S %p %Z", "%I:%M %p"],
+        "korean" : ["%p %I시%M분 %S초 %Z", "%p %I:%M"],
+        "kurmanji" : ["%H:%M:%S %Z", "%H:%M:%S"],
+        "lao" : ["%H ໂມງ%M ນາທີ  %S ວິນາທີ %Z", "%H:%M"],
+        "latin" : ["%H:%M:%S %Z", "%H:%M:%S"],
+        "latvian" : ["%H:%M:%S %Z", "%H:%M"],
+        "lithuanian" : ["%H:%M:%S %Z", "%H:%M"],
+        "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"],
+        "newzealand" : ["%I:%M:%S %p %Z", "%I:%M %p"],
+        "ngerman" : ["%H:%M:%S %Z", "%H:%M"],
+        "norsk" : ["%H:%M:%S %Z", "%H:%M"],
+        "nynorsk" : ["kl. %H:%M:%S %Z", "%H:%M"],
+        "occitan" : ["%H:%M:%S %Z", "%H:%M"],
+        "piedmontese" : ["%H:%M:%S %Z", "%H:%M:%S"],
+        "polish" : ["%H:%M:%S %Z", "%H:%M"],
+        "polutonikogreek" : ["%I:%M:%S %p %Z", "%I:%M %p"],
+        "portuguese" : ["%H:%M:%S %Z", "%H:%M"],
+        "romanian" : ["%H:%M:%S %Z", "%H:%M"],
+        "romansh" : ["%H:%M:%S %Z", "%H:%M"],
+        "russian" : ["%H:%M:%S %Z", "%H:%M"],
+        "samin" : ["%H:%M:%S %Z", "%H:%M"],
+        "sanskrit" : ["%H:%M:%S %Z", "%H:%M"],
+        "scottish" : ["%H:%M:%S %Z", "%H:%M"],
+        "serbian" : ["%H:%M:%S %Z", "%H:%M"],
+        "serbian-latin" : ["%H:%M:%S %Z", "%H:%M"],
+        "slovak" : ["%H:%M:%S %Z", "%H:%M"],
+        "slovene" : ["%H:%M:%S %Z", "%H:%M"],
+        "spanish" : ["%H:%M:%S (%Z)", "%H:%M"],
+        "spanish-mexico" : ["%H:%M:%S %Z", "%H:%M"],
+        "swedish" : ["kl. %H:%M:%S %Z", "%H:%M"],
+        "syriac" : ["%H:%M:%S %Z", "%H:%M"],
+        "tamil" : ["%p %I:%M:%S %Z", "%p %I:%M"],
+        "telugu" : ["%I:%M:%S %p %Z", "%I:%M %p"],
+        "thai" : ["%H นาฬิกา %M นาที  %S วินาที %Z", "%H:%M"],
+        "tibetan" : ["%I:%M:%S %p %Z", "%I:%M %p"],
+        "turkish" : ["%H:%M:%S %Z", "%H:%M"],
+        "turkmen" : ["%H:%M:%S %Z", "%H:%M"],
+        "ukrainian" : ["%H:%M:%S %Z", "%H:%M"],
+        "uppersorbian" : ["%H:%M:%S %Z", "%H:%M hodź."],
+        "urdu" : ["%I:%M:%S %p %Z", "%I:%M %p"],
+        "vietnamese" : ["%H:%M:%S %Z", "%H:%M"],
+        "welsh" : ["%H:%M:%S %Z", "%H:%M"]
+    }
+
+    types = ["time", "fixtime", "modtime" ]
+    i = 0
+    i = find_token(document.header, "\\language", 0)
+    if i == -1:
+        # this should not happen
+        document.warning("Malformed LyX document! No \\language header found!")
+        return
+    lang = get_value(document.header, "\\language", i)
+
+    i = 0
+    while True:
+        i = find_token(document.body, "\\begin_inset Info", i)
+        if i == -1:
+            return
+        j = find_end_of_inset(document.body, i + 1)
+        if j == -1:
+            document.warning("Malformed LyX document: Could not find end of Info inset.")
+            i = i + 1
+            continue
+        tp = find_token(document.body, 'type', i, j)
+        tpv = get_quoted_value(document.body, "type", tp)
+        if tpv not in types:
+            i = i + 1
+            continue
+        arg = find_token(document.body, 'arg', i, j)
+        argv = get_quoted_value(document.body, "arg", arg)
+        isotime = ""
+        dtme = datetime.now()
+        tme = dtme.time()
+        if tpv == "fixtime":
+            timecomps = argv.split('@')
+            if len(timecomps) > 1:
+                argv = timecomps[0]
+                isotime = timecomps[1]
+                m = re.search('(\d\d):(\d\d):(\d\d)', isotime)
+                if m:
+                    tme = time(int(m.group(1)), int(m.group(2)), int(m.group(3)))
+                else:
+                    m = re.search('(\d\d):(\d\d)', isotime)
+                    if m:
+                        tme = time(int(m.group(1)), int(m.group(2)))
+# FIXME if we had the path to the original document (not the one in the tmp dir),
+#        we could use the mtime.
+#        elif tpv == "moddate":
+#            dte = date.fromtimestamp(os.path.getmtime(document.dir))
+        result = ""
+        if argv == "ISO":
+            result = tme.isoformat()
+        elif argv == "long":
+            result = tme.strftime(timeformats[lang][0])
+        elif argv == "short":
+            result = tme.strftime(timeformats[lang][1])
+        else:
+            fmt = argv.replace("HH", "%H").replace("H", "%H").replace("hh", "%I").replace("h", "%I")
+            fmt = fmt.replace("mm", "%M").replace("m", "%M").replace("ss", "%S").replace("s", "%S")
+            fmt = fmt.replace("zzz", "%f").replace("z", "%f").replace("t", "%Z")
+            fmt = fmt.replace("AP", "%p").replace("ap", "%p").replace("A", "%p").replace("a", "%p")
+            fmt = fmt.replace("'", "")
+            result = dte.strftime(fmt)
         document.body[i : j+1] = result
         i = i + 1
 
 
+def revert_namenoextinfo(document):
+    " Merge buffer Info inset type name-noext to name. "
+
+    i = 0
+    while True:
+        i = find_token(document.body, "\\begin_inset Info", i)
+        if i == -1:
+            return
+        j = find_end_of_inset(document.body, i + 1)
+        if j == -1:
+            document.warning("Malformed LyX document: Could not find end of Info inset.")
+            i = i + 1
+            continue
+        tp = find_token(document.body, 'type', i, j)
+        tpv = get_quoted_value(document.body, "type", tp)
+        if tpv != "buffer":
+            i = i + 1
+            continue
+        arg = find_token(document.body, 'arg', i, j)
+        argv = get_quoted_value(document.body, "arg", arg)
+        if argv != "name-noext":
+            i = i + 1
+            continue
+        document.body[arg] = "arg \"name\""
+        i = i + 1
+
+
+def revert_l7ninfo(document):
+    " Revert l7n Info inset to text. "
+
+    i = 0
+    while True:
+        i = find_token(document.body, "\\begin_inset Info", i)
+        if i == -1:
+            return
+        j = find_end_of_inset(document.body, i + 1)
+        if j == -1:
+            document.warning("Malformed LyX document: Could not find end of Info inset.")
+            i = i + 1
+            continue
+        tp = find_token(document.body, 'type', i, j)
+        tpv = get_quoted_value(document.body, "type", tp)
+        if tpv != "l7n":
+            i = i + 1
+            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 " & "
+        argv = argv.rstrip(':').split('|')[0].replace(" & ", "</amp;>").replace("&", "").replace("</amp;>", " & ")
+        document.body[i : j+1] = argv
+        i = i + 1
+
+
+def revert_listpargs(document):
+    " Reverts listpreamble arguments to TeX-code "
+    i = 0
+    while True:
+        i = find_token(document.body, "\\begin_inset Argument listpreamble:", i)
+        if i == -1:
+            return
+        j = find_end_of_inset(document.body, i)
+        # Find containing paragraph layout
+        parent = get_containing_layout(document.body, i)
+        if parent == False:
+            document.warning("Malformed LyX document: Can't find parent paragraph layout")
+            i += 1
+            continue
+        parbeg = parent[3]
+        beginPlain = find_token(document.body, "\\begin_layout Plain Layout", i)
+        endPlain = find_end_of_layout(document.body, beginPlain)
+        content = document.body[beginPlain + 1 : endPlain]
+        del document.body[i:j+1]
+        subst = ["\\begin_inset ERT", "status collapsed", "", "\\begin_layout Plain Layout",
+                 "{"] + content + ["}", "\\end_layout", "", "\\end_inset", ""]
+        document.body[parbeg : parbeg] = subst
+        i += 1
+
+
+def revert_lformatinfo(document):
+    " Revert layout format Info inset to text. "
+
+    i = 0
+    while True:
+        i = find_token(document.body, "\\begin_inset Info", i)
+        if i == -1:
+            return
+        j = find_end_of_inset(document.body, i + 1)
+        if j == -1:
+            document.warning("Malformed LyX document: Could not find end of Info inset.")
+            i = i + 1
+            continue
+        tp = find_token(document.body, 'type', i, j)
+        tpv = get_quoted_value(document.body, "type", tp)
+        if tpv != "lyxinfo":
+            i = i + 1
+            continue
+        arg = find_token(document.body, 'arg', i, j)
+        argv = get_quoted_value(document.body, "arg", arg)
+        if argv != "layoutformat":
+            i = i + 1
+            continue
+        # hardcoded for now
+        document.body[i : j+1] = "69"
+        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
 #
@@ -866,10 +1433,26 @@ convert = [
            [556, []],
            [557, [convert_vcsinfo]],
            [558, [removeFrontMatterStyles]],
-           [559, []]
+           [559, []],
+           [560, []],
+           [561, [convert_latexFonts]], # Handle dejavu, ibmplex fonts in GUI
+           [562, []],
+           [563, []],
+           [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]],
+           [560, [revert_latexFonts]], # Handle dejavu, ibmplex fonts in user preamble
+           [559, [revert_timeinfo, revert_namenoextinfo]],
            [558, [revert_dateinfo]],
            [557, [addFrontMatterStyles]],
            [556, [revert_vcsinfo]],