liaoch commited on
Commit
5faa943
·
1 Parent(s): cf2ef40

Remove unused mermaid-rendering.py file

Browse files
Files changed (1) hide show
  1. mermaid-rendering.py +0 -165
mermaid-rendering.py DELETED
@@ -1,165 +0,0 @@
1
- #!/usr/bin/env python3
2
- # Instructions to install dependencies and run the program:
3
- #
4
- # 1. Install Node.js:
5
- # - Download and install Node.js from https://nodejs.org/
6
- # - Verify installation by running `node --version` in your terminal.
7
- #
8
- # 2. Install the Mermaid CLI (@mermaid-js/mermaid-cli):
9
- # - Run `npm install -g @mermaid-js/mermaid-cli` in your terminal.
10
- # - If you encounter permissions issues, you might need to use `sudo` (for macOS/Linux)
11
- # or run the command prompt as an administrator (for Windows).
12
- # - Verify installation by running `mmdc --version`.
13
- #
14
- # 3. Run the script:
15
- # - You can run the script directly with the embedded example by uncommenting the lines in the `if __name__ == "__main__":` block and running:
16
- # `python3 mermaid-rendering.py`
17
- # - Alternatively, you can use the command-line interface:
18
- # - To render Mermaid code from a string:
19
- # `python3 mermaid-rendering.py -c "your_mermaid_code"`
20
- # - To render Mermaid code from a file:
21
- # `python3 mermaid-rendering.py -f /path/to/your/file.mmd`
22
- # - You can specify the output file with `-o /path/to/output.png`, output type with `-t [png, pdf, svg]`, and theme with `--theme [default, forest, dark, neutral]`.
23
- # - For example:
24
- # `python3 mermaid-rendering.py -f input.mmd -o output.png -t png --theme default`
25
-
26
- import os
27
- import sys
28
- import subprocess
29
- import argparse
30
- import tempfile
31
- import json
32
- from pathlib import Path
33
- import textwrap
34
-
35
- class MermaidRenderer:
36
- """
37
- A Python class to render Mermaid diagrams to various formats
38
- using puppeteer-mermaid behind the scenes
39
- """
40
-
41
- def __init__(self):
42
- """Initialize the renderer and check if dependencies are installed"""
43
- self._check_dependencies()
44
-
45
- def _check_dependencies(self):
46
- """Check if Node.js and puppeteer-mermaid are installed"""
47
- try:
48
- # Check for Node.js
49
- subprocess.run(["node", "--version"], capture_output=True, check=True)
50
- except (subprocess.SubprocessError, FileNotFoundError):
51
- sys.exit("Error: Node.js is not installed. Please install Node.js from https://nodejs.org/")
52
-
53
- # Check if @mermaid-js/mermaid-cli is installed
54
- result = subprocess.run(["npm", "list", "-g", "@mermaid-js/mermaid-cli"],
55
- capture_output=True, text=True)
56
-
57
- if "mermaid-cli" not in result.stdout:
58
- print("Installing @mermaid-js/mermaid-cli globally...")
59
- try:
60
- subprocess.run(["npm", "install", "-g", "@mermaid-js/mermaid-cli"], check=True)
61
- print("@mermaid-js/mermaid-cli installed successfully.")
62
- except subprocess.SubprocessError:
63
- sys.exit("Error: Failed to install @mermaid-js/mermaid-cli. Please install manually using: npm install -g @mermaid-js/mermaid-cli")
64
-
65
- def render(self, mermaid_code, output_file=None, output_format="png", theme="default"):
66
- """
67
- Render Mermaid code to the specified format
68
-
69
- Args:
70
- mermaid_code (str): The Mermaid diagram code
71
- output_file (str, optional): Output file path. If None, generates a filename based on format.
72
- output_format (str, optional): Output format. Options: png, pdf, svg. Default: png
73
- theme (str, optional): Mermaid theme. Default: default
74
-
75
- Returns:
76
- str: Path to the generated file
77
- """
78
- # Validate output format
79
- valid_formats = ["png", "pdf", "svg"]
80
- if output_format not in valid_formats:
81
- sys.exit(f"Error: Invalid output format. Choose from: {', '.join(valid_formats)}")
82
-
83
- # Create a temporary file for the Mermaid code
84
- with tempfile.NamedTemporaryFile(mode='w', suffix='.mmd', delete=False) as temp_file:
85
- temp_file.write(mermaid_code)
86
- input_path = temp_file.name
87
-
88
- # Generate output file name if not provided
89
- if not output_file:
90
- output_file = f"diagram.{output_format}"
91
-
92
- # Ensure output directory exists
93
- output_dir = os.path.dirname(output_file)
94
- if output_dir:
95
- os.makedirs(output_dir, exist_ok=True)
96
-
97
- # Run puppeteer-mermaid
98
- try:
99
- cmd = [
100
- "mmdc",
101
- "-i", input_path,
102
- "-o", output_file,
103
- "-t", theme,
104
- "-f", output_format
105
- ]
106
-
107
- subprocess.run(cmd, check=True)
108
- print(f"Diagram saved to: {output_file}")
109
-
110
- # Clean up the temporary file
111
- os.unlink(input_path)
112
-
113
- return output_file
114
-
115
- except subprocess.SubprocessError as e:
116
- os.unlink(input_path)
117
- sys.exit(f"Error rendering diagram: {str(e)}")
118
-
119
- def main():
120
- """Command line interface for the Mermaid renderer"""
121
- parser = argparse.ArgumentParser(description="Render Mermaid diagrams to PNG, PDF, or SVG.")
122
-
123
- input_group = parser.add_mutually_exclusive_group(required=True)
124
- input_group.add_argument("-c", "--code", help="Mermaid code as a string")
125
- input_group.add_argument("-f", "--file", help="Path to a file containing Mermaid code")
126
-
127
- parser.add_argument("-o", "--output", help="Output file path")
128
- parser.add_argument("-t", "--type", default="png", choices=["png", "pdf", "svg"],
129
- help="Output file type (default: png)")
130
- parser.add_argument("--theme", default="default",
131
- choices=["default", "forest", "dark", "neutral"],
132
- help="Mermaid theme (default: default)")
133
-
134
- args = parser.parse_args()
135
-
136
- # Get Mermaid code from string or file
137
- if args.code:
138
- mermaid_code = args.code
139
- else:
140
- try:
141
- with open(args.file, 'r') as f:
142
- mermaid_code = f.read()
143
- except (IOError, FileNotFoundError):
144
- sys.exit(f"Error: Could not read file {args.file}")
145
-
146
- # Create renderer and render the diagram
147
- renderer = MermaidRenderer()
148
- renderer.render(mermaid_code, args.output, args.type, args.theme)
149
-
150
- # Example usage with multiline string for easy copy-paste
151
- if __name__ == "__main__":
152
- # You can replace this multiline string with your own Mermaid diagram code
153
- MERMAID_CODE = """
154
- pie title NETFLIX
155
- "Time spent looking for movie" : 90
156
- "Time spent watching it" : 10
157
- """
158
-
159
- # Uncomment and modify these lines to run directly
160
- renderer = MermaidRenderer()
161
- renderer.render(MERMAID_CODE, "flowchart.svg", "svg", "default")
162
- renderer.render(MERMAID_CODE, "flowchart.pdf", "pdf", "default")
163
-
164
- # Or use the command-line interface
165
- #main()