Skip to content

Commit

Permalink
error if the wiki article file name is included
Browse files Browse the repository at this point in the history
  • Loading branch information
Walavouchey committed Nov 30, 2023
1 parent 14b2c35 commit 1673a5b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions tests/test_articles/wiki/malformed_link/en.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
[Malformed link](//example.com)

[Erroneously includes article file name](/wiki/malformed_link/en.md)
2 changes: 1 addition & 1 deletion tests/test_articles/wiki/not_found/en.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

![][flag_AA] No flag here

[Error only when case-sensitive](/wiki/NOT_FOUND/en.md)
[Error only when case-sensitive](/wiki/NOT_FOUND)

[flag_AA]: /wiki/shared/flag/AA.gif
13 changes: 13 additions & 0 deletions tests/test_link_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ def test__invalid_absolute_link(self, root):
)
assert isinstance(error, error_types.LinkNotFoundError)

def test__invalid_link_with_filename(self, root):
utils.create_files(
root,
('wiki/Another_article/en.md', '# Another article')
)

link = link_parser.find_link("This link [shouldn't include the filename](/wiki/Another_article/en.md).")
assert link
error = link_checker.check_link(
article=dummy_article('does/not/matter'), link=link, redirects={}, references={}, all_articles={}
)
assert isinstance(error, error_types.MalformedLinkError)

def test__valid_reference(self, root):
utils.create_files(
root,
Expand Down
7 changes: 4 additions & 3 deletions wikitools/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ def pos(self):

class MalformedLinkError(
LinkError,
collections.namedtuple('MalformedLinkError', 'link')
collections.namedtuple('MalformedLinkError', 'link reason')
):
"""
An error indicating an erroneous link (for example, with several leading slashes).
An error indicating an erroneous link (for example, with several leading slashes, or including the wiki article file name).
"""

link: link_parser.Link
reason: str

def __repr__(self):
return f'Incorrect link structure (typo?): "{self.link.raw_location}"'
return f'"{self.link.raw_location}": {self.reason}'


class LinkNotFoundError(
Expand Down
6 changes: 5 additions & 1 deletion wikitools/link_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from wikitools import redirect_parser, reference_parser, errors, link_parser, article_parser
from wikitools import console
from wikitools.file_utils import exists_case_sensitive, exists_case_insensitive
from wikitools.file_utils import is_article


class PathType:
Expand Down Expand Up @@ -84,14 +85,17 @@ def get_repo_path(
if parsed_location.scheme:
return None

if is_article(parsed_location.path):
return errors.MalformedLinkError(link, "wiki links must not include the article file name")

if parsed_location.path.startswith("/"):
# absolute wiki link
path = pathlib.Path(parsed_location.path[1:])
return RepositoryPath(path_type=PathType.WIKI, path=path, fragment=parsed_location.fragment)

# domain is non-empty, but the link is internal?
if parsed_location.netloc:
return errors.MalformedLinkError(link)
return errors.MalformedLinkError(link, "incorrect link structure (typo?)")

# relative wiki link
path = current_article / parsed_location.path
Expand Down

0 comments on commit 1673a5b

Please sign in to comment.