-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblorp.py
More file actions
123 lines (85 loc) · 4.38 KB
/
blorp.py
File metadata and controls
123 lines (85 loc) · 4.38 KB
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
#!/usr/bin/python3
import os
import shutil
from argparse import ArgumentParser
from pathlib import Path
# #############################################################################
# argument parsing
# #############################################################################
parser = ArgumentParser()
parser.add_argument("-i", "--inputDirectory", help="input directory", default=".")
parser.add_argument("-b", "--blockDirectory", help="directory with block files", default=".")
parser.add_argument("-o", "--outputDirectory", help="output directory", default="out")
parser.add_argument("-d", "--delete_output_first", help="if specified, content of the output directory will be deleted (cleaned up) before processing", action="store_true")
pArgs = parser.parse_args()
argsDict = vars(pArgs)
inputDirectory = argsDict["inputDirectory"]
blockDirectory = argsDict["blockDirectory"]
outputDirectory = argsDict["outputDirectory"]
cleanupFirst = pArgs.delete_output_first
# #############################################################################
# global variables and utility functions
# #############################################################################
BlockFileExt = ".block"
BlockTagStart = "<blorp>"
BlockTagEnd = "</blorp>"
def getBlockStr(blockName, blockFiles):
for bf in blockFiles:
if bf.stem == blockName:
with open(bf) as blockFileHandle:
return blockFileHandle.read()
errorMsg = "ERROR: Could not replace block \"" + blockName + "\""
print(errorMsg)
return "<strong>" + errorMsg + "</strong>"
# #############################################################################
# main script
# #############################################################################
if os.path.exists(outputDirectory):
if cleanupFirst:
print("Cleaning up output directory")
shutil.rmtree(outputDirectory)
else:
print("No cleanup flag specified, using output directory as-is.")
htmlFiles = list(Path(inputDirectory).glob('**/*.html'))
blockFiles = list(Path(blockDirectory).glob('**/*' + BlockFileExt))
print("Collected " + str(len(htmlFiles)) + " HTML input files.")
print("Collected " + str(len(blockFiles)) + " block files.")
outputCnt = 0
for htmlFile in htmlFiles:
with open(htmlFile) as htmlFileHandle:
htmlFileContent = htmlFileHandle.read()
replacedBlockNames = []
newHTMLFileContent = htmlFileContent
blockIdx = htmlFileContent.find(BlockTagStart)
# parse blocks within the file and replace them
while blockIdx != -1:
blockEndIdx = htmlFileContent.find(BlockTagEnd, blockIdx + len(BlockTagStart))
# very basic error check
if (blockEndIdx == -1):
print("ERROR: Incomplete block. Must start with \"" + BlockTagStart +
"\" and end with \"" + BlockTagEnd + "\".")
blockIdx = htmlFileContent.find(BlockTagStart, blockIdx + 1)
continue
# parse block name
blockName = htmlFileContent[blockIdx+len(BlockTagStart):blockEndIdx]
if blockName in replacedBlockNames:
blockIdx = htmlFileContent.find(BlockTagStart, blockIdx + 1)
continue
fullBlockNew = getBlockStr(blockName, blockFiles)
# perform replacement for all instances
fullBlockOld = htmlFileContent[blockIdx:blockEndIdx + len(BlockTagEnd)]
newHTMLFileContent = newHTMLFileContent.replace(fullBlockOld, fullBlockNew)
# remember that this block name has been processed for this HTML files
replacedBlockNames.append(blockName)
blockIdx = htmlFileContent.find(BlockTagStart, blockIdx + 1)
# write output file
inFileDir = os.path.dirname(htmlFile)
relDir = os.path.relpath(inFileDir, inputDirectory)
outputFilePath = os.path.join(outputDirectory, relDir)
if not os.path.exists(outputFilePath):
os.makedirs(outputFilePath)
outputFile = os.path.join(outputFilePath, os.path.basename(htmlFile))
with open(outputFile, "w") as htmlOutputFileHandle:
htmlOutputFileHandle.write(newHTMLFileContent)
outputCnt += 1
print("Wrote " + str(outputCnt) + " HTML output files.")