multi-convert 0.1.0__py3-none-any.whl
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.
- multi_convert/__init__.py +5 -0
- multi_convert/cli.py +23 -0
- multi_convert/core.py +90 -0
- multi_convert-0.1.0.dist-info/METADATA +74 -0
- multi_convert-0.1.0.dist-info/RECORD +9 -0
- multi_convert-0.1.0.dist-info/WHEEL +5 -0
- multi_convert-0.1.0.dist-info/entry_points.txt +2 -0
- multi_convert-0.1.0.dist-info/licenses/LICENSE +21 -0
- multi_convert-0.1.0.dist-info/top_level.txt +1 -0
multi_convert/cli.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""Command line interface for multi_convert."""
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from .core import ConversionError, convert
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def main():
|
|
10
|
+
parser = argparse.ArgumentParser(
|
|
11
|
+
prog="multi-convert",
|
|
12
|
+
description="Convert documents between Markdown, HTML, and PDF.",
|
|
13
|
+
)
|
|
14
|
+
parser.add_argument("input", type=Path, help="File to convert")
|
|
15
|
+
parser.add_argument("output", type=Path, help="Destination file; the format is taken from its extension")
|
|
16
|
+
args = parser.parse_args()
|
|
17
|
+
|
|
18
|
+
try:
|
|
19
|
+
convert(args.input, args.output)
|
|
20
|
+
except ConversionError as error:
|
|
21
|
+
parser.exit(1, f"error: {error}\n")
|
|
22
|
+
|
|
23
|
+
print(f"Converted {args.input} -> {args.output}")
|
multi_convert/core.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"""Document conversion between Markdown, HTML, and PDF."""
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
import html2text
|
|
6
|
+
import markdown
|
|
7
|
+
from markdown_pdf import MarkdownPdf, Section
|
|
8
|
+
from pypdf import PdfReader
|
|
9
|
+
|
|
10
|
+
FORMATS = ("md", "html", "pdf")
|
|
11
|
+
|
|
12
|
+
_HTML_HEAD = """<!DOCTYPE html>
|
|
13
|
+
<html>
|
|
14
|
+
<head>
|
|
15
|
+
<meta charset="utf-8">
|
|
16
|
+
<style>
|
|
17
|
+
body { font-family: -apple-system, sans-serif; line-height: 1.6; max-width: 800px; margin: 2rem auto; padding: 0 1rem; }
|
|
18
|
+
code { background: #f4f4f4; padding: 2px 4px; border-radius: 4px; }
|
|
19
|
+
pre { background: #f4f4f4; padding: 1rem; overflow-x: auto; }
|
|
20
|
+
table { border-collapse: collapse; width: 100%; }
|
|
21
|
+
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
|
|
22
|
+
</style>
|
|
23
|
+
</head>
|
|
24
|
+
<body>
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
_HTML_TAIL = """
|
|
28
|
+
</body>
|
|
29
|
+
</html>
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class ConversionError(Exception):
|
|
34
|
+
"""Raised when a conversion cannot be performed."""
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _format_of(path):
|
|
38
|
+
fmt = path.suffix.lower().lstrip(".")
|
|
39
|
+
if fmt not in FORMATS:
|
|
40
|
+
raise ConversionError(f"Unsupported format {path.suffix!r}; expected one of {', '.join(FORMATS)}")
|
|
41
|
+
return fmt
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _read_md(path):
|
|
45
|
+
return path.read_text(encoding="utf-8")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _read_html(path):
|
|
49
|
+
handler = html2text.HTML2Text()
|
|
50
|
+
handler.ignore_links = False
|
|
51
|
+
return handler.handle(path.read_text(encoding="utf-8"))
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _read_pdf(path):
|
|
55
|
+
pages = (page.extract_text() for page in PdfReader(path).pages)
|
|
56
|
+
return "\n\n".join(page for page in pages if page)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _write_md(content, path):
|
|
60
|
+
path.write_text(content, encoding="utf-8")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _write_html(content, path):
|
|
64
|
+
body = markdown.markdown(content, extensions=["tables", "fenced_code"])
|
|
65
|
+
path.write_text(_HTML_HEAD + body + _HTML_TAIL, encoding="utf-8")
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _write_pdf(content, path):
|
|
69
|
+
pdf = MarkdownPdf(toc_level=2)
|
|
70
|
+
pdf.add_section(Section(content))
|
|
71
|
+
pdf.save(str(path))
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
_READERS = {"md": _read_md, "html": _read_html, "pdf": _read_pdf}
|
|
75
|
+
_WRITERS = {"md": _write_md, "html": _write_html, "pdf": _write_pdf}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def convert(source, target):
|
|
79
|
+
"""Convert ``source`` to ``target``, inferring both formats from file extensions.
|
|
80
|
+
|
|
81
|
+
Every conversion routes through Markdown as an intermediate representation, so
|
|
82
|
+
reading a PDF yields extracted plain text rather than structured markup.
|
|
83
|
+
"""
|
|
84
|
+
source, target = Path(source), Path(target)
|
|
85
|
+
if not source.is_file():
|
|
86
|
+
raise ConversionError(f"Input file not found: {source}")
|
|
87
|
+
|
|
88
|
+
reader, writer = _READERS[_format_of(source)], _WRITERS[_format_of(target)]
|
|
89
|
+
writer(reader(source), target)
|
|
90
|
+
return target
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: multi_convert
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A python package for converting multiple document formats
|
|
5
|
+
Home-page: https://github.com/tasteslikecobrachicken/multi_convert
|
|
6
|
+
Author: Cobra Chicken
|
|
7
|
+
Author-email: Cobra Chicken <tasteslikecobrachicken@gmail.com>
|
|
8
|
+
Project-URL: Homepage, https://github.com/tasteslikecobrachicken/multi_convert
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.10
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: markdown>=3.5
|
|
16
|
+
Requires-Dist: markdown-pdf>=1.4
|
|
17
|
+
Requires-Dist: html2text>=2024.2.26
|
|
18
|
+
Requires-Dist: pypdf>=4.0
|
|
19
|
+
Provides-Extra: test
|
|
20
|
+
Requires-Dist: pytest>=8; extra == "test"
|
|
21
|
+
Dynamic: author
|
|
22
|
+
Dynamic: home-page
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
Dynamic: requires-python
|
|
25
|
+
|
|
26
|
+
# multi_convert
|
|
27
|
+
|
|
28
|
+
Python package for converting document formats between Markdown, HTML, and PDF.
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pip install multi_convert
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Requires Python 3.10 or newer.
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
### Library
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
from multi_convert import convert
|
|
44
|
+
|
|
45
|
+
convert("notes.md", "notes.pdf")
|
|
46
|
+
convert("page.html", "page.md")
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Both formats are inferred from the file extensions. `convert` returns the output
|
|
50
|
+
path and raises `ConversionError` if the input is missing or either extension is
|
|
51
|
+
not one of `.md`, `.html`, or `.pdf`.
|
|
52
|
+
|
|
53
|
+
### Command line
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
multi-convert notes.md notes.pdf
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Supported conversions
|
|
60
|
+
|
|
61
|
+
| From | To |
|
|
62
|
+
| --- | --- |
|
|
63
|
+
| Markdown | HTML, PDF |
|
|
64
|
+
| HTML | Markdown, PDF |
|
|
65
|
+
| PDF | Markdown, HTML |
|
|
66
|
+
|
|
67
|
+
Every conversion routes through Markdown as an intermediate representation.
|
|
68
|
+
Because PDF text extraction recovers plain text only, converting *from* PDF
|
|
69
|
+
loses headings, tables, and emphasis; the other five directions preserve
|
|
70
|
+
structure.
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
MIT
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
multi_convert/__init__.py,sha256=nGxvTj9Tj9GQ7PtG4SIO1nVFJ0pSsc5dnqHVl432o6c,163
|
|
2
|
+
multi_convert/cli.py,sha256=rMe6E8kid5uKCW1umG_g1qpWcrBMyuMp6cVeuHZtnm0,692
|
|
3
|
+
multi_convert/core.py,sha256=y5Tsf-HzsjrT1A-C-5ci7hTvVh6aHvSycDboRh9Zqwc,2508
|
|
4
|
+
multi_convert-0.1.0.dist-info/licenses/LICENSE,sha256=vxvQmJ-9FHlKlumoI5ptuBf6EwR6w87UkfIq_jJ5_0Y,1073
|
|
5
|
+
multi_convert-0.1.0.dist-info/METADATA,sha256=hJ0FYhqiViFEvBP1KOreKPq_Vys9ci4Zg6SE30shVvI,1829
|
|
6
|
+
multi_convert-0.1.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
7
|
+
multi_convert-0.1.0.dist-info/entry_points.txt,sha256=HPVT6gV0xkqPluiAnu6ut6K7hxRNXPfyH6CJ-elPT7I,57
|
|
8
|
+
multi_convert-0.1.0.dist-info/top_level.txt,sha256=56Vm8Ow94pRFPkINmKgixtk0tVQr__x5EvgEJjdM-94,14
|
|
9
|
+
multi_convert-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) [2026] [Cobra Chicken]
|
|
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 @@
|
|
|
1
|
+
multi_convert
|