Open yves-chevallier opened 4 months ago
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 realize one could simplify the parsing if using bs4 and lxml:
What do you think?
I think the robustness gain is worth the additional dependencies, and I can see value in depending on an XML parser for other issues too (e.g. #9, #48). Less for us to test 🙌
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 GraphViewer
min.js.
I don't need any plugin on MkDocs, but just a js file. You can see it in action here. You may enable editable
to edit, copy the figure in place. The script is pretty small docs/js/drawio.js.
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 replacefill
andstroke
attributes with CSS variables.In this example, I used the following
mkdocs.yml
:The
style.css
is 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