-
Notifications
You must be signed in to change notification settings - Fork 9
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
Add link_css option #70
Open
yves-chevallier
wants to merge
9
commits into
LukeCarrier:main
Choose a base branch
from
yves-chevallier:main
base: main
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.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a5be2da
Add link_css option
yves-chevallier cc3bc33
Allow attr_list link_css=1
yves-chevallier ab02f02
Add documentation to readme
yves-chevallier a0e9d04
Brit painful u
yves-chevallier 58f5058
Rename option, and fix readme
yves-chevallier 24028b0
Update README.md
yves-chevallier c800710
Change option name
yves-chevallier c0ab08f
Change option name
yves-chevallier 5a50bb8
Update README.md
yves-chevallier 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 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 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 |
---|---|---|
|
@@ -248,6 +248,34 @@ def validate_config(self, config: Configuration): | |
'embed_format', config['embed_format'], | ||
'cannot inline content of non-SVG format') | ||
|
||
def set_css_classes(self, svg_content, prefix="--drawio-color"): | ||
"""Rewrite SVG styles to use CSS variables. | ||
Will replace all `stroke` and `fill` attributes with CSS variables | ||
such as `var(--drawio-color-ffffff)`. | ||
|
||
:param str svg_content: SVG content. | ||
:param str prefix: CSS variable prefix. | ||
|
||
:return str: SVG content with styles rewritten. | ||
""" | ||
pattern = re.compile( | ||
(r'(fill|stroke)\s*=' | ||
r'\s*\"(\#[A-Fa-f0-9]+|' | ||
r'rgb\([0-9]{1,3}\s*,\s*[0-9]{1,3}\s*,\s*[0-9]{1,3}\))\"')) | ||
|
||
def apply_style(match): | ||
fill = match.group(2) | ||
if fill.startswith("#"): | ||
hexcolor = fill[1:] | ||
else: | ||
r, g, b = [ | ||
int(x) for x in | ||
re.match(r"rgb\((\d+),\s*(\d+),\s*(\d+)\)", fill).groups()] | ||
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. Likewise, I think we ought to write tests for this. 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. I'll do it. |
||
hexcolor = f"{r:02x}{g:02x}{b:02x}" | ||
return f'{match.group(1)}="{f"var({prefix}-{hexcolor})"}"' | ||
|
||
return pattern.sub(apply_style, svg_content) | ||
|
||
def rewrite_image_embeds(self, page_dest_path, output_content, config: Configuration): | ||
"""Rewrite image embeds. | ||
|
||
|
@@ -289,9 +317,12 @@ def replace(match): | |
self.log.error(f'Export failed with exit status {exit_status}; skipping rewrite') | ||
return match.group(0) | ||
|
||
with open(img_path, "r") as f: | ||
with open(img_path, "r", encoding="utf-8") as f: | ||
content = f.read() | ||
|
||
if config["use_theme_colors"] or not re.findall(r'use_theme_colors="(?:false|no|0)"', match.group(0)): | ||
content = self.set_css_classes(content) | ||
|
||
return config["embed_format"].format( | ||
img_open=match.group(1), img_close=match.group(3), | ||
img_src=img_src, content=content) | ||
|
This file contains 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
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.
We'll need tests for this regex. Are you happy to add these, or would you rather I took a look?
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.
Yes I can add some tests