Skip to content

Commit

Permalink
Test updated linter
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilyBourne committed Dec 12, 2024
1 parent 841c82d commit 8c0b354
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions ci_tools/markdown_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def index_to_line_info(idx, text_block):
if end_tag is None:
success = False
line_info = start_line_info
print(f"The {tag_names[key_idx]} at line {line_info.line}, position {line_info.char} does not have a matching closing tag.", file=sys.stderr)
print(f"::error file={file},line={line_info.line}:: The {tag_names[key_idx]} at line {line_info.line}, position {line_info.char} does not have a matching closing tag.", file=sys.stderr)
tags = [[]]
else:
expr = file_contents[found_start:end_tag.start()]
Expand All @@ -108,16 +108,16 @@ def index_to_line_info(idx, text_block):
end_code = '$'
if expr.contents[0] == ' ' or expr.contents[-1] == ' ':
success = False
print(f"Simple inline maths expressions should not start or end with a space. ({file}: Line {expr.start_pos.line}, position {expr.start_pos.char})", file=sys.stderr)
print(f"::error file={file},line={line_info.line}:: Simple inline maths expressions should not start or end with a space. ({file}: Line {expr.start_pos.line}, position {expr.start_pos.char})", file=sys.stderr)
suggestion = suggestion.strip()
if any(c in markdown_special_chars for c in expr.contents):
success = False
start_code = '$`'
end_code = '`$'
print(f"Simple inline maths expressions should not contain Markdown special characters {markdown_special_chars}. Please avoid these characters or use $` tags. ({file}: Line {expr.start_pos.line}, position {expr.start_pos.char})", file=sys.stderr)
print(f"::error file={file},line={line_info.line}:: Simple inline maths expressions should not contain Markdown special characters {markdown_special_chars}. Please avoid these characters or use $` tags. ({file}: Line {expr.start_pos.line}, position {expr.start_pos.char})", file=sys.stderr)
if expr.start_pos.line != expr.end_pos.line:
success = False
print(f"Inline maths expressions should be written in one line. ({file}: Line {expr.start_pos.line}-{expr.end_pos.line}, position {expr.start_pos.char})", file=sys.stderr)
print(f"::error file={file},line={line_info.line}:: Inline maths expressions should be written in one line. ({file}: Line {expr.start_pos.line}-{expr.end_pos.line}, position {expr.start_pos.char})", file=sys.stderr)
suggestion = suggestion.replace('\n','')

suggested_expressions.append(Expression(expr.start_pos, expr.end_pos, start_code+suggestion+end_code))
Expand All @@ -129,7 +129,7 @@ def index_to_line_info(idx, text_block):
end_code = '`$'
if expr.start_pos.line != expr.end_pos.line:
success = False
print(f"Inline maths expressions should be written in one line. ({file}: Line {expr.start_pos.line}-{expr.end_pos.line}, position {expr.start_pos.char})", file=sys.stderr)
print(f"::error file={file},line={line_info.line}:: Inline maths expressions should be written in one line. ({file}: Line {expr.start_pos.line}-{expr.end_pos.line}, position {expr.start_pos.char})", file=sys.stderr)
suggestion = suggestion.replace('\n','')

suggested_expressions.append(Expression(expr.start_pos, expr.end_pos, start_code+suggestion+end_code))
Expand All @@ -141,24 +141,24 @@ def index_to_line_info(idx, text_block):
end_code = '$$'
if expr.start_pos.line != expr.end_pos.line and (suggestion[0]!='\n' or suggestion[-1]!='\n'):
success = False
print(f"Muli-line maths expressions written with one line syntax. ({file}: Line {expr.start_pos.line}-{expr.end_pos.line}, position {expr.start_pos.char})", file=sys.stderr)
print(f"::error file={file},line={line_info.line}:: Muli-line maths expressions written with one line syntax. ({file}: Line {expr.start_pos.line}-{expr.end_pos.line}, position {expr.start_pos.char})", file=sys.stderr)
if suggestion[0]!='\n':
suggestion = '\n'+suggestion
if suggestion[-1]!='\n':
suggestion = suggestion+'\n'

if expr.start_pos.char != 2:
success = False
print(f"One-line maths expressions should be written in their own line and should start at the start of that line. ({file}: Line {expr.start_pos.line}, position {expr.start_pos.char})", file=sys.stderr)
print(f"::error file={file},line={line_info.line}:: One-line maths expressions should be written in their own line and should start at the start of that line. ({file}: Line {expr.start_pos.line}, position {expr.start_pos.char})", file=sys.stderr)

