epub2md 0.1.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.
@@ -0,0 +1,10 @@
1
+ # Python-generated files
2
+ __pycache__/
3
+ *.py[oc]
4
+ build/
5
+ dist/
6
+ wheels/
7
+ *.egg-info
8
+
9
+ # Virtual environments
10
+ .venv
@@ -0,0 +1 @@
1
+ 3.13
epub2md-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024
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.
epub2md-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,38 @@
1
+ Metadata-Version: 2.4
2
+ Name: epub2md
3
+ Version: 0.1.0
4
+ Summary: Convert EPUB to clean Markdown chapters
5
+ Project-URL: Repository, https://github.com/mefengl/epub2md
6
+ License-File: LICENSE
7
+ Requires-Python: >=3.8
8
+ Description-Content-Type: text/markdown
9
+
10
+ # epub2md
11
+
12
+ Convert EPUB to clean Markdown chapters.
13
+
14
+ ## Install
15
+
16
+ ```bash
17
+ pip install epub2md
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ```bash
23
+ epub2md book.epub
24
+ epub2md book.epub my-chapters
25
+ ```
26
+
27
+ Output:
28
+ - `chapters/` - Markdown files
29
+ - `chapters-media/` - Images
30
+
31
+ ## Requirements
32
+
33
+ - Python 3.8+
34
+ - [pandoc](https://pandoc.org/installing.html)
35
+
36
+ ## License
37
+
38
+ MIT
@@ -0,0 +1,29 @@
1
+ # epub2md
2
+
3
+ Convert EPUB to clean Markdown chapters.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pip install epub2md
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```bash
14
+ epub2md book.epub
15
+ epub2md book.epub my-chapters
16
+ ```
17
+
18
+ Output:
19
+ - `chapters/` - Markdown files
20
+ - `chapters-media/` - Images
21
+
22
+ ## Requirements
23
+
24
+ - Python 3.8+
25
+ - [pandoc](https://pandoc.org/installing.html)
26
+
27
+ ## License
28
+
29
+ MIT
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/env python3
2
+ import sys, os, re, subprocess, tempfile, shutil
3
+ import xml.etree.ElementTree as ET
4
+ from pathlib import Path
5
+
6
+ LUA = """
7
+ function Div(el) return el.content end
8
+ function Span(el) return el.content end
9
+ function Para(el)
10
+ if el.content and #el.content==1 and el.content[1].t=='Str' and el.content[1].text=='\\\\' then return {} end
11
+ return el
12
+ end
13
+ function Plain(el)
14
+ if el.content and #el.content==1 and el.content[1].t=='Str' and el.content[1].text=='\\\\' then return {} end
15
+ return el
16
+ end
17
+ function Image(el) el.classes={} el.attributes={} return el end
18
+ """
19
+
20
+ def main():
21
+ if len(sys.argv) < 2 or sys.argv[1] in ['-h', '--help']:
22
+ print("epub2md - Convert EPUB to Markdown\n\nUsage: epub2md <book.epub> [outdir]\n\nOutput:\n <outdir>/: Markdown chapters\n <outdir>-media/: Images")
23
+ sys.exit(0)
24
+
25
+ epub = Path(sys.argv[1]).resolve()
26
+ out = sys.argv[2] if len(sys.argv) > 2 else 'chapters'
27
+
28
+ if not epub.exists(): sys.exit(f"Error: {epub} not found")
29
+ if not shutil.which('pandoc'): sys.exit("Error: pandoc not found. Install: brew install pandoc")
30
+
31
+ print(f"Converting {epub.name}...")
32
+ outdir = Path(out).resolve()
33
+ media = Path(f"{out}-media").resolve()
34
+ outdir.mkdir(exist_ok=True)
35
+
36
+ with tempfile.TemporaryDirectory() as tmp:
37
+ t = Path(tmp)
38
+ subprocess.run(['unzip', '-q', str(epub), '-d', str(t)], check=True)
39
+ (t/'f.lua').write_text(LUA)
40
+
41
+ toc = t/'toc.ncx'
42
+ if not toc.exists(): sys.exit("Error: toc.ncx not found")
43
+
44
+ tree = ET.parse(toc)
45
+ ns = {'n': 'http://www.daisy.org/z3986/2005/ncx/'}
46
+
47
+ n = 0
48
+ for nav in tree.findall('.//n:navPoint', ns):
49
+ te = nav.find('.//n:text', ns)
50
+ ce = nav.find('.//n:content', ns)
51
+ if te is None or ce is None: continue
52
+
53
+ title = te.text or 'untitled'
54
+ src = ce.get('src', '').split('#')[0]
55
+ if not src.endswith(('.xhtml', '.html')): continue
56
+ if not (t/src).exists(): continue
57
+
58
+ n += 1
59
+ safe = re.sub(r'[^a-z0-9]+', '-', title.lower()).strip('-') or 'untitled'
60
+ name = outdir / f"{n:02d}-{safe}.md"
61
+
62
+ r = subprocess.run([
63
+ 'pandoc', src, '-f', 'html', '-t', 'gfm', '--wrap=none',
64
+ '--lua-filter', 'f.lua', '--extract-media', str(media), '-o', str(name)
65
+ ], cwd=t, capture_output=True)
66
+
67
+ if r.returncode == 0:
68
+ print(f"✓ {n:02d} {title}")
69
+ else:
70
+ print(f"✗ {title}")
71
+
72
+ print(f"\nDone! {n} chapters → {out}/")
73
+ if media.exists() and list(media.iterdir()):
74
+ print(f"Images → {media.name}/")
75
+
76
+ if __name__ == '__main__':
77
+ main()
@@ -0,0 +1,17 @@
1
+ [project]
2
+ name = "epub2md"
3
+ version = "0.1.0"
4
+ description = "Convert EPUB to clean Markdown chapters"
5
+ readme = "README.md"
6
+ requires-python = ">=3.8"
7
+ dependencies = []
8
+
9
+ [project.scripts]
10
+ epub2md = "epub2md:main"
11
+
12
+ [project.urls]
13
+ Repository = "https://github.com/mefengl/epub2md"
14
+
15
+ [build-system]
16
+ requires = ["hatchling"]
17
+ build-backend = "hatchling.build"
epub2md-0.1.0/uv.lock ADDED
@@ -0,0 +1,8 @@
1
+ version = 1
2
+ revision = 3
3
+ requires-python = ">=3.8"
4
+
5
+ [[package]]
6
+ name = "epub2md"
7
+ version = "0.1.0"
8
+ source = { editable = "." }