def replace_protected_separator(document): " Replace protected separator. " lines = document.body i=0 while 1: i = find_token(lines, "\\protected_separator", i) if i == -1: break j = find_token_backwards(lines, "\\layout", i) #if j == -1: print error layout = get_layout(lines[j], document.default_layout) if layout == "LyX-Code": result = "" while lines[i] == "\\protected_separator ": result = result + " " del lines[i] lines[i-1] = lines[i-1] + result + lines[i] else: lines[i-1] = lines[i-1]+ "\\SpecialChar ~" del lines[i]
def replace_protected_separator(document): " Replace protected separator. " lines = document.body i = 0 while True: i = find_token(lines, "\\protected_separator", i) if i == -1: break j = find_token_backwards(lines, "\\layout", i) #if j == -1: print error layout = get_layout(lines[j], document.default_layout) if layout == "LyX-Code": result = "" while lines[i] == "\\protected_separator ": result = result + " " del lines[i] lines[i - 1] = lines[i - 1] + result + lines[i] else: lines[i - 1] = lines[i - 1] + "\\SpecialChar ~" del lines[i]
def convert_separator(document): """ Convert layout separators to separator insets and add (LaTeX) paragraph breaks in order to mimic previous LaTeX export. """ parins = ["\\begin_inset Separator parbreak", "\\end_inset", ""] parlay = ["\\begin_layout Standard", "\\begin_inset Separator parbreak", "\\end_inset", "", "\\end_layout", ""] sty_dict = { "family" : "default", "series" : "default", "shape" : "default", "size" : "default", "bar" : "default", "color" : "inherit" } i = 0 while 1: i = find_token(document.body, "\\begin_deeper", i) if i == -1: break j = find_token_backwards(document.body, "\\end_layout", i-1) if j != -1: # reset any text style before inserting the inset lay = get_containing_layout(document.body, j-1) if lay != False: content = "\n".join(document.body[lay[1]:lay[2]]) for val in sty_dict.keys(): if content.find("\\%s" % val) != -1: document.body[j:j] = ["\\%s %s" % (val, sty_dict[val])] i = i + 1 j = j + 1 document.body[j:j] = parins i = i + len(parins) + 1 else: i = i + 1 i = 0 while 1: i = find_token(document.body, "\\align", i) if i == -1: break lay = get_containing_layout(document.body, i) if lay != False and lay[0] == "Plain Layout": i = i + 1 continue j = find_token_backwards(document.body, "\\end_layout", i-1) if j != -1: lay = get_containing_layout(document.body, j-1) if lay != False and lay[0] == "Standard" \ and find_token(document.body, "\\align", lay[1], lay[2]) == -1 \ and find_token(document.body, "\\begin_inset VSpace", lay[1], lay[2]) == -1: # reset any text style before inserting the inset content = "\n".join(document.body[lay[1]:lay[2]]) for val in sty_dict.keys(): if content.find("\\%s" % val) != -1: document.body[j:j] = ["\\%s %s" % (val, sty_dict[val])] i = i + 1 j = j + 1 document.body[j:j] = parins i = i + len(parins) + 1 else: i = i + 1 else: i = i + 1 regexp = re.compile(r'^\\begin_layout (?:(-*)|(\s*))(Separator|EndOfSlide)(?:(-*)|(\s*))$', re.IGNORECASE) i = 0 while 1: i = find_re(document.body, regexp, i) if i == -1: return j = find_end_of_layout(document.body, i) if j == -1: document.warning("Malformed LyX document: Missing `\\end_layout'.") return lay = get_containing_layout(document.body, j-1) if lay != False: lines = document.body[lay[3]:lay[2]] else: lines = [] document.body[i:j+1] = parlay if len(lines) > 0: document.body[i+1:i+1] = lines i = i + len(parlay) + len(lines) + 1
def convert_separator(document): """ Convert layout separators to separator insets and add (LaTeX) paragraph breaks in order to mimic previous LaTeX export. """ parins = ["\\begin_inset Separator parbreak", "\\end_inset", ""] parlay = [ "\\begin_layout Standard", "\\begin_inset Separator parbreak", "\\end_inset", "", "\\end_layout", "" ] sty_dict = { "family": "default", "series": "default", "shape": "default", "size": "default", "bar": "default", "color": "inherit" } i = 0 while 1: i = find_token(document.body, "\\begin_deeper", i) if i == -1: break j = find_token_backwards(document.body, "\\end_layout", i - 1) if j != -1: # reset any text style before inserting the inset lay = get_containing_layout(document.body, j - 1) if lay != False: content = "\n".join(document.body[lay[1]:lay[2]]) for val in sty_dict.keys(): if content.find("\\%s" % val) != -1: document.body[j:j] = ["\\%s %s" % (val, sty_dict[val])] i = i + 1 j = j + 1 document.body[j:j] = parins i = i + len(parins) + 1 else: i = i + 1 i = 0 while 1: i = find_token(document.body, "\\align", i) if i == -1: break lay = get_containing_layout(document.body, i) if lay != False and lay[0] == "Plain Layout": i = i + 1 continue j = find_token_backwards(document.body, "\\end_layout", i - 1) if j != -1: lay = get_containing_layout(document.body, j - 1) if lay != False and lay[0] == "Standard" \ and find_token(document.body, "\\align", lay[1], lay[2]) == -1 \ and find_token(document.body, "\\begin_inset VSpace", lay[1], lay[2]) == -1: # reset any text style before inserting the inset content = "\n".join(document.body[lay[1]:lay[2]]) for val in sty_dict.keys(): if content.find("\\%s" % val) != -1: document.body[j:j] = ["\\%s %s" % (val, sty_dict[val])] i = i + 1 j = j + 1 document.body[j:j] = parins i = i + len(parins) + 1 else: i = i + 1 else: i = i + 1 regexp = re.compile( r'^\\begin_layout (?:(-*)|(\s*))(Separator|EndOfSlide)(?:(-*)|(\s*))$', re.IGNORECASE) i = 0 while 1: i = find_re(document.body, regexp, i) if i == -1: return j = find_end_of_layout(document.body, i) if j == -1: document.warning("Malformed LyX document: Missing `\\end_layout'.") return lay = get_containing_layout(document.body, j - 1) if lay != False: lines = document.body[lay[3]:lay[2]] else: lines = [] document.body[i:j + 1] = parlay if len(lines) > 0: document.body[i + 1:i + 1] = lines i = i + len(parlay) + len(lines) + 1
def remove_pextra(document): " Remove pextra token." lines = document.body i = 0 flag = 0 while 1: i = find_re(lines, pextra_type2_rexp, i) if i == -1: break # Sometimes the \pextra_widthp argument comes in it own # line. If that happens insert it back in this line. if pextra_widthp.search(lines[i+1]): lines[i] = lines[i] + ' ' + lines[i+1] del lines[i+1] mo = pextra_rexp.search(lines[i]) width = get_width(mo) if mo.group(1) == "1": # handle \pextra_type 1 (indented paragraph) lines[i] = re.sub(pextra_rexp, "\\leftindent "+width+" ", lines[i]) i = i+1 continue # handle \pextra_type 2 (minipage) position = mo.group(3) hfill = mo.group(5) lines[i] = re.sub(pextra_rexp, "", lines[i]) start = ["\\begin_inset Minipage", "position " + position, "inner_position 0", 'height "0pt"', 'width "%s"' % width, "collapsed false" ] if flag: flag = 0 if hfill: start = ["","\hfill",""]+start else: start = ['\\layout %s' % document.default_layout,''] + start j0 = find_token_backwards(lines,"\\layout", i-1) j = get_next_paragraph(lines, i, document.format + 1) count = 0 while 1: # collect more paragraphs to the minipage count = count+1 if j == -1 or not check_token(lines[j], "\\layout"): break i = find_re(lines, pextra_type2_rexp2, j+1) if i == -1: break mo = pextra_rexp.search(lines[i]) if not mo: break if mo.group(7) == "1": flag = 1 break lines[i] = re.sub(pextra_rexp, "", lines[i]) j = find_tokens(lines, ["\\layout", "\\end_float"], i+1) mid = lines[j0:j] end = ["\\end_inset "] lines[j0:j] = start+mid+end i = i+1
def remove_pextra(document): " Remove pextra token." lines = document.body i = 0 flag = 0 while 1: i = find_re(lines, pextra_type2_rexp, i) if i == -1: break # Sometimes the \pextra_widthp argument comes in it own # line. If that happens insert it back in this line. if pextra_widthp.search(lines[i + 1]): lines[i] = lines[i] + ' ' + lines[i + 1] del lines[i + 1] mo = pextra_rexp.search(lines[i]) width = get_width(mo) if mo.group(1) == "1": # handle \pextra_type 1 (indented paragraph) lines[i] = re.sub(pextra_rexp, "\\leftindent " + width + " ", lines[i]) i = i + 1 continue # handle \pextra_type 2 (minipage) position = mo.group(3) hfill = mo.group(5) lines[i] = re.sub(pextra_rexp, "", lines[i]) start = [ "\\begin_inset Minipage", "position " + position, "inner_position 0", 'height "0pt"', 'width "%s"' % width, "collapsed false" ] if flag: flag = 0 if hfill: start = ["", "\hfill", ""] + start else: start = ['\\layout %s' % document.default_layout, ''] + start j0 = find_token_backwards(lines, "\\layout", i - 1) j = get_next_paragraph(lines, i, document.format + 1) count = 0 while 1: # collect more paragraphs to the minipage count = count + 1 if j == -1 or not check_token(lines[j], "\\layout"): break i = find_re(lines, pextra_type2_rexp2, j + 1) if i == -1: break mo = pextra_rexp.search(lines[i]) if not mo: break if mo.group(7) == "1": flag = 1 break lines[i] = re.sub(pextra_rexp, "", lines[i]) j = find_tokens(lines, ["\\layout", "\\end_float"], i + 1) mid = lines[j0:j] end = ["\\end_inset "] lines[j0:j] = start + mid + end i = i + 1