12345678910111213141516171819202122232425262728293031323334353637383940 |
- import re
- def add_html_links(html: str) -> str:
- """
- Если в коде есть ссылки просто текстом, без хтмл элемента а, то функция
- заменит их все на полноценный html тэг с нужным атрибутом href
- """
- html = fix_links_with_brakets(html)
- html_cleared = html.replace('="htt', '') # remove any src and href patterns
- links = []
- for p in html_cleared.strip().split('http'):
- if p.startswith('://') or p.startswith('s://'):
- p = p.split('\n')
- p[0] = p[0].split('<')[0]
- links.append('http' + p[0])
- for l in links:
- link = ' '.join(l.split()) # убираем символы другой кодировки, которые
- # не убираются по-другому
- a_elem = f'<a href="{link}">{link}</a>'
- html = html.replace(l, a_elem)
- return html
- # костыль от дурака, от плохих ссылок
- # <a href="http://(https://pay.raschet.by
- def fix_links_with_brakets(html: str) -> str:
- html = ' '.join(html.split()) # убираем символы другой кодировки, которые
- result = re.findall('<a href="http://\([а-яА-Яa-zA-Z0-9\(\):/\._#>" =]+<\/a>', html)
- if result:
- for shitlink in result:
- if '(' in shitlink:
- result = result[0]
- link = result.split('(')[1].split(')')[0]
- anchor = result.split('</a>')[0].split('>')[-1].strip()
- a_tag = f'<a href="{link}">{anchor}</a>'
- html = html.replace(result, a_tag)
- return html
|