mdformat-slw 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mdformat_slw-0.2.0/LICENSE +21 -0
- mdformat_slw-0.2.0/PKG-INFO +264 -0
- mdformat_slw-0.2.0/README.md +239 -0
- mdformat_slw-0.2.0/mdformat_slw/__init__.py +11 -0
- mdformat_slw-0.2.0/mdformat_slw/_helpers.py +32 -0
- mdformat_slw-0.2.0/mdformat_slw/_language_data.py +172 -0
- mdformat_slw-0.2.0/mdformat_slw/_sentence_wrapper.py +587 -0
- mdformat_slw-0.2.0/mdformat_slw/plugin.py +126 -0
- mdformat_slw-0.2.0/mdformat_slw/py.typed +0 -0
- mdformat_slw-0.2.0/pyproject.toml +213 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Kyle King
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mdformat_slw
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: An mdformat plugin for slw
|
|
5
|
+
Keywords: markdown,markdown-it,mdformat,mdformat_plugin_template
|
|
6
|
+
Author: kyleking
|
|
7
|
+
Author-email: kyleking <dev.act.kyle@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
14
|
+
Requires-Dist: mdformat>=0.7.19
|
|
15
|
+
Requires-Dist: mdit-py-plugins>=0.4.1
|
|
16
|
+
Requires-Dist: pytest>=9.0.1 ; extra == 'test'
|
|
17
|
+
Requires-Dist: pytest-beartype>=0.2.0 ; extra == 'test'
|
|
18
|
+
Requires-Dist: pytest-cov>=7.0.0 ; extra == 'test'
|
|
19
|
+
Requires-Python: >=3.10.0
|
|
20
|
+
Project-URL: Bug Tracker, https://github.com/kyleking/mdformat-slw/issues
|
|
21
|
+
Project-URL: Changelog, https://github.com/kyleking/mdformat-slw/releases
|
|
22
|
+
Project-URL: homepage, https://github.com/kyleking/mdformat-slw
|
|
23
|
+
Provides-Extra: test
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# mdformat-slw
|
|
27
|
+
|
|
28
|
+
[![Build Status][ci-badge]][ci-link] [![PyPI version][pypi-badge]][pypi-link]
|
|
29
|
+
|
|
30
|
+
An [mdformat](https://github.com/executablebooks/mdformat) plugin for semantic line wrapping (slw).
|
|
31
|
+
|
|
32
|
+
This plugin wraps markdown text by inserting line breaks after sentence-ending punctuation, making diffs cleaner and easier to review.
|
|
33
|
+
|
|
34
|
+
## Features
|
|
35
|
+
|
|
36
|
+
- **Automatic sentence wrapping** at configurable punctuation marks (default: `.!?:`)
|
|
37
|
+
- **Enabled by default** - no flags needed to activate
|
|
38
|
+
- Optional maximum line width enforcement with `--slw-wrap`
|
|
39
|
+
- Preserves markdown formatting (bold, italic, links, etc.)
|
|
40
|
+
- Handles edge cases: quoted text, parentheses, brackets
|
|
41
|
+
|
|
42
|
+
## `mdformat` Usage
|
|
43
|
+
|
|
44
|
+
Add this package wherever you use `mdformat` and the plugin will be auto-recognized. Sentence wrapping is **enabled by default**. See [additional information on `mdformat` plugins here](https://mdformat.readthedocs.io/en/stable/users/plugins.html)
|
|
45
|
+
|
|
46
|
+
### CLI
|
|
47
|
+
|
|
48
|
+
```sh
|
|
49
|
+
# Sentence wrapping enabled by default
|
|
50
|
+
mdformat document.md
|
|
51
|
+
|
|
52
|
+
# With line width enforcement
|
|
53
|
+
mdformat document.md --slw-wrap 80 --wrap=keep
|
|
54
|
+
|
|
55
|
+
# Disable sentence wrapping
|
|
56
|
+
mdformat document.md --no-wrap-sentences
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
#### Options
|
|
60
|
+
|
|
61
|
+
**Basic Options:**
|
|
62
|
+
|
|
63
|
+
- `--no-wrap-sentences`: Disable sentence wrapping (enabled by default)
|
|
64
|
+
- `--slw-markers TEXT`: Characters that mark sentence endings (default: `.!?:`)
|
|
65
|
+
- `--slw-wrap INTEGER`: Maximum line width for wrapping (default: 80)
|
|
66
|
+
|
|
67
|
+
**Abbreviation Detection:**
|
|
68
|
+
|
|
69
|
+
- `--slw-lang TEXT`: Space-separated language codes for abbreviation lists (default: `ac`)
|
|
70
|
+
- Supported: `ac` (all-caps only), `en`, `de`, `es`, `fr`, `it`
|
|
71
|
+
- `--slw-abbreviations-mode TEXT`: Abbreviation detection mode (default: `default`)
|
|
72
|
+
- `default`: Use built-in language lists
|
|
73
|
+
- `off`: Disable abbreviation detection
|
|
74
|
+
- `extend`: Add custom abbreviations to built-in lists
|
|
75
|
+
- `override`: Replace built-in lists with custom abbreviations
|
|
76
|
+
- `--slw-abbreviations TEXT`: Comma-separated custom abbreviations (e.g., `Dr.,Prof.,etc.`)
|
|
77
|
+
- `--slw-suppressions TEXT`: Space-separated words to add to suppression list
|
|
78
|
+
- `--slw-ignores TEXT`: Space-separated words to remove from suppression list
|
|
79
|
+
- `--slw-case-sensitive`: Enable case-sensitive abbreviation matching (default: case-insensitive)
|
|
80
|
+
|
|
81
|
+
> **Note:** When using `--slw-wrap`, consider adding `--wrap=keep` to disable mdformat's built-in line wrapping and avoid conflicts.
|
|
82
|
+
|
|
83
|
+
### Configuration File
|
|
84
|
+
|
|
85
|
+
Create a `.mdformat.toml` file in your project root:
|
|
86
|
+
|
|
87
|
+
```toml
|
|
88
|
+
[plugin.slw]
|
|
89
|
+
# Disable sentence wrapping (enabled by default)
|
|
90
|
+
no_wrap_sentences = false
|
|
91
|
+
|
|
92
|
+
# Customize sentence markers
|
|
93
|
+
slw_markers = ".!?:"
|
|
94
|
+
|
|
95
|
+
# Set line width wrapping (default: 80)
|
|
96
|
+
slw_wrap = 80
|
|
97
|
+
|
|
98
|
+
# Configure abbreviation detection
|
|
99
|
+
lang = "en de" # Use English and German abbreviations
|
|
100
|
+
abbreviations_mode = "extend" # Add custom to built-in lists
|
|
101
|
+
abbreviations = "Dr.,Prof.,etc." # Custom abbreviations
|
|
102
|
+
case_sensitive = false # Case-insensitive matching (default)
|
|
103
|
+
|
|
104
|
+
# Recommended: disable mdformat's wrapping to avoid conflicts
|
|
105
|
+
[mdformat]
|
|
106
|
+
wrap = "keep"
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### pre-commit / prek
|
|
110
|
+
|
|
111
|
+
```yaml
|
|
112
|
+
repos:
|
|
113
|
+
- repo: https://github.com/executablebooks/mdformat
|
|
114
|
+
rev: 1.0.0
|
|
115
|
+
hooks:
|
|
116
|
+
- id: mdformat
|
|
117
|
+
additional_dependencies:
|
|
118
|
+
- mdformat-slw
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### uvx
|
|
122
|
+
|
|
123
|
+
```sh
|
|
124
|
+
uvx --with mdformat-slw mdformat
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Or with pipx:
|
|
128
|
+
|
|
129
|
+
```sh
|
|
130
|
+
pipx install mdformat
|
|
131
|
+
pipx inject mdformat mdformat-slw
|
|
132
|
+
mdformat document.md
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Python API
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
import mdformat
|
|
139
|
+
|
|
140
|
+
text = """
|
|
141
|
+
This is a test. It has multiple sentences! Does it work?
|
|
142
|
+
"""
|
|
143
|
+
|
|
144
|
+
# Sentence wrapping enabled by default
|
|
145
|
+
result = mdformat.text(text, extensions={"slw"})
|
|
146
|
+
|
|
147
|
+
print(result)
|
|
148
|
+
# Output:
|
|
149
|
+
# This is a test.
|
|
150
|
+
# It has multiple sentences!
|
|
151
|
+
# Does it work?
|
|
152
|
+
|
|
153
|
+
# With line width wrapping
|
|
154
|
+
result = mdformat.text(
|
|
155
|
+
text,
|
|
156
|
+
extensions={"slw"},
|
|
157
|
+
options={"slw_wrap": 80}
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
# Disable sentence wrapping
|
|
161
|
+
result = mdformat.text(
|
|
162
|
+
text,
|
|
163
|
+
extensions={"slw"},
|
|
164
|
+
options={"no_wrap_sentences": True}
|
|
165
|
+
)
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Example
|
|
169
|
+
|
|
170
|
+
### Basic Wrapping
|
|
171
|
+
|
|
172
|
+
**Input:**
|
|
173
|
+
|
|
174
|
+
```markdown
|
|
175
|
+
This is a long sentence. It contains multiple clauses! Does it work? Yes it does.
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
**Output (default behavior):**
|
|
179
|
+
|
|
180
|
+
```markdown
|
|
181
|
+
This is a long sentence.
|
|
182
|
+
It contains multiple clauses!
|
|
183
|
+
Does it work?
|
|
184
|
+
Yes it does.
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Abbreviation Detection
|
|
188
|
+
|
|
189
|
+
**Input:**
|
|
190
|
+
|
|
191
|
+
```markdown
|
|
192
|
+
Dr. Smith met with Prof. Johnson at 3 p.m. They discussed the project etc. and other topics.
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
**Output (abbreviations preserved):**
|
|
196
|
+
|
|
197
|
+
```markdown
|
|
198
|
+
Dr. Smith met with Prof. Johnson at 3 p.m.
|
|
199
|
+
They discussed the project etc. and other topics.
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Link Protection
|
|
203
|
+
|
|
204
|
+
**Input:**
|
|
205
|
+
|
|
206
|
+
```markdown
|
|
207
|
+
Check [example.com](https://example.com). Use `config.json` for settings. Done!
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
**Output (links and code preserved):**
|
|
211
|
+
|
|
212
|
+
```markdown
|
|
213
|
+
Check [example.com](https://example.com).
|
|
214
|
+
Use `config.json` for settings.
|
|
215
|
+
Done!
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## How It Works
|
|
219
|
+
|
|
220
|
+
### Wrapping Rules
|
|
221
|
+
|
|
222
|
+
When one of the limited number of characters (`.!?:` by default) which serve as end-of-sentence markers occur alone, mdformat-slw will wrap **except** when:
|
|
223
|
+
|
|
224
|
+
1. Not in a context where auto-wrapping is possible:
|
|
225
|
+
- Inline code, links, definition lists, etc.
|
|
226
|
+
- Code Blocks
|
|
227
|
+
- Tables
|
|
228
|
+
- HTML Blocks
|
|
229
|
+
1. When the wrapped term is an abbreviation:
|
|
230
|
+
- Multiple end-of-sentence markers occur (such as `p.m.` or `e.g.`)
|
|
231
|
+
- Identified as an abbreviation from language-specific lists (`Dr.`, `Prof.`, `etc.`, etc.)
|
|
232
|
+
- Matched against custom abbreviations specified via `--slw-abbreviations`
|
|
233
|
+
- Part of user-specified suppression words via `--slw-suppressions`
|
|
234
|
+
1. Abbreviation matching is case-insensitive by default (use `--slw-case-sensitive` to change)
|
|
235
|
+
|
|
236
|
+
### Algorithm
|
|
237
|
+
|
|
238
|
+
1. Insert a line break after every character that ends a sentence which complies with the above rules and exceptions
|
|
239
|
+
1. Collapse all consecutive whitespace into a single space. While doing so, preserve both non-breaking spaces and linebreaks that are preceded by non-breaking spaces
|
|
240
|
+
1. Before line wrapping, replace all spaces in link texts by non-breaking spaces (and similar inline content that can't be wrapped)
|
|
241
|
+
1. Wrap lines that are longer than the maximum line width, if set, (80 characters by default) without splitting words or splitting at non-breaking spaces while also keeping indents in tact.
|
|
242
|
+
|
|
243
|
+
## Acknowledgments
|
|
244
|
+
|
|
245
|
+
This plugin is inspired by and named after [mdslw](https://github.com/razziel89/mdslw) by [@razziel89](https://github.com/razziel89). The original `mdslw` is an excellent standalone tool for semantic line wrapping of markdown files.
|
|
246
|
+
|
|
247
|
+
**Why create mdformat-slw?**
|
|
248
|
+
|
|
249
|
+
The [mdformat](https://github.com/executablebooks/mdformat) ecosystem uses a plugin architecture where formatters must integrate with mdformat's AST-based processing pipeline. While `mdslw` works great as a standalone tool, it cannot be used as an mdformat plugin directly. This plugin reimplements semantic line wrapping concepts to work natively within mdformat, enabling:
|
|
250
|
+
|
|
251
|
+
- Seamless integration with other mdformat plugins
|
|
252
|
+
- Consistent formatting when mdformat is your primary markdown formatter
|
|
253
|
+
- Use in pre-commit hooks alongside mdformat's other capabilities
|
|
254
|
+
|
|
255
|
+
The plugin is named `slw` (not `mdslw`) to clearly distinguish it from the original tool and avoid confusion, since the implementations differ.
|
|
256
|
+
|
|
257
|
+
## Contributing
|
|
258
|
+
|
|
259
|
+
See [CONTRIBUTING.md](https://github.com/kyleking/mdformat-slw/blob/main/CONTRIBUTING.md)
|
|
260
|
+
|
|
261
|
+
[ci-badge]: https://github.com/kyleking/mdformat-slw/actions/workflows/tests.yml/badge.svg?branch=main
|
|
262
|
+
[ci-link]: https://github.com/kyleking/mdformat-slw/actions?query=workflow%3ACI+branch%3Amain+event%3Apush
|
|
263
|
+
[pypi-badge]: https://img.shields.io/pypi/v/mdformat-slw.svg
|
|
264
|
+
[pypi-link]: https://pypi.org/project/mdformat-slw
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
# mdformat-slw
|
|
2
|
+
|
|
3
|
+
[![Build Status][ci-badge]][ci-link] [![PyPI version][pypi-badge]][pypi-link]
|
|
4
|
+
|
|
5
|
+
An [mdformat](https://github.com/executablebooks/mdformat) plugin for semantic line wrapping (slw).
|
|
6
|
+
|
|
7
|
+
This plugin wraps markdown text by inserting line breaks after sentence-ending punctuation, making diffs cleaner and easier to review.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **Automatic sentence wrapping** at configurable punctuation marks (default: `.!?:`)
|
|
12
|
+
- **Enabled by default** - no flags needed to activate
|
|
13
|
+
- Optional maximum line width enforcement with `--slw-wrap`
|
|
14
|
+
- Preserves markdown formatting (bold, italic, links, etc.)
|
|
15
|
+
- Handles edge cases: quoted text, parentheses, brackets
|
|
16
|
+
|
|
17
|
+
## `mdformat` Usage
|
|
18
|
+
|
|
19
|
+
Add this package wherever you use `mdformat` and the plugin will be auto-recognized. Sentence wrapping is **enabled by default**. See [additional information on `mdformat` plugins here](https://mdformat.readthedocs.io/en/stable/users/plugins.html)
|
|
20
|
+
|
|
21
|
+
### CLI
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
# Sentence wrapping enabled by default
|
|
25
|
+
mdformat document.md
|
|
26
|
+
|
|
27
|
+
# With line width enforcement
|
|
28
|
+
mdformat document.md --slw-wrap 80 --wrap=keep
|
|
29
|
+
|
|
30
|
+
# Disable sentence wrapping
|
|
31
|
+
mdformat document.md --no-wrap-sentences
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
#### Options
|
|
35
|
+
|
|
36
|
+
**Basic Options:**
|
|
37
|
+
|
|
38
|
+
- `--no-wrap-sentences`: Disable sentence wrapping (enabled by default)
|
|
39
|
+
- `--slw-markers TEXT`: Characters that mark sentence endings (default: `.!?:`)
|
|
40
|
+
- `--slw-wrap INTEGER`: Maximum line width for wrapping (default: 80)
|
|
41
|
+
|
|
42
|
+
**Abbreviation Detection:**
|
|
43
|
+
|
|
44
|
+
- `--slw-lang TEXT`: Space-separated language codes for abbreviation lists (default: `ac`)
|
|
45
|
+
- Supported: `ac` (all-caps only), `en`, `de`, `es`, `fr`, `it`
|
|
46
|
+
- `--slw-abbreviations-mode TEXT`: Abbreviation detection mode (default: `default`)
|
|
47
|
+
- `default`: Use built-in language lists
|
|
48
|
+
- `off`: Disable abbreviation detection
|
|
49
|
+
- `extend`: Add custom abbreviations to built-in lists
|
|
50
|
+
- `override`: Replace built-in lists with custom abbreviations
|
|
51
|
+
- `--slw-abbreviations TEXT`: Comma-separated custom abbreviations (e.g., `Dr.,Prof.,etc.`)
|
|
52
|
+
- `--slw-suppressions TEXT`: Space-separated words to add to suppression list
|
|
53
|
+
- `--slw-ignores TEXT`: Space-separated words to remove from suppression list
|
|
54
|
+
- `--slw-case-sensitive`: Enable case-sensitive abbreviation matching (default: case-insensitive)
|
|
55
|
+
|
|
56
|
+
> **Note:** When using `--slw-wrap`, consider adding `--wrap=keep` to disable mdformat's built-in line wrapping and avoid conflicts.
|
|
57
|
+
|
|
58
|
+
### Configuration File
|
|
59
|
+
|
|
60
|
+
Create a `.mdformat.toml` file in your project root:
|
|
61
|
+
|
|
62
|
+
```toml
|
|
63
|
+
[plugin.slw]
|
|
64
|
+
# Disable sentence wrapping (enabled by default)
|
|
65
|
+
no_wrap_sentences = false
|
|
66
|
+
|
|
67
|
+
# Customize sentence markers
|
|
68
|
+
slw_markers = ".!?:"
|
|
69
|
+
|
|
70
|
+
# Set line width wrapping (default: 80)
|
|
71
|
+
slw_wrap = 80
|
|
72
|
+
|
|
73
|
+
# Configure abbreviation detection
|
|
74
|
+
lang = "en de" # Use English and German abbreviations
|
|
75
|
+
abbreviations_mode = "extend" # Add custom to built-in lists
|
|
76
|
+
abbreviations = "Dr.,Prof.,etc." # Custom abbreviations
|
|
77
|
+
case_sensitive = false # Case-insensitive matching (default)
|
|
78
|
+
|
|
79
|
+
# Recommended: disable mdformat's wrapping to avoid conflicts
|
|
80
|
+
[mdformat]
|
|
81
|
+
wrap = "keep"
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### pre-commit / prek
|
|
85
|
+
|
|
86
|
+
```yaml
|
|
87
|
+
repos:
|
|
88
|
+
- repo: https://github.com/executablebooks/mdformat
|
|
89
|
+
rev: 1.0.0
|
|
90
|
+
hooks:
|
|
91
|
+
- id: mdformat
|
|
92
|
+
additional_dependencies:
|
|
93
|
+
- mdformat-slw
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### uvx
|
|
97
|
+
|
|
98
|
+
```sh
|
|
99
|
+
uvx --with mdformat-slw mdformat
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Or with pipx:
|
|
103
|
+
|
|
104
|
+
```sh
|
|
105
|
+
pipx install mdformat
|
|
106
|
+
pipx inject mdformat mdformat-slw
|
|
107
|
+
mdformat document.md
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Python API
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
import mdformat
|
|
114
|
+
|
|
115
|
+
text = """
|
|
116
|
+
This is a test. It has multiple sentences! Does it work?
|
|
117
|
+
"""
|
|
118
|
+
|
|
119
|
+
# Sentence wrapping enabled by default
|
|
120
|
+
result = mdformat.text(text, extensions={"slw"})
|
|
121
|
+
|
|
122
|
+
print(result)
|
|
123
|
+
# Output:
|
|
124
|
+
# This is a test.
|
|
125
|
+
# It has multiple sentences!
|
|
126
|
+
# Does it work?
|
|
127
|
+
|
|
128
|
+
# With line width wrapping
|
|
129
|
+
result = mdformat.text(
|
|
130
|
+
text,
|
|
131
|
+
extensions={"slw"},
|
|
132
|
+
options={"slw_wrap": 80}
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
# Disable sentence wrapping
|
|
136
|
+
result = mdformat.text(
|
|
137
|
+
text,
|
|
138
|
+
extensions={"slw"},
|
|
139
|
+
options={"no_wrap_sentences": True}
|
|
140
|
+
)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Example
|
|
144
|
+
|
|
145
|
+
### Basic Wrapping
|
|
146
|
+
|
|
147
|
+
**Input:**
|
|
148
|
+
|
|
149
|
+
```markdown
|
|
150
|
+
This is a long sentence. It contains multiple clauses! Does it work? Yes it does.
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
**Output (default behavior):**
|
|
154
|
+
|
|
155
|
+
```markdown
|
|
156
|
+
This is a long sentence.
|
|
157
|
+
It contains multiple clauses!
|
|
158
|
+
Does it work?
|
|
159
|
+
Yes it does.
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Abbreviation Detection
|
|
163
|
+
|
|
164
|
+
**Input:**
|
|
165
|
+
|
|
166
|
+
```markdown
|
|
167
|
+
Dr. Smith met with Prof. Johnson at 3 p.m. They discussed the project etc. and other topics.
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
**Output (abbreviations preserved):**
|
|
171
|
+
|
|
172
|
+
```markdown
|
|
173
|
+
Dr. Smith met with Prof. Johnson at 3 p.m.
|
|
174
|
+
They discussed the project etc. and other topics.
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Link Protection
|
|
178
|
+
|
|
179
|
+
**Input:**
|
|
180
|
+
|
|
181
|
+
```markdown
|
|
182
|
+
Check [example.com](https://example.com). Use `config.json` for settings. Done!
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
**Output (links and code preserved):**
|
|
186
|
+
|
|
187
|
+
```markdown
|
|
188
|
+
Check [example.com](https://example.com).
|
|
189
|
+
Use `config.json` for settings.
|
|
190
|
+
Done!
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## How It Works
|
|
194
|
+
|
|
195
|
+
### Wrapping Rules
|
|
196
|
+
|
|
197
|
+
When one of the limited number of characters (`.!?:` by default) which serve as end-of-sentence markers occur alone, mdformat-slw will wrap **except** when:
|
|
198
|
+
|
|
199
|
+
1. Not in a context where auto-wrapping is possible:
|
|
200
|
+
- Inline code, links, definition lists, etc.
|
|
201
|
+
- Code Blocks
|
|
202
|
+
- Tables
|
|
203
|
+
- HTML Blocks
|
|
204
|
+
1. When the wrapped term is an abbreviation:
|
|
205
|
+
- Multiple end-of-sentence markers occur (such as `p.m.` or `e.g.`)
|
|
206
|
+
- Identified as an abbreviation from language-specific lists (`Dr.`, `Prof.`, `etc.`, etc.)
|
|
207
|
+
- Matched against custom abbreviations specified via `--slw-abbreviations`
|
|
208
|
+
- Part of user-specified suppression words via `--slw-suppressions`
|
|
209
|
+
1. Abbreviation matching is case-insensitive by default (use `--slw-case-sensitive` to change)
|
|
210
|
+
|
|
211
|
+
### Algorithm
|
|
212
|
+
|
|
213
|
+
1. Insert a line break after every character that ends a sentence which complies with the above rules and exceptions
|
|
214
|
+
1. Collapse all consecutive whitespace into a single space. While doing so, preserve both non-breaking spaces and linebreaks that are preceded by non-breaking spaces
|
|
215
|
+
1. Before line wrapping, replace all spaces in link texts by non-breaking spaces (and similar inline content that can't be wrapped)
|
|
216
|
+
1. Wrap lines that are longer than the maximum line width, if set, (80 characters by default) without splitting words or splitting at non-breaking spaces while also keeping indents in tact.
|
|
217
|
+
|
|
218
|
+
## Acknowledgments
|
|
219
|
+
|
|
220
|
+
This plugin is inspired by and named after [mdslw](https://github.com/razziel89/mdslw) by [@razziel89](https://github.com/razziel89). The original `mdslw` is an excellent standalone tool for semantic line wrapping of markdown files.
|
|
221
|
+
|
|
222
|
+
**Why create mdformat-slw?**
|
|
223
|
+
|
|
224
|
+
The [mdformat](https://github.com/executablebooks/mdformat) ecosystem uses a plugin architecture where formatters must integrate with mdformat's AST-based processing pipeline. While `mdslw` works great as a standalone tool, it cannot be used as an mdformat plugin directly. This plugin reimplements semantic line wrapping concepts to work natively within mdformat, enabling:
|
|
225
|
+
|
|
226
|
+
- Seamless integration with other mdformat plugins
|
|
227
|
+
- Consistent formatting when mdformat is your primary markdown formatter
|
|
228
|
+
- Use in pre-commit hooks alongside mdformat's other capabilities
|
|
229
|
+
|
|
230
|
+
The plugin is named `slw` (not `mdslw`) to clearly distinguish it from the original tool and avoid confusion, since the implementations differ.
|
|
231
|
+
|
|
232
|
+
## Contributing
|
|
233
|
+
|
|
234
|
+
See [CONTRIBUTING.md](https://github.com/kyleking/mdformat-slw/blob/main/CONTRIBUTING.md)
|
|
235
|
+
|
|
236
|
+
[ci-badge]: https://github.com/kyleking/mdformat-slw/actions/workflows/tests.yml/badge.svg?branch=main
|
|
237
|
+
[ci-link]: https://github.com/kyleking/mdformat-slw/actions?query=workflow%3ACI+branch%3Amain+event%3Apush
|
|
238
|
+
[pypi-badge]: https://img.shields.io/pypi/v/mdformat-slw.svg
|
|
239
|
+
[pypi-link]: https://pypi.org/project/mdformat-slw
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""An mdformat plugin for slw-style sentence wrapping."""
|
|
2
|
+
|
|
3
|
+
__version__ = "0.2.0"
|
|
4
|
+
|
|
5
|
+
__plugin_name__ = "slw"
|
|
6
|
+
|
|
7
|
+
# FYI see source code for available interfaces:
|
|
8
|
+
# https://github.com/executablebooks/mdformat/blob/5d9b573ce33bae219087984dd148894c774f41d4/src/mdformat/plugins.py
|
|
9
|
+
from .plugin import POSTPROCESSORS, RENDERERS, add_cli_argument_group, update_mdit
|
|
10
|
+
|
|
11
|
+
__all__ = ("POSTPROCESSORS", "RENDERERS", "add_cli_argument_group", "update_mdit")
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""General Helpers for configuration management."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Mapping
|
|
6
|
+
from typing import Any, TypeAlias
|
|
7
|
+
|
|
8
|
+
from . import __plugin_name__
|
|
9
|
+
|
|
10
|
+
ContextOptions: TypeAlias = Mapping[str, Any]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def get_conf(options: ContextOptions, key: str) -> bool | str | int | None:
|
|
14
|
+
"""Read setting from mdformat configuration Context.
|
|
15
|
+
|
|
16
|
+
Configuration can be provided via:
|
|
17
|
+
1. API call: mdformat.text(..., options={key: value})
|
|
18
|
+
2. CLI/TOML: stored in options["mdformat"]["plugin"]["slw"][key]
|
|
19
|
+
|
|
20
|
+
Args:
|
|
21
|
+
options: Configuration options from mdformat rendering context
|
|
22
|
+
key: Configuration key to retrieve
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
Configuration value (bool, str, int, or None if not found)
|
|
26
|
+
|
|
27
|
+
"""
|
|
28
|
+
if (api := options["mdformat"].get(key)) is not None:
|
|
29
|
+
return api # From API
|
|
30
|
+
return (
|
|
31
|
+
options["mdformat"].get("plugin", {}).get(__plugin_name__, {}).get(key)
|
|
32
|
+
) # from cli_or_toml
|