gendoc: improve title line matching
This makes it possible to include tables etc without the script trying to turn them into titles.
This commit is contained in:
parent
3b82b2ce09
commit
87d90ab58a
1 changed files with 29 additions and 10 deletions
|
@ -53,20 +53,14 @@ Example:
|
||||||
"""
|
"""
|
||||||
def load_with_adjusted_titles(filename, file_stream, title_level, title_styles):
|
def load_with_adjusted_titles(filename, file_stream, title_level, title_styles):
|
||||||
rst_lines = []
|
rst_lines = []
|
||||||
title_chars = "".join(title_styles)
|
|
||||||
title_regex = re.compile("^[" + re.escape(title_chars) + "]{3,}$")
|
|
||||||
|
|
||||||
prev_line_title_level = 0 # We expect the file to start with '=' titles
|
prev_line_title_level = 0 # We expect the file to start with '=' titles
|
||||||
file_offset = None
|
file_offset = None
|
||||||
prev_non_title_line = None
|
prev_non_title_line = None
|
||||||
for i, line in enumerate(file_stream, 1):
|
for i, line in enumerate(file_stream):
|
||||||
# ignore anything which isn't a title (e.g. '===============')
|
if (prev_non_title_line is None
|
||||||
if not title_regex.match(line):
|
or not is_title_line(prev_non_title_line, line, title_styles)
|
||||||
rst_lines.append(line)
|
):
|
||||||
prev_non_title_line = line
|
|
||||||
continue
|
|
||||||
# The title underline must match at a minimum the length of the title
|
|
||||||
if len(prev_non_title_line) > len(line):
|
|
||||||
rst_lines.append(line)
|
rst_lines.append(line)
|
||||||
prev_non_title_line = line
|
prev_non_title_line = line
|
||||||
continue
|
continue
|
||||||
|
@ -130,6 +124,31 @@ def load_with_adjusted_titles(filename, file_stream, title_level, title_styles):
|
||||||
return "".join(rst_lines)
|
return "".join(rst_lines)
|
||||||
|
|
||||||
|
|
||||||
|
def is_title_line(prev_line, line, title_styles):
|
||||||
|
# The title underline must match at a minimum the length of the title
|
||||||
|
if len(prev_line) > len(line):
|
||||||
|
return False
|
||||||
|
|
||||||
|
line = line.rstrip()
|
||||||
|
|
||||||
|
# must be at least 3 chars long
|
||||||
|
if len(line) < 3:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# must start with a title char
|
||||||
|
title_char = line[0]
|
||||||
|
if title_char not in title_styles:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# all characters must be the same
|
||||||
|
for char in line[1:]:
|
||||||
|
if char != title_char:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# looks like a title line
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def get_rst(file_info, title_level, title_styles, spec_dir, adjust_titles):
|
def get_rst(file_info, title_level, title_styles, spec_dir, adjust_titles):
|
||||||
# string are file paths to RST blobs
|
# string are file paths to RST blobs
|
||||||
if isinstance(file_info, basestring):
|
if isinstance(file_info, basestring):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue