Example #1
0
def revert_smash(document):
    " Set amsmath to on if smash commands are used "

    commands = ["smash[t]", "smash[b]", "notag"]
    i = find_token(document.header, "\\use_package amsmath", 0)
    if i == -1:
        document.warning(
            "Malformed LyX document: Can't find \\use_package amsmath.")
        return
    value = get_value(document.header, "\\use_package amsmath", i).split()[1]
    if value != "1":
        # nothing to do if package is not auto but on or off
        return
    j = 0
    while True:
        j = find_token(document.body, '\\begin_inset Formula', j)
        if j == -1:
            return
        k = find_end_of_inset(document.body, j)
        if k == -1:
            document.warning(
                "Malformed LyX document: Can't find end of Formula inset at line "
                + str(j))
            j += 1
            continue
        code = "\n".join(document.body[j:k])
        for c in commands:
            if code.find("\\%s" % c) != -1:
                # set amsmath to on, since it is loaded by the newer format
                document.header[i] = "\\use_package amsmath 2"
                return
        j = k
Example #2
0
File: LyX.py Project: cburschka/lyx
 def get_module_list(self):
   " Return list of modules."
   i = find_token(self.header, "\\begin_modules", 0)
   if (i == -1):
     return []
   j = find_token(self.header, "\\end_modules", i)
   return self.header[i + 1 : j]
def revert_layout_command(lines, name, LaTeXname):
    " Reverts a command from a layout to TeX code "
    i = 0
    while True:
        i = find_token(lines, '\\begin_layout ' + name, i)
        if i == -1:
            return
        k = -1
        # find the next layout
        j = i + 1
        while k == -1:
            j = find_token(lines, '\\begin_layout', j)
            l = len(lines)
            # if nothing was found it was the last layout of the document
            if j == -1:
                lines[l - 4:l - 4] = put_cmd_in_ert("}")
                k = 0
            # exclude plain layout because this can be TeX code or another inset
            elif lines[j] != '\\begin_layout Plain Layout':
                lines[j - 2:j - 2] = put_cmd_in_ert("}")
                k = 0
            else:
                j += 1
        lines[i] = '\\begin_layout Standard'
        lines[i + 1:i + 1] = put_cmd_in_ert(LaTeXname + "{")
        i += 1
Example #4
0
def revert_smash(document):
    " Set amsmath to on if smash commands are used "

    commands = ["smash[t]", "smash[b]", "notag"]
    i = find_token(document.header, "\\use_package amsmath", 0)
    if i == -1:
        document.warning("Malformed LyX document: Can't find \\use_package amsmath.")
        return;
    value = get_value(document.header, "\\use_package amsmath", i).split()[1]
    if value != "1":
        # nothing to do if package is not auto but on or off
        return;
    j = 0
    while True:
        j = find_token(document.body, '\\begin_inset Formula', j)
        if j == -1:
            return
        k = find_end_of_inset(document.body, j)
        if k == -1:
            document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(j))
            j += 1
            continue
        code = "\n".join(document.body[j:k])
        for c in commands:
            if code.find("\\%s" % c) != -1:
                # set amsmath to on, since it is loaded by the newer format
                document.header[i] = "\\use_package amsmath 2"
                return
        j = k
Example #5
0
def revert_font_attrs(lines, name, LaTeXname):
  " Reverts font changes to TeX code "
  i = 0
  changed = False
  while True:
    i = find_token(lines, name + ' on', i)
    if i == -1:
      break
    j = find_token(lines, name + ' default', i)
    k = find_token(lines, name + ' on', i + 1)
    # if there is no default set, the style ends with the layout
    # assure hereby that we found the correct layout end
    if j != -1 and (j < k or k == -1):
      lines[j:j + 1] = put_cmd_in_ert("}")
    else:
      j = find_token(lines, '\\end_layout', i)
      lines[j:j] = put_cmd_in_ert("}")
    lines[i:i + 1] = put_cmd_in_ert(LaTeXname + "{")
    changed = True
    i += 1

  # now delete all remaining lines that manipulate this attribute
  i = 0
  while True:
    i = find_token(lines, name, i)
    if i == -1:
      break
    del lines[i]

  return changed
Example #6
0
def change_infoinset(document):
    " Change info inset."
    lines = document.body
    i = 0
    while 1:
        i = find_token(lines, "\\begin_inset Info", i)
        if i == -1:
            break
        txt = lines[i][18:].lstrip()
        new = ["\\begin_inset Note", "collapsed true", ""]
        j = find_token(lines, "\\end_inset", i)
        if j == -1:
            break

        note_lines = lines[i+1:j]
        if len(txt) > 0:
            note_lines = [txt]+note_lines

        for line in note_lines:
            new = new + ['\layout %s' % document.default_layout, ""]
            tmp = line.split('\\')
            new = new + [tmp[0]]
            for x in tmp[1:]:
                new = new + ["\\backslash ", x]
        lines[i:j] = new
        i = i+5
Example #7
0
def change_infoinset(document):
    " Change info inset."
    lines = document.body
    i = 0
    while 1:
        i = find_token(lines, "\\begin_inset Info", i)
        if i == -1:
            break
        txt = lines[i][18:].lstrip()
        new = ["\\begin_inset Note", "collapsed true", ""]
        j = find_token(lines, "\\end_inset", i)
        if j == -1:
            break

        note_lines = lines[i + 1:j]
        if len(txt) > 0:
            note_lines = [txt] + note_lines

        for line in note_lines:
            new = new + ['\layout %s' % document.default_layout, ""]
            tmp = line.split('\\')
            new = new + [tmp[0]]
            for x in tmp[1:]:
                new = new + ["\\backslash ", x]
        lines[i:j] = new
        i = i + 5
Example #8
0
def combine_ert(document):
    " Combine ERT paragraphs."
    lines = document.body
    i = 0
    while 1:
        i = find_token(lines, "\\begin_inset ERT", i)
        if i == -1:
            break
        j = get_paragraph(lines, i, document.format + 1)
        count = 0
        text = []
        while is_ert_paragraph(document, j):

            count = count + 1
            i2 = find_token(lines, "\\layout", j + 1)
            k = find_token(lines, "\\end_inset", i2 + 1)
            text = text + lines[i2:k]
            j = find_token(lines, "\\layout", k + 1)
            if j == -1:
                break

        if count >= 2:
            j = find_token(lines, "\\layout", i + 1)
            lines[j:k] = text

        i = i + 1
Example #9
0
def revert_layout_command(lines, name, LaTeXname):
  " Reverts a command from a layout to TeX code "
  i = 0
  while True:
    i = find_token(lines, '\\begin_layout ' + name, i)
    if i == -1:
      return
    k = -1
    # find the next layout
    j = i + 1
    while k == -1:
      j = find_token(lines, '\\begin_layout', j)
      l = len(lines)
      # if nothing was found it was the last layout of the document
      if j == -1:
        lines[l - 4:l - 4] = put_cmd_in_ert("}")
        k = 0
      # exclude plain layout because this can be TeX code or another inset
      elif lines[j] != '\\begin_layout Plain Layout':
        lines[j - 2:j - 2] = put_cmd_in_ert("}")
        k = 0
      else:
        j += 1
    lines[i] = '\\begin_layout Standard'
    lines[i + 1:i + 1] = put_cmd_in_ert(LaTeXname + "{")
    i += 1
def revert_font_attrs(lines, name, LaTeXname):
    " Reverts font changes to TeX code "
    i = 0
    changed = False
    while True:
        i = find_token(lines, name + ' on', i)
        if i == -1:
            break
        j = find_token(lines, name + ' default', i)
        k = find_token(lines, name + ' on', i + 1)
        # if there is no default set, the style ends with the layout
        # assure hereby that we found the correct layout end
        if j != -1 and (j < k or k == -1):
            lines[j:j + 1] = put_cmd_in_ert("}")
        else:
            j = find_token(lines, '\\end_layout', i)
            lines[j:j] = put_cmd_in_ert("}")
        lines[i:i + 1] = put_cmd_in_ert(LaTeXname + "{")
        changed = True
        i += 1

    # now delete all remaining lines that manipulate this attribute
    i = 0
    while True:
        i = find_token(lines, name, i)
        if i == -1:
            break
        del lines[i]

    return changed
Example #11
0
def combine_ert(document):
    " Combine ERT paragraphs."
    lines = document.body
    i = 0
    while 1:
        i = find_token(lines, "\\begin_inset ERT", i)
        if i == -1:
            break
        j = get_paragraph(lines, i, document.format + 1)
        count = 0
        text = []
        while is_ert_paragraph(document, j):

            count = count+1
            i2 = find_token(lines, "\\layout", j+1)
            k = find_token(lines, "\\end_inset", i2+1)
            text = text+lines[i2:k]
            j = find_token(lines, "\\layout", k+1)
            if j == -1:
                break

        if count >= 2:
            j = find_token(lines, "\\layout", i+1)
            lines[j:k] = text

        i = i+1
Example #12
0
 def get_module_list(self):
     " Return list of modules."
     i = find_token(self.header, "\\begin_modules", 0)
     if (i == -1):
         return []
     j = find_token(self.header, "\\end_modules", i)
     return self.header[i + 1:j]
Example #13
0
    def append_local_layout(self, new_layout):
        " Append `new_layout` to the local layouts."
        # new_layout may be a string or a list of strings (lines)
        try:
            new_layout = new_layout.splitlines()
        except AttributeError:
            pass
        i = find_token(self.header, "\\begin_local_layout", 0)
        if i == -1:
            k = find_token(self.header, "\\language", 0)
            if k == -1:
                # this should not happen
                self.warning(
                    "Malformed LyX document! No \\language header found!")
                return
            self.header[k:k] = ["\\begin_local_layout", "\\end_local_layout"]
            i = k

        j = find_end_of(self.header, i, "\\begin_local_layout",
                        "\\end_local_layout")
        if j == -1:
            # this should not happen
            self.warning(
                "Malformed LyX document: Can't find end of local layout!")
            return

        self.header[i + 1:i + 1] = new_layout
