Skip to content

Commit

Permalink
plots: special chars handling for vega plots paths
Browse files Browse the repository at this point in the history
Fixes: #6894
  • Loading branch information
pared authored and efiop committed Nov 16, 2021
1 parent f99d3cd commit e0fd079
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
11 changes: 10 additions & 1 deletion dvc/render/base.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
)

0 comments on commit e0fd079

Please sign in to comment.