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

plots: special chars handling for vega plots paths #6900

Merged
merged 1 commit into from Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion dvc/render/base.py
Expand Up @@ -45,8 +45,17 @@ def SCRIPTS(self):
def as_json(self):
raise NotImplementedError

@staticmethod
def _remove_special_chars(string: str):
return string.translate(
{ord(c): "_" for c in r"!@#$%^&*()[]{};,<>?\/:.|`~=_+"}
)

def generate_html(self, path: "StrPath"):
"""this method might edit content of path"""
partial = self._convert(path)
div_id = f"plot_{self.filename.replace('.', '_').replace('/', '_')}"

div_id = self._remove_special_chars(self.filename)
div_id = f"plot_{div_id}"

return self.DIV.format(id=div_id, partial=partial)
9 changes: 9 additions & 0 deletions tests/unit/render/test_renderer.py
@@ -0,0 +1,9 @@
from dvc.render.base import Renderer


def test_remove_special_characters():
special_chars = r"!@#$%^&*()[]{};,<>?\/:.|`~=_+"
dirty = f"plot_name{special_chars}"
assert Renderer._remove_special_chars(dirty) == "plot_name" + "_" * len(
special_chars
)