Example #14
0
def convert_beamer_article_styles(document):
    " Remove included (scr)article styles in beamer article "

    beamer_articles = ["article-beamer", "scrarticle-beamer"]
    if document.textclass not in beamer_articles:
        return

    while True:
        i = find_token(document.header, "\\begin_local_layout", 0)
        if i == -1:
            return

        j = find_end_of(document.header, i, "\\begin_local_layout",
                        "\\end_local_layout")
        if j == -1:
            # this should not happen
            break

        k = find_token(
            document.header,
            "### Inserted by lyx2lyx (more [scr]article styles) ###", i, j)
        if k != -1:
            l = find_token(
                document.header,
                "### End of insertion by lyx2lyx (more [scr]article styles) ###",
                i, j)
            if l == -1:
                # this should not happen
                document.warning(
                    "End of lyx2lyx local layout insertion not found!")
                break

            document.header[k:l + 1] = []

        return
Example #15
0
def remove_vcid(document):
    " Remove \\lyxvcid and \\lyxrcsid. "
    lines = document.header
    i = find_token(lines, '\\lyxvcid', 0)
    if i != -1:
        del lines[i]
    i = find_token(lines, '\\lyxrcsid', 0)
    if i != -1:
        del lines[i]
Example #16
0
def remove_vcid(document):
    " Remove \\lyxvcid and \\lyxrcsid. "
    lines = document.header
    i = find_token(lines, '\\lyxvcid', 0)
    if i != -1:
        del lines[i]
    i = find_token(lines, '\\lyxrcsid', 0)
    if i != -1:
        del lines[i]
Example #17
0
def fix_oldfloatinset(document):
    " Figure insert are hidden feature of lyx 1.1.6. This might be removed in the future."
    lines = document.body
    i = 0
    while 1:
        i = find_token(lines, "\\begin_inset Float ", i)
        if i == -1:
            break
        j = find_token(lines, "collapsed", i)
        if j != -1:
            lines[j:j] = ["wide false"]
        i = i+1
Example #18
0
def fix_oldfloatinset(document):
    " Figure insert are hidden feature of lyx 1.1.6. This might be removed in the future."
    lines = document.body
    i = 0
    while 1:
        i = find_token(lines, "\\begin_inset Float ", i)
        if i == -1:
            break
        j = find_token(lines, "collapsed", i)
        if j != -1:
            lines[j:j] = ["wide false"]
        i = i + 1
def insert_document_option(document, option):
    "Insert _option_ as a document option."

    # Find \options in the header
    i = find_token(document.header, "\\options", 0)
    # if the options does not exists add it after the textclass
    if i == -1:
        i = find_token(document.header, "\\textclass", 0) + 1
        document.header.insert(i, r"\options %s" % option)
        return
    # otherwise append to options
    if not is_document_option(document, option):
        document.header[i] += ",%s" % option
Example #20
0
def convert_microtype(document):
    " Add microtype settings. "
    i = find_token(document.header, "\\font_tt_scale", 0)
    if i == -1:
        document.warning("Malformed LyX document: Can't find \\font_tt_scale.")
        i = len(document.header) - 1

    j = find_token(document.preamble, "\\usepackage{microtype}", 0)
    if j == -1:
        document.header.insert(i + 1, "\\use_microtype false")
    else:
        document.header.insert(i + 1, "\\use_microtype true")
        del document.preamble[j]
Example #21
0
def remove_formula_latex(document):
    " Remove formula latex."
    lines = document.body
    i = 0
    while True:
        i = find_token(lines, '\\latex formula_latex ', i)
        if i == -1:
            break
        del lines[i]

        i = find_token(lines, '\\latex default', i)
        if i == -1:
            break
        del lines[i]
Example #22
0
def remove_formula_latex(document):
    " Remove formula latex."
    lines = document.body
    i = 0
    while 1:
        i = find_token(lines, '\\latex formula_latex ', i)
        if i == -1:
            break
        del lines[i]

        i = find_token(lines, '\\latex default', i)
        if i == -1:
            break
        del lines[i]
