Created
August 8, 2016 18:06
-
-
Save helb/1865ee35eab84955b3fd512b408d5651 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import pyphen | |
NONBREAKABLE_WORDS = [ # nektera slova (napr. nazev firmy) se hodi nerozdelovat | |
"…", | |
"…" | |
] | |
def hyphens(text, lang): | |
""" | |
Jinja2 filter. | |
Adds ­ marks to suggest word breaks to the browser. | |
Usage: {{ page.foo | hyphens }}. | |
""" | |
dic = pyphen.Pyphen(lang=lang) | |
wordlist = text.split() | |
hyphenated = [] | |
for word in wordlist: | |
if re.sub(r"[,\.:;]$", "", word) in NONBREAKABLE_WORDS: | |
hyphenated.append(word) | |
else: | |
hyphenated.append(dic.inserted(word, "­")) | |
return " ".join(hyphenated) | |
def dont_break(text, lang): | |
""" | |
Jinja2 filter. | |
Adds a non-breakable space after short prepositions etc. | |
Usage: {{ page.foo | dont_break }} | |
""" | |
if lang == "cs": | |
SHORTWORDS = r"([aA]|[Vv]|[Oo]|[Nn]a|[Dd]o|[Ii])\s" | |
else if lang == "en": | |
SHORTWORDS = r"([Tt]he|[Oo]f|[Tt]o|[Ii]t|[Ii]s|[Bb]y|[Aa]|[Ii]n|[Aa]n)\s" | |
else if …: | |
… | |
else: | |
return text | |
return re.sub(SHORTWORDS, r"\1 ", text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment