Last active
September 25, 2022 22:59
-
-
Save rahulbanerjee26/56a10ae348601e232a8a5aecff07f0e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def replaceCodeSnippetsWithGists( | |
content, gists, title, hashnode=False, devto=False, medium=False, wp=False | |
): | |
start_pat = "```(.*?)\n" | |
end_pat = "```" | |
def replaceCode(blg): | |
new_content = content | |
snippets = re.findall(start_pat + "(.*?)" + end_pat, new_content, re.S) | |
counter = 0 | |
for item in snippets: | |
if item[0] != "bash": | |
new_content = new_content.replace( | |
"\n".join([f"```{item[0]}", f"{item[1]}```"]), | |
embedGist[blg](gists[counter]), | |
) | |
counter += 1 | |
newFileName = f"{blg}_gist" | |
with open(newFileName, "w+") as f: | |
f.write(new_content) | |
print("Saved file with gists for ", blg) | |
return new_content | |
result = {} | |
if hashnode: | |
result["hashnode"] = replaceCode("hashnode") | |
if devto: | |
result["devto"] = replaceCode("devto") | |
if medium: | |
result["medium"] = replaceCode("medium") | |
if wp: | |
result["wp"] = replaceCode("wp") | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment