diff --git a/src/cc2olx/dataclasses.py b/src/cc2olx/dataclasses.py new file mode 100644 index 0000000..c53870f --- /dev/null +++ b/src/cc2olx/dataclasses.py @@ -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) diff --git a/src/cc2olx/main.py b/src/cc2olx/main.py index 8eeda3b..cfe0210 100644 --- a/src/cc2olx/main.py +++ b/src/cc2olx/main.py @@ -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) diff --git a/src/cc2olx/models.py b/src/cc2olx/models.py index d134642..02dbed9 100644 --- a/src/cc2olx/models.py +++ b/src/cc2olx/models.py @@ -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 @@ -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 @@ -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 = ( '
' ''.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 = ( '' ''.format( diff --git a/src/cc2olx/olx.py b/src/cc2olx/olx.py index 12c4a6a..979f668 100644 --- a/src/cc2olx/olx.py +++ b/src/cc2olx/olx.py @@ -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. @@ -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 @@ -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