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

Markdown reloads when component classes change. #4185

Merged
merged 4 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Fixed

- Markdown component classes weren't refreshed when watching for CSS https://github.com/Textualize/textual/issues/3464

## [0.52.0] - 2023-02-19

### Changed
Expand Down
152 changes: 91 additions & 61 deletions src/textual/widgets/_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def __init__(self, markdown: Markdown, *args, **kwargs) -> None:
self._markdown: Markdown = markdown
"""A reference to the Markdown document that contains this block."""
self._text = Text()
self._token: Token | None = None
self._blocks: list[MarkdownBlock] = []
super().__init__(*args, **kwargs)

Expand All @@ -118,6 +119,95 @@ async def action_link(self, href: str) -> None:
"""Called on link click."""
self.post_message(Markdown.LinkClicked(self._markdown, href))

def notify_style_update(self) -> None:
"""If CSS was reloaded, try to rebuild this block from its token."""
super().notify_style_update()
self.rebuild()

def rebuild(self) -> None:
"""Rebuild the content of the block if we have a source token."""
if self._token is not None:
self.build_from_token(self._token)

def build_from_token(self, token: Token) -> None:
"""Build the block content from its source token.

This method allows the block to be rebuilt on demand, which is useful
when the styles assigned to the
[Markdown.COMPONENT_CLASSES][textual.widgets.Markdown.COMPONENT_CLASSES]
change.

See https://github.com/Textualize/textual/issues/3464 for more information.

Args:
token: The token from which this block is built.
"""

self._token = token
style_stack: list[Style] = [Style()]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

From style_stack: ... to the end of this method, I copied and pasted the code.
The only change happens when I'm getting the component classes.
Instead of getting them from self, I get them from self._markdown.

content = Text()
if token.children:
for child in token.children:
if child.type == "text":
content.append(child.content, style_stack[-1])
if child.type == "hardbreak":
content.append("\n")
if child.type == "softbreak":
content.append(" ", style_stack[-1])
elif child.type == "code_inline":
content.append(
child.content,
style_stack[-1]
+ self._markdown.get_component_rich_style(
"code_inline", partial=True
),
)
elif child.type == "em_open":
style_stack.append(
style_stack[-1]
+ self._markdown.get_component_rich_style("em", partial=True)
)
elif child.type == "strong_open":
style_stack.append(
style_stack[-1]
+ self._markdown.get_component_rich_style(
"strong", partial=True
)
)
elif child.type == "s_open":
style_stack.append(
style_stack[-1]
+ self._markdown.get_component_rich_style("s", partial=True)
)
elif child.type == "link_open":
href = child.attrs.get("href", "")
action = f"link({href!r})"
style_stack.append(
style_stack[-1] + Style.from_meta({"@click": action})
)
elif child.type == "image":
href = child.attrs.get("src", "")
alt = child.attrs.get("alt", "")

action = f"link({href!r})"
style_stack.append(
style_stack[-1] + Style.from_meta({"@click": action})
)

content.append("🖼 ", style_stack[-1])
if alt:
content.append(f"({alt})", style_stack[-1])
if child.children is not None:
for grandchild in child.children:
content.append(grandchild.content, style_stack[-1])

style_stack.pop()

elif child.type.endswith("_close"):
style_stack.pop()

self.set_content(content)


class MarkdownHeader(MarkdownBlock):
"""Base class for a Markdown header."""
Expand Down Expand Up @@ -833,67 +923,7 @@ def update(self, markdown: str) -> AwaitComplete:
else:
output.append(block)
elif token.type == "inline":
style_stack: list[Style] = [Style()]
content = Text()
if token.children:
for child in token.children:
if child.type == "text":
content.append(child.content, style_stack[-1])
if child.type == "hardbreak":
content.append("\n")
if child.type == "softbreak":
content.append(" ", style_stack[-1])
elif child.type == "code_inline":
content.append(
child.content,
style_stack[-1]
+ self.get_component_rich_style(
"code_inline", partial=True
),
)
elif child.type == "em_open":
style_stack.append(
style_stack[-1]
+ self.get_component_rich_style("em", partial=True)
)
elif child.type == "strong_open":
style_stack.append(
style_stack[-1]
+ self.get_component_rich_style("strong", partial=True)
)
elif child.type == "s_open":
style_stack.append(
style_stack[-1]
+ self.get_component_rich_style("s", partial=True)
)
elif child.type == "link_open":
href = child.attrs.get("href", "")
action = f"link({href!r})"
style_stack.append(
style_stack[-1] + Style.from_meta({"@click": action})
)
elif child.type == "image":
href = child.attrs.get("src", "")
alt = child.attrs.get("alt", "")

action = f"link({href!r})"
style_stack.append(
style_stack[-1] + Style.from_meta({"@click": action})
)

content.append("🖼 ", style_stack[-1])
if alt:
content.append(f"({alt})", style_stack[-1])
if child.children is not None:
for grandchild in child.children:
content.append(grandchild.content, style_stack[-1])

style_stack.pop()

elif child.type.endswith("_close"):
style_stack.pop()

stack[-1].set_content(content)
stack[-1].build_from_token(token)
elif token.type in ("fence", "code_block"):
(stack[-1]._blocks if stack else output).append(
MarkdownFence(self, token.content.rstrip(), token.info)
Expand Down
Loading
Loading