Skip to content
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
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

yves-chevallier
Copy link

@yves-chevallier yves-chevallier commented Jul 3, 2024

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= or fill= attributes.

The idea is to leverage the embed_format: '{content}' option to replace fill and stroke attributes with CSS variables.

image

image

In this example, I used the following mkdocs.yml:

site_name: Draw Exporter with CSS
plugins:
  - drawio-exporter:
      embed_format: '{content}'
      link_css: true
theme:
  name: material
  palette:
    - scheme: default
      toggle:
        icon: material/toggle-switch-off-outline
        name: Switch to dark mode
    - scheme: slate
      toggle:
        icon: material/toggle-switch
        name: Switch to light mode

extra_css:
  - style.css

The style.css is the following, where the colours are those from the first set in draw.io:

:root {
    --drawio-color-ffffff: #000000;
    --drawio-color-f5f5f5: #e9b3b3;
    --drawio-color-dae8fc: #9bbbe7;
    --drawio-color-d5e8d4: #75c770;
    --drawio-color-ffe6cc: #cc9e6d;
    --drawio-color-fff2cc: #dabd66;
    --drawio-color-f8cecc: #b95c57;
    --drawio-color-e1d5e7: #8c49ad;
}

[data-md-color-scheme="slate"] {
    --drawio-color-ffffff: #ffffff;
    --drawio-color-f5f5f5: #f5f5f5;
    --drawio-color-dae8fc: #dae8fc;
    --drawio-color-d5e8d4: #d5e8d4;
    --drawio-color-ffe6cc: #ffe6cc;
    --drawio-color-fff2cc: #fff2cc;
    --drawio-color-f8cecc: #f8cecc;
    --drawio-color-e1d5e7: #e1d5e7;
}

And the index.md:

# Test

![](test.drawio)

Another option to add is to let the user choose which drawing requires local CSS. We could imagine later using attr_list

![](test.drawio)
![](test.drawio){ link_css=true }

Copy link
Owner

@LukeCarrier LukeCarrier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
Comment on lines +261 to +264
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}\))\"'))
Copy link
Owner

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?

Copy link
Author

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

else:
r, g, b = [
int(x) for x in
re.match(r"rgb\((\d+),\s*(\d+),\s*(\d+)\)", fill).groups()]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise, I think we ought to write tests for this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do it.

mkdocs_drawio_exporter/exporter.py Outdated Show resolved Hide resolved
mkdocs_drawio_exporter/plugin.py Outdated Show resolved Hide resolved
@LukeCarrier LukeCarrier added the enhancement New feature or request label Jul 12, 2024
yves-chevallier and others added 6 commits July 15, 2024 07:58
Co-authored-by: Luke Carrier <[email protected]>
Co-authored-by: Luke Carrier <[email protected]>
Co-authored-by: Luke Carrier <[email protected]>
Co-authored-by: Luke Carrier <[email protected]>
Co-authored-by: Luke Carrier <[email protected]>
@yves-chevallier
Copy link
Author

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?

@LukeCarrier
Copy link
Owner

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 🙌

@yves-chevallier
Copy link
Author

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.

@LukeCarrier
Copy link
Owner

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. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants