Advertisement
applehelpwriter

Format Links.py

Aug 19th, 2020
1,193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. # BBEdit Text Filter
  4.  
  5. # find all html links, and add /" target="_blank" rel="noopener noreferrer"/ to them
  6. # to ensure the link opens in a new window when clicked
  7. # Written by Phil Stokes
  8.  
  9. import re
  10. from sys import stdin, stdout
  11.  
  12. def filter(txt):
  13.     # constants
  14.     attr = '" target="_blank" rel="noopener noreferrer'
  15.     ptrn = '(<a href.+?(?=">))'
  16.  
  17.    
  18.     # copy the text
  19.     str = txt
  20.     str = re.sub((ptrn), r'\1'+attr, txt)
  21.     str2 = re.sub(r'&lt;', r'<', str)
  22.     str = re.sub(r'&gt;', r'>', str2)
  23.  
  24.     return str
  25.  
  26.  
  27. if __name__ == "__main__":
  28.     text = stdin.read()
  29.     stdout.write(filter(text))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement