Compare commits

..

14 commits
pkg ... main

8 changed files with 248 additions and 414 deletions

View file

@ -0,0 +1,20 @@
on:
push:
tags:
- 'v*'
jobs:
publish:
runs-on: based-alpine
steps:
- uses: actions/checkout@v4
- name: setup cache
id: uv-cache
uses: https://git.ficd.sh/ficd/uv-cache@v1
- name: build
run: |
uv sync
uv build
- name: publish
run: |
uv publish --token ${{ secrets.PYPI_TOKEN }}

View file

@ -1,22 +1,21 @@
<h1>Mail Format</h1> <h1>Mail Format</h1>
`mailfmt` is a simple plain text email formatter. It's designed to ensure `mailfmt` is a simple plain text email formatter. It's designed to ensure
consistent paragraph spacing while preserving markdown syntax, email headers, consistent paragraph spacing while preserving markdown syntax, email
sign-offs, and signature blocks. headers, sign-offs, and signature blocks.
By default, this script accepts its input on `stdin` and prints to `stdout`. By default, the command accepts its input on `stdin` and prints to
This makes it well suited for use as a formatter with a text editor like Kakoune `stdout`. This makes it well suited for use as a formatter with a text
or Helix. It has no dependencies besides the standard Python interpreter, and editor like Kakoune or Helix.
was written and tested against Python 3.13.3.
<!--toc:start--> <!--toc:start-->
- [Features](#features) - [Features](#features)
- [Installation](#installation)
- [Usage](#usage) - [Usage](#usage)
- [Output Example](#output-example) - [Output Example](#output-example)
- [Markdown Safety](#markdown-safety) - [Markdown Safety](#markdown-safety)
- [Aerc Integration](#aerc-integration) - [Aerc Integration](#aerc-integration)
- [Contributing](#contributing)
<!--toc:end--> <!--toc:end-->
@ -33,9 +32,27 @@ was written and tested against Python 3.13.3.
- Markdown-style code blocks. - Markdown-style code blocks.
- Usenet-style signature block at EOF. - Usenet-style signature block at EOF.
- Sign-offs. - Sign-offs.
- If specified, output can be made safe for passing to a Markdown renderer. - If specified, output can be made safe for passing to a Markdown
- Use case: piping the output to `pandoc` to write a `text/html` message. See renderer.
[Markdown Safety](#markdown-safety). - Use case: piping the output to `pandoc` to write a `text/html`
message. See [Markdown Safety](#markdown-safety).
## Installation
`mailfmt` is intended for use as a standaole tool. The package is
available on PyPI as `mailfmt`. I recommend using
[uv](https://github.com/astral-sh/uv) or `pipx` to install it so the
`mailfmt` command is available on your path:
```sh
uv tool install mailfmt
```
Verify that the installation was successful:
```sh
mailfmt --help
```
## Usage ## Usage
@ -63,8 +80,7 @@ options:
-o, --output OUTPUT Output file. (default: STDOUT) -o, --output OUTPUT Output file. (default: STDOUT)
Author : Daniel Fichtinger Author : Daniel Fichtinger
License: ISC Contact: daniel@ficd.sh
Contact: daniel@ficd.ca
``` ```
## Output Example ## Output Example
@ -87,8 +103,7 @@ Daniel
-- --
Daniel Daniel
sr.ht/~ficd daniel@ficd.sh
daniel@ficd.ca
``` ```
After: After:
@ -110,23 +125,24 @@ Daniel
-- --
Daniel Daniel
sr.ht/~ficd daniel@ficd.sh
daniel@ficd.ca
``` ```
## Markdown Safety ## Markdown Safety
In some cases, you may want to generate an HTML email. Ideally, you'd want the In some cases, you may want to generate an HTML email. Ideally, you'd want
HTML to be generated directly from the plain text message, and for _both_ the HTML to be generated directly from the plain text message, and for
versions to be legible and have the same semantics. _both_ versions to be legible and have the same semantics.
Although `mailfmt` was written with Markdown markup in mind, its intended output Although `mailfmt` was written with Markdown markup in mind, its intended
is still the `text/plain` format. If you pass its output directly to a Markdown output is still the `text/plain` format. If you pass its output directly
renderer, line breaks in sign-offs and the signature block won't be preserved. to a Markdown renderer, line breaks in sign-offs and the signature block
won't be preserved.
If you invoke `mailfmt --markdown-safe`, then `\` characters will be appended to If you invoke `mailfmt --markdown-safe`, then `\` characters will be
mark line breaks that would otherwise be squashed, making the output suitable appended to mark line breaks that would otherwise be squashed, making the
for conversion into HTML. Here's an example of one such pipeline: output suitable for conversion into HTML. Here's an example of one such
pipeline:
```bash ```bash
cat message.txt | mailfmt --markdown-safe | pandoc -f markdown -t html cat message.txt | mailfmt --markdown-safe | pandoc -f markdown -t html
@ -152,24 +168,20 @@ Daniel \
-- \ -- \
Daniel \ Daniel \
sr.ht/~ficd \ daniel@ficd.sh \
daniel@ficd.ca \
``` ```
## Aerc Integration ## Aerc Integration
For integration with `aerc`, consider adding the following to your `aerc.conf`: For integration with `aerc`, consider adding the following to your
`aerc.conf`:
```ini ```ini
[multipart-converters] [multipart-converters]
text/html=mailfmt --markdown-safe | pandoc -f markdown -t html --standalone text/html=mailfmt --markdown-safe | pandoc -f markdown -t html --standalone
``` ```
When you're done writing your email, you can call the `:multipart text/html` When you're done writing your email, you can call the
command to generate a `multipart/alternative` message which includes _both_ your `:multipart text/html` command to generate a `multipart/alternative`
original `text/plain` _and_ the newly generated `text/html` content. message which includes _both_ your original `text/plain` _and_ the newly
generated `text/html` content.
## Contributing
Please send patches, requests, and concerns to my
[public inbox](https://lists.sr.ht/~ficd/public-inbox).

21
justfile Normal file
View file

@ -0,0 +1,21 @@
clean:
#!/bin/sh
if [ ! -d "dist" ] && [ ! -d "__pycache__" ]; then
echo "Nothing to clean."
exit 0
fi
if [ -d "dist" ]; then
echo "Removing dist/"
rm -r dist/
fi
if [ -d "__pycache__" ]; then
echo "Removing __pycache__/"
rm -r "__pycache__"
fi
publish:
#!/bin/sh
just clean
uv build
export UV_PUBLISH_TOKEN="$(pass show pypi)"
uv publish

View file

@ -11,10 +11,11 @@
# Author: Daniel Fichtinger # Author: Daniel Fichtinger
# License: ISC # License: ISC
import textwrap
import sys
import re
import argparse import argparse
import re
import sys
import textwrap
from importlib.metadata import version
def main() -> None: def main() -> None:
@ -75,7 +76,7 @@ def main() -> None:
if not signoff_cache and 1 <= n <= 5 and line[-1] == ",": if not signoff_cache and 1 <= n <= 5 and line[-1] == ",":
return True return True
# second potential line # second potential line
elif signoff_cache and 1 <= n <= 5 and line[-1].isalpha(): elif signoff_cache and 1 <= n <= 5:
for w in words: for w in words:
if not w[0].isupper(): if not w[0].isupper():
return False return False
@ -83,146 +84,156 @@ def main() -> None:
else: else:
return False return False
if __name__ == "__main__": parser = argparse.ArgumentParser(
parser = argparse.ArgumentParser( description="Heuristic formatter for plain text email. Preserves markup, signoffs, and signature blocks.",
description='Formatter for plain text email.\n"--no-*" options are NOT passed by default.', epilog="""
epilog=""" Author : Daniel Fichtinger
Author : Daniel Fichtinger Repository: https://git.ficd.sh/ficd/mailfmt
License: ISC License : ISC
Contact: daniel@ficd.ca Contact : daniel@ficd.sh
""", """,
formatter_class=argparse.RawDescriptionHelpFormatter, formatter_class=argparse.RawDescriptionHelpFormatter,
) )
parser.add_argument( parser.add_argument(
"-w", "-v",
"--width", "--version",
required=False, required=False,
help="Text width for wrapping. (default: %(default)s)", help="Print version info and exit.",
default=width, action="store_true",
type=int, )
) parser.add_argument(
parser.add_argument( "-w",
"-b", "--width",
"--break-long-words", required=False,
required=False, help="Text width for wrapping. (default: %(default)s)",
help="Break long words while wrapping. (default: %(default)s)", default=width,
action="store_true", type=int,
) )
parser.add_argument( parser.add_argument(
"--no-replace-whitespace", "-b",
required=False, "--break-long-words",
help="Don't normalize whitespace when wrapping.", required=False,
action="store_false", help="Break long words while wrapping. (default: %(default)s)",
) action="store_true",
parser.add_argument( )
"--no-reflow", parser.add_argument(
required=False, "--no-replace-whitespace",
help="Don't reflow lines.", required=False,
action="store_false", help="Don't normalize whitespace when wrapping.",
) action="store_false",
parser.add_argument( )
"--no-signoff", parser.add_argument(
required=False, "--no-reflow",
help="Don't preserve signoff line breaks.", required=False,
action="store_false", help="Don't reflow lines.",
) action="store_false",
parser.add_argument( )
"--no-signature", parser.add_argument(
required=False, "--no-signoff",
help="Don't preserve signature block.", required=False,
action="store_false", help="Don't preserve signoff line breaks.",
) action="store_false",
parser.add_argument( )
"--no-squash", parser.add_argument(
required=False, "--no-signature",
help="Don't squash consecutive paragraph breaks.", required=False,
action="store_false", help="Don't preserve signature block.",
) action="store_false",
parser.add_argument( )
"-m", parser.add_argument(
"--markdown-safe", "--no-squash",
required=False, required=False,
help="Output format safe for Markdown rendering.", help="Don't squash consecutive paragraph breaks.",
action="store_true", action="store_false",
) )
parser.add_argument( parser.add_argument(
"-i", "-m",
"--input", "--markdown-safe",
required=False, required=False,
type=str, help="Output format safe for Markdown rendering.",
default="STDIN", action="store_true",
help="Input file. (default: %(default)s)", )
) parser.add_argument(
parser.add_argument( "-i",
"-o", "--input",
"--output", required=False,
required=False, type=str,
type=str, default="STDIN",
default="STDOUT", help="Input file. (default: %(default)s)",
help="Output file. (default: %(default)s)", )
) parser.add_argument(
args = parser.parse_args() "-o",
width = args.width "--output",
should_check_signoff = args.no_signoff required=False,
should_check_signature = args.no_signature type=str,
reflow = args.no_reflow default="STDOUT",
squash = args.no_squash help="Output file. (default: %(default)s)",
replace_whitespace = args.no_replace_whitespace )
break_long_words = args.break_long_words args = parser.parse_args()
markdown_safe = args.markdown_safe if args.version:
print(version("mailfmt"))
exit(0)
width = args.width
should_check_signoff = args.no_signoff
should_check_signature = args.no_signature
reflow = args.no_reflow
squash = args.no_squash
replace_whitespace = args.no_replace_whitespace
break_long_words = args.break_long_words
markdown_safe = args.markdown_safe
if args.input == "STDIN": if args.input == "STDIN":
reader = sys.stdin reader = sys.stdin
else: else:
with open(args.input, "r") as in_stream: with open(args.input, "r") as in_stream:
reader = in_stream reader = in_stream
if args.output != "STDOUT": if args.output != "STDOUT":
out_stream = open(args.output, "w") out_stream = open(args.output, "w")
for line in reader: for line in reader:
line = line.rstrip() line = line.rstrip()
if should_check_signoff: if should_check_signoff:
is_signoff = check_signoff(line) is_signoff = check_signoff(line)
if is_signoff: if is_signoff:
in_signoff = True in_signoff = True
if not signoff_cache: if not signoff_cache:
signoff_cache = line signoff_cache = line
else: else:
pprint(signoff_cache) pprint(signoff_cache)
pprint(line) pprint(line)
in_signoff = False
signoff_cache = ""
continue
elif not is_signoff and signoff_cache:
paragraph.append(signoff_cache)
signoff_cache = ""
in_signoff = False in_signoff = False
if line.startswith("```"): signoff_cache = ""
flush_paragraph() continue
skipping = not skipping elif not is_signoff and signoff_cache:
pprint(line)
elif should_check_signature and line == "--":
flush_paragraph()
skipping = True
in_signature = True
pprint("-- ")
elif not line or re.match(
r"^(\s+|-\s+|\+\s+|\*\s+|>\s*|#\s+|From:|To:|Cc:|Bcc:|Subject:|Reply-To:|In-Reply-To:|References:|Date:|Message-Id:|User-Agent:)",
line,
):
flush_paragraph()
pprint(line)
elif skipping:
pprint(line)
else:
paragraph.append(line)
else:
if signoff_cache:
paragraph.append(signoff_cache) paragraph.append(signoff_cache)
signoff_cache = "" signoff_cache = ""
in_signoff = False
if line.startswith("```"):
flush_paragraph() flush_paragraph()
if out_stream is not None: skipping = not skipping
out_stream.close() pprint(line)
elif should_check_signature and line == "--":
flush_paragraph()
skipping = True
in_signature = True
pprint("-- ")
elif not line or re.match(
r"^(\s+|-\s+|\+\s+|\*\s+|>\s*|#\s+|From:|To:|Cc:|Bcc:|Subject:|Reply-To:|In-Reply-To:|References:|Date:|Message-Id:|User-Agent:)",
line,
):
flush_paragraph()
pprint(line)
elif skipping:
pprint(line)
else:
paragraph.append(line)
else:
if signoff_cache:
paragraph.append(signoff_cache)
signoff_cache = ""
flush_paragraph()
if out_stream is not None:
out_stream.close()
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -1,13 +1,19 @@
[project] [project]
name = "mailfmt" name = "mailfmt"
version = "0.1.0" version = "1.0.4"
description = "Add your description here" description = "Heuristic plain text email formatter."
readme = "README.md" readme = "README.md"
authors = [ authors = [
{ name = "Daniel Fichtinger", email = "daniel@ficd.ca" } { name = "Daniel Fichtinger", email = "daniel@ficd.sh" }
] ]
requires-python = ">=3.11" requires-python = ">=3.11"
dependencies = [] dependencies = []
license = "ISC"
license-files = ["LICENSE"]
keywords = ["email", "formatter", "cli"]
[project.urls]
Repository = "https://git.ficd.sh/ficd/mailfmt"
[project.scripts] [project.scripts]
mailfmt = "mailfmt:main" mailfmt = "mailfmt:main"

View file

@ -1,6 +0,0 @@
from . import mailfmt
def main() -> None:
print("Hello from mail format")
mailfmt.mailfmt()

View file

@ -1,230 +0,0 @@
#!/bin/env python
# Simple text-wrapping script for email.
# Preserves code blocks, quotes, and signature.
# Automatically joins and re-wraps paragraphs to
# ensure even spacing & avoid ugly wrapping.
# Preserves signoffs.
# Signoff heuristic:
# 1-5 words ending with a comma, followed by
# 1-5 words that each start with capital letters.
# Author: Daniel Fichtinger
# License: ISC
import textwrap
import sys
import re
import argparse
def mailfmt() -> None:
paragraph: list[str] = []
skipping = False
squash = True
prev_is_parbreak = False
out_stream = sys.stdout
reflow = True
width = 74
break_long_words = False
replace_whitespace = True
markdown_safe = False
in_signoff = False
in_signature = False
def pprint(string: str):
if markdown_safe and (in_signoff or in_signature) and string:
string += " \\"
if not squash:
print(string, file=out_stream)
else:
parbreak = not string
nonlocal prev_is_parbreak
if skipping or not (parbreak and prev_is_parbreak):
print(string, file=out_stream)
prev_is_parbreak = parbreak
def wrap(text: str):
return textwrap.wrap(
text,
width=width,
break_long_words=break_long_words,
replace_whitespace=replace_whitespace,
)
def flush_paragraph():
if paragraph:
if reflow:
joined = " ".join(paragraph)
wrapped = wrap(joined)
pprint("\n".join(wrapped))
else:
for line in paragraph:
for wrapped_line in wrap(line):
pprint(wrapped_line)
paragraph.clear()
signoff_cache: str = ""
def check_signoff(line: str) -> bool:
if not line:
return False
words = line.split()
n = len(words)
# first potential signoff line
if not signoff_cache and 1 <= n <= 5 and line[-1] == ",":
return True
# second potential line
elif signoff_cache and 1 <= n <= 5 and line[-1].isalpha():
for w in words:
if not w[0].isupper():
return False
return True
else:
return False
parser = argparse.ArgumentParser(
description='Formatter for plain text email.\n"--no-*" options are NOT passed by default.',
epilog="""
Author : Daniel Fichtinger
License: ISC
Contact: daniel@ficd.ca
""",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"-w",
"--width",
required=False,
help="Text width for wrapping. (default: %(default)s)",
default=width,
type=int,
)
parser.add_argument(
"-b",
"--break-long-words",
required=False,
help="Break long words while wrapping. (default: %(default)s)",
action="store_true",
)
parser.add_argument(
"--no-replace-whitespace",
required=False,
help="Don't normalize whitespace when wrapping.",
action="store_false",
)
parser.add_argument(
"--no-reflow",
required=False,
help="Don't reflow lines.",
action="store_false",
)
parser.add_argument(
"--no-signoff",
required=False,
help="Don't preserve signoff line breaks.",
action="store_false",
)
parser.add_argument(
"--no-signature",
required=False,
help="Don't preserve signature block.",
action="store_false",
)
parser.add_argument(
"--no-squash",
required=False,
help="Don't squash consecutive paragraph breaks.",
action="store_false",
)
parser.add_argument(
"-m",
"--markdown-safe",
required=False,
help="Output format safe for Markdown rendering.",
action="store_true",
)
parser.add_argument(
"-i",
"--input",
required=False,
type=str,
default="STDIN",
help="Input file. (default: %(default)s)",
)
parser.add_argument(
"-o",
"--output",
required=False,
type=str,
default="STDOUT",
help="Output file. (default: %(default)s)",
)
args = parser.parse_args()
width = args.width
should_check_signoff = args.no_signoff
should_check_signature = args.no_signature
reflow = args.no_reflow
squash = args.no_squash
replace_whitespace = args.no_replace_whitespace
break_long_words = args.break_long_words
markdown_safe = args.markdown_safe
if args.input == "STDIN":
reader = sys.stdin
else:
with open(args.input, "r") as in_stream:
reader = in_stream
if args.output != "STDOUT":
out_stream = open(args.output, "w")
for line in reader:
line = line.rstrip()
if should_check_signoff:
is_signoff = check_signoff(line)
if is_signoff:
in_signoff = True
if not signoff_cache:
signoff_cache = line
else:
pprint(signoff_cache)
pprint(line)
in_signoff = False
signoff_cache = ""
continue
elif not is_signoff and signoff_cache:
paragraph.append(signoff_cache)
signoff_cache = ""
in_signoff = False
if line.startswith("```"):
flush_paragraph()
skipping = not skipping
pprint(line)
elif should_check_signature and line == "--":
flush_paragraph()
skipping = True
in_signature = True
pprint("-- ")
elif not line or re.match(
r"^(\s+|-\s+|\+\s+|\*\s+|>\s*|#\s+|From:|To:|Cc:|Bcc:|Subject:|Reply-To:|In-Reply-To:|References:|Date:|Message-Id:|User-Agent:)",
line,
):
flush_paragraph()
pprint(line)
elif skipping:
pprint(line)
else:
paragraph.append(line)
else:
if signoff_cache:
paragraph.append(signoff_cache)
signoff_cache = ""
flush_paragraph()
if out_stream is not None:
out_stream.close()
if __name__ == "__main__":
mailfmt()
# pyright: basic

2
uv.lock generated
View file

@ -4,5 +4,5 @@ requires-python = ">=3.11"
[[package]] [[package]]
name = "mailfmt" name = "mailfmt"
version = "0.1.0" version = "1.0.3"
source = { editable = "." } source = { editable = "." }