-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjdoctr.py
More file actions
47 lines (41 loc) · 978 Bytes
/
jdoctr.py
File metadata and controls
47 lines (41 loc) · 978 Bytes
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
# Jdoctr.py
# Simple utility to format JSON for use in a javadoc.
#
# Example:
#
# Input:
# {"greeting": "Hello!"}
#
# Output:
# * <pre>
# * {
# * "greeting": "Hello!"
# * }
# * </pre>
#
# By Andrew Garner 2018
import json
import sys
import argparse
# Shows help
parser = argparse.ArgumentParser(description='Utility for making JSON paste-able into a Javadoc. Adds asterisks and <pre> tags.')
args = parser.parse_args()
# Get Input from user
inputJson = input('Enter JSON: ')
# Parse JSON
try:
parsedJson = json.loads(inputJson)
except ValueError as error:
print("Bad JSON input: ", str(error))
quit()
except:
print("Unexpected error: ", sys.exc_info()[0])
raise
# format input
formattedJson = json.dumps(parsedJson, indent=4, sort_keys=False)
# Add asterisks to all lines, preserving new lines
formattedJson = formattedJson.replace('\n', '\n* ')
# Add pre tags
formattedJson = "\n* <pre>\n* " + formattedJson + "\n* </pre>\n"
# Return
print(formattedJson)