def convertToRST(algName, tempDir, convertedDir, version): #Set input name and change working directory inputN = algName+'.txt' outputN = algName+'.rst' cwd = os.getcwd() os.chdir(tempDir) args = ['pandoc', '-f', 'mediawiki', '-t', 'rst', inputN, '-o', outputN] #run pandoc and wait for output obj = subprocess.Popen(args) obj.wait() os.chdir(cwd) #move completed rst file into completed folder for tidyness mvFile = tempDir+algName+'.rst' destname = None if AlgorithmFactory.exists(algName): destname = convertedDir + "/algorithms/" + algName + "-v" + str(version) + ".rst" elif AlgorithmFactory.exists(algName[:-1]): destname = convertedDir + "/algorithms/" + algName[:-1] + "-v" + str(version) + ".rst" elif algName in FUNC_NAMES: destname = convertedDir + "/functions/" + algName + ".rst" elif "USAGE" in algName: destname = convertedDir + algName + ".rst" else: unknowns_file = open("unknowns.txt", 'a') unknowns_file.write("Unknown object type %s\n" % algName) unknowns_file.close() return shutil.move(mvFile, destname)
def create(algName, convertedDir): #read the rst content in with open (convertedDir+algName+'.rst', 'r') as rstFile: rst=rstFile.read() #remove extra whitespace and newlines from the end of the documentation rst = rst.rstrip() template_dir = os.path.dirname(__file__) if AlgorithmFactory.exists(algName): template_file = os.path.join(template_dir,'algorithm_template.rst') elif AlgorithmFactory.exists(algName[:-3]): template_file = os.path.join(template_dir,'algorithm_template.rst') elif algName in FUNC_NAMES: template_file = os.path.join(template_dir,'function_template.rst') elif "USAGE" in algName: raise RuntimeError("Usage section rst encountered in directive insertion: %s" % algName) else: raise RuntimeError("Unknown object in directive insertion: %s" % algName) with open (template_file, 'r') as templateFile: template=templateFile.read() built = template.replace('[TITLE]', "="*len(algName) + "\n" + algName + "\n" + "="*len(algName) + "\n") built = built.replace('[DOCUMENTATION]', rst) builtFile = open(convertedDir+algName+'.rst', 'w') builtFile.write(built)