-
Notifications
You must be signed in to change notification settings - Fork 14
feat: Add support for embedded images #464
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
Draft
MoritzWeber0
wants to merge
1
commit into
master
Choose a base branch
from
support-embedded-images
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,9 @@ | |
|
||
import markupsafe | ||
import typing_extensions as te | ||
from lxml import etree | ||
|
||
import capellambse | ||
from capellambse import helpers | ||
|
||
from . import E, U | ||
|
@@ -77,7 +79,7 @@ def __get__(self, obj, objtype=None): | |
assert data is None or isinstance(data, str) | ||
if data is None: | ||
return self.default | ||
return self._from_xml(data) | ||
return self._from_xml(data, obj._model) | ||
|
||
def __set__(self, obj: t.Any, value: U | None) -> None: | ||
if not self.writable and self.attribute in obj._element.attrib: | ||
|
@@ -101,7 +103,9 @@ def __set_name__(self, owner: type[t.Any], name: str) -> None: | |
self.__objclass__ = owner | ||
|
||
@abc.abstractmethod | ||
def _from_xml(self, value: str, /) -> U: ... | ||
def _from_xml( | ||
self, value: str, model: capellambse.MelodyModel, / | ||
) -> U: ... | ||
@abc.abstractmethod | ||
def _to_xml(self, value: U, /) -> str | None: ... | ||
|
||
|
@@ -123,7 +127,8 @@ def __init__(self, attribute: str, /, *, writable: bool = True) -> None: | |
""" | ||
super().__init__(attribute, default="", writable=writable) | ||
|
||
def _from_xml(self, value: str, /) -> str: | ||
def _from_xml(self, value: str, model: capellambse.MelodyModel, /) -> str: | ||
del model | ||
return value | ||
|
||
def _to_xml(self, value: str, /) -> str: | ||
|
@@ -151,14 +156,38 @@ def __init__(self, attribute: str, /, *, writable: bool = True) -> None: | |
writable=writable, | ||
) | ||
|
||
def _from_xml(self, value: str, /) -> markupsafe.Markup: | ||
def _from_xml( | ||
self, | ||
value: str, | ||
model: capellambse.MelodyModel, | ||
/, | ||
) -> markupsafe.Markup: | ||
if os.getenv("CAPELLAMBSE_XHTML") == "1": | ||
value = helpers.repair_html(value) | ||
return markupsafe.Markup(value) | ||
|
||
return self._resolve_embedded_images(value, model) | ||
|
||
def _to_xml(self, value: markupsafe.Markup, /) -> str: | ||
return helpers.repair_html(value) | ||
|
||
def _resolve_embedded_images( | ||
self, value: str, model: capellambse.MelodyModel, / | ||
) -> markupsafe.Markup: | ||
Comment on lines
+173
to
+175
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, please use |
||
"""Resolve embedded images in HTML markup. | ||
|
||
Embedded images in Capella use the project name as prefix. Since | ||
the project name is not part of the real file system path, we | ||
remove the prefix from the image source attribute. | ||
""" | ||
|
||
def cb(node: etree._Element) -> None: | ||
if node.tag == "img" and model.info.title: | ||
node.attrib["src"] = node.attrib["src"].removeprefix( | ||
model.info.title + "/" | ||
) | ||
|
||
return helpers.process_html_fragments(value, cb) | ||
|
||
|
||
class BoolPOD(BasePOD[bool]): | ||
"""A POD containing a boolean.""" | ||
|
@@ -175,7 +204,8 @@ def __init__(self, attribute: str, /) -> None: | |
""" | ||
super().__init__(attribute, default=False, writable=True) | ||
|
||
def _from_xml(self, value: str, /) -> bool: | ||
def _from_xml(self, value: str, model: capellambse.MelodyModel, /) -> bool: | ||
del model | ||
return value == "true" | ||
|
||
def _to_xml(self, value: bool, /) -> str: | ||
|
@@ -200,7 +230,8 @@ def __init__(self, attribute: str, /, *, writable: bool = True) -> None: | |
""" | ||
super().__init__(attribute, default=0, writable=writable) | ||
|
||
def _from_xml(self, data: str, /) -> int: | ||
def _from_xml(self, data: str, model: capellambse.MelodyModel, /) -> int: | ||
del model | ||
return int(data) | ||
|
||
def _to_xml(self, value: int, /) -> str | None: | ||
|
@@ -232,7 +263,8 @@ def __init__(self, attribute: str, /, *, writable: bool = True) -> None: | |
""" | ||
super().__init__(attribute, default=0.0, writable=writable) | ||
|
||
def _from_xml(self, data: str, /) -> float: | ||
def _from_xml(self, data: str, model: capellambse.MelodyModel, /) -> float: | ||
del model | ||
return float(data) | ||
|
||
def _to_xml(self, value: float, /) -> str | None: | ||
|
@@ -278,7 +310,10 @@ def __init__(self, attribute: str, /, *, writable: bool = True) -> None: | |
""" | ||
super().__init__(attribute, default=None, writable=writable) | ||
|
||
def _from_xml(self, value: str, /) -> datetime.datetime: | ||
def _from_xml( | ||
self, value: str, model: capellambse.MelodyModel, / | ||
) -> datetime.datetime: | ||
del model | ||
formatted = self.re_get.sub(":", value) | ||
return datetime.datetime.fromisoformat(formatted) | ||
|
||
|
@@ -360,7 +395,8 @@ def __init__( | |
super().__init__(attribute, default=default, writable=writable) | ||
self.enumcls = enumcls | ||
|
||
def _from_xml(self, value: str, /) -> E: | ||
def _from_xml(self, value: str, model: capellambse.MelodyModel, /) -> E: | ||
del model | ||
return self.enumcls(value) | ||
|
||
def _to_xml(self, value: E | str, /) -> str | None: | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor thing: Please change the order to be
self, model, value
instead ofself, value, model
, for more consistency overall. (Almost) All functions that require a model expect it as first non-self argument.It's an API break anyways, as old methods only expect a single argument.