Example #23
0
def revert_beamer_article_styles(document):
    " Include (scr)article styles in beamer article "

    beamer_articles = ["article-beamer", "scrarticle-beamer"]
    if document.textclass not in beamer_articles:
        return

    inclusion = "article.layout"
    if document.textclass == "scrarticle-beamer":
        inclusion = "scrartcl.layout"

    while True:
        i = find_token(document.header, "\\begin_local_layout", 0)
        if i == -1:
            k = find_token(document.header, "\\language", 0)
            if k == -1:
                # this should not happen
                document.warning(
                    "Malformed LyX document! No \\language header found!")
                break
            document.header[k - 1:k - 1] = [
                "\\begin_local_layout", "\\end_local_layout"
            ]
            i = find_token(document.header, "\\begin_local_layout", 0)
        if i != -1:
            j = find_end_of(document.header, i, "\\begin_local_layout",
                            "\\end_local_layout")
            if j == -1:
                # this should not happen
                break

            document.header[i + 1:i + 1] = [
                "### Inserted by lyx2lyx (more [scr]article styles) ###",
                "Input " + inclusion, "Input beamer.layout",
                "Provides geometry 0", "Provides hyperref 0", "DefaultFont",
                "	Family                Roman",
                "	Series                Medium", "	Shape                 Up",
                "	Size                  Normal", "	Color                 None",
                "EndFont", "Preamble", "	\\usepackage{beamerarticle,pgf}",
                "	% this default might be overridden by plain title style",
                "	\\newcommand\makebeamertitle{\\frame{\\maketitle}}%",
                "	\\AtBeginDocument{",
                "		\\let\\origtableofcontents=\\tableofcontents",
                "		\\def\\tableofcontents{\\@ifnextchar[{\\origtableofcontents}{\\gobbletableofcontents}}",
                "		\\def\\gobbletableofcontents#1{\\origtableofcontents}",
                "	}", "EndPreamble",
                "### End of insertion by lyx2lyx (more [scr]article styles) ###"
            ]
        return
Example #24
0
def insert_document_option(document, option):
    "Insert _option_ as a document option."

    # Find \options in the header
    options_line = find_token(document.header, "\\options", 0)

    # if the options does not exists add it after the textclass
    if options_line == -1:
        textclass_line = find_token(document.header, "\\textclass", 0)
        document.header.insert(textclass_line +1,
                               r"\options %s" % option)
        return

    # add it to the end of the options
    document.header[options_line] += ",%s" % option
Example #25
0
def revert_swissgerman(document):
    " Set language german-ch-old to german "
    i = 0
    if document.language == "german-ch-old":
        document.language = "german"
        i = find_token(document.header, "\\language", 0)
        if i != -1:
            document.header[i] = "\\language german"
    j = 0
    while True:
        j = find_token(document.body, "\\lang german-ch-old", j)
        if j == -1:
            return
        document.body[j] = document.body[j].replace("\\lang german-ch-old", "\\lang german")
        j = j + 1
Example #26
0
def update_latexaccents(document):
    " Update latex accent insets."
    body = document.body
    i = 1
    while True:
        i = find_token(body, '\\i ', i)
        if i == -1:
            return

        contents = body[i][2:].strip()

        if contents.find('{') != -1 and contents.find('}') != -1:
            i = i + 1
            continue

        if len(contents) == 2:
            contents = contents + '{}'
        elif len(contents) == 3:
            contents = contents[:2] + '{' + contents[2] + '}'
        elif len(contents) == 4:
            if contents[2] == ' ':
                contents = contents[:2] + '{' + contents[3] + '}'
            elif contents[2:4] == '\\i' or contents[2:4] == '\\j':
                contents = contents[:2] + '{' + contents[2:] + '}'

        body[i] = '\\i ' + contents
        i = i + 1
Example #27
0
def update_tabular(document):
    " Update tabular format to version 2 (xml like syntax)."
    regexp = re.compile(r'^\\begin_inset\s+Tabular')
    lines = document.body
    i=0
    while True:
        i = find_re(lines, regexp, i)
        if i == -1:
            break

        i = i +1

        # scan table header meta-info
        res = table_meta_re.match( lines[i] )
        if res:
            val = res.groups()
            lines[i] = '<lyxtabular version="2" rows="%s" columns="%s">' % val

        j = find_token(lines, '</LyXTabular>', i) + 1
        if j == 0:
            document.warning( "Error: Bad lyx format i=%d j=%d" % (i,j))
            break

        new_table = table_update(lines[i:j])
        lines[i:j] = new_table
        i = i + len(new_table)
Example #28
0
def remove_inset_latex(document):
    "Replace inset latex with layout LaTeX"
    body = document.body

    i = 0
    while True:
        i = find_token(body, '\\begin_inset Latex', i)
        if i == -1:
            return

        body[i] = body[i].replace('\\begin_inset Latex', '\\layout LaTeX')
        i = find_token(body, '\\end_inset', i)
        if i == -1:
            #this should not happen
            return
        del body[i]
Example #29
0
def get_length(lines, name, start, end):
    " Get lenght."
    i = find_token(lines, name, start, end)
    if i == -1:
        return ""
    x = lines[i].split()
    return x[2]+oldunits[int(x[1])]
Example #30
0
def remove_document_option(document, option):
    """ Remove _option_ as a document option.

    It is assumed that option belongs to the \options.
    That can be done running is_document_option(document, option)."""

    options_line = find_token(document.header, "\\options", 0)
    option_pos = document.header[options_line].find(option)

    # Remove option from \options
    comma_before_pos = document.header[options_line].rfind(',', 0, option_pos)
    comma_after_pos  = document.header[options_line].find(',', option_pos)

    # if there are no commas then it is the single option
    # and the options line should be removed since it will be empty
    if comma_before_pos == comma_after_pos == -1:
        del document.header[options_line]
        return

    # last option
    options = document.header[options_line]
    if comma_after_pos == -1:
        document.header[options_line] = options[:comma_before_pos].rsplit()
        return

    document.header[options_line] = options[comma_before_pos: comma_after_pos]
Example #31
0
def remove_oldertinset(document):
    " ERT insert are hidden feature of lyx 1.1.6. This might be removed in the future."
    lines = document.body
    i = 0
    while 1:
        i = find_token(lines, "\\begin_inset ERT", i)
        if i == -1:
            break
        j = find_end_of_inset(lines, i)
        k = find_token(lines, "\\layout", i+1)
        l = get_paragraph(lines, i, document.format + 1)
        if lines[k] == lines[l]: # same layout
            k = k+1
        new = lines[k:j]
        lines[i:j+1] = new
        i = i+1
Example #32
0
def update_latexaccents(document):
    " Update latex accent insets."
    body = document.body
    i = 1
    while 1:
        i = find_token(body, '\\i ', i)
        if i == -1:
            return

        contents = body[i][2:].strip()

        if contents.find('{') != -1 and contents.find('}') != -1:
            i = i + 1
            continue

        if len(contents) == 2:
            contents = contents + '{}'
        elif len(contents) == 3:
            contents = contents[:2] + '{' + contents[2] + '}'
        elif len(contents) == 4:
            if contents[2] == ' ':
                contents = contents[:2] + '{' + contents[3] + '}'
            elif contents[2:4] == '\\i' or contents[2:4] == '\\j':
                contents = contents[:2] + '{' + contents[2:] + '}'

        body[i] = '\\i ' + contents
        i = i + 1
Example #33
0
def remove_inset_latex(document):
    "Replace inset latex with layout LaTeX"
    body = document.body

    i = 0
    while 1:
        i = find_token(body, '\\begin_inset Latex', i)
        if i == -1:
            return

        body[i] = body[i].replace('\\begin_inset Latex', '\\layout LaTeX')
        i = find_token(body, '\\end_inset', i)
        if i == -1:
            #this should not happen
            return
        del body[i]
Example #34
0
def get_length(lines, name, start, end):
    " Get lenght."
    i = find_token(lines, name, start, end)
    if i == -1:
        return ""
    x = lines[i].split()
    return x[2] + oldunits[int(x[1])]
Example #35
0
def update_tabular(document):
    " Update tabular format to version 2 (xml like syntax)."
    regexp = re.compile(r'^\\begin_inset\s+Tabular')
    lines = document.body
    i = 0
    while True:
        i = find_re(lines, regexp, i)
        if i == -1:
            break

        i = i + 1

        # scan table header meta-info
        res = table_meta_re.match(lines[i])
        if res:
            val = res.groups()
            lines[i] = '<lyxtabular version="2" rows="%s" columns="%s">' % val

        j = find_token(lines, '</LyXTabular>', i) + 1
        if j == 0:
            document.warning("Error: Bad lyx format i=%d j=%d" % (i, j))
            break

        new_table = table_update(lines[i:j])
        lines[i:j] = new_table
        i = i + len(new_table)
Example #36
0
def remove_oldertinset(document):
    " ERT insert are hidden feature of lyx 1.1.6. This might be removed in the future."
    lines = document.body
    i = 0
    while 1:
        i = find_token(lines, "\\begin_inset ERT", i)
        if i == -1:
            break
        j = find_end_of_inset(lines, i)
        k = find_token(lines, "\\layout", i + 1)
        l = get_paragraph(lines, i, document.format + 1)
        if lines[k] == lines[l]:  # same layout
            k = k + 1
        new = lines[k:j]
        lines[i:j + 1] = new
        i = i + 1
Example #37
0
 def set_parameter(self, param, value):
     " Set the value of the header parameter."
     i = find_token(self.header, '\\' + param, 0)
     if i == -1:
         self.warning('Parameter not found in the header: %s' % param, 3)
         return
     self.header[i] = '\\%s %s' % (param, str(value))
Example #38
0
 def set_parameter(self, param, value):
     " Set the value of the header parameter."
     i = find_token(self.header, '\\' + param, 0)
     if i == -1:
         self.warning('Parameter not found in the header: %s' % param, 3)
         return
     self.header[i] = '\\%s %s' % (param, str(value))
