From 8fc2c1dd05e48c3a4230a0dad881186f52694a6e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Matox?= Date: Thu, 27 Jul 2006 18:30:13 +0000 Subject: [PATCH] * LyX.py * 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 --- lib/lyx2lyx/LyX.py | 13 ++++++- lib/lyx2lyx/lyx_1_1_5.py | 13 ++++++- lib/lyx2lyx/lyx_1_2.py | 75 +++++++++++++++++++++++++++++++++++-- lib/lyx2lyx/lyx_1_3.py | 13 ++++++- lib/lyx2lyx/lyx_1_4.py | 63 +++++++++++++++++++++++++++++-- lib/lyx2lyx/lyx_1_5.py | 12 +++++- lib/lyx2lyx/parser_tools.py | 68 --------------------------------- 7 files changed, 177 insertions(+), 80 deletions(-) diff --git a/lib/lyx2lyx/LyX.py b/lib/lyx2lyx/LyX.py index 1ee2fb0c06..6d9605f06a 100644 --- a/lib/lyx2lyx/LyX.py +++ b/lib/lyx2lyx/LyX.py @@ -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 diff --git a/lib/lyx2lyx/lyx_1_1_5.py b/lib/lyx2lyx/lyx_1_1_5.py index d8dc59006e..bb924dd167 100644 --- a/lib/lyx2lyx/lyx_1_1_5.py +++ b/lib/lyx2lyx/lyx_1_1_5.py @@ -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}"] diff --git a/lib/lyx2lyx/lyx_1_2.py b/lib/lyx2lyx/lyx_1_2.py index 3ac5d9cfb9..3d9cd8b912 100644 --- a/lib/lyx2lyx/lyx_1_2.py +++ b/lib/lyx2lyx/lyx_1_2.py @@ -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, " 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 # diff --git a/lib/lyx2lyx/lyx_1_5.py b/lib/lyx2lyx/lyx_1_5.py index 78f703a762..b2ed93ab47 100644 --- a/lib/lyx2lyx/lyx_1_5.py +++ b/lib/lyx2lyx/lyx_1_5.py @@ -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 # diff --git a/lib/lyx2lyx/parser_tools.py b/lib/lyx2lyx/parser_tools.py index 379f961bb3..e459cedd48 100644 --- a/lib/lyx2lyx/parser_tools.py +++ b/lib/lyx2lyx/parser_tools.py @@ -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, "