On Thursday 27 July 2006 12:40, Jose' Matos wrote: > On Thursday 27 July 2006 09:48, Jose' Matos wrote: > > I will move the functions used from parser_tools.py to the > > corresponding files. This will allow us to avoid the dreadful > > alternatives scenarios where we have to consider all possible syntaxes. > > Just to be clear, I intend to move those function that are not generic. > The list is: > > get_layout > get_paragraph > get_next_paragraph > find_begin_of_inset > find_end_of_inset > find_end_of_tabular > get_tabular_lines
I attach a patch that does exactly this. Unless someone objects this will go in. The plan next is to specialise some of these functions. It does not make any sense for versions newer than 1.2 to see if the layout is empty, and so on. I have run pylint over those files and there are some warnings that deserve a second look. Like the case where we override file, a python built-in. -- José Abílio
Index: lyx_1_1_5.py =================================================================== --- lyx_1_1_5.py (revision 14498) +++ lyx_1_1_5.py (working copy) @@ -18,9 +18,20 @@ 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}"] def replace_protected_separator(file): Index: parser_tools.py =================================================================== --- parser_tools.py (revision 14499) +++ parser_tools.py (working copy) @@ -111,13 +111,6 @@ 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,52 +120,6 @@ 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) - while i < n: - i = find_tokens(lines, [end_token, start_token], i+1) - if check_token(lines[i], start_token): - count = count+1 - else: - count = count-1 - if count == 0: - return i - return -1 - - -# Finds the matching \end_inset def find_beginning_of(lines, i, start_token, end_token): count = 1 while i > 0: @@ -186,36 +133,6 @@ 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) Index: LyX.py =================================================================== --- LyX.py (revision 14498) +++ LyX.py (working copy) @@ -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 @@ 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*)") Index: lyx_1_2.py =================================================================== --- lyx_1_2.py (revision 14498) +++ lyx_1_2.py (working copy) @@ -20,11 +20,74 @@ 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_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 + + +# 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", "collapsed true"], Index: lyx_1_3.py =================================================================== --- lyx_1_3.py (revision 14498) +++ lyx_1_3.py (working copy) @@ -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 Index: lyx_1_4.py =================================================================== --- lyx_1_4.py (revision 14498) +++ lyx_1_4.py (working copy) @@ -21,15 +21,67 @@ 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,\ +from parser_tools import find_token, find_end_of,\ + get_value, del_token, is_nonempty_line,\ find_tokens, find_end_of, find_token_exact, find_tokens_exact,\ - find_re, get_layout + find_re 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 = string.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 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 # Index: lyx_1_5.py =================================================================== --- lyx_1_5.py (revision 14498) +++ lyx_1_5.py (working copy) @@ -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 #