+def update_tabular(document):
+ " Convert tabular format 2 to 3."
+ regexp = re.compile(r'^\\begin_inset\s+Tabular')
+ lines = document.body
+ i = 0
+ while 1:
+ i = find_re(lines, regexp, i)
+ if i == -1:
+ break
+
+ for k in get_tabular_lines(lines, i):
+ if check_token(lines[k], "<lyxtabular"):
+ lines[k] = lines[k].replace('version="2"', 'version="3"')
+ elif check_token(lines[k], "<column"):
+ lines[k] = lines[k].replace('width=""', 'width="0pt"')
+
+ if line_re.match(lines[k]):
+ lines[k] = re.sub(attr_re, "", lines[k])
+
+ i = i+1
+
+
+##
+# Convert tabular format 2 to 3
+#
+# compatibility read for old longtable options. Now we can make any
+# row part of the header/footer type we want before it was strict
+# sequential from the first row down (as LaTeX does it!). So now when
+# we find a header/footer line we have to go up the rows and set it
+# on all preceding rows till the first or one with already a h/f option
+# set. If we find a firstheader on the same line as a header or a
+# lastfooter on the same line as a footer then this should be set empty.
+# (Jug 20011220)
+
+# just for compatibility with old python versions
+# python >= 2.3 has real booleans (False and True)
+false = 0
+true = 1
+
+class row:
+ " Simple data structure to deal with long table info."
+ def __init__(self):
+ self.endhead = false # header row
+ self.endfirsthead = false # first header row
+ self.endfoot = false # footer row
+ self.endlastfoot = false # last footer row
+
+
+def haveLTFoot(row_info):
+ " Does row has LTFoot?"
+ for row_ in row_info:
+ if row_.endfoot:
+ return true
+ return false
+
+
+def setHeaderFooterRows(hr, fhr, fr, lfr, rows_, row_info):
+ " Set Header/Footer rows."
+ endfirsthead_empty = false
+ endlastfoot_empty = false
+ # set header info
+ while (hr > 0):
+ hr = hr - 1
+ row_info[hr].endhead = true
+
+ # set firstheader info
+ if fhr and fhr < rows_:
+ if row_info[fhr].endhead:
+ while fhr > 0:
+ fhr = fhr - 1
+ row_info[fhr].endfirsthead = true
+ row_info[fhr].endhead = false
+ elif row_info[fhr - 1].endhead:
+ endfirsthead_empty = true
+ else:
+ while fhr > 0 and not row_info[fhr - 1].endhead:
+ fhr = fhr - 1
+ row_info[fhr].endfirsthead = true
+
+ # set footer info
+ if fr and fr < rows_:
+ if row_info[fr].endhead and row_info[fr - 1].endhead:
+ while fr > 0 and not row_info[fr - 1].endhead:
+ fr = fr - 1
+ row_info[fr].endfoot = true
+ row_info[fr].endhead = false
+ elif row_info[fr].endfirsthead and row_info[fr - 1].endfirsthead:
+ while fr > 0 and not row_info[fr - 1].endfirsthead:
+ fr = fr - 1
+ row_info[fr].endfoot = true
+ row_info[fr].endfirsthead = false
+ elif not row_info[fr - 1].endhead and not row_info[fr - 1].endfirsthead:
+ while fr > 0 and not row_info[fr - 1].endhead and not row_info[fr - 1].endfirsthead:
+ fr = fr - 1
+ row_info[fr].endfoot = true
+
+ # set lastfooter info
+ if lfr and lfr < rows_:
+ if row_info[lfr].endhead and row_info[lfr - 1].endhead:
+ while lfr > 0 and not row_info[lfr - 1].endhead:
+ lfr = lfr - 1
+ row_info[lfr].endlastfoot = true
+ row_info[lfr].endhead = false
+ elif row_info[lfr].endfirsthead and row_info[lfr - 1].endfirsthead:
+ while lfr > 0 and not row_info[lfr - 1].endfirsthead:
+ lfr = lfr - 1
+ row_info[lfr].endlastfoot = true
+ row_info[lfr].endfirsthead = false
+ elif row_info[lfr].endfoot and row_info[lfr - 1].endfoot:
+ while lfr > 0 and not row_info[lfr - 1].endfoot:
+ lfr = lfr - 1
+ row_info[lfr].endlastfoot = true
+ row_info[lfr].endfoot = false
+ elif not row_info[fr - 1].endhead and not row_info[fr - 1].endfirsthead and not row_info[fr - 1].endfoot:
+ while lfr > 0 and not row_info[lfr - 1].endhead and not row_info[lfr - 1].endfirsthead and not row_info[lfr - 1].endfoot:
+ lfr = lfr - 1
+ row_info[lfr].endlastfoot = true
+ elif haveLTFoot(row_info):
+ endlastfoot_empty = true
+
+ return endfirsthead_empty, endlastfoot_empty
+
+
+def insert_attribute(lines, i, attribute):
+ " Insert attribute in lines[i]."
+ last = lines[i].find('>')
+ lines[i] = lines[i][:last] + ' ' + attribute + lines[i][last:]
+
+
+rows_re = re.compile(r'rows="(\d*)"')
+longtable_re = re.compile(r'islongtable="(\w)"')
+ltvalues_re = re.compile(r'endhead="(-?\d*)" endfirsthead="(-?\d*)" endfoot="(-?\d*)" endlastfoot="(-?\d*)"')
+lt_features_re = re.compile(r'(endhead="-?\d*" endfirsthead="-?\d*" endfoot="-?\d*" endlastfoot="-?\d*")')
+def update_longtables(document):
+ " Update longtables to new format."
+ regexp = re.compile(r'^\\begin_inset\s+Tabular')
+ body = document.body