Skip to content

Commit

Permalink
Supporting column alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenburns committed May 31, 2023
1 parent fe7fc30 commit 53633c8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Added support for tables in `Markdown` https://github.com/Textualize/rich/pull/2977



## [13.3.5] - 2023-04-27

### Fixed
Expand Down
26 changes: 24 additions & 2 deletions rich/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from markdown_it import MarkdownIt
from markdown_it.token import Token
from typing_extensions import get_args

from rich.table import Table

Expand Down Expand Up @@ -226,6 +227,8 @@ def __rich_console__(


class TableElement(MarkdownElement):
"""MarkdownElement corresponding to `table_open`."""

def __init__(self) -> None:
self.header: TableHeaderElement | None = None
self.body: TableBodyElement | None = None
Expand Down Expand Up @@ -305,11 +308,30 @@ class TableDataElement(MarkdownElement):
"""MarkdownElement corresponding to `td_open` and `td_close`
and `th_open` and `th_close`."""

def __init__(self) -> None:
@classmethod
def create(cls, markdown: "Markdown", token: Token) -> "MarkdownElement":
style = str(token.attrs.get("style" "")) or ""

justify: JustifyMethod
if "text-align:right" in style:
justify = "right"
elif "text-align:center" in style:
justify = "center"
elif "text-align:left" in style:
justify = "left"
else:
justify = "default"

assert justify in get_args(JustifyMethod)
return cls(justify=justify)

def __init__(self, justify: JustifyMethod) -> None:
self.content: TextType = ""
self.justify = justify

def on_text(self, context: "MarkdownContext", text: TextType) -> None:
self.content = text
plain = text.plain if isinstance(text, Text) else text
self.content = Text(plain, justify=self.justify)


class ListElement(MarkdownElement):
Expand Down

0 comments on commit 53633c8

Please sign in to comment.