if expr.end_pos.char != len(content_lines[expr.end_pos.line-1]):
success = False
print(f"Trailing characters after a one-line maths expression. ({file}: Line {expr.start_pos.line}, position {expr.start_pos.char})", file=sys.stderr)
print(f"::error file={file},line={line_info.line}:: Trailing characters after a one-line maths expression. ({file}: Line {expr.start_pos.line}, position {expr.start_pos.char})", file=sys.stderr)

if (problematic_math_underscore.search(suggestion) or problematic_markdown_special_chars.search(suggestion)) and \
(suggestion[0]!='\n' or suggestion[-1]!='\n'):
success = False
print(f"GitLab error found. Please use a multi-line maths block. ({file}: Line {expr.start_pos.line}-{expr.end_pos.line}, position {expr.start_pos.char})", file=sys.stderr)
print(f"::error file={file},line={line_info.line}:: GitLab error found. Please use a multi-line maths block. ({file}: Line {expr.start_pos.line}-{expr.end_pos.line}, position {expr.start_pos.char})", file=sys.stderr)
if suggestion[0]!='\n':
suggestion = '\n'+suggestion
if suggestion[-1]!='\n':
Expand All @@ -173,7 +173,7 @@ def index_to_line_info(idx, text_block):
end_code = '`'
if expr.start_pos.line != expr.end_pos.line:
success = False
print(f"Inline code should be written in one line. ({file}: Line {expr.start_pos.line}-{expr.end_pos.line}, position {expr.start_pos.char})", file=sys.stderr)
print(f"::error file={file},line={line_info.line}:: Inline code should be written in one line. ({file}: Line {expr.start_pos.line}-{expr.end_pos.line}, position {expr.start_pos.char})", file=sys.stderr)
suggestion = suggestion.replace('\n','')

suggested_expressions.append(Expression(expr.start_pos, expr.end_pos, start_code+suggestion+end_code))
Expand All @@ -193,7 +193,7 @@ def index_to_line_info(idx, text_block):
line_info = LineInfo(expr.start_pos.line, expr.start_pos.char + internal_line_info.char-1)
else:
line_info = LineInfo(expr.start_pos.line + internal_line_info.line-1, internal_line_info.char)
print(f"Found non-escaped underscore. Please escape underscores and use *a* to indicate italics. ({file}: Line {line_info.line}, position {line_info.char})", file=sys.stderr)
print(f"::error file={file},line={line_info.line}:: Found non-escaped underscore. Please escape underscores and use *a* to indicate italics. ({file}: Line {line_info.line}, position {line_info.char})", file=sys.stderr)
suggestion = unquoted_underscore.sub(r'\_', suggestion)
suggested_expressions.append(Expression(expr.start_pos, expr.end_pos, '['+suggestion+expr.closing_tag))

Expand All @@ -209,14 +209,14 @@ def index_to_line_info(idx, text_block):
line_info = LineInfo(expr.start_pos.line, expr.start_pos.char + internal_line_info.char-1)
else:
line_info = LineInfo(expr.start_pos.line + internal_line_info.line-1, internal_line_info.char)
print(f"Found non-escaped underscore. Please escape underscores and use *a* to indicate italics. ({file}: Line {line_info.line}, position {line_info.char})", file=sys.stderr)
print(f"::error file={file},line={line_info.line}:: Found non-escaped underscore. Please escape underscores and use *a* to indicate italics. ({file}: Line {line_info.line}, position {line_info.char})", file=sys.stderr)
suggestion = unquoted_underscore.sub(r'\_', suggestion)
suggested_expressions.append(Expression(expr.start_pos, expr.end_pos, suggestion))

suggested_expressions.sort(key=lambda expr: (expr.start_pos.line, expr.start_pos.char))

if n_math_blocks > 50:
print(f"File contains {n_math_blocks} math blocks but GitLab will only render 50. Some equations may not show up correctly on GitLab. ({file})", file=sys.stderr)
print(f"::warning file={file},line={line_info.line}:: File contains {n_math_blocks} math blocks but GitLab will only render 50. Some equations may not show up correctly on GitLab. ({file})", file=sys.stderr)
warnings = True

if args.fix:
Expand All @@ -227,7 +227,3 @@ def index_to_line_info(idx, text_block):

if not success:
sys.exit(1)
elif warnings:
sys.exit(2)
else:
sys.exit(0)

0 comments on commit 8c0b354

Please sign in to comment.