Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gruns/undent
⬅️ Dedent and format source code strings into their intended human-readable strings
https://github.com/gruns/undent
dedent format formatter indent indentation python string-manipulation strings text
Last synced: 16 days ago
JSON representation
⬅️ Dedent and format source code strings into their intended human-readable strings
- Host: GitHub
- URL: https://github.com/gruns/undent
- Owner: gruns
- License: mit
- Created: 2021-02-20T22:31:16.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-01-04T15:19:39.000Z (10 months ago)
- Last Synced: 2024-10-17T15:37:46.234Z (27 days ago)
- Topics: dedent, format, formatter, indent, indentation, python, string-manipulation, strings, text
- Language: Python
- Homepage:
- Size: 29.3 KB
- Stars: 17
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Undent
Undent turns strings stored in source code, which may have newlines,
tabs, and whitespace inserted to adhere to code formatting and linting
rules, into human-readable strings.Strings in source code may be indented, bookended with whitespace, or
contain newlines to adhere to code style guidelines. `undent()`
gracefully removes such code style formatting and returns the original,
intended string for human consumption.To accomplish this, `undent()`
1. [Dedents](https://docs.python.org/3/library/textwrap.html#textwrap.dedent)
the multiline string to remove common indentation.2. Strips preceeding and trailing whitespace, while preserving
post-dedent indentation, to remove whitespace added for source code
formatting.3. Unwraps paragraphs to remove newlines inserted for source code
formatting. E.g. newlines inserted to adhere to PEP 8's 79
characters per line limit.Or, optionally line wrap paragraphs to a custom width, e.g. 72
character per line.### Usage
Just import `undent()` and give it a multiline string.
```python
from undent import undentdef createEmail():
name = 'Billy'
emailAddr = '[email protected]'
email = undent(f'''
Hi {name}!Thank you for registering with email address
{emailAddr}
We'd love to hear from you; please email us
and say hello!''')return email
```Above, `undent()` dedents, formats, and wraps the multiline string into
a beautiful, human-readable string.```
Hi Billy!Thank you for registering with email address
We'd love to hear from you; please email us and say hello!
````undent(s, width=False, strip=True)` takes two, optional arguments.
#### width
(default: `False`) `width` is the maximum length of wrapped lines, in
characters, or `False` to unwrap lines. If `width` is an integer, as
long as there are no individual words in the input string longer than
`width`, no output line will be wider than `width` characters. Examples:```python
undent('Once upon a time, there was a little girl named Goldilocks.', width=30)
```returns
```
Once upon a time, there was a
little girl named Goldilocks.
```Conversely,
```python
undent('''Once upon a time, there was a
little girl named Goldilocks.''', width=False)
```returns
```
Once upon a time, there was a little girl named Goldilocks.
```#### strip
(default: `True`) `strip` determines whether or not to remove preceeding
and trailing whitespace. Examples:```python
undent('''
Once upon a time, there was a
little girl named Goldilocks.
''', strip=True)
```returns
```
Once upon a time, there was a little girl named Goldilocks.
````Alternatively
```python
undent('''Once upon a time, there was a
little girl named Goldilocks.''', strip=False)
```returns
```
Once upon a time, there was a little girl named Goldilocks.
```
### Examples
#### `undent()` [dedents](https://docs.python.org/3/library/textwrap.html#textwrap.dedent) the string so indentation added for source code formatting isn't preserved.
```python
if True:
print(undent('''commonindentation
is removed'''))
```outputs
```
commonindentation
is removed
```#### `undent()` strips preceeding and trailing whitespace, while preserving post-dedent indentation, so whitespace added for source code formatting isn't unintentionally preserved.
```python
if True:
print(undent('''
preceedingand trailing
whitespace is removed
'''))
```outputs
```
preceedingand trailing
whitespace is removed
```#### `undent()` unwraps paragraphs so newlines inserted for source code formatting aren't unintentionally preserved in the output, e.g. newlines inserted to avoid lines wider than PEP 8's 80 characters per line.
```python
if someIndentation:
if moreIndentation:
if evenDeeperIndentation:
print(undent(f'''
Once upon a time, there was a little girl named
Goldilocks. She went for a walk in the forest. Pretty
soon, she came upon a house. She knocked and, when no
one answered, she walked right in.
At the table in the kitchen, there were three bowls of
porridge. Goldilocks was hungry. She tasted the porridge
from the first bowl.
'''))
```outputs
```
Once upon a time, there was a little girl named Goldilocks. She went for a walk in the forest. Pretty soon, she came upon a house. She knocked and, when no one answered, she walked right in.At the table in the kitchen, there were three bowls of porridge. Goldilocks was hungry. She tasted the porridge from the first bowl.
```#### Or, optionally line wrap output paragraphs to your intended width, e.g. 72 character per line.
```python
if someIndentation:
if moreIndentation:
if evenDeeperIndentation:
width = 72
print(undent(f'''
Once upon a time, there was a little girl named
Goldilocks. She went for a walk in the forest. Pretty
soon, she came upon a house. She knocked and, when no
one answered, she walked right in.
At the table in the kitchen, there were three bowls of
porridge. Goldilocks was hungry. She tasted the porridge
from the first bowl.
''', width))
```outputs
```
Once upon a time, there was a little girl named Goldilocks. She went for
a walk in the forest. Pretty soon, she came upon a house. She knocked
and, when no one answered, she walked right in.At the table in the kitchen, there were three bowls of porridge.
Goldilocks was hungry. She tasted the porridge from the first bowl.
```### Installation
Installing Undent with pip is easy.
```
$ pip install undent
```