Skip to content

Commit

Permalink
sanitize file names
Browse files Browse the repository at this point in the history
  • Loading branch information
reinhrst committed Oct 31, 2023
1 parent 1e523db commit 50fcaf5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
Binary file added __pycache__/sanitize.cpython-311.pyc
Binary file not shown.
3 changes: 2 additions & 1 deletion bot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import logging
import os
from sanitize import sanitize

import discord
from pytz import timezone
Expand Down Expand Up @@ -39,7 +40,7 @@ async def on_ready():
directory = start_time.strftime("%Y/%m/%d")
start_time = start_time.strftime("%H:%M")
end_time = end_time.strftime("%H:%M")
filename = f"{date}-{event.name.replace('/', '').replace(':', '')}.md"
filename = f"{date}-{sanitize(event.name)}.md"
fields = f"""---
title: {json.dumps(event.name)}
tags: ["hs3"]
Expand Down
27 changes: 27 additions & 0 deletions sanitize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import re

safe = "abcdefghijklmnopqrstuvwxyz01234567890-_ .,"
tomap = ("éàèùçâêîôûëïüąćęłńóśźżříšžčýůňúěďáéäüößœ",
(*"eaeucaeioueiuacelnoszzriszcyunuedae",
"ae", "ue", "oe", "ss", "oe"))
map = {
**{c: c for c in safe + safe.upper()},
**{s: d for (s, d) in zip(*tomap)},
**{s.upper(): d.upper() for (s, d) in zip(*tomap)},
}


def sanitize(s):
return re.sub("_+", "_", "".join([map[c] if c in map else "_" for c in s]))


if __name__ == "__main__":
def test(s):
print(f"{s} --> {sanitize(s)}")
print(f"{s.upper()} --> {sanitize(s.upper())}")

test("Pójdźże, kiń tę chmurność w głąb flaszy!")
test("Příliš žluťoučký kůň úpěl ďábelské ódy")
test("Fix, Schwyz! quäkt Jürgen blöd vom Paß")
test("HellOO 😂🤣--😍🥰 there, you!.md")
test("Voix ambiguë d’un cœur qui au zéphyr préfère les jattes de kiwis")

0 comments on commit 50fcaf5

Please sign in to comment.