This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
flowchart-maker.py
68 lines (54 loc) · 2.15 KB
/
flowchart-maker.py
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
import re
import json
import string
filepath = './app/questions.js'
def load_questions():
with open(filepath) as f:
questions = f.read()
# Regular expression to find json array contained within JavaScript file:
array_finder = r"\[.*\]"
# Switch any single quotation marks to double (expected by Python json module), and watch out for gotchas
# like 'don"t':
tidy_questions = re.findall(array_finder, questions, re.DOTALL)[0].replace("'", '"').replace('don"t', "don't")
return json.loads(tidy_questions)
questions = load_questions()
alphabet = string.ascii_uppercase
output = """
```mermaid
flowchart TD
"""
def format_question(idx, question):
letter_id = alphabet[idx]
question_text = question['flowchart']
return f'{letter_id}{{{question_text}}};'
formatted_question_nodes = [format_question(idx, question) for idx, question in enumerate(questions)]
def add_spacing(inner):
def wrapper(*args, **kwargs):
return '\t' + inner(*args, **kwargs) + '\n'
return wrapper
@add_spacing
def format_terminal_node(letter_id, answer, next_move_id):
text_to_display = next_move_id.replace('_', ' ')
return f'{letter_id} -- {answer} --> {next_move_id}[{text_to_display}];'
@add_spacing
def format_edge(letter_id, answer, next_idx):
formatted_next_question = formatted_question_nodes[next_idx]
return f'{letter_id} -- {answer} --> {formatted_next_question}'
for idx, question in enumerate(questions):
output += '\t' + formatted_question_nodes[idx] + '\n'
letter_id = alphabet[idx]
for answer, next_move in question['edges'].items():
next_move_text, next_move_id = next_move
if 'TIER' in next_move_id:
output += format_terminal_node(letter_id, answer, next_move_id)
continue
elif next_move_id == 'next':
next_idx = idx + 1
elif 'move' in next_move_id:
next_idx = idx + int(next_move_id.replace('move ', ''))
else:
raise Exception("Unexpected directions in JSON file")
output += format_edge(letter_id, answer, next_idx)
output += "```"
with open('README.md', 'w') as f:
f.write(output)