]> 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 4d8e3da9774113506691b190967965df0d96b49c..cf68d70a3470f8e0d645a8f37baa2465e2ec3d9f 100644 (file)
@@ -27,15 +27,16 @@ 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,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
 
@@ -921,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"],
@@ -1050,6 +1310,109 @@ def revert_namenoextinfo(document):
         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
 #
@@ -1071,10 +1434,24 @@ convert = [
            [557, [convert_vcsinfo]],
            [558, [removeFrontMatterStyles]],
            [559, []],
-           [560, []]
+           [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]],