Регулярно требуется преобразовать какой-либо текст в максимально совместимый текст для URL, имени файла, имени объекта в каком-то софте и тд. Требования совместимости простые: в тексте должны быть только допустимые символы. Обычно это a-z, 0-9 и "_" или "-". То есть, только прописные буквы латинского алфавита и цифры (как пример).
Допустим, нам нужно название статьи в блоге преобразовать в slug для добавления его в URL этой статьи. Как это лучше всего сделать?
В Django по умолчанию есть готовая функция slugify для таких случаев.
Но я её никогда не использую. Почему? Потому что её недостаточно!
Приведём пример
>>> from django.utils.text import slugify
>>> slugify('This is a Title')
'this-is-a-title'
Пока всё отлично
>>> slugify('This is a "Title!"')
'this-is-a-title'
Спец символы удалились, всё хорошо.
>>> slugify('Это заголовок статьи')
''
Вот и приехали 😢. Если текст не английский то буквы просто игнорируются. Можно это поправить
>>> slugify('Это заголовок статьи', allow_unicode=True)
'это-заголовок-статьи'
Но тогда мы не вписываемся в условие. У нас появилась кириллица в тексте.
Так как я часто пишу сайты для русскоязычных пользователей эта проблема весьма актуальна. Я не использую стандартную функцию и всегда пишу свою.
Оригинал я не беру в расчёт и пишу полностью свою функцию. И так, по порядку:
🔸1. Исходный текст:
>>> text = 'Мой заголовок №10 😁!'
Взял специально посложней со специальными символами.
🔸2. Транслит
Необходимо сделать транслит всех символов в латиницу. Здесь очень выручает библиотека unidecode. Помимо простого транслита кириллицы в латиницу она умеет преобразовывать спец символы и иероглифы в текстовые аналоги.
from unidecode import unidecode
>>> unidecode("Ñ Σ ® µ ¶ ¼ 月 山")
'N S (r) u P 1/4 Yue Shan'
Очень крутая библиотека, советую👍
В нашем случае получаем такое преобразование:
>>> text = unidecode(text)
>>> print(text)
'Moi zagolovok No. 10 !'
Отличный транслит. Смайл просто удалился, хотя я ждал что-то вроде :). Ну и ладно, всë равно невалидные символы.
А еще наш код уже поддерживает любой язык, будь то хинди или корейский.
🔸4. Фильтр символов
Unidecode не занимается фильтрацией по недопустимым символам. Это мы делаем в следующем шаге через regex. Просто заменим все символы на "_" если они вне указанного диапазона.
>>> text = re.sub(r'[^a-zA-Z0-9]+', '_', text)
>>> print(text)
'Moi_zagolovok_No_10_'
Символ "+" в паттерне выручает когда несколько недопустимых символов идут рядом. Все они заменяются на один символ "_".
🔸5. Slugify
Осталось удалить лишние символы по краям и сделать нижний регистр
>>> text = text.strip('_').lower()
>>> print(text)
'moi_zagolovok_no_10'
Получаем отличный slug! 😎
🌎 Полный код в виде функции.
______________
PS. Проверку что в строке остался хоть один допустимый символ я бы вынес в отдельную функцию.
#libs#tricks#django
🔴 Urgent: Gaza power blackout due to Israel's blockade prompts international condemnation. Hospitals risk becoming morgues. ICRC calls situation "abhorrent". Clean water access and healthcare at risk. HRW labels blockade a war crime, accuses Israel of collective punishment. Israel refuses aid until abducted Israelis are released. Humanitarian crisis unfolding. World urged to take action. #GazaUnderAttack#EndTheBlockade
https://www.aljazeera.com/news/2023/10/12/gaza-hospitals-risk-turning-into-morgues-rights-groups-call-for-action
Subscribe to @BadVolfNews
Retired US Army Colonel & former US diplomat Ann Wright introduces our upcoming Freedom Flotilla emergency mission to #breakthesiege and #endtheblockade of Gaza.
Ann will be a participant on the civilian flotilla sailing soon to Gaza.
LIVESTREAM FROM THE SEAS WITH THE FREEDOM FLOTILLA AND RASHIDA TLAIB.
July 23 on Instagram and Telegram
10:00 AM (New York | EDT GMT-4)
5:00 PM (Palestine | EEST GMT+3)
Join us for a powerful conversation between Congresswoman Rashida Tlaib and the U.S. citizens aboard ‘Handala’ as they sail to break Israel’s illegal siege on Gaza. Tune in to hear them discuss the moral responsibility of ordinary people when governments fail, the urgent need to change U.S. policy that enables Israel and their genocide of Palestinians, why they are sailing, what they are demanding, and how we can all stand with Gaza.
#alleyesondeck#handala#forthechildrenofgaza#breakthesiege#endtheblockade
Takis, from Greece, sailed with the Free Gaza Movement in 2008 - one of the first missions by sea that reached Gaza. Now, 17 years later, he is back, ready to help break Israel's illegal siege. #AllEyesOnDeck#DemandSafePassage#BreakIsraelsSiege#EndTheBlockade#FreeGaza🔽
Freedom Flotilla Official Update: It's time for our governments to do their jobs and sanction Israel. Its illegal attack on 'Madleen' while sailing in international waters is a small extension of their war crimes in Gaza. Governments, institutions, media, and corporations must stop protecting Israel, now. They must stop prioritising profit over human lives, now. Protect our volunteers while protecting Palestinians in Gaza and wider Palestine.
#BreakIsraelsSiege#EndTheBlockade#Madleen#FreedomFlotilla#Volunteers#IOF#StandWithGaza#StandWithPalestine
Our 'Madleen' was on a mission of urgency after a year of set backs, carrying baby formula, food, medicine, and basic humanity. Then came unidentified vessels, speedboats, quadcopters, and finally, Israeli soldiers. Francesca Albanese recounts what happened in real-time as she spoke to the crew live. She reminds us that 'Madleen' was a UK-flagged boat seized in international waters. Israel's interception is an act of piracy. Abduction is illegal, as is their siege, genocide, occupation and countless other war crimes across Palestine. So what makes a small, aid boat so scary for the Israeli occupation? According to Francesca Albanese: it is our humanity. It is everyone's defiance. It is our collective power to break the siege.
#BreakIsraelsSiege#EndTheBlockade#StandWithGaza#FrancescaAlbanese#Madleen
Bob Suberi, a U.S. and Israeli citizen and veteran, calls himself a “recovering Zionist.” Now in his third year attempting to help break Israel's illegal siege of Gaza on 'Handala', he’s taking direct action once again. After years working in Palestinian solidarity in the West Bank, Suberi is urging others, especially in the Jewish community, to speak out: “Let’s talk openly about a genocide. We’ve spent decades saying ‘Never again,’ and yet we’re justifying it.”
#BreakTheSiege#BreakIsraelsSiege#EndTheBlockade#Handala#FreedomFlotilla#StandWithGaza#StandWithPalestine
The Freedom Flotilla Coalition invites you to join us from May 30th to June 1st in Catania, Sicily, Italy, as we stand together in solidarity to launch our new mission, the ‘Madleen’.
The summit will be happening in Porto di San Giovanni li Cuti, where we will stand in solidarity with the Palestinian people in a series of panels, cultural programs, and community gatherings.
The ‘Madleen‘ will sail to break Israel's illegal siege as we bring humanitarian aid, hope, and solidarity to Gaza. The world stands united against the deliberate starvation, genocide, Apartheid, colonial occupation, and the ethnic cleansing of the Palestinian people. Join us!
Let's sail in spirit and solidarity. Free Palestine!
#FreedomFlotilla#BreakIsraelsSiege#EndTheBlockade#StandWithGaza#AllEyesOnDeck#Madleen#Conscience
Meet just some of the panelists, volunteers and supporters that will join us as we launch the ‘Madleen’ that will sail in a matter of days to break Israel's illegal siege of Gaza.
We urge media, press and supporters to join us in Catania, Sicily - your coverage and solidarity is critical in helping ensure our safety. March with us. Make noise with us. Show the world that Europe will not be silent. Share our content. Make it impossible for every politician, for the European Union and media to ignore what is happening in Gaza. Make it impossible for them to ignore the voice of the people, to ignore this mission, and to ignore this moment.
#BreakTheSiege#BreakIsraelsSiege#AllEyesOnDeck#EndTheBlockade#StandWithGaza#AllEyesOnDeck#Madleen#Conscience
Thousands of people are rising across continents - on foot, in boats, in streets and squares - to say no more.
No more siege. No more starvation. No more occupation. No more genocide. From the river to the sea, Palestine will be free. Follow and support March to Gaza today.
#BreakTheSiege#MarchToGaza#EndTheBlockade#GlobalMarchToGaza#StandWithPalestine
‘Handala’ is due to depart, just weeks after ‘Madleen’ set sail to Gaza. In a time of unprecedented levels of global repression of Palestinian solidarity, with individuals having livelihoods and careers threatened for supporting Palestine, we are reminded that repressive tactics only work if people are bullied into silence.
The ‘Handala’ mission is only one part of a global wave of solidarity. Investigative group Forensic Architecture are making an online tracker to monitor the vessel’s passage publicly available online, as with the ‘Madleen’ mission. Every act of documentation and witnessing is part of holding Israel - and complicit governments- accountable.
#FreedomFlotilla#Gaza#BreakTheSiege#AllEyesOnGaza#AllEyesOnDeck#DirectAction#EndTheBlockade#BreakIsraelsSiege
Lubna Tuma from Adalah Legal Centre provided us with a briefing on the status of the Flotilla members who were abducted from international waters by Israeli forces on 9th June 2025. Recorded just after leaving a legal counsel meeting held with the Flotilla volunteers in Givon prison, she comments on the numerous violations of human rights they were subjected to during the court hearing and in Israeli custody. Since recording Rima Hassan is no longer in solitary confinement. Please donate to Adalah Legal Centre today. QR code included and information provided at the end of the video.
#FreedomFlotilla#Gaza#BreakTheSiege#AllEyesOnGaza#AllEyesOnDeck#DirectAction#EndTheBlockade#BreakIsraelsSiege