Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

developer-guide/02-projects/images: restructure "images" for relative links #121

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions readme-list
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ bootc-image-builder/main/README.md:docs/bootc/index.md
osbuild/main/README.md:docs/developer-guide/02-projects/osbuild/index.md
# Image definitions
images/main/README.md:docs/developer-guide/02-projects/images/index.md
images/main/docs/developer/README.md:docs/developer-guide/02-projects/images/developer.md
images/main/test/README.md:docs/developer-guide/02-projects/images/testing.md
images/main/docs/developer/cmds.md:docs/developer-guide/02-projects/images/cmds.md
images/main/docs/developer/code-manifest-generation.md:docs/developer-guide/02-projects/images/code-manifest-generation.md
images/main/docs/developer/README.md:docs/developer-guide/02-projects/images/docs/developer/README.md
images/main/test/README.md:docs/developer-guide/02-projects/images/test/README.md
images/main/docs/developer/cmds.md:docs/developer-guide/02-projects/images/docs/developer/cmds.md
images/main/docs/developer/code-manifest-generation.md:docs/developer-guide/02-projects/images/docs/developer/code-manifest-generation.md
# OSBuild composer
osbuild-composer/main/README.md:docs/developer-guide/02-projects/osbuild-composer/index.md
osbuild-composer/main/HACKING.md:docs/developer-guide/02-projects/osbuild-composer/HACKING.md
Expand Down
6 changes: 4 additions & 2 deletions scripts/pull_readmes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
-->
"""

RELATIVE_LINK_PATTERN = r'\[(.*?)\]\((.*?)\)'


def resolve_dirs(baseurl, relative_link):
"""
Resolve relative links to absolute links.
Expand Down Expand Up @@ -61,8 +64,7 @@ def parse_markdown_and_replace_links(file_path, baseurl):
with open(file_path, 'r', encoding='utf-8') as file:
markdown_content = file.read()

relative_link_pattern = r'\[(.*?)\]\((.*?)\)'
parsed_content = re.sub(relative_link_pattern,
parsed_content = re.sub(RELATIVE_LINK_PATTERN,
lambda match: replace_relative_links(match, baseurl),
markdown_content, flags=re.DOTALL)

Expand Down
45 changes: 39 additions & 6 deletions scripts/test_pull_readmes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import re
import unittest
from pull_readmes import resolve_dirs
from pull_readmes import resolve_dirs, replace_relative_links, RELATIVE_LINK_PATTERN


class TestResolveDirs(unittest.TestCase):
def setUp(self):
Expand All @@ -8,27 +10,58 @@ def setUp(self):
def test_resolve_dirs_no_change(self):
relative_link = "file.md"
absolute_link = resolve_dirs(self.baseurl, relative_link)
self.assertEqual(absolute_link, "https://github.com/osbuild/tree/main/docs/file.md")
self.assertEqual( "https://github.com/osbuild/tree/main/docs/file.md", absolute_link)

def test_resolve_dirs_starting_with_dot_slash(self):
relative_link = "./path/to/file.md"
absolute_link = resolve_dirs(self.baseurl, relative_link)
self.assertEqual(absolute_link, "https://github.com/osbuild/tree/main/docs/path/to/file.md")
self.assertEqual( "https://github.com/osbuild/tree/main/docs/path/to/file.md", absolute_link)

def test_resolve_dirs_starting_with_dot_dot_slash(self):
relative_link = "../path/to/file.md"
absolute_link = resolve_dirs(self.baseurl, relative_link)
self.assertEqual(absolute_link, "https://github.com/osbuild/tree/main/path/to/file.md")
self.assertEqual( "https://github.com/osbuild/tree/main/path/to/file.md", absolute_link)

def test_resolve_dirs_with_anchor(self):
relative_link = "path/to/file.md#section"
absolute_link = resolve_dirs(self.baseurl, relative_link)
self.assertEqual(absolute_link, "https://github.com/osbuild/tree/main/docs/path/to/file.md#section")
self.assertEqual( "https://github.com/osbuild/tree/main/docs/path/to/file.md#section", absolute_link)

def test_resolve_dirs_local_anchor(self):
relative_link = "#section"
absolute_link = resolve_dirs(self.baseurl, relative_link)
self.assertEqual(absolute_link, "#section")
self.assertEqual( "#section", absolute_link)

def test_replace_relative_links(self):

test_cases = [
{
'description': "Test Markdown relative link replacement",
'content': "[my link](../path/to/file.md)",
'expected': "[my link](../path/to/file.md)"
},
{
'description': "Test HTTP absolute link remains unchanged",
'content': "[my link](http://example.com/path/to/)",
'expected': "[my link](http://example.com/path/to/)"
},
{
'description': "Test folder relative link replacement",
'content': "[my link](./some/path/to/)",
'expected': "[my link](https://github.com/osbuild/tree/main/docs/some/path/to/)"
}
]

for case in test_cases:
with self.subTest(case['description']):
absolute_link = re.sub(
RELATIVE_LINK_PATTERN,
lambda match: replace_relative_links(match, self.baseurl),
case['content'],
flags=re.DOTALL
)

self.assertEqual(case['expected'], absolute_link)


if __name__ == '__main__':
Expand Down
Loading