Skip to content

Commit

Permalink
feat: abbreviate URLs
Browse files Browse the repository at this point in the history
It’s probably nicer not to use regexes for this.
  • Loading branch information
andylolz committed May 15, 2024
1 parent e376f6b commit d95e3c4
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions x_notes/notes.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import re
from datetime import datetime, timedelta, timezone
import re
from typing import Any

from .helpers import to_isoformat
from .tsv import get_generator

url_re = re.compile(r"(https?://[^\s]+)")
url_re = re.compile(r"(?P<domain>https?:\/\/[^\s/]+)(?:(?P<path>\/[^\s]{14})(?P<more_path>[^\s]*))?")
one_week_ago = (datetime.now(timezone.utc) - timedelta(days=7)).timestamp()


def urlize(inp: str) -> str:
return url_re.sub(r'<a target="_blank" href="\1">\1</a>', inp)
def format_url(match: re.Match) -> str:
tmpl = '<a target="_blank" href="{url}">{domain}{path}{abbrev}</a>'
return tmpl.format(
url=match.group(),
domain=match.group("domain"),
path=match.group("path") if match.group("path") else "",
abbrev="…" if match.group("more_path") else "",
)

return url_re.sub(lambda match: format_url(match), inp)


reasons_lookup = {
Expand Down

0 comments on commit d95e3c4

Please sign in to comment.