Returns list of strings *exactly* n width
Project description
JustifyText
*for monospaced fonts
Takes a string and inserts
spaces between words, until
line is desired width, like
you see here.
Importing the module and using the function:
- Option 1:
from justifytext import justify
justify(text, width)
- Option 2:
import justify
justifytext.justify(text, width)
Module Requirements/Dependencies:
- Python code tab = 4 spaces
- Requires textwrap module
Module Assumptions:
- Line has at least one space between words (at least in this version).
- Relies on break_on_long words equalling true in textwrapper. (You could make the change to the textwrapper function call in this module, but at a small enough width you would just get back lines with single words and nothing but spaces on the ends of them, i.e not justified.)
Required positional arguments:
- text == string to be formatted as list of strings exactly n characters long (You'll have to handle quotes and special characters of course.)
- width == desired width of the resulting line
Optional and default arguments:
justify_last_line = False
This is the default for handling the final line of the string. Convention is to NOT stretch a few words across the span of the formatted block, but I've included a way to undo that - if that's your kind of thing. For example:
text = "The end of this bit of text will be these three words."
justify(text, 35)
will return a list that can iterate over to display:
The end of this bit of text will be
these three words.
whereas
justify(text, 35, justify_last_line = True)
will return a iterable list that will display:
The end of this bit of text will be
these three words.
Module Flow:
-
Accepts a string (Module seems to ignore \n, not sure why yet -- I'm very tired. Probably has something to do with textwrap behavior. To preserve a special character, escape it, i.e. '\n'). Otherwise normal string manipulation rules apply, you'll need to handle quotation marks and special characters accordingly. One of the earliest steps in this module puts text through textwrapper, so if your input doesn't raise a textwrapper error there, you should be all right. (Probably).
-
Break into lines no longer than n using textwrap and TextWrapper. See discussion at end of 1. above.
-
Loop, starting from the last space (actually, the word before the last space), and walk backward, adding a space between words (on the theory that readers will be less likely to notice extra spaces when reading from left-to-right and encountering them at the end of a line.
-
If a line is still short after adding a space between all words in a line, module will start again at the last space (IOW, it enters a second or third or eighth space - beginning at the end - to reach desired width).
-
Once all lines are exactly n width, return list of lines. Module could just print them into a nice block, but I prefer to hand the reformatted text back to you, so you can do with it what you like. To display the reformatted text a simple iterable will do. e.g.:
for i in returnText:
print(i)
Sample Usage
Raw text:
text = "Now did the Lord say, \"First thou pullest the Holy Pin. Then thou must count to three.
Three shall be the number of the counting and the number of the counting shall be three. Four
shalt thou not count, neither shalt thou count two, excepting that thou then proceedeth to three.
Five is right out. Once the number three, being the number of the counting, be reached, then lobbest
thou the Holy Hand Grenade in the direction of thine foe, who, being naughty in my sight, shall snuff it."
Call:
x = justify(text, 30)
Returns:
x = ['Now did the Lord say, "First', 'thou pullest the Holy Pin.', 'Then thou must count to three.',
'Three shall be the number of', 'the counting and the number of', 'the counting shall be three.',
'Four shalt thou not count,', 'neither shalt thou count two,', 'excepting that thou then', 'proceedeth
to three. Five is', 'right out. Once the number', 'three, being the number of the', 'counting, be
reached, then', 'lobbest thou the Holy Hand', 'Grenade in the direction of', 'thine foe, who, being
naughty', 'in my sight, shall snuff it.']
Process list for justified display:
for i in x:
print(i)
Sample output:
Now did the Lord say, "First
thou pullest the Holy Pin.
Then thou must count to three.
Three shall be the number of
the counting and the number of
the counting shall be three.
Four shalt thou not count,
neither shalt thou count two,
excepting that thou then
proceedeth to three. Five is
right out. Once the number
three, being the number of the
counting, be reached, then
lobbest thou the Holy Hand
Grenade in the direction of
thine foe, who, being naughty
in my sight, shall snuff it.
Other/Misc.:
This package was prepared per the advice at: https://packaging.python.org
Found bugs? Let me know! mailto: cjtx.code@gmail.com
-cjtx / May 27, 2019
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file JustifyText-0.2.1.post3.tar.gz
.
File metadata
- Download URL: JustifyText-0.2.1.post3.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cedd8cae2d5e832399560aa7a252efc3fc1fc591ce271b54685026c2ee0483ea |
|
MD5 | 6d526147f59001f28f5d561cad68ed15 |
|
BLAKE2b-256 | 07852d4f064d6732fb4551fd434c6aacf0bc1b336e156c36394b77cfe9d9c206 |
File details
Details for the file JustifyText-0.2.1.post3-py2.py3-none-any.whl
.
File metadata
- Download URL: JustifyText-0.2.1.post3-py2.py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1785e6845dc600c8cb38d1ffcb9d747e78d6ca453605af4cb30688842920fd95 |
|
MD5 | 160655d4988039442b72e979bb3535f9 |
|
BLAKE2b-256 | 4f348c35cde4be22815cade6611141afe0b2dca8ade8de2149bfb7981d647ced |