-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprettycode.py
More file actions
34 lines (25 loc) · 853 Bytes
/
prettycode.py
File metadata and controls
34 lines (25 loc) · 853 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
#script to create html from source code. reads source code from the file passed
#as command line arg. changes tab size from 4 spaces to 2 spaces.
import sys
import os
tab = " " #change if you want bigger/smaller tabs
codeTab = " " #change if code indent width != 4 spaces
def prettify():
if(len(sys.argv) != 2):
print("correct usage: python prettycode.py pathToCode.txt")
return 1
if(not os.path.isfile(sys.argv[1])):
print("invalid file")
return 1
codeFile = open(sys.argv[1], "r")
#read no more than 10KB
code = codeFile.read(10000)
codeFile.close()
codeLines = code.splitlines()
print('<div class="code">')
print(tab + '<pre>')
for line in codeLines:
print(tab + tab + line.replace(codeTab, tab))
print(tab + '</pre>')
print('</div>')
prettify()