Example #39
0
def add_to_preamble(document, text):
    " Add text to the preamble if it is not already there. "

    if not type(text) is list:
        # split on \n just in case
        # it'll give us the one element list we want
        # if there's no \n, too
        text = text.split('\n')

    i = 0
    prelen = len(document.preamble)
    while True:
        i = find_token(document.preamble, text[0], i)
        if i == -1:
            break
        # we need a perfect match
        matched = True
        for line in text:
            if i >= prelen or line != document.preamble[i]:
                matched = False
                break
            i += 1
        if matched:
            return

    document.preamble.extend(["% Added by lyx2lyx"])
    document.preamble.extend(text)
Example #40
0
def add_to_preamble(document, text):
    " Add text to the preamble if it is not already there. "

    if not type(text) is list:
      # split on \n just in case
      # it'll give us the one element list we want
      # if there's no \n, too
      text = text.split('\n')

    i = 0
    prelen = len(document.preamble)
    while True:
      i = find_token(document.preamble, text[0], i)
      if i == -1:
        break
      # we need a perfect match
      matched = True
      for line in text:
        if i >= prelen or line != document.preamble[i]:
          matched = False
          break
        i += 1
      if matched:
        return

    document.preamble.extend(["% Added by lyx2lyx"])
    document.preamble.extend(text)
Example #41
0
def revert_use_package(document, pkg, commands, oldauto):
    # oldauto defines how the version we are reverting to behaves:
    # if it is true, the old version uses the package automatically.
    # if it is false, the old version never uses the package.
    regexp = re.compile(r'(\\use_package\s+%s)' % pkg)
    i = find_re(document.header, regexp, 0)
    value = "1"  # default is auto
    if i != -1:
        value = get_value(document.header, "\\use_package", i).split()[1]
        del document.header[i]
    if value == "2":  # on
        add_to_preamble(document, ["\\usepackage{" + pkg + "}"])
    elif value == "1" and not oldauto:  # auto
        i = 0
        while True:
            i = find_token(document.body, '\\begin_inset Formula', i)
            if i == -1:
                return
            j = find_end_of_inset(document.body, i)
            if j == -1:
                document.warning(
                    "Malformed LyX document: Can't find end of Formula inset at line "
                    + str(i))
                i += 1
                continue
            code = "\n".join(document.body[i:j])
            for c in commands:
                if code.find("\\%s" % c) != -1:
                    add_to_preamble(document, ["\\usepackage{" + pkg + "}"])
                    return
            i = j
Example #42
0
def revert_swissgerman(document):
    " Set language german-ch-old to german "
    i = 0
    if document.language == "german-ch-old":
        document.language = "german"
        i = find_token(document.header, "\\language", 0)
        if i != -1:
            document.header[i] = "\\language german"
    j = 0
    while True:
        j = find_token(document.body, "\\lang german-ch-old", j)
        if j == -1:
            return
        document.body[j] = document.body[j].replace("\\lang german-ch-old",
                                                    "\\lang german")
        j = j + 1
Example #43
0
def revert_use_package(document, pkg, commands, oldauto):
    # oldauto defines how the version we are reverting to behaves:
    # if it is true, the old version uses the package automatically.
    # if it is false, the old version never uses the package.
    regexp = re.compile(r'(\\use_package\s+%s)' % pkg)
    i = find_re(document.header, regexp, 0)
    value = "1" # default is auto
    if i != -1:
        value = get_value(document.header, "\\use_package" , i).split()[1]
        del document.header[i]
    if value == "2": # on
        add_to_preamble(document, ["\\usepackage{" + pkg + "}"])
    elif value == "1" and not oldauto: # auto
        i = 0
        while True:
            i = find_token(document.body, '\\begin_inset Formula', i)
            if i == -1:
                return
            j = find_end_of_inset(document.body, i)
            if j == -1:
                document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(i))
                i += 1
                continue
            code = "\n".join(document.body[i:j])
            for c in commands:
                if code.find("\\%s" % c) != -1:
                    add_to_preamble(document, ["\\usepackage{" + pkg + "}"])
                    return
            i = j
Example #44
0
def change_header(document):
    " Update header."
    lines = document.header
    i = find_token(lines, "\\use_amsmath", 0)
    if i == -1:
        return
    lines[i + 1:i + 1] = ["\\use_natbib 0", "\use_numerical_citations 0"]
Example #45
0
def change_header(document):
    " Update header."
    lines = document.header
    i = find_token(lines, "\\use_amsmath", 0)
    if i == -1:
        return
    lines[i+1:i+1] = ["\\use_natbib 0",
                      "\use_numerical_citations 0"]
Example #46
0
 def set_format(self):
     " Set the file format of the file, in the header."
     if self.format <= 217:
         format = str(float(self.format) / 100)
     else:
         format = str(self.format)
     i = find_token(self.header, "\\lyxformat", 0)
     self.header[i] = "\\lyxformat %s" % format
Example #47
0
 def set_format(self):
     " Set the file format of the file, in the header."
     if self.format <= 217:
         format = str(float(self.format)/100)
     else:
         format = str(self.format)
     i = find_token(self.header, "\\lyxformat", 0)
     self.header[i] = "\\lyxformat %s" % format
