]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx_1_5.py
* doc/cs_Tutorial.lyx: update by Pavel Sanda
[lyx.git] / lib / lyx2lyx / lyx_1_5.py
index 365f6a17dcb12fff51fb8300d26209eacb2d09b2..d4f00cfe3a30df2d593a5e3939db08437562ca61 100644 (file)
@@ -244,7 +244,7 @@ def revert_cs_label(document):
         while 1:
             if (document.body[i][:10] == "show_label"):
                 del document.body[i]
-               break
+                break
             elif (document.body[i][:13] == "\\begin_layout"):
                 document.warning("Malformed LyX document: Missing 'show_label'.")
                 break
@@ -253,6 +253,357 @@ def revert_cs_label(document):
         i = i + 1
 
 
+def convert_bibitem(document):
+    """ Convert
+\bibitem [option]{argument}
+
+to
+
+\begin_inset LatexCommand bibitem
+label "option"
+key "argument"
+
+\end_inset
+
+This must be called after convert_commandparams.
+"""
+    regex = re.compile(r'\S+\s*(\[[^\[\{]*\])?(\{[^}]*\})')
+    i = 0
+    while 1:
+        i = find_token(document.body, "\\bibitem", i)
+        if i == -1:
+            break
+        match = re.match(regex, document.body[i])
+        option = match.group(1)
+        argument = match.group(2)
+        lines = ['\\begin_inset LatexCommand bibitem']
+        if option != None:
+            lines.append('label "%s"' % option[1:-1].replace('"', '\\"'))
+        lines.append('key "%s"' % argument[1:-1].replace('"', '\\"'))
+        lines.append('')
+        lines.append('\\end_inset')
+        document.body[i:i+1] = lines
+        i = i + 1
+
+
+commandparams_info = {
+    # command : [option1, option2, argument]
+    "bibitem" : ["label", "", "key"],
+    "bibtex" : ["options", "btprint", "bibfiles"],
+    "cite"        : ["after", "before", "key"],
+    "citet"       : ["after", "before", "key"],
+    "citep"       : ["after", "before", "key"],
+    "citealt"     : ["after", "before", "key"],
+    "citealp"     : ["after", "before", "key"],
+    "citeauthor"  : ["after", "before", "key"],
+    "citeyear"    : ["after", "before", "key"],
+    "citeyearpar" : ["after", "before", "key"],
+    "citet*"      : ["after", "before", "key"],
+    "citep*"      : ["after", "before", "key"],
+    "citealt*"    : ["after", "before", "key"],
+    "citealp*"    : ["after", "before", "key"],
+    "citeauthor*" : ["after", "before", "key"],
+    "Citet"       : ["after", "before", "key"],
+    "Citep"       : ["after", "before", "key"],
+    "Citealt"     : ["after", "before", "key"],
+    "Citealp"     : ["after", "before", "key"],
+    "Citeauthor"  : ["after", "before", "key"],
+    "Citet*"      : ["after", "before", "key"],
+    "Citep*"      : ["after", "before", "key"],
+    "Citealt*"    : ["after", "before", "key"],
+    "Citealp*"    : ["after", "before", "key"],
+    "Citeauthor*" : ["after", "before", "key"],
+    "citefield"   : ["after", "before", "key"],
+    "citetitle"   : ["after", "before", "key"],
+    "cite*"       : ["after", "before", "key"],
+    "hfill" : ["", "", ""],
+    "index"      : ["", "", "name"],
+    "printindex" : ["", "", "name"],
+    "label" : ["", "", "name"],
+    "eqref"     : ["name", "", "reference"],
+    "pageref"   : ["name", "", "reference"],
+    "prettyref" : ["name", "", "reference"],
+    "ref"       : ["name", "", "reference"],
+    "vpageref"  : ["name", "", "reference"],
+    "vref"      : ["name", "", "reference"],
+    "tableofcontents" : ["", "", "type"],
+    "htmlurl" : ["name", "", "target"],
+    "url"     : ["name", "", "target"]}
+
+
+def convert_commandparams(document):
+    """ Convert
+
+ \begin_inset LatexCommand \cmdname[opt1][opt2]{arg}
+ \end_inset
+
+ to
+
+ \begin_inset LatexCommand cmdname
+ name1 "opt1"
+ name2 "opt2"
+ name3 "arg"
+ \end_inset
+
+ name1, name2 and name3 can be different for each command.
+"""
+    # \begin_inset LatexCommand bibitem was not the official version (see
+    # convert_bibitem()), but could be read in, so we convert it here, too.
+
+    i = 0
+    while 1:
+        i = find_token(document.body, "\\begin_inset LatexCommand", i)
+        if i == -1:
+            break
+        command = document.body[i][26:].strip()
+        if command == "":
+            document.warning("Malformed LyX document: Missing LatexCommand name.")
+            i = i + 1
+            continue
+
+        # The following parser is taken from the original InsetCommandParams::scanCommand
+        name = ""
+        option1 = ""
+        option2 = ""
+        argument = ""
+        state = "WS"
+        # Used to handle things like \command[foo[bar]]{foo{bar}}
+        nestdepth = 0
+        b = 0
+        for c in command:
+            if ((state == "CMDNAME" and c == ' ') or
+                (state == "CMDNAME" and c == '[') or
+                (state == "CMDNAME" and c == '{')):
+                state = "WS"
+            if ((state == "OPTION" and c == ']') or
+                (state == "SECOPTION" and c == ']') or
+                (state == "CONTENT" and c == '}')):
+                if nestdepth == 0:
+                    state = "WS"
+                else:
+                    nestdepth = nestdepth - 1
+            if ((state == "OPTION" and c == '[') or
+                (state == "SECOPTION" and c == '[') or
+                (state == "CONTENT" and c == '{')):
+                nestdepth = nestdepth + 1
+            if state == "CMDNAME":
+                    name += c
+            elif state == "OPTION":
+                    option1 += c
+            elif state == "SECOPTION":
+                    option2 += c
+            elif state == "CONTENT":
+                    argument += c
+            elif state == "WS":
+                if c == '\\':
+                    state = "CMDNAME"
+                elif c == '[' and b != ']':
+                    state = "OPTION"
+                    nestdepth = 0 # Just to be sure
+                elif c == '[' and b == ']':
+                    state = "SECOPTION"
+                    nestdepth = 0 # Just to be sure
+                elif c == '{':
+                    state = "CONTENT"
+                    nestdepth = 0 # Just to be sure
+            b = c
+
+        # Now we have parsed the command, output the parameters
+        lines = ["\\begin_inset LatexCommand %s" % name]
+        if option1 != "":
+            if commandparams_info[name][0] == "":
+                document.warning("Ignoring invalid option `%s' of command `%s'." % (option1, name))
+            else:
+                lines.append('%s "%s"' % (commandparams_info[name][0], option1.replace('"', '\\"')))
+        if option2 != "":
+            if commandparams_info[name][1] == "":
+                document.warning("Ignoring invalid second option `%s' of command `%s'." % (option2, name))
+            else:
+                lines.append('%s "%s"' % (commandparams_info[name][1], option2.replace('"', '\\"')))
+        if argument != "":
+            if commandparams_info[name][2] == "":
+                document.warning("Ignoring invalid argument `%s' of command `%s'." % (argument, name))
+            else:
+                lines.append('%s "%s"' % (commandparams_info[name][2], argument.replace('"', '\\"')))
+        document.body[i:i+1] = lines
+        i = i + 1
+
+
+def revert_commandparams(document):
+    regex = re.compile(r'(\S+)\s+(.+)')
+    i = 0
+    while 1:
+        i = find_token(document.body, "\\begin_inset LatexCommand", i)
+        if i == -1:
+            break
+        name = document.body[i].split()[2]
+        j = find_end_of_inset(document.body, i + 1)
+        preview_line = ""
+        option1 = ""
+        option2 = ""
+        argument = ""
+        for k in range(i + 1, j):
+            match = re.match(regex, document.body[k])
+            if match:
+                pname = match.group(1)
+                pvalue = match.group(2)
+                if pname == "preview":
+                    preview_line = document.body[k]
+                elif (commandparams_info[name][0] != "" and
+                      pname == commandparams_info[name][0]):
+                    option1 = pvalue.strip('"').replace('\\"', '"')
+                elif (commandparams_info[name][1] != "" and
+                      pname == commandparams_info[name][1]):
+                    option2 = pvalue.strip('"').replace('\\"', '"')
+                elif (commandparams_info[name][2] != "" and
+                      pname == commandparams_info[name][2]):
+                    argument = pvalue.strip('"').replace('\\"', '"')
+            elif document.body[k].strip() != "":
+                document.warning("Ignoring unknown contents `%s' in command inset %s." % (document.body[k], name))
+        if name == "bibitem":
+            if option1 == "":
+                lines = ["\\bibitem {%s}" % argument]
+            else:
+                lines = ["\\bibitem [%s]{%s}" % (option1, argument)]
+        else:
+            if option1 == "":
+                if option2 == "":
+                    lines = ["\\begin_inset LatexCommand \\%s{%s}" % (name, argument)]
+                else:
+                    lines = ["\\begin_inset LatexCommand \\%s[][%s]{%s}" % (name, option2, argument)]
+            else:
+                if option2 == "":
+                    lines = ["\\begin_inset LatexCommand \\%s[%s]{%s}" % (name, option1, argument)]
+                else:
+                    lines = ["\\begin_inset LatexCommand \\%s[%s][%s]{%s}" % (name, option1, option2, argument)]
+        if name != "bibitem":
+            if preview_line != "":
+                lines.append(preview_line)
+            lines.append('')
+            lines.append('\\end_inset')
+        document.body[i:j+1] = lines
+        i = j + 1
+
+
+def revert_nomenclature(document):
+    " Convert nomenclature entry to ERT. "
+    regex = re.compile(r'(\S+)\s+(.+)')
+    i = 0
+    use_nomencl = 0
+    while 1:
+        i = find_token(document.body, "\\begin_inset LatexCommand nomenclature", i)
+        if i == -1:
+            break
+        use_nomencl = 1
+        j = find_end_of_inset(document.body, i + 1)
+        preview_line = ""
+        symbol = ""
+        description = ""
+        prefix = ""
+        for k in range(i + 1, j):
+            match = re.match(regex, document.body[k])
+            if match:
+                name = match.group(1)
+                value = match.group(2)
+                if name == "preview":
+                    preview_line = document.body[k]
+                elif name == "symbol":
+                    symbol = value.strip('"').replace('\\"', '"')
+                elif name == "description":
+                    description = value.strip('"').replace('\\"', '"')
+                elif name == "prefix":
+                    prefix = value.strip('"').replace('\\"', '"')
+            elif document.body[k].strip() != "":
+                document.warning("Ignoring unknown contents `%s' in nomenclature inset." % document.body[k])
+        if prefix == "":
+            command = 'nomenclature{%s}{%s}' % (symbol, description)
+        else:
+            command = 'nomenclature[%s]{%s}{%s}' % (prefix, symbol, description)
+        document.body[i:j+1] = ['\\begin_inset ERT',
+                                'status collapsed',
+                                '',
+                                '\\begin_layout %s' % document.default_layout,
+                                '',
+                                '',
+                                '\\backslash',
+                                command,
+                                '\\end_layout',
+                                '',
+                                '\\end_inset']
+        i = i + 11
+    if use_nomencl and find_token(document.preamble, '\\usepackage{nomencl}[2005/09/22]', 0) == -1:
+        document.preamble.append('\\usepackage{nomencl}[2005/09/22]')
+        document.preamble.append('\\makenomenclature')
+
+
+def revert_printnomenclature(document):
+    " Convert printnomenclature to ERT. "
+    regex = re.compile(r'(\S+)\s+(.+)')
+    i = 0
+    use_nomencl = 0
+    while 1:
+        i = find_token(document.body, "\\begin_inset LatexCommand printnomenclature", i)
+        if i == -1:
+            break
+        use_nomencl = 1
+        j = find_end_of_inset(document.body, i + 1)
+        preview_line = ""
+        labelwidth = ""
+        for k in range(i + 1, j):
+            match = re.match(regex, document.body[k])
+            if match:
+                name = match.group(1)
+                value = match.group(2)
+                if name == "preview":
+                    preview_line = document.body[k]
+                elif name == "labelwidth":
+                    labelwidth = value.strip('"').replace('\\"', '"')
+            elif document.body[k].strip() != "":
+                document.warning("Ignoring unknown contents `%s' in printnomenclature inset." % document.body[k])
+        if labelwidth == "":
+            command = 'nomenclature{}'
+        else:
+            command = 'nomenclature[%s]' % labelwidth
+        document.body[i:j+1] = ['\\begin_inset ERT',
+                                'status collapsed',
+                                '',
+                                '\\begin_layout %s' % document.default_layout,
+                                '',
+                                '',
+                                '\\backslash',
+                                command,
+                                '\\end_layout',
+                                '',
+                                '\\end_inset']
+        i = i + 11
+    if use_nomencl and find_token(document.preamble, '\\usepackage{nomencl}[2005/09/22]', 0) == -1:
+        document.preamble.append('\\usepackage{nomencl}[2005/09/22]')
+        document.preamble.append('\\makenomenclature')
+
+
+def convert_esint(document):
+    " Add \\use_esint setting to header. "
+    i = find_token(document.header, "\\cite_engine", 0)
+    if i == -1:
+        document.warning("Malformed LyX document: Missing `\\cite_engine'.")
+        return
+    # 0 is off, 1 is auto, 2 is on.
+    document.header.insert(i, '\\use_esint 0')
+
+
+def revert_esint(document):
+    " Remove \\use_esint setting from header. "
+    i = find_token(document.header, "\\use_esint", 0)
+    if i == -1:
+        document.warning("Malformed LyX document: Missing `\\use_esint'.")
+        return
+    use_esint = document.header[i].split()[1]
+    del document.header[i]
+    # 0 is off, 1 is auto, 2 is on.
+    if (use_esint == 2):
+        document.preamble.append('\\usepackage{esint}')
+
+
 ##
 # Conversion hub
 #
@@ -263,9 +614,15 @@ convert = [[246, []],
            [248, []],
            [249, [convert_utf8]],
            [250, []],
-           [251, []]]
-
-revert =  [[250, [revert_cs_label]],
+           [251, []],
+           [252, [convert_commandparams, convert_bibitem]],
+           [253, []],
+           [254, [convert_esint]]]
+
+revert =  [[253, [revert_esint]],
+           [252, [revert_nomenclature, revert_printnomenclature]],
+           [251, [revert_commandparams]],
+           [250, [revert_cs_label]],
            [249, []],
            [248, [revert_utf8]],
            [247, [revert_booktabs]],