-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruskko.py
137 lines (117 loc) · 4.24 KB
/
ruskko.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import sys, re
#---------------------------------------------------------#
# Export files #
#---------------------------------------------------------#
def exportFile(file_contents, output):
export = open(output, "w")
export.write("<!DOCTYPE html>\n") #begins export
export.write("<html>\n")
for i in file_contents:
export.write(i + "\n")
export.write("</html>\n")
#---------------------------------------------------------#
# Define Ruskko built tags #
#---------------------------------------------------------#
# <.someClass> </.>
def classTag(line):
classTitle = line.split("<.")[1].split(">")[0]
classString = line.split(">")[1].split("<")[0]
classReplace = "<span class=\"" + classTitle + "\">"
classEndReplace = "</span>"
line = classReplace + classString + classEndReplace
return line
# <#some-id> </#>
def idTag(line):
tagIDTitle = line.split("<#")[1].split(">")[0]
tagIDString = line.split(">")[1].split("<")[0]
tagIDReplace = "<span id=\"" + tagIDTitle + "\">"
tagIDEndReplace = "</span>"
line = tagIDReplace + tagIDString + tagIDEndReplace
return line
def parser(file, output):
# Remove comments
for i in file:
if (i.startswith('//')):
file.remove(i)
if (re.search("<#.*>", i)):
line = file.index(i)
file[line] = idTag(i)
if (re.search("<\..*>", i)):
line = file.index(i)
file[line] = classTag(i)
print(file)
exportFile(file, output)
#---------------------------------------------------------#
# Reading the file #
#---------------------------------------------------------#
def readFile(output):
file = arguments[0]
print("File Input: ", file)
print("File Output: ", output)
all_lines = []
run = open(file, 'r')
for line in run:
line = line.rstrip()
line = line.lstrip()
all_lines.append(line)
all_lines = list(filter(None, all_lines))
parser(all_lines, output)
#---------------------------------------------------------#
# CLI Options for User #
#---------------------------------------------------------#
def help():
print("Here is a list of options:")
print("--------------------------")
print("")
print(" -o | output (sets output name)")
print(" | Usage: ruskko file.rsko -o index")
print(" | Will set to a .html file automatically")
print("========|=======================================")
print(" -of | output file (sets exact file name)")
print(" | Usage: ruskko file.rsko -o index.html")
print(" | Has no automatic file extension set")
print("")
exit()
def fileInput(file):
if (arguments[0] == "-help"):
help()
else:
if (not arguments[0].endswith(".rsko")):
incorrectFile(arguments[0])
else:
pass
#---------------------------------------------------------#
# Get/Set arguments such as file and CLI for future use #
#---------------------------------------------------------#
def setOutput(output_name):
output = output_name
readFile(output)
def incorrectUsage(err):
print("Error Detected: ", err, " - Incorrect command usage")
print("Usage: ruskko <filename> [OPTIONS] [COMMAND]")
print("do 'ruskko -help' for a list of options.")
def incorrectFile(file):
print("got file: ", file)
print("expected a \".rsko\" file!")
# this is where the program starts:
arguments = sys.argv
del arguments[0] # Remove the python file name
args_len = len(arguments)
print("Total Arguments: ", args_len)
print("Arguments Seen: ", arguments)
# Check Argument Lengths, and run code accordingly
if (args_len == 1):
fileInput(arguments[0])
setOutput("index.html")
elif (args_len == 2):
incorrectUsage("Unused Option")
elif (args_len == 3):
fileInput(arguments[0])
if (arguments[1] == "-o"):
setOutput(arguments[2] + ".html")
elif(arguments[1] == "-of"):
setOutput(arguments[2])
elif (args_len == 0):
incorrectUsage("No File Specified")
else:
incorrectUsage("Too many arguments! Limit = 3")