File size: 9,594 Bytes
8ede856
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env python3
from __future__ import annotations

import argparse
import re
import shutil
import subprocess
import sys
import tempfile
from collections.abc import Iterable, Sequence
from pathlib import Path
from urllib.parse import quote

IMAGE_EXTS = {
    ".png",
    ".jpg",
    ".jpeg",
    ".gif",
    ".webp",
    ".svg",
    ".avif",
    ".bmp",
    ".ico",
    ".tif",
    ".tiff",
}

MD_IMAGE_RE = re.compile(r"!\[[^\]]*\]\(([^)]+)\)")
HTML_IMG_RE = re.compile(
    r"<img\b[^>]*\bsrc\s*=\s*([\"'])([^\"']+)\1[^>]*>", re.IGNORECASE
)


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description="Upload all locally referenced images from Markdown docs to Cloudflare R2 using rclone."
    )
    parser.add_argument("--remote", required=True, help="rclone remote name, e.g. r2")
    parser.add_argument("--bucket", default="", help="bucket name in remote path")
    parser.add_argument(
        "--prefix",
        default="docs-images",
        help="destination prefix inside bucket/remote (default: docs-images)",
    )
    parser.add_argument(
        "--docs-root",
        default=".",
        help="docs root to scan for .md files (default: current directory)",
    )
    parser.add_argument(
        "--dry-run", action="store_true", help="preview uploads without sending files"
    )
    parser.add_argument(
        "--list-only", action="store_true", help="only print matched image files"
    )
    parser.add_argument(
        "--rewrite-markdown",
        action="store_true",
        help="rewrite local image links in markdown/html to public URL after upload",
    )
    parser.add_argument(
        "--public-base-url",
        default="",
        help="public URL base used for replacement, e.g. https://cdn.example.com/docs",
    )
    parser.add_argument(
        "--backup-ext",
        default=".bak",
        help="backup extension used when rewriting markdown (default: .bak)",
    )
    return parser.parse_args()


def is_local_ref(ref: str) -> bool:
    lower = ref.lower()
    return not (
        lower.startswith("http://")
        or lower.startswith("https://")
        or lower.startswith("//")
        or lower.startswith("data:")
        or lower.startswith("mailto:")
    )


def parse_md_ref(raw: str) -> str:
    ref = raw.strip()
    if ref.startswith("<") and ">" in ref:
        ref = ref[1 : ref.find(">")]
    else:
        ref = re.split(r"\s+", ref, maxsplit=1)[0]
    ref = ref.split("#", 1)[0].split("?", 1)[0]
    return ref.strip()


def clean_ref(raw: str) -> str:
    ref = raw.strip().strip("<>")
    ref = ref.split("#", 1)[0].split("?", 1)[0]
    return ref.strip()


def resolve_local_ref(md_file: Path, ref: str, root: Path) -> Path | None:
    if not ref:
        return None
    if ref.startswith("/"):
        candidate = root / ref.lstrip("/")
    else:
        candidate = (md_file.parent / ref).resolve()

    try:
        resolved = candidate.resolve()
    except FileNotFoundError:
        return None

    if not resolved.is_file():
        return None

    try:
        resolved.relative_to(root)
    except ValueError:
        return None

    if resolved.suffix.lower() not in IMAGE_EXTS:
        return None

    return resolved


def find_markdown_files(root: Path) -> list[Path]:
    files: list[Path] = []
    for path in root.rglob("*.md"):
        if "node_modules" in path.parts:
            continue
        files.append(path)
    return sorted(files)


def collect_images(
    root: Path, md_files: Sequence[Path]
) -> tuple[set[Path], list[tuple[Path, str]]]:
    images: set[Path] = set()
    missing: list[tuple[Path, str]] = []

    for md_file in md_files:
        text = md_file.read_text(encoding="utf-8")

        for m in MD_IMAGE_RE.finditer(text):
            ref = parse_md_ref(m.group(1))
            if not ref or not is_local_ref(ref):
                continue
            resolved = resolve_local_ref(md_file, ref, root)
            if resolved:
                images.add(resolved)
            else:
                missing.append((md_file, ref))

        for m in HTML_IMG_RE.finditer(text):
            ref = clean_ref(m.group(2))
            if not ref or not is_local_ref(ref):
                continue
            resolved = resolve_local_ref(md_file, ref, root)
            if resolved:
                images.add(resolved)
            else:
                missing.append((md_file, ref))

    return images, missing


def build_target(remote: str, bucket: str, prefix: str) -> str:
    target = f"{remote}:"
    if bucket:
        target = f"{remote}:{bucket}"

    p = prefix.strip("/")
    if p:
        target = f"{target}/{p}"

    return target


