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

poc: format event html descriptions #1283

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 5 additions & 2 deletions khal/khalendar/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from ..icalendar import cal_from_ics, delete_instance, invalid_timezone
from ..parse_datetime import timedelta2str
from ..terminal import get_color
from ..utils import generate_random_uid, is_aware, to_naive_utc, to_unix_time
from ..utils import format_text, generate_random_uid, is_aware, to_naive_utc, to_unix_time

logger = logging.getLogger('khal')

Expand Down Expand Up @@ -684,7 +684,10 @@ def format(self, format_string: str, relative_to, env=None, colors: bool=True):
attributes["alarm-symbol"] = self._alarm_str
attributes["title"] = self.summary
attributes["organizer"] = self.organizer.strip()
attributes["description"] = self.description.strip()
if bool(os.getenv("HTML_FORMATTED")):
attributes["description"] = format_text(self.description.strip())
else:
attributes["description"] = self.description.strip()
attributes["description-separator"] = ""
if attributes["description"]:
attributes["description-separator"] = " :: "
Expand Down
12 changes: 12 additions & 0 deletions khal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from typing import Iterator, List, Optional, Tuple

import pytz
from markdownify import markdownify as md


def generate_random_uid() -> str:
Expand Down Expand Up @@ -78,6 +79,17 @@ def find_unmatched_sgr(string: str) -> Optional[str]:
return None


def format_text(raw_data: str) -> str:
md_opts = {
"convert": ["br", "li", "a"],
"escape_asterisks": False,
"escape_underscores": False,
"wrap": True,
}
html_data = "\n".join(("<html>", raw_data, "</html>"))
return md(html_data, **md_opts)


def color_wrap(text: str, width: int = 70) -> List[str]:
"""A variant of wrap that takes SGR codes (somewhat) into account.

Expand Down