48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
|
||
|
|
import sys
|
||
|
|
import json
|
||
|
|
import base64
|
||
|
|
import zlib
|
||
|
|
|
||
|
|
def pako_deflate(data):
|
||
|
|
"""Compress data using the same parameters as pako.deflate"""
|
||
|
|
compress = zlib.compressobj(9, zlib.DEFLATED, 15, 8, zlib.Z_DEFAULT_STRATEGY)
|
||
|
|
compressed_data = compress.compress(data)
|
||
|
|
compressed_data += compress.flush()
|
||
|
|
return compressed_data
|
||
|
|
|
||
|
|
def main():
|
||
|
|
# Read all input from stdin
|
||
|
|
mermaid_text = sys.stdin.read().strip()
|
||
|
|
|
||
|
|
if not mermaid_text:
|
||
|
|
print("Error: No mermaid text provided via stdin", file=sys.stderr)
|
||
|
|
print("Usage: cat mermaid.md | python3 mermaid-to-url.py", file=sys.stderr)
|
||
|
|
print(" or: echo 'graph TD; A-->B' | python3 mermaid-to-url.py", file=sys.stderr)
|
||
|
|
sys.exit(1)
|
||
|
|
|
||
|
|
# Create the mermaid object structure expected by mermaid.live
|
||
|
|
mermaid_obj = {
|
||
|
|
"code": mermaid_text,
|
||
|
|
"mermaid": {
|
||
|
|
"theme": "default"
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# Convert to JSON
|
||
|
|
mermaid_json = json.dumps(mermaid_obj)
|
||
|
|
|
||
|
|
# Compress using pako deflate parameters
|
||
|
|
byte_data = mermaid_json.encode('ascii')
|
||
|
|
compressed = pako_deflate(byte_data)
|
||
|
|
|
||
|
|
# Base64 encode (URL-safe)
|
||
|
|
encoded = base64.b64encode(compressed).decode('ascii')
|
||
|
|
encoded = encoded.replace('+', '-').replace('/', '_')
|
||
|
|
|
||
|
|
# Output the mermaid.live URL
|
||
|
|
print(f"https://mermaid.live/edit#pako:{encoded}")
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|