diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..729a846 --- /dev/null +++ b/.gitignore @@ -0,0 +1,177 @@ +typings/ + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# UV +# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +#uv.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/latest/usage/project/#working-with-version-control +.pdm.toml +.pdm-python +.pdm-build/ + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +# Ruff stuff: +.ruff_cache/ + +# PyPI configuration file +.pypirc + diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..2c07333 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.11 diff --git a/install.sh b/install.sh deleted file mode 100755 index f72f4c4..0000000 --- a/install.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh - -SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" -sudo cp "$SCRIPT_DIR/mailfmt" /usr/bin/mailfmt -echo "mailfmt has been installed to /usr/bin/mailfmt." diff --git a/mailfmt.py b/mailfmt.py new file mode 100755 index 0000000..fadc3dc --- /dev/null +++ b/mailfmt.py @@ -0,0 +1,231 @@ +#!/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 main() -> 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 + + if __name__ == "__main__": + 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__": + main() + +# pyright: basic diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..d8dedf2 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,17 @@ +[project] +name = "mailfmt" +version = "0.1.0" +description = "Add your description here" +readme = "README.md" +authors = [ + { name = "Daniel Fichtinger", email = "daniel@ficd.ca" } +] +requires-python = ">=3.11" +dependencies = [] + +[project.scripts] +mailfmt = "mailfmt:main" + +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" diff --git a/src/mailfmt/__init__.py b/src/mailfmt/__init__.py new file mode 100644 index 0000000..ffdcf09 --- /dev/null +++ b/src/mailfmt/__init__.py @@ -0,0 +1,6 @@ +from . import mailfmt + + +def main() -> None: + print("Hello from mail format") + mailfmt.mailfmt() diff --git a/mailfmt b/src/mailfmt/mailfmt.py similarity index 71% rename from mailfmt rename to src/mailfmt/mailfmt.py index a12e4c7..2a1f964 100755 --- a/mailfmt +++ b/src/mailfmt/mailfmt.py @@ -16,78 +16,73 @@ import sys import re import argparse -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 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 - global prev_is_parbreak - if skipping or not (parbreak and prev_is_parbreak): + def pprint(string: str): + if markdown_safe and (in_signoff or in_signature) and string: + string += " \\" + if not squash: 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() + 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, + ) -signoff_cache: str = "" + 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 + 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 - -if __name__ == "__main__": parser = argparse.ArgumentParser( description='Formatter for plain text email.\n"--no-*" options are NOT passed by default.', epilog=""" @@ -228,4 +223,8 @@ Contact: daniel@ficd.ca if out_stream is not None: out_stream.close() + +if __name__ == "__main__": + mailfmt() + # pyright: basic diff --git a/uninstall.sh b/uninstall.sh deleted file mode 100755 index e995aa0..0000000 --- a/uninstall.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh - -sudo rm /usr/bin/mailfmt -echo "mailfmt has been uninstalled." diff --git a/uv.lock b/uv.lock new file mode 100644 index 0000000..6688ee9 --- /dev/null +++ b/uv.lock @@ -0,0 +1,8 @@ +version = 1 +revision = 2 +requires-python = ">=3.11" + +[[package]] +name = "mailfmt" +version = "0.1.0" +source = { editable = "." }