import unicodedata
import sys, os
-from parser_tools import find_token, find_end_of, find_tokens, get_value
+from parser_tools import find_token, find_end_of, find_tokens, get_value, get_value_string
####################################################################
# Private helper functions
document.preamble.extend(text)
+# Convert a LyX length into a LaTeX length
+def convert_len(len):
+ units = {"text%":"\\backslash\ntextwidth", "col%":"\\backslash\ncolumnwidth",
+ "page%":"\\backslash\npagewidth", "line%":"\\backslash\nlinewidth",
+ "theight%":"\\backslash\ntextheight", "pheight%":"\\backslash\npageheight"}
+
+ # Convert LyX units to LaTeX units
+ for unit in units.keys():
+ if len.find(unit) != -1:
+ len = '%f' % (len2value(len) / 100)
+ len = len.strip('0') + units[unit]
+ break
+
+ return len
+
+# Return the value of len without the unit in numerical form.
+def len2value(len):
+ result = re.search('([+-]?[0-9.]+)', len)
+ if result:
+ return float(result.group(1))
+ # No number means 1.0
+ return 1.0
+
+# Unfortunately, this doesn't really work, since Standard isn't always default.
+# But it's as good as we can do right now.
+def find_default_layout(doc, start, end):
+ l = find_token(document.body, "\\begin_layout Standard", start, end)
+ if l == -1:
+ l = find_token(document.body, "\\begin_layout PlainLayout", start, end)
+ if l == -1:
+ l = find_token(document.body, "\\begin_layout Plain Layout", start, end)
+ return l
+
####################################################################
def get_option(document, m, option, default):
# Discard PDF options for hyperref
def revert_pdf_options(document):
"Revert PDF options for hyperref."
+ # store the PDF options and delete the entries from the Lyx file
i = 0
+ hyperref = False
+ title = ""
+ author = ""
+ subject = ""
+ keywords = ""
+ bookmarks = ""
+ bookmarksnumbered = ""
+ bookmarksopen = ""
+ bookmarksopenlevel = ""
+ breaklinks = ""
+ pdfborder = ""
+ colorlinks = ""
+ backref = ""
+ pagebackref = ""
+ pagemode = ""
+ otheroptions = ""
i = find_token(document.header, "\\use_hyperref", i)
if i != -1:
+ hyperref = get_value(document.header, "\\use_hyperref", i) == 'true'
del document.header[i]
i = find_token(document.header, "\\pdf_store_options", i)
if i != -1:
del document.header[i]
i = find_token(document.header, "\\pdf_title", 0)
if i != -1:
+ title = get_value_string(document.header, '\\pdf_title', 0, 0, True)
+ title = ' pdftitle={' + title + '}'
del document.header[i]
i = find_token(document.header, "\\pdf_author", 0)
if i != -1:
+ author = get_value_string(document.header, '\\pdf_author', 0, 0, True)
+ if title == "":
+ author = ' pdfauthor={' + author + '}'
+ else:
+ author = ',\n pdfauthor={' + author + '}'
del document.header[i]
i = find_token(document.header, "\\pdf_subject", 0)
if i != -1:
+ subject = get_value_string(document.header, '\\pdf_subject', 0, 0, True)
+ if title == "" and author == "":
+ subject = ' pdfsubject={' + subject + '}'
+ else:
+ subject = ',\n pdfsubject={' + subject + '}'
del document.header[i]
i = find_token(document.header, "\\pdf_keywords", 0)
if i != -1:
+ keywords = get_value_string(document.header, '\\pdf_keywords', 0, 0, True)
+ if title == "" and author == "" and subject == "":
+ keywords = ' pdfkeywords={' + keywords + '}'
+ else:
+ keywords = ',\n pdfkeywords={' + keywords + '}'
del document.header[i]
i = find_token(document.header, "\\pdf_bookmarks", 0)
if i != -1:
+ bookmarks = get_value_string(document.header, '\\pdf_bookmarks', 0)
+ bookmarks = ',\n bookmarks=' + bookmarks
del document.header[i]
i = find_token(document.header, "\\pdf_bookmarksnumbered", i)
if i != -1:
+ bookmarksnumbered = get_value_string(document.header, '\\pdf_bookmarksnumbered', 0)
+ bookmarksnumbered = ',\n bookmarksnumbered=' + bookmarksnumbered
del document.header[i]
i = find_token(document.header, "\\pdf_bookmarksopen", i)
if i != -1:
+ bookmarksopen = get_value_string(document.header, '\\pdf_bookmarksopen', 0)
+ bookmarksopen = ',\n bookmarksopen=' + bookmarksopen
del document.header[i]
i = find_token(document.header, "\\pdf_bookmarksopenlevel", i)
if i != -1:
+ bookmarksopenlevel = get_value_string(document.header, '\\pdf_bookmarksopenlevel', 0, 0, True)
+ bookmarksopenlevel = ',\n bookmarksopenlevel=' + bookmarksopenlevel
del document.header[i]
i = find_token(document.header, "\\pdf_breaklinks", i)
if i != -1:
+ breaklinks = get_value_string(document.header, '\\pdf_breaklinks', 0)
+ breaklinks = ',\n breaklinks=' + breaklinks
del document.header[i]
i = find_token(document.header, "\\pdf_pdfborder", i)
if i != -1:
+ pdfborder = get_value_string(document.header, '\\pdf_pdfborder', 0)
+ if pdfborder == 'true':
+ pdfborder = ',\n pdfborder={0 0 0}'
+ else:
+ pdfborder = ',\n pdfborder={0 0 1}'
del document.header[i]
i = find_token(document.header, "\\pdf_colorlinks", i)
if i != -1:
+ colorlinks = get_value_string(document.header, '\\pdf_colorlinks', 0)
+ colorlinks = ',\n colorlinks=' + colorlinks
del document.header[i]
i = find_token(document.header, "\\pdf_backref", i)
if i != -1:
+ backref = get_value_string(document.header, '\\pdf_backref', 0)
+ backref = ',\n backref=' + backref
del document.header[i]
i = find_token(document.header, "\\pdf_pagebackref", i)
if i != -1:
+ pagebackref = get_value_string(document.header, '\\pdf_pagebackref', 0)
+ pagebackref = ',\n pagebackref=' + pagebackref
del document.header[i]
i = find_token(document.header, "\\pdf_pagemode", 0)
if i != -1:
+ pagemode = get_value_string(document.header, '\\pdf_pagemode', 0)
+ pagemode = ',\n pdfpagemode=' + pagemode
del document.header[i]
i = find_token(document.header, "\\pdf_quoted_options", 0)
if i != -1:
+ otheroptions = get_value_string(document.header, '\\pdf_quoted_options', 0, 0, True)
+ if title == "" and author == "" and subject == "" and keywords == "":
+ otheroptions = ' ' + otheroptions
+ else:
+ otheroptions = ',\n ' + otheroptions
del document.header[i]
+ # write to the preamble when hyperref was used
+ if hyperref == True:
+ # preamble write preparations
+ # bookmark numbers are only output when they are turned on
+ if bookmarksopen == ',\n bookmarksopen=true':
+ bookmarksopen = bookmarksopen + bookmarksopenlevel
+ if bookmarks == ',\n bookmarks=true':
+ bookmarks = bookmarks + bookmarksnumbered + bookmarksopen
+ else:
+ bookmarks = bookmarks
+ # hypersetup is only output when there are things to be set up
+ setupstart = '\\hypersetup{%\n'
+ setupend = ' }\n'
+ if otheroptions == "" and title == "" and author == ""\
+ and subject == "" and keywords == "":
+ setupstart = ""
+ setupend = ""
+ # write the preamble
+ add_to_preamble(document,
+ ['% Commands inserted by lyx2lyx for PDF properties',
+ '\\usepackage[unicode=true'
+ + bookmarks
+ + breaklinks
+ + pdfborder
+ + backref
+ + pagebackref
+ + colorlinks
+ + pagemode
+ + ']\n'
+ ' {hyperref}\n'
+ + setupstart
+ + title
+ + author
+ + subject
+ + keywords
+ + otheroptions
+ + setupend])
+
def remove_inzip_options(document):
"Remove inzipName and embed options from the Graphics inset"
line = line[20:]
if line.startswith("\\begin_layout Standard"):
line = line[22:]
+ if line.startswith("\\begin_layout Plain"):
+ line = line[22:]
if line.startswith("\\end_layout"):
line = line[11:]
if line.startswith("\\end_inset"):
return
m = r.match(document.body[i])
if m == None:
- document.warning("Weirdly formed \\begin_layout at line " + i + " of body!")
+ document.warning("Weirdly formed \\begin_layout at line %d of body!" % i)
i += 1
continue
m = m.group(1)
document.warning("Malformed LyX document: Missing `status' tag in Box inset.")
return
status = document.body[k]
- l = find_token(document.body, "\\begin_layout Standard", i + 1, j)
+ l = find_default_layout(document, i + 1, j)
if l == -1:
- document.warning("Malformed LyX document: Missing `\\begin_layout Standard' in Box inset.")
+ document.warning("Malformed LyX document: Missing `\\begin_layout' in Box inset.")
return
m = find_token(document.body, "\\end_layout", i + 1, j)
if m == -1:
i = i + 1
continue
if get_value(document.body, 'sideways', i, j) != "false":
- l = find_token(document.body, "\\begin_layout Standard", i + 1, j)
+ l = find_default_layout(doc, i + 1, j)
if l == -1:
- document.warning("Malformed LyX document: Missing `\\begin_layout Standard' in Float inset.")
+ document.warning("Malformed LyX document: Missing `\\begin_layout' in Float inset.")
return
document.body[j] = '\\begin_layout Standard\n\\begin_inset ERT\nstatus collapsed\n\n' \
'\\begin_layout Standard\n\n\n\\backslash\n' \
continue
if get_value(document.body, 'sideways', i, j) != "false":
if get_value(document.body, 'wide', i, j) != "false":
- l = find_token(document.body, "\\begin_layout Standard", i + 1, j)
+ l = find_default_layout(document, i + 1, j)
if l == -1:
- document.warning("Malformed LyX document: Missing `\\begin_layout Standard' in Float inset.")
+ document.warning("Malformed LyX document: Missing `\\begin_layout' in Float inset.")
return
document.body[j] = '\\begin_layout Standard\n\\begin_inset ERT\nstatus collapsed\n\n' \
'\\begin_layout Standard\n\n\n\\backslash\n' \
i = i + 1
+def revert_external_embedding(document):
+ ' Remove embed tag from external inset '
+ i = 0
+ while 1:
+ i = find_token(document.body, "\\begin_inset External", i)
+ if i == -1:
+ return
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Malformed lyx document: Missing '\\end_inset'.")
+ i = i + 1
+ continue
+ k = find_token(document.body, "\tembed", i, j)
+ if k != -1:
+ del document.body[k]
+ i = i + 1
+
+
def convert_subfig(document):
" Convert subfigures to subfloats. "
i = 0
del document.body[l]
del document.body[k]
document.body[i] = '\\begin_inset Float figure\nwide false\nsideways false\n' \
- 'status open\n\n\\begin_layout PlainLayout\n\\begin_inset Caption\n\n\\begin_layout PlainLayout\n' \
- + caption + '\n\\end_layout\n\n\\end_inset\n\n\\end_layout\n\n\\begin_layout PlainLayout\n' + savestr
+ 'status open\n\n\\begin_layout Plain Layout\n\\begin_inset Caption\n\n\\begin_layout Plain Layout\n' \
+ + caption + '\n\\end_layout\n\n\\end_inset\n\n\\end_layout\n\n\\begin_layout Plain Layout\n' + savestr
savestr = document.body[j]
document.body[j] = '\n\\end_layout\n\n\\end_inset\n' + savestr
document.warning("Malformed lyx document: Missing '\\end_inset' (embedded float).")
i = i + 1
continue
- m = find_token(document.body, "\\begin_layout PlainLayout", k + 1, l)
+ m = find_default_layout(document.body, k + 1, l)
# caption?
cap = find_token(document.body, '\\begin_inset Caption', k + 1, l)
caption = ''
if optend == -1:
document.warning("Malformed lyx document: Missing '\\end_inset' (OptArg).")
return
- optc = find_token(document.body, "\\begin_layout PlainLayout", opt, optend)
+ optc = find_default_layout(document.body, opt, optend)
if optc == -1:
- document.warning("Malformed LyX document: Missing `\\begin_layout PlainLayout' in Float inset.")
+ document.warning("Malformed LyX document: Missing `\\begin_layout' in Float inset.")
return
optcend = find_end_of(document.body, optc, "\\begin_layout", "\\end_layout")
for line in document.body[optc:optcend]:
caption += line.strip()
if len(label) > 0:
caption += "\\backslash\nlabel{" + label + "}"
- document.body[l] = '\\begin_layout PlainLayout\n\\begin_inset ERT\nstatus collapsed\n\n' \
- '\\begin_layout PlainLayout\n\n}\n\\end_layout\n\n\\end_inset\n\n\\end_layout\n\n\\begin_layout PlainLayout\n'
+ document.body[l] = '\\begin_layout Plain Layout\n\\begin_inset ERT\nstatus collapsed\n\n' \
+ '\\begin_layout Plain Layout\n\n}\n\\end_layout\n\n\\end_inset\n\n\\end_layout\n\n\\begin_layout Plain Layout\n'
del document.body[cap:capend+1]
del document.body[k+1:m-1]
insertion = '\\begin_inset ERT\nstatus collapsed\n\n' \
- '\\begin_layout PlainLayout\n\n\\backslash\n' \
+ '\\begin_layout Plain Layout\n\n\\backslash\n' \
'subfloat'
if len(shortcap) > 0:
insertion = insertion + "[" + shortcap + "]"
document.body[i] = document.body[i].replace('\\begin_inset Newpage cleardoublepage', '\\cleardoublepage')
+def convert_linebreaks(document):
+ ' Convert inline Newline insets to new format '
+ i = 0
+ while True:
+ i = find_token(document.body, '\\newline', i)
+ if i == -1:
+ break
+ document.body[i:i+1] = ['\\begin_inset Newline newline',
+ '\\end_inset']
+ i = 0
+ while True:
+ i = find_token(document.body, '\\linebreak', i)
+ if i == -1:
+ break
+ document.body[i:i+1] = ['\\begin_inset Newline linebreak',
+ '\\end_inset']
+
+
+def revert_linebreaks(document):
+ ' Revert \\begin_inset Newline to previous inline format '
+ i = 0
+ while True:
+ i = find_token(document.body, '\\begin_inset Newline', i)
+ if i == -1:
+ return
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Malformed LyX document: Could not find end of Newline inset.")
+ continue
+ del document.body[j]
+ document.body[i] = document.body[i].replace('\\begin_inset Newline newline', '\\newline')
+ document.body[i] = document.body[i].replace('\\begin_inset Newline linebreak', '\\linebreak')
+
+
+def convert_japanese_plain(document):
+ ' Set language japanese-plain to japanese '
+ i = 0
+ if document.language == "japanese-plain":
+ document.language = "japanese"
+ i = find_token(document.header, "\\language", 0)
+ if i != -1:
+ document.header[i] = "\\language japanese"
+ j = 0
+ while True:
+ j = find_token(document.body, "\\lang japanese-plain", j)
+ if j == -1:
+ return
+ document.body[j] = document.body[j].replace("\\lang japanese-plain", "\\lang japanese")
+ j = j + 1
+
+
+def revert_pdfpages(document):
+ ' Revert pdfpages external inset to ERT '
+ i = 0
+ while 1:
+ i = find_token(document.body, "\\begin_inset External", i)
+ if i == -1:
+ return
+ j = find_end_of_inset(document.body, i)
+ if j == -1:
+ document.warning("Malformed lyx document: Missing '\\end_inset'.")
+ i = i + 1
+ continue
+ if get_value(document.body, 'template', i, j) == "PDFPages":
+ filename = get_value(document.body, 'filename', i, j)
+ extra = ''
+ r = re.compile(r'\textra PDFLaTeX \"(.*)\"$')
+ for k in range(i, j):
+ m = r.match(document.body[k])
+ if m:
+ extra = m.group(1)
+ angle = get_value(document.body, 'rotateAngle', i, j)
+ width = get_value(document.body, 'width', i, j)
+ height = get_value(document.body, 'height', i, j)
+ scale = get_value(document.body, 'scale', i, j)
+ keepAspectRatio = find_token(document.body, "\tkeepAspectRatio", i, j)
+ options = extra
+ if angle != '':
+ if options != '':
+ options += ",angle=" + angle
+ else:
+ options += "angle=" + angle
+ if width != '':
+ if options != '':
+ options += ",width=" + convert_len(width)
+ else:
+ options += "width=" + convert_len(width)
+ if height != '':
+ if options != '':
+ options += ",height=" + convert_len(height)
+ else:
+ options += "height=" + convert_len(height)
+ if scale != '':
+ if options != '':
+ options += ",scale=" + scale
+ else:
+ options += "scale=" + scale
+ if keepAspectRatio != '':
+ if options != '':
+ options += ",keepaspectratio"
+ else:
+ options += "keepaspectratio"
+ if options != '':
+ options = '[' + options + ']'
+ del document.body[i+1:j+1]
+ document.body[i:i+1] = ['\\begin_inset ERT',
+ 'status collapsed',
+ '',
+ '\\begin_layout Standard',
+ '',
+ '\\backslash',
+ 'includepdf' + options + '{' + filename + '}',
+ '\\end_layout',
+ '',
+ '\\end_inset']
+ add_to_preamble(document, ['\\usepackage{pdfpages}\n'])
+ i = i + 1
+ continue
+ i = i + 1
+
+
+def revert_mexican(document):
+ "Set language Spanish(Mexico) to Spanish"
+ i = 0
+ if document.language == "spanish-mexico":
+ document.language = "spanish"
+ i = find_token(document.header, "\\language", 0)
+ if i != -1:
+ document.header[i] = "\\language spanish"
+ j = 0
+ while True:
+ j = find_token(document.body, "\\lang spanish-mexico", j)
+ if j == -1:
+ return
+ document.body[j] = document.body[j].replace("\\lang spanish-mexico", "\\lang spanish")
+ j = j + 1
+
+
##
# Conversion hub
#
[320, []],
[321, [convert_tablines]],
[322, []],
- [323, [convert_pagebreaks]]
+ [323, [convert_pagebreaks]],
+ [324, [convert_linebreaks]],
+ [325, [convert_japanese_plain]],
+ [326, []],
+ [327, []]
]
-revert = [[322, [revert_pagebreaks]],
+revert = [[326, [revert_mexican]],
+ [325, [revert_pdfpages]],
+ [324, []],
+ [323, [revert_linebreaks]],
+ [322, [revert_pagebreaks]],
[321, [revert_local_layout]],
[320, [revert_tablines]],
[319, [revert_protected_hfill]],
[313, []],
[312, [revert_module_names]],
[311, [revert_rotfloat, revert_widesideways]],
- [310, []],
+ [310, [revert_external_embedding]],
[309, [revert_btprintall]],
[308, [revert_nocite]],
[307, [revert_serbianlatin]],