-
-
Save gregturn/6039247 to your computer and use it in GitHub Desktop.
This script will parse a markdown file and replace any lines that look like <@snippet path="pom.xml" prefix="complete"/> with <@snippet path="pom.xml" prefix="initial"/>. It writes the new files into <file>.new first, and then moves it to replace the existing file. See comments for more details
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
#!/usr/bin/env python | |
import os | |
import re | |
import sys | |
if len(sys.argv) < 2: | |
print "Usage: %s <file>" % sys.argv[0] | |
sys.exit(1) | |
with open(sys.argv[1]) as input: | |
with open(sys.argv[1] + ".new", "w") as output: | |
for line in input.readlines(): | |
if "snippet" in line and "pom.xml" in line and "complete" in line: | |
output.write(re.sub("complete", "initial", line)) | |
else: | |
output.write(line) | |
os.rename(sys.argv[1]+".new", sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script will parse a markdown file and replace any lines that look like
<@snippet path="pom.xml" prefix="complete"/>
with<@snippet path="pom.xml" prefix="initial"/>
. It writes the new files into<file>.new
first, and then moves it to replace the existing file.USAGE:
fix <file>
. Very handy inside a loop, likefor repo in $(find . -name .git); do cd $(dirname $repo); pwd; fix README.ftl.md; cd ..; done;
REUSE: Edit the if blocks to alter the pattern you seek as well as the substitution pattern. Before committing to a final version, comment out
os.rename
anddiff <file> <file>.new
to see if it's what you want. Put the script somewhere like /usr/local/bin so it's on your path.RECOMMEND: Only use with version controlled files so you can back out changes if need be.