Skip to content

Commit

Permalink
fix: All static files are considered during relative external links p…
Browse files Browse the repository at this point in the history
…rocessing
  • Loading branch information
myhailo-chernyshov-rg committed Dec 19, 2024
1 parent 751e8ed commit 9da9855
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
18 changes: 18 additions & 0 deletions src/cc2olx/dataclasses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from dataclasses import dataclass, field
from collections import ChainMap
from typing import Dict


@dataclass
class OlxToOriginalStaticFilePaths:
"""
Provide OLX static file to Common cartridge static file mappings.
"""

# Static files from `web_resources` directory
web_resources: Dict[str, str] = field(default_factory=dict)
# Static files that are outside of `web_resources` directory, but still required
extra: Dict[str, str] = field(default_factory=dict)

def __post_init__(self) -> None:
self.all = ChainMap(self.extra, self.web_resources)
4 changes: 2 additions & 2 deletions src/cc2olx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def convert_one_file(

# Add static files that are outside of web_resources directory
file_list += [
(str(cartridge.directory / filepath), "/static/{}".format(filepath))
for filepath in cartridge.extra_static_files
(str(cartridge.directory / original_filepath), olx_static_path)
for olx_static_path, original_filepath in cartridge.olx_to_original_static_file_paths.extra.items()
]

filesystem.add_in_tar_gz(str(tgz_filename), file_list)
Expand Down
9 changes: 5 additions & 4 deletions src/cc2olx/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from cc2olx import filesystem
from cc2olx.constants import OLX_STATIC_PATH_TEMPLATE
from cc2olx.dataclasses import OlxToOriginalStaticFilePaths
from cc2olx.external.canvas import ModuleMeta
from cc2olx.qti import QtiParser
from cc2olx.utils import clean_file_name
Expand Down Expand Up @@ -70,8 +71,7 @@ def __init__(self, cartridge_file, workspace):
self.is_canvas_flavor = False
self.module_meta = {}

# List of static files that are outside of `web_resources` directory, but still required
self.extra_static_files = []
self.olx_to_original_static_file_paths = OlxToOriginalStaticFilePaths()

self.workspace = workspace

Expand Down Expand Up @@ -329,16 +329,17 @@ def get_resource_content(self, identifier):
elif "web_resources" in str(res_filename) and imghdr.what(str(res_filename)):
static_filename = str(res_filename).split("web_resources/")[1]
olx_static_path = OLX_STATIC_PATH_TEMPLATE.format(static_filename=static_filename)
self.olx_to_original_static_file_paths.web_resources[olx_static_path] = static_filename
html = (
'<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>'
'</head><body><p><img src="{}" alt="{}"></p></body></html>'.format(olx_static_path, static_filename)
)
return "html", {"html": html}
elif "web_resources" not in str(res_filename):
olx_static_path = OLX_STATIC_PATH_TEMPLATE.format(static_filename=res_relative_path)
# This webcontent is outside of ``web_resources`` directory
# So we need to manually copy it to OLX_STATIC_DIR
self.extra_static_files.append(res_relative_path)
olx_static_path = OLX_STATIC_PATH_TEMPLATE.format(static_filename=res_relative_path)
self.olx_to_original_static_file_paths.extra[olx_static_path] = res_relative_path
html = (
'<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>'
'</head><body><p><a href="{}" alt="{}">{}<a></p></body></html>'.format(
Expand Down
10 changes: 3 additions & 7 deletions src/cc2olx/olx.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def process_external_tools_link(item, html):
html = html.replace(item, external_tool_url)
return html

def process_extra_static_files(item, html):
def process_relative_external_links(item, html):
"""
Turn static file URLs outside OLX_STATIC_DIR into absolute URLs.
Expand All @@ -262,13 +262,9 @@ def process_extra_static_files(item, html):
course. The function adds the origin source to URLs to make them
absolute ones.
"""
if self.relative_links_source is None:
if self.relative_links_source is None or item in self.cartridge.olx_to_original_static_file_paths.all:
return html

for static_file in self.cartridge.extra_static_files:
if item == OLX_STATIC_PATH_TEMPLATE.format(static_filename=static_file):
return html

url = urllib.parse.urljoin(self.relative_links_source, item)
html = html.replace(item, url)
return html
Expand All @@ -283,7 +279,7 @@ def process_extra_static_files(item, html):
elif "CANVAS_OBJECT_REFERENCE" in item:
html = process_canvas_reference(item, html)
else:
html = process_extra_static_files(item, html)
html = process_relative_external_links(item, html)

return html

Expand Down

0 comments on commit 9da9855

Please sign in to comment.