Conversation
There was a problem hiding this comment.
Hey @yves-chevallier, I love this idea, thanks for the thoughtful implementation!
I think we ought to add tests as there's some reasonably complex regular expressions here, but I'm happy to take this on if you'd rather.
| 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}\))\"')) |
There was a problem hiding this comment.
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.
Yes I can add some tests
| 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.
Likewise, I think we ought to write tests for this.
Co-authored-by: Luke Carrier <luke@carrier.family>
Co-authored-by: Luke Carrier <luke@carrier.family>
Co-authored-by: Luke Carrier <luke@carrier.family>
Co-authored-by: Luke Carrier <luke@carrier.family>
Co-authored-by: Luke Carrier <luke@carrier.family>
Co-authored-by: Luke Carrier <luke@carrier.family>
|
I realize one could simplify the parsing if using bs4 and lxml: def set_css_classes(self, svg_content, prefix="--drawio-color"):
def color2hex(string):
if m := re.match(r'^#([0-9a-fA-F]{6})$', string):
return m.group(1)
if m := re.match('^rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$'):
return ''.join([f"{int(x):02x}" for x in m.groups()])
return None
tags = ['fill', 'stroke']
soup = BeautifulSoup(svg_content, 'xml')
elements = soup.select(','.join([f'[{tag}]' for tag in tags]))
for element in elements:
for attr in tags:
hexcolor = color2hex(element.get(attr))
if not hexcolor:
self.log.error(f'Failed to parse color: {element.get(attr)}')
element[attr] = f'var({prefix}-{hexcolor})'
return str(soup)It would certainly be more robust than complex regex, but it adds dependencies. I think bs4 is used by all mkdocs themes anyway. What do you think? |
|
I am changing my mind. I found a more elegant way to render drawio figures in browser. I am rendering figures directly in the browser using I don't need any plugin on MkDocs, but just a js file. You can see it in action here. You may enable |
|
That’s okay, glad you find a workable solution. Thanks for the work on this though; I’ll leave the PR open as I’d like to get this feature integrated when I next work on this. 🙂 |
I was disappointed to find that the colors in Draw.io do not match the MkDocs theme. Unfortunately, Draw.io does not support applying CSS styles directly; instead, it uses a set of predefined colors. These colors are applied to SVG elements using the
stroke=orfill=attributes.The idea is to leverage the
embed_format: '{content}'option to replacefillandstrokeattributes with CSS variables.In this example, I used the following
mkdocs.yml:The
style.cssis the following, where the colours are those from the first set in draw.io:And the
index.md:Another option to add is to let the user choose which drawing requires local CSS. We could imagine later using
attr_list