modify_readme.py 649 B

123456789101112131415
  1. import re
  2. import sys
  3. repo_name = sys.argv[1]
  4. branch_name = sys.argv[2]
  5. # Regex pattern <img\s+([^>]*?)(?<!['"])(?<!\/)src\s*=\s*(['"])(?!http|\/)(.*?)\2([^>]*?)>
  6. pattern = re.compile("<img\\s+([^>]*?)(?<!['\"])(?<!\\/)src\\s*=\\s*(['\"])(?!http|\\/)(.*?)\\2([^>]*?)>")
  7. replacement = r'<img \1src="https://raw.githubusercontent.com/Avaiga/{repo_name}/{branch_name}/\3"\4>'
  8. with open("README.md") as readme_file:
  9. readme_str = readme_file.read()
  10. modified_readme = re.sub(pattern, replacement.format(repo_name=repo_name, branch_name=branch_name), readme_str)
  11. with open("README.md", "w") as readme_file:
  12. readme_file.write(modified_readme)