Example #48
0
def change_insetgraphics(document):
    " Change inset Graphics."
    lines = document.body
    i = 0
    while 1:
        i = find_token(lines, "\\begin_inset Graphics", i)
        if i == -1:
            break
        j = find_end_of_inset(lines, i)

        lines[i] = "\\begin_inset Graphics"

        if get_value(lines, "display", i, j) == "default":
            j = del_token(lines, "display", i, j)
        if get_value(lines, "rotateOrigin", i, j) == "leftBaseline":
            j = del_token(lines, "rotateOrigin", i, j)

        k = find_token_exact(lines, "rotate", i, j)
        if k != -1:
            del lines[k]
            j = j-1
        else:
            j = del_token(lines, "rotateAngle", i, j)

        k = find_token_exact(lines, "size_type", i, j)
        if k == -1:
            k = find_token_exact(lines, "size_kind", i, j)
        if k != -1:
            size_type = lines[k].split()[1]
            del lines[k]
            j = j-1
            if size_type in ["0", "original"]:
                j = del_token(lines, "width", i, j)
                j = del_token(lines, "height", i, j)
                j = del_token(lines, "scale", i, j)
            elif size_type in ["2", "scale"]:
                j = del_token(lines, "width", i, j)
                j = del_token(lines, "height", i, j)
                if get_value(lines, "scale", i, j) == "100":
                    j = del_token(lines, "scale", i, j)
            else:
                j = del_token(lines, "scale", i, j)

        k = find_token_exact(lines, "lyxsize_type", i, j)
        if k == -1:
            k = find_token_exact(lines, "lyxsize_kind", i, j)
        if k != -1:
            lyxsize_type = lines[k].split()[1]
            del lines[k]
            j = j-1
            j = del_token(lines, "lyxwidth", i, j)
            j = del_token(lines, "lyxheight", i, j)
            if lyxsize_type not in ["2", "scale"] or \
               get_value(lines, "lyxscale", i, j) == "100":
                j = del_token(lines, "lyxscale", i, j)

        i = i+1
Example #49
0
def revert_microtype(document):
    " Remove microtype settings. "
    i = find_token(document.header, "\\use_microtype", 0)
    if i == -1:
        return
    use_microtype = get_bool_value(document.header, "\\use_microtype", i)
    del document.header[i]
    if use_microtype:
        add_to_preamble(document, ["\\usepackage{microtype}"])
Example #50
0
def convert_ibranches(document):
    ' Add "inverted 0" to branch insets'
    i = 0
    while True:
        i = find_token(document.body, "\\begin_inset Branch", i)
        if i == -1:
            return
        document.body.insert(i + 1, "inverted 0")
        i += 1
Example #51
0
 def set_module_list(self, mlist):
   i = find_token(self.header, "\\begin_modules", 0)
   if (i == -1):
     #No modules yet included
     tclass = find_token(self.header, "\\textclass", 0)
     if tclass == -1:
       self.warning("Malformed LyX document: No \\textclass!!")
       return
     i = j = tclass + 1
   else:
     j = find_token(self.header, "\\end_modules", i)
     if j == -1:
         self.warning("(set_module_list) Malformed LyX document: No \\end_modules.")
         return
     j += 1
   if mlist:
       mlist = ['\\begin_modules'] + mlist + ['\\end_modules']
   self.header[i:j] = mlist
Example #52
0
File: LyX.py Project: cburschka/lyx
 def set_module_list(self, mlist):
   i = find_token(self.header, "\\begin_modules", 0)
   if (i == -1):
     #No modules yet included
     tclass = find_token(self.header, "\\textclass", 0)
     if tclass == -1:
       self.warning("Malformed LyX document: No \\textclass!!")
       return
     i = j = tclass + 1
   else: 
     j = find_token(self.header, "\\end_modules", i)
     if j == -1:
         self.warning("(set_module_list) Malformed LyX document: No \\end_modules.")
         return
     j += 1
   if mlist:
       mlist = ['\\begin_modules'] + mlist + ['\\end_modules']
   self.header[i:j] = mlist
Example #53
0
 def set_module_list(self, mlist):
   modbegin = find_token(self.header, "\\begin_modules", 0)
   newmodlist = ['\\begin_modules'] + mlist + ['\\end_modules']
   if (modbegin == -1):
     #No modules yet included
     tclass = find_token(self.header, "\\textclass", 0)
     if tclass == -1:
       self.warning("Malformed LyX document: No \\textclass!!")
       return
     modbegin = tclass + 1
     self.header[modbegin:modbegin] = newmodlist
     return
   modend = find_token(self.header, "\\end_modules", modbegin)
   if modend == -1:
     self.warning("(set_module_list)Malformed LyX document: No \\end_modules.")
     return
   newmodlist = ['\\begin_modules'] + mlist + ['\\end_modules']
   self.header[modbegin:modend + 1] = newmodlist