def rel_object_path(root: Path, image_path: Path, prefix: str) -> str:
    rel = image_path.relative_to(root).as_posix()
    p = prefix.strip("/")
    return f"{p}/{rel}" if p else rel


def build_public_url(base: str, object_path: str) -> str:
    base = base.rstrip("/")
    encoded_path = quote(object_path, safe="/-._~")
    return f"{base}/{encoded_path}"


def run_rclone_upload(
    root: Path, target: str, rel_files: Iterable[str], dry_run: bool
) -> None:
    if shutil.which("rclone") is None:
        raise RuntimeError("rclone not found in PATH")

    with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", delete=False) as tmp:
        tmp_path = Path(tmp.name)
        for rel in rel_files:
            tmp.write(f"{rel}\n")

    try:
        cmd = [
            "rclone",
            "copy",
            str(root),
            target,
            "--files-from",
            str(tmp_path),
            "--create-empty-src-dirs",
        ]
        if dry_run:
            cmd.append("--dry-run")

        print()
        if dry_run:
            print("Dry-run:", " ".join(cmd))
        else:
            print(f"Uploading to: {target}")

        subprocess.run(cmd, check=True)
    finally:
        tmp_path.unlink(missing_ok=True)


def rewrite_markdown_files(
    root: Path,
    md_files: Sequence[Path],
    image_set: set[Path],
    prefix: str,
    public_base_url: str,
    backup_ext: str,
) -> int:
    changed_count = 0

    def to_url(md_file: Path, raw_ref: str, is_markdown: bool) -> str | None:
        ref = parse_md_ref(raw_ref) if is_markdown else clean_ref(raw_ref)
        if not ref or not is_local_ref(ref):
            return None
        resolved = resolve_local_ref(md_file, ref, root)
        if not resolved or resolved not in image_set:
            return None
        obj = rel_object_path(root, resolved, prefix)
        return build_public_url(public_base_url, obj)

    for md_file in md_files:
        text = md_file.read_text(encoding="utf-8")

        def md_repl(match: re.Match[str]) -> str:
            raw = match.group(1)
            url = to_url(md_file, raw, is_markdown=True)
            if not url:
                return match.group(0)
            return match.group(0).replace(raw, url, 1)

        def html_repl(match: re.Match[str]) -> str:
            quote_ch = match.group(1)
            raw = match.group(2)
            url = to_url(md_file, raw, is_markdown=False)
            if not url:
                return match.group(0)
            return match.group(0).replace(
                f"src={quote_ch}{raw}{quote_ch}", f"src={quote_ch}{url}{quote_ch}", 1
            )

        updated = MD_IMAGE_RE.sub(md_repl, text)
        updated = HTML_IMG_RE.sub(html_repl, updated)

        if updated != text:
            if backup_ext:
                backup_path = md_file.with_suffix(md_file.suffix + backup_ext)
                backup_path.write_text(text, encoding="utf-8")
            md_file.write_text(updated, encoding="utf-8")
            changed_count += 1

    return changed_count


def main() -> int:
    args = parse_args()

    if args.rewrite_markdown and not args.public_base_url:
        print(
            "Error: --public-base-url is required when using --rewrite-markdown",
            file=sys.stderr,
        )
        return 1

    root = Path(args.docs_root).resolve()
    if not root.is_dir():
        print(f"Error: docs root not found: {args.docs_root}", file=sys.stderr)
        return 1

    if shutil.which("rg") is None:
        print("Error: rg (ripgrep) not found in PATH", file=sys.stderr)
        return 1

    md_files = find_markdown_files(root)
    images, missing = collect_images(root, md_files)

    if not images:
        print("No local image references found in Markdown docs.")
        return 0

    rel_files = sorted(p.relative_to(root).as_posix() for p in images)

    print(f"Found {len(rel_files)} image files:")
    for rel in rel_files:
        print(rel)

    if missing:
        print(file=sys.stderr)
        print(
            f"Warning: {len(missing)} referenced files were not found (showing up to 20):",
            file=sys.stderr,
        )
        for md, ref in missing[:20]:
            print(f"{md}\t{ref}", file=sys.stderr)

    if args.list_only:
        return 0

    target = build_target(args.remote, args.bucket, args.prefix)
    run_rclone_upload(root, target, rel_files, dry_run=args.dry_run)

    if args.rewrite_markdown and not args.dry_run:
        changed = rewrite_markdown_files(
            root=root,
            md_files=md_files,
            image_set=images,
            prefix=args.prefix,
            public_base_url=args.public_base_url,
            backup_ext=args.backup_ext,
        )
        print(f"Rewrote {changed} markdown files.")

    print("Done.")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())