insert_css_link 678 B

123456789101112131415161718192021222324252627
  1. #!/usr/bin/env python3
  2. import sys
  3. import re
  4. def insert_css_link(file_path, css_href):
  5. with open(file_path, 'r') as f:
  6. content = f.read()
  7. head_pattern = r'</head>'
  8. new_link = f'<link rel="stylesheet" type="text/css" href="{css_href}">\n'
  9. modified_content = re.sub(head_pattern, f'{new_link}\\g<0>', content, count=1)
  10. with open(file_path, 'w') as f:
  11. f.write(modified_content)
  12. if __name__ == "__main__":
  13. if len(sys.argv) != 3:
  14. print("Usage: {} input_html stylesheet_href".format(sys.argv[0]))
  15. sys.exit(1)
  16. input_html = sys.argv[1]
  17. stylesheet_href = sys.argv[2]
  18. insert_css_link(input_html, stylesheet_href)