* lyx_1_1_5.py
	* lyx_1_2.py
	* lyx_1_3.py
	* lyx_1_4.py
	* lyx_1_5.py

	* parser_tools.py: remove functions that are not generic, i.e.
assume a specific pattern for the file format, and move them to the
places where they are used.



git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@14503 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
José Matox 2006-07-27 18:30:13 +00:00
parent 60fa713737
commit 8fc2c1dd05
7 changed files with 177 additions and 80 deletions

View File

@ -17,7 +17,7 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from parser_tools import get_value, check_token, find_token,\
find_tokens, find_end_of, find_end_of_inset
find_tokens, find_end_of
import os.path
import gzip
import sys
@ -30,6 +30,16 @@ version_lyx2lyx = lyx2lyx_version.version
default_debug_level = 2
####################################################################
# Private helper functions
def find_end_of_inset(lines, i):
return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
# End of helper functions
####################################################################
# Regular expressions used
format_re = re.compile(r"(\d)[\.,]?(\d\d)")
fileformat = re.compile(r"\\lyxformat\s*(\S*)")
@ -230,6 +240,7 @@ class LyX_Base:
except:
self.input = open(input)
else:
self.dir = ''
self.input = sys.stdin

View File

@ -18,8 +18,19 @@
import re
import string
from parser_tools import find_token, find_token_backwards, find_re, get_layout
from parser_tools import find_token, find_token_backwards, find_re
####################################################################
# Private helper functions
def get_layout(line, default_layout):
tokens = string.split(line)
if len(tokens) > 1:
return tokens[1]
return default_layout
####################################################################
math_env = ["\\[","\\begin{eqnarray*}","\\begin{eqnarray}","\\begin{equation}"]

View File

