1
0
Fork 0
arangodb/Documentation/Books/bot.py

54 lines
1.8 KiB
Python

import sys
import re
import os
def walk_on_files(dirpath):
for root, dirs, files in os.walk(dirpath):
for file in files:
if file.endswith(".md"):
full_path= os.path.join(root, file)
f=open(full_path,"rU")
print "checking file: " + full_path
textFile=f.read()
f.close()
findStartCode(textFile,full_path)
return
def findStartCode(textFile,full_path):
match = re.findall(r'@startDocuBlock\s*(\w+)', textFile)
if match:
for find in match:
textToReplace=getTextFromSourceFile(find, full_path)
def getTextFromSourceFile(searchText, full_path):
f=open("allComments.txt", 'rU')
s=f.read()
match = re.search(r'@startDocuBlock\s+'+ searchText + "(?:\s+|$)" +'(.+?)@endDocuBlock', s,re.DOTALL)
if match:
textExtracted = match.group(1)
textExtracted = textExtracted.replace("<br />","\n")
textExtracted = re.sub(r"@RESTHEADER{(.*)}", r"`\g<1>`", textExtracted)
textExtracted = re.sub(r"@RESTRETURNCODE{(.*)}", r"`HTTP \g<1>`", textExtracted)
textExtracted = re.sub(r"@RESTBODYPARAM{(.*)}", r"*(\g<1>)*", textExtracted)
textExtracted = textExtracted.replace("@RESTDESCRIPTION","")
textExtracted = textExtracted.replace("@EXAMPLES","*Examples*")
textExtracted = textExtracted.replace("@RESTRETURNCODES","###Return Codes")
replaceText(textExtracted, full_path, searchText)
def replaceText(text, pathOfFile, searchText):
f=open(pathOfFile,"rU")
if f:
s=f.read()
f.close()
f=open(pathOfFile,'w')
replaced=re.sub("@startDocuBlock\s+"+ searchText + "(?:\s+|$)",text,s)
f.write(replaced)
f.close()
if __name__ == '__main__':
path = ["Documentation/Books/Users"]
for i in path:
dirpath = os.path.abspath(os.path.join(os.path.dirname( __file__ ), os.pardir,"ArangoDB/../../"+i))
walk_on_files(dirpath)