Example #54
0
def change_insetgraphics(document):
    " Change inset Graphics."
    lines = document.body
    i = 0
    while True:
        i = find_token(lines, "\\begin_inset Graphics", i)
        if i == -1:
            break
        j = find_end_of_inset(lines, i)

        lines[i] = "\\begin_inset Graphics"

        if get_value(lines, "display", i, j) == "default":
            j = del_token(lines, "display", i, j)
        if get_value(lines, "rotateOrigin", i, j) == "leftBaseline":
            j = del_token(lines, "rotateOrigin", i, j)

        k = find_token_exact(lines, "rotate", i, j)
        if k != -1:
            del lines[k]
            j = j - 1
        else:
            j = del_token(lines, "rotateAngle", i, j)

        k = find_token_exact(lines, "size_type", i, j)
        if k == -1:
            k = find_token_exact(lines, "size_kind", i, j)
        if k != -1:
            size_type = lines[k].split()[1]
            del lines[k]
            j = j - 1
            if size_type in ["0", "original"]:
                j = del_token(lines, "width", i, j)
                j = del_token(lines, "height", i, j)
                j = del_token(lines, "scale", i, j)
            elif size_type in ["2", "scale"]:
                j = del_token(lines, "width", i, j)
                j = del_token(lines, "height", i, j)
                if get_value(lines, "scale", i, j) == "100":
                    j = del_token(lines, "scale", i, j)
            else:
                j = del_token(lines, "scale", i, j)

        k = find_token_exact(lines, "lyxsize_type", i, j)
        if k == -1:
            k = find_token_exact(lines, "lyxsize_kind", i, j)
        if k != -1:
            lyxsize_type = lines[k].split()[1]
            del lines[k]
            j = j - 1
            j = del_token(lines, "lyxwidth", i, j)
            j = del_token(lines, "lyxheight", i, j)
            if lyxsize_type not in ["2", "scale"] or \
               get_value(lines, "lyxscale", i, j) == "100":
                j = del_token(lines, "lyxscale", i, j)

        i = i + 1
Example #55
0
 def set_module_list(self, mlist):
   modbegin = find_token(self.header, "\\begin_modules", 0)
   newmodlist = ['\\begin_modules'] + mlist + ['\\end_modules']
   if (modbegin == -1):
     #No modules yet included
     tclass = find_token(self.header, "\\textclass", 0)
     if tclass == -1:
       self.warning("Malformed LyX document: No \\textclass!!")
       return
     modbegin = tclass + 1
     self.header[modbegin:modbegin] = newmodlist
     return
   modend = find_token(self.header, "\\end_modules", modbegin)
   if modend == -1:
     self.warning("(set_module_list)Malformed LyX document: No \\end_modules.")
     return
   newmodlist = ['\\begin_modules'] + mlist + ['\\end_modules']
   self.header[modbegin:modend + 1] = newmodlist
Example #56
0
 def add_module(self, module):
   i = find_token(self.header, "\\begin_modules", 0)
   if i == -1:
     #No modules yet included
     i = find_token(self.header, "\\textclass", 0)
     if i == -1:
       self.warning("Malformed LyX document: No \\textclass!!")
       return
     modinfo = ["\\begin_modules", module, "\\end_modules"]
     self.header[i + 1: i + 1] = modinfo
     return
   j = find_token(self.header, "\\end_modules", i)
   if j == -1:
     self.warning("(add_module)Malformed LyX document: No \\end_modules.")
     return
   k = find_token(self.header, module, i)
   if k != -1 and k < j:
     return
   self.header.insert(j, module)
Example #57
0
def update_inset_label(document):
    " Update inset Label."
    lines = document.body
    i = 0
    while 1:
        i = find_token(lines, '\\begin_inset Label', i)
        if i == -1:
            return
        lines[i] = '\\begin_inset LatexCommand \label{' + lines[i][19:] + '}'
        i = i + 1
Example #58
0
def update_toc(document):
    " Update table of contents. "
    lines = document.body
    i = 0
    while 1:
        i = find_token(lines,
                       '\\begin_inset LatexCommand \\tableofcontents', i)
        if i == -1:
            break
        lines[i] = lines[i] + '{}'
        i = i + 1
Example #59
0
def merge_formula_inset(document):
    " Merge formula insets. "
    lines = document.body
    i=0
    while 1:
        i = find_token(lines, "\\begin_inset Formula", i)
        if i == -1: break
        if lines[i+1] in math_env:
            lines[i] = lines[i] + lines[i+1]
            del lines[i+1]
        i = i + 1
Example #60
0
def change_listof(document):
    " Change listof insets."
    lines = document.body
    i = 0
    while 1:
        i = find_token(lines, "\\begin_inset LatexCommand \\listof", i)
        if i == -1:
            break
        type = re.search(r"listof(\w*)", lines[i]).group(1)[:-1]
        lines[i] = "\\begin_inset FloatList "+type
        i = i+1