@ -20,10 +20,77 @@
import string
import re
from parser_tools import find_token, find_token_backwards, get_next_paragraph,\
find_tokens, find_end_of_inset, find_re, \
is_nonempty_line, get_paragraph, find_nonempty_line, \
get_value, get_tabular_lines, check_token, get_layout
from parser_tools import find_token, find_token_backwards, \
find_tokens, find_tokens_backwards, find_beginning_of, find_end_of, find_re, \
is_nonempty_line, find_nonempty_line, \
get_value, check_token
####################################################################
# Private helper functions
def get_layout(line, default_layout):
tokens = string.split(line)
if len(tokens) > 1:
return tokens[1]
return default_layout
# Finds the paragraph that contains line i.
def get_paragraph(lines, i, format):
begin_layout = "\\layout"
while i != -1:
i = find_tokens_backwards(lines, ["\\end_inset", begin_layout], i)
if i == -1: return -1
if check_token(lines[i], begin_layout):
return i
i = find_beginning_of_inset(lines, i)
return -1
# Finds the paragraph after the paragraph that contains line i.
def get_next_paragraph(lines, i, format):
tokens = ["\\begin_inset", "\\layout", "\\end_float", "\\the_end"]
while i != -1:
i = find_tokens(lines, tokens, i)
if not check_token(lines[i], "\\begin_inset"):
return i
i = find_end_of_inset(lines, i)
return -1
def find_beginning_of_inset(lines, i):
return find_beginning_of(lines, i, "\\begin_inset", "\\end_inset")
# Finds the matching \end_inset
def find_end_of_inset(lines, i):
return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
def find_end_of_tabular(lines, i):
return find_end_of(lines, i, "<lyxtabular", "</lyxtabular")
def get_tabular_lines(lines, i):
result = []
i = i+1
j = find_end_of_tabular(lines, i)
if j == -1:
return []
while i <= j:
if check_token(lines[i], "\\begin_inset"):
i = find_end_of_inset(lines, i)+1
else:
result.append(i)
i = i+1
return result
# End of helper functions
####################################################################
floats = {
"footnote": ["\\begin_inset Foot",

View File

@ -19,9 +19,20 @@
import string
import re
from parser_tools import find_token, find_end_of_inset, get_value,\
from parser_tools import find_token, find_end_of, get_value,\
find_token_exact, del_token
####################################################################
# Private helper functions
def find_end_of_inset(lines, i):
"Finds the matching \end_inset"
return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
# End of helper functions
####################################################################
def change_insetgraphics(file):
lines = file.body
i = 0

View File

@ -21,15 +21,70 @@
import re
from os import access, F_OK
import os.path
from parser_tools import find_token, find_end_of_inset, get_next_paragraph, \
get_paragraph, get_value, del_token, is_nonempty_line,\
find_tokens, find_end_of, find_token_exact, find_tokens_exact,\
find_re, get_layout
from parser_tools import check_token, find_token, \
get_value, del_token, is_nonempty_line, \
find_tokens, find_end_of, find_beginning_of, find_token_exact, find_tokens_exact, \
find_re, find_tokens_backwards
from sys import stdin
from string import replace, split, find, strip, join
from lyx_0_12 import update_latexaccents
####################################################################
# Private helper functions
def get_layout(line, default_layout):
tokens = split(line)
if len(tokens) > 1:
return tokens[1]
return default_layout
def get_paragraph(lines, i, format):
"Finds the paragraph that contains line i."
if format < 225:
begin_layout = "\\layout"
else:
begin_layout = "\\begin_layout"
while i != -1:
i = find_tokens_backwards(lines, ["\\end_inset", begin_layout], i)
if i == -1: return -1
if check_token(lines[i], begin_layout):
return i
i = find_beginning_of_inset(lines, i)
return -1
def find_beginning_of_inset(lines, i):
return find_beginning_of(lines, i, "\\begin_inset", "\\end_inset")
def get_next_paragraph(lines, i, format):
"Finds the paragraph after the paragraph that contains line i."
if format < 225:
tokens = ["\\begin_inset", "\\layout", "\\end_float", "\\the_end"]
elif format < 236:
tokens = ["\\begin_inset", "\\begin_layout", "\\end_float", "\\end_document"]
else:
tokens = ["\\begin_inset", "\\begin_layout", "\\end_float", "\\end_body", "\\end_document"]
while i != -1:
i = find_tokens(lines, tokens, i)
if not check_token(lines[i], "\\begin_inset"):
return i
i = find_end_of_inset(lines, i)
return -1
def find_end_of_inset(lines, i):
"Finds the matching \end_inset"
return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
# End of helper functions
####################################################################
##
# Remove \color default
#

View File

@ -18,10 +18,20 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import re
from parser_tools import find_token, find_token_exact, find_tokens, find_end_of_inset, get_value
from parser_tools import find_token, find_token_exact, find_tokens, find_end_of, get_value
from string import replace
####################################################################
# Private helper functions
def find_end_of_inset(lines, i):
return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
# End of helper functions
####################################################################
##
# Notes: Framed/Shaded
#

View File

@ -111,13 +111,6 @@ def get_value(lines, token, start, end = 0):
return ""
def get_layout(line, default_layout):
tokens = string.split(line)
if len(tokens) > 1:
return tokens[1]
return default_layout
def del_token(lines, token, start, end):
k = find_token_exact(lines, token, start, end)
if k == -1:
@ -127,37 +120,6 @@ def del_token(lines, token, start, end):
return end - 1
# Finds the paragraph that contains line i.
def get_paragraph(lines, i, format):
if format < 225:
begin_layout = "\\layout"
else:
begin_layout = "\\begin_layout"
while i != -1:
i = find_tokens_backwards(lines, ["\\end_inset", begin_layout], i)
if i == -1: return -1
if check_token(lines[i], begin_layout):
return i
i = find_beginning_of_inset(lines, i)
return -1
# Finds the paragraph after the paragraph that contains line i.
def get_next_paragraph(lines, i, format):
if format < 225:
tokens = ["\\begin_inset", "\\layout", "\\end_float", "\\the_end"]
elif format < 236:
tokens = ["\\begin_inset", "\\begin_layout", "\\end_float", "\\end_document"]
else:
tokens = ["\\begin_inset", "\\begin_layout", "\\end_float", "\\end_body", "\\end_document"]
while i != -1:
i = find_tokens(lines, tokens, i)
if not check_token(lines[i], "\\begin_inset"):
return i
i = find_end_of_inset(lines, i)
return -1
def find_end_of(lines, i, start_token, end_token):
count = 1
n = len(lines)
@ -186,36 +148,6 @@ def find_beginning_of(lines, i, start_token, end_token):
return -1
# Finds the matching \end_inset
def find_end_of_inset(lines, i):
return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
# Finds the matching \end_inset
def find_beginning_of_inset(lines, i):
return find_beginning_of(lines, i, "\\begin_inset", "\\end_inset")
def find_end_of_tabular(lines, i):
return find_end_of(lines, i, "<lyxtabular", "</lyxtabular")
def get_tabular_lines(lines, i):
result = []
i = i+1
j = find_end_of_tabular(lines, i)
if j == -1:
return []
while i <= j:
if check_token(lines[i], "\\begin_inset"):
i = find_end_of_inset(lines, i)+1
else:
result.append(i)
i = i+1
return result
def is_nonempty_line(line):
return line != " "*len(line)