|
26 | 26 | ThemeProvider = Union[Tagifiable, HTMLDependency, List[HTMLDependency]]
|
27 | 27 |
|
28 | 28 |
|
| 29 | +def mark_shiny_theme_stylesheets(dep: HTMLDependency, name: str = "") -> HTMLDependency: |
| 30 | + if not isinstance(dep, HTMLDependency): |
| 31 | + raise ValueError( |
| 32 | + "Expected an HTMLDependency, but received a value with type " |
| 33 | + f"{type(dep)}." |
| 34 | + ) |
| 35 | + |
| 36 | + if len(dep.stylesheet) > 0: |
| 37 | + for sheet in dep.stylesheet: |
| 38 | + sheet["data-shiny-theme"] = name # type: ignore |
| 39 | + |
| 40 | + return dep |
| 41 | + |
| 42 | + |
29 | 43 | def shiny_page_theme_deps(theme: str | Path | Theme | ThemeProvider | None) -> TagList:
|
30 | 44 | deps_bootstrap = bootstrap_deps(include_css=theme is None)
|
31 | 45 |
|
32 | 46 | if theme is None:
|
33 | 47 | deps_theme = None
|
34 | 48 | elif isinstance(theme, Theme):
|
35 |
| - deps_theme = theme._html_dependency() |
| 49 | + deps_theme = mark_shiny_theme_stylesheets( |
| 50 | + theme._html_dependency(), |
| 51 | + name=theme.name or theme._preset, |
| 52 | + ) |
36 | 53 | elif isinstance(theme, str) and theme.startswith(("http", "//")):
|
37 |
| - deps_theme = head_content(link(rel="stylesheet", href=theme, type="text/css")) |
| 54 | + theme_link = link( |
| 55 | + rel="stylesheet", href=theme, type="text/css", data_shiny_theme="" |
| 56 | + ) |
| 57 | + deps_theme = head_content(theme_link) |
38 | 58 | elif isinstance(theme, (str, Path)):
|
39 | 59 | check_path(theme)
|
40 |
| - deps_theme = head_content(include_css(theme)) |
41 |
| - elif isinstance(theme, Tagifiable) or isinstance(theme, HTMLDependency): |
42 |
| - deps_theme = theme |
| 60 | + theme_link = include_css(theme) |
| 61 | + theme_link.attrs["data-shiny-theme"] = "" |
| 62 | + deps_theme = head_content(theme_link) |
| 63 | + elif isinstance(theme, HTMLDependency): |
| 64 | + deps_theme = mark_shiny_theme_stylesheets(theme) |
43 | 65 | elif isinstance(theme, list) and all(
|
44 | 66 | [isinstance(dep, HTMLDependency) for dep in theme]
|
45 | 67 | ):
|
46 |
| - deps_theme = theme |
| 68 | + deps_theme = [mark_shiny_theme_stylesheets(t) for t in theme] |
| 69 | + elif isinstance(theme, Tagifiable): |
| 70 | + deps_theme = TagList(theme.tagify()) |
| 71 | + for dep in deps_theme.get_dependencies(): |
| 72 | + mark_shiny_theme_stylesheets(dep) |
47 | 73 | else:
|
48 | 74 | raise ValueError(
|
49 | 75 | "Invalid `theme`. "
|
|
0 commit comments