Skip to main content

Add soft hyphens to HTML documents

Project description

shyster

The problem this package is trying to solve is that while I can set hyphens: auto; in CSS, many browsers do a poor job of hyphenating Finnish. Even if they have Finnish hyphenation patterns, they often fail to recognise compound words, which should be hyphenated at compound boundaries (saippua-kauppias, not saip-pua-kaup-pias). One solution is to set hyphens: manual; and add soft hyphens at acceptable hyphenation spots.

Install

pip install https://github.com/jkseppan/shyster/

How to use

One top-level function does it all:

import shyster
shyster.hyphenate_html_file('input.html', 'output.html', 'patterns/hyphen.tex')

If more control is needed:

pat, ex = read_patterns(open('patterns/hyph-fi.tex').readlines())
pat_re, pat_map = convert_patterns(pat)
ex = convert_exceptions(ex)
hyph = hyphenator(pat_re, pat_map, ex, righthyphenmin=2)

[hyph(word) for word in 
 'Jukolan talo, eteläisessä Hämeessä, seisoo erään mäen pohjaisella rinteellä, liki Toukolan kylää'\
 .replace(',','').split()]
['Ju-ko-lan',
 'ta-lo',
 'ete-läi-ses-sä',
 'Hä-mees-sä',
 'sei-soo',
 'erään',
 'mäen',
 'poh-jai-sel-la',
 'rin-teel-lä',
 'li-ki',
 'Tou-ko-lan',
 'ky-lää']
html = """
<!doctype html><title>Seitsemän veljestä</title>
<script>var veljekset = 7;</script>
<body>
<p style="margin-top: 2em">Jukolan talo, eteläisessä Hämeessä, seisoo erään mäen pohjaisella
rinteellä, liki Toukolan kylää. Sen läheisin ympäristö on kivinen
tanner, mutta alempana alkaa pellot, joissa, ennenkuin talo oli häviöön
mennyt, aaltoili teräinen vilja.</p>
</body>
"""
soup = BeautifulSoup(html, 'lxml')
hyph = hyphenator(pat_re, pat_map, ex, righthyphenmin=2)
hyphenate_soup(soup, hyph)
print(str(soup))
<!DOCTYPE html>
<html><head><title>Seit-se-män vel-jes-tä</title>
<script>var veljekset = 7;</script>
</head><body>
<p style="margin-top: 2em">Ju-ko-lan ta-lo, ete-läi-ses-sä Hä-mees-sä, sei-soo erään mäen poh-jai-sel-la
rin-teel-lä, li-ki Tou-ko-lan ky-lää. Sen lä-hei-sin ym-pä-ris-tö on ki-vi-nen
tan-ner, mut-ta alem-pa-na al-kaa pel-lot, jois-sa, en-nen-kuin ta-lo oli hä-vi-öön
men-nyt, aal-toi-li te-räi-nen vil-ja.</p>
</body>
</html>
pat, ex = read_patterns(open('patterns/hyphen.tex').readlines())
pat_re, pat_map = convert_patterns(pat)
ex = convert_exceptions(ex)
hyph = hyphenator(pat_re, pat_map, ex)

import textwrap
textwrap.wrap(' '.join(hyph(word) for word in '''
It is a truth universally acknowledged, that a single man in possession of a good fortune,
must be in want of a wife.
'''.replace(',','').replace('.','').split()))
['It is a truth univer-sally ac-knowl-edged that a single man in posses-',
 'sion of a good for-tune must be in want of a wife']

Copying

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.

The files in patterns/ are distributed with this program as example input files. The Finnish patterns are covered by the terms “Patterns may be freely distributed” and the English ones by “Unlimited copying and redistribution of this file are permitted as long as this file is not modified.”

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shyster-0.0.1.tar.gz (21.4 kB view details)

Uploaded Source

Built Distribution

shyster-0.0.1-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

Details for the file shyster-0.0.1.tar.gz.

File metadata

  • Download URL: shyster-0.0.1.tar.gz
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for shyster-0.0.1.tar.gz
Algorithm Hash digest
SHA256 6e5dacfee4b307602544fe5703a2c31fa8daa5c874965057213a7fed4b847766
MD5 1cecc56a7388ec8b230b19f7441667ed
BLAKE2b-256 d6949bd3aa4a3ab5b45c9fcfacfeeabfa60a79fd11fca3a9f4262e5e988552a3

See more details on using hashes here.

File details

Details for the file shyster-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: shyster-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 20.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for shyster-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dcb4a4fe660a48c3ab2a0f206ad7bcd9e820001ed1fb25eb9d52061a7a435a3b
MD5 9aa4e52c190ede2a7f00e3e0df78cdb8
BLAKE2b-256 e9437171d5b21229485e40f937e7c6ba636c70a15cf23c402cf3fc866052b3ce

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page