+ revert_flex_inset(document.body, "IEEE membership", "\\IEEEmembership")
+ revert_flex_inset(document.body, "Lowercase", "\\MakeLowercase")
+ layouts = ("Special Paper Notice", "After Title Text", "Publication ID",
+ "Page headings", "Biography without photo")
+ latexcmd = {"Special Paper Notice": "\\IEEEspecialpapernotice",
+ "After Title Text": "\\IEEEaftertitletext",
+ "Publication ID": "\\IEEEpubid"}
+ obsoletedby = {"Page headings": "MarkBoth",
+ "Biography without photo": "BiographyNoPhoto"}
+ for layout in layouts:
+ i = 0
+ while True:
+ i = find_token(document.body, '\\begin_layout ' + layout, i)
+ if i == -1:
+ break
+ j = find_end_of_layout(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: Can't find end of " + layout + " layout.")
+ i += 1
+ continue
+ if layout in obsoletedby:
+ document.body[i] = "\\begin_layout " + obsoletedby[layout]
+ i = j
+ continue
+ content = lyx2latex(document, document.body[i:j + 1])
+ add_to_preamble(document, [latexcmd[layout] + "{" + content + "}"])
+ del document.body[i:j + 1]
+ # no need to reset i
+
+
+def convert_prettyref(document):
+ " Converts prettyref references to neutral formatted refs "
+ re_ref = re.compile("^\s*reference\s+\"(\w+):(\S+)\"")
+ nm_ref = re.compile("^\s*name\s+\"(\w+):(\S+)\"")
+
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_inset CommandInset ref", i)
+ if i == -1:
+ break
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: No end of InsetRef!")
+ i += 1
+ continue
+ k = find_token(document.body, "LatexCommand prettyref", i, j)
+ if k != -1:
+ document.body[k] = "LatexCommand formatted"
+ i = j + 1
+ document.header.insert(-1, "\\use_refstyle 0")
+
+
+def revert_refstyle(document):
+ " Reverts neutral formatted refs to prettyref "
+ re_ref = re.compile("^reference\s+\"(\w+):(\S+)\"")
+ nm_ref = re.compile("^\s*name\s+\"(\w+):(\S+)\"")
+
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_inset CommandInset ref", i)
+ if i == -1:
+ break
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: No end of InsetRef")
+ i += 1
+ continue
+ k = find_token(document.body, "LatexCommand formatted", i, j)
+ if k != -1:
+ document.body[k] = "LatexCommand prettyref"
+ i = j + 1
+ i = find_token(document.header, "\\use_refstyle", 0)
+ if i != -1:
+ document.header.pop(i)
+
+
+def revert_nameref(document):
+ " Convert namerefs to regular references "
+ cmds = ["Nameref", "nameref"]
+ foundone = False
+ rx = re.compile(r'reference "(.*)"')
+ for cmd in cmds:
+ i = 0
+ oldcmd = "LatexCommand " + cmd
+ while 1:
+ # It seems better to look for this, as most of the reference
+ # insets won't be ones we care about.
+ i = find_token(document.body, oldcmd, i)
+ if i == -1:
+ break
+ cmdloc = i
+ i += 1
+ # Make sure it is actually in an inset!
+ # A normal line could begin with "LatexCommand nameref"!
+ val = is_in_inset(document.body, cmdloc, \
+ "\\begin_inset CommandInset ref")
+ if not val:
+ continue
+ stins, endins = val
+
+ # ok, so it is in an InsetRef
+ refline = find_token(document.body, "reference", stins, endins)
+ if refline == -1:
+ document.warning("Can't find reference for inset at line " + stinst + "!!")
+ continue
+ m = rx.match(document.body[refline])
+ if not m:
+ document.warning("Can't match reference line: " + document.body[ref])
+ continue
+ foundone = True
+ ref = m.group(1)
+ newcontent = put_cmd_in_ert('\\' + cmd + '{' + ref + '}')
+ document.body[stins:endins + 1] = newcontent
+
+ if foundone:
+ add_to_preamble(document, ["\usepackage{nameref}"])
+
+
+def remove_Nameref(document):
+ " Convert Nameref commands to nameref commands "
+ i = 0
+ while 1:
+ # It seems better to look for this, as most of the reference
+ # insets won't be ones we care about.
+ i = find_token(document.body, "LatexCommand Nameref" , i)
+ if i == -1:
+ break
+ cmdloc = i
+ i += 1
+
+ # Make sure it is actually in an inset!
+ val = is_in_inset(document.body, cmdloc, \
+ "\\begin_inset CommandInset ref")
+ if not val:
+ continue
+ document.body[cmdloc] = "LatexCommand nameref"
+
+
+def revert_mathrsfs(document):
+ " Load mathrsfs if \mathrsfs us use in the document "
+ i = 0
+ for line in document.body:
+ if line.find("\\mathscr{") != -1:
+ add_to_preamble(document, ["\\usepackage{mathrsfs}"])
+ return
+
+
+def convert_flexnames(document):
+ "Convert \\begin_inset Flex Custom:Style to \\begin_inset Flex Style and similarly for CharStyle and Element."
+
+ i = 0
+ rx = re.compile(r'^\\begin_inset Flex (?:Custom|CharStyle|Element):(.+)$')
+ while True:
+ i = find_token(document.body, "\\begin_inset Flex", i)
+ if i == -1:
+ return
+ m = rx.match(document.body[i])
+ if m:
+ document.body[i] = "\\begin_inset Flex " + m.group(1)
+ i += 1
+
+
+flex_insets = {
+ "Alert" : "CharStyle:Alert",
+ "Code" : "CharStyle:Code",
+ "Concepts" : "CharStyle:Concepts",
+ "E-Mail" : "CharStyle:E-Mail",
+ "Emph" : "CharStyle:Emph",
+ "Expression" : "CharStyle:Expression",
+ "Initial" : "CharStyle:Initial",
+ "Institute" : "CharStyle:Institute",
+ "Meaning" : "CharStyle:Meaning",
+ "Noun" : "CharStyle:Noun",
+ "Strong" : "CharStyle:Strong",
+ "Structure" : "CharStyle:Structure",
+ "ArticleMode" : "Custom:ArticleMode",
+ "Endnote" : "Custom:Endnote",
+ "Glosse" : "Custom:Glosse",
+ "PresentationMode" : "Custom:PresentationMode",
+ "Tri-Glosse" : "Custom:Tri-Glosse"
+}
+
+flex_elements = {
+ "Abbrev" : "Element:Abbrev",
+ "CCC-Code" : "Element:CCC-Code",
+ "Citation-number" : "Element:Citation-number",
+ "City" : "Element:City",
+ "Code" : "Element:Code",
+ "CODEN" : "Element:CODEN",
+ "Country" : "Element:Country",
+ "Day" : "Element:Day",
+ "Directory" : "Element:Directory",
+ "Dscr" : "Element:Dscr",
+ "Email" : "Element:Email",
+ "Emph" : "Element:Emph",
+ "Filename" : "Element:Filename",
+ "Firstname" : "Element:Firstname",
+ "Fname" : "Element:Fname",
+ "GuiButton" : "Element:GuiButton",
+ "GuiMenu" : "Element:GuiMenu",
+ "GuiMenuItem" : "Element:GuiMenuItem",
+ "ISSN" : "Element:ISSN",
+ "Issue-day" : "Element:Issue-day",
+ "Issue-months" : "Element:Issue-months",
+ "Issue-number" : "Element:Issue-number",
+ "KeyCap" : "Element:KeyCap",
+ "KeyCombo" : "Element:KeyCombo",
+ "Keyword" : "Element:Keyword",
+ "Literal" : "Element:Literal",
+ "MenuChoice" : "Element:MenuChoice",
+ "Month" : "Element:Month",
+ "Orgdiv" : "Element:Orgdiv",
+ "Orgname" : "Element:Orgname",
+ "Postcode" : "Element:Postcode",
+ "SS-Code" : "Element:SS-Code",
+ "SS-Title" : "Element:SS-Title",
+ "State" : "Element:State",
+ "Street" : "Element:Street",
+ "Surname" : "Element:Surname",
+ "Volume" : "Element:Volume",
+ "Year" : "Element:Year"
+}
+
+
+def revert_flexnames(document):
+ if document.backend == "latex":
+ flexlist = flex_insets
+ else:
+ flexlist = flex_elements
+
+ rx = re.compile(r'^\\begin_inset Flex\s+(.+)$')
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_inset Flex", i)
+ if i == -1:
+ return
+ m = rx.match(document.body[i])
+ if not m:
+ document.warning("Illegal flex inset: " + document.body[i])
+ i += 1
+ continue
+ style = m.group(1)
+ if style in flexlist:
+ document.body[i] = "\\begin_inset Flex " + flexlist[style]
+ i += 1
+
+
+def convert_mathdots(document):
+ " Load mathdots automatically "
+ i = find_token(document.header, "\\use_mhchem" , 0)
+ if i == -1:
+ i = find_token(document.header, "\\use_esint" , 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Can't find \\use_mhchem.")
+ return;
+ j = find_token(document.preamble, "\\usepackage{mathdots}", 0)
+ if j == -1:
+ document.header.insert(i + 1, "\\use_mathdots 0")
+ else:
+ document.header.insert(i + 1, "\\use_mathdots 2")
+ del document.preamble[j]
+
+
+def revert_mathdots(document):
+ " Load mathdots if used in the document "
+
+ mathdots = find_token(document.header, "\\use_mathdots" , 0)
+ if mathdots == -1:
+ document.warning("No \\use_mathdots line. Assuming auto.")
+ else:
+ val = get_value(document.header, "\\use_mathdots", mathdots)
+ del document.header[mathdots]
+ try:
+ usedots = int(val)
+ except:
+ document.warning("Invalid \\use_mathdots value: " + val + ". Assuming auto.")
+ # probably usedots has not been changed, but be safe.
+ usedots = 1
+
+ if usedots == 0:
+ # do not load case
+ return
+ if usedots == 2:
+ # force load case
+ add_to_preamble(document, ["\\usepackage{mathdots}"])
+ return
+
+ # so we are in the auto case. we want to load mathdots if \iddots is used.
+ 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])
+ if code.find("\\iddots") != -1:
+ add_to_preamble(document, ["\\@ifundefined{iddots}{\\usepackage{mathdots}}"])
+ return
+ i = j
+
+
+def convert_rule(document):
+ " Convert \\lyxline to CommandInset line. "
+ i = 0
+
+ inset = ['\\begin_inset CommandInset line',
+ 'LatexCommand rule',
+ 'offset "0.5ex"',
+ 'width "100line%"',
+ 'height "1pt"', '',
+ '\\end_inset', '', '']
+
+ # if paragraphs are indented, we may have to unindent to get the
+ # line to be full-width.
+ indent = get_value(document.header, "\\paragraph_separation", 0)
+ have_indent = (indent == "indent")
+
+ while True:
+ i = find_token(document.body, "\\lyxline" , i)
+ if i == -1:
+ return
+
+ # we need to find out if this line follows other content
+ # in its paragraph. find its layout....
+ lastlay = find_token_backwards(document.body, "\\begin_layout", i)
+ if lastlay == -1:
+ document.warning("Can't find layout for line at " + str(i))
+ # do the best we can.
+ document.body[i:i+1] = inset
+ i += len(inset)
+ continue
+
+ # ...and look for other content before it.
+ lineisfirst = True
+ for line in document.body[lastlay + 1:i]:
+ # is it empty or a paragraph option?
+ if not line or line[0] == '\\':
+ continue
+ lineisfirst = False
+ break
+
+ if lineisfirst:
+ document.body[i:i+1] = inset
+ if indent:
+ # we need to unindent, lest the line be too long
+ document.body.insert(lastlay + 1, "\\noindent")
+ i += len(inset)
+ else:
+ # so our line is in the middle of a paragraph
+ # we need to add a new line, lest this line follow the
+ # other content on that line and run off the side of the page
+ document.body[i:i+1] = inset
+ document.body[i:i] = ["\\begin_inset Newline newline", "\\end_inset", ""]
+ i += len(inset)
+
+
+def revert_rule(document):
+ " Revert line insets to Tex code "
+ i = 0
+ while 1:
+ i = find_token(document.body, "\\begin_inset CommandInset line" , i)
+ if i == -1:
+ return
+ # find end of inset
+ j = find_token(document.body, "\\end_inset" , i)
+ if j == -1:
+ document.warning("Malformed LyX document: Can't find end of line inset.")
+ return
+ # determine the optional offset
+ offset = get_quoted_value(document.body, 'offset', i, j)
+ if offset:
+ offset = '[' + offset + ']'
+ # determine the width
+ width = get_quoted_value(document.body, 'width', i, j, "100col%")
+ width = latex_length(width)[1]
+ # determine the height
+ height = get_quoted_value(document.body, 'height', i, j, "1pt")
+ height = latex_length(height)[1]
+ # output the \rule command
+ subst = "\\rule[" + offset + "]{" + width + "}{" + height + "}"
+ document.body[i:j + 1] = put_cmd_in_ert(subst)
+ i += len(subst) - (j - i)
+
+
+def revert_diagram(document):
+ " Add the feyn package if \\Diagram is used in math "
+ 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.")
+ return
+ lines = "\n".join(document.body[i:j])
+ if lines.find("\\Diagram") == -1:
+ i = j
+ continue
+ add_to_preamble(document, ["\\usepackage{feyn}"])
+ # only need to do it once!
+ return
+
+chapters = ("amsbook", "book", "docbook-book", "elsart", "extbook", "extreport",
+ "jbook", "jreport", "jsbook", "literate-book", "literate-report", "memoir",
+ "mwbk", "mwrep", "recipebook", "report", "scrbook", "scrreprt", "svmono",
+ "svmult", "tbook", "treport", "tufte-book")
+
+def convert_bibtex_clearpage(document):
+ " insert a clear(double)page bibliographystyle if bibtotoc option is used "