Advertisement
Guest User

Tvtropes despoiler script

a guest
Jul 14th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import re
  3. re.purge()
  4.  
  5. #purge spoiler tags from Tvtropes document
  6.  
  7. #There's probably a simpler way to do this that doesn't require 3 separate loops,
  8. #but I'm not very good at regular expressions
  9. #so this was the best I could come up with that didn't have undesirable side effects.
  10.  
  11.  
  12. #corner case: this will fail if the original document contains 3 consecutive double-quote (") characters.
  13. article = """simply paste
  14. the article you want to despoiler
  15. into this multi-line
  16. triple-quoted string"""
  17.  
  18. #corner case: this will fail if this obscure Unicode emoji character is actually in the original document
  19. pl1 = "🐰"
  20. theList = []
  21.  
  22. # this loop evacuates all bracketed expressions besides spoilertags
  23. # into a list of strings, replacing them with our adorable placeholder character
  24. while re.search(r'((\[\[)(?!spoiler:))(.+?\]\])',article) != None:
  25.     tmp1 = re.search(r'((\[\[)(?!spoiler:))(.+?\]\])',article)
  26.     tmp2 = tmp1.group()
  27.     theList.append(tmp2)
  28.     article = re.sub(r'((\[\[)(?!spoiler:))(.+?\]\])',pl1,article,count=1)
  29.  
  30. # remove spoiler tags from the document
  31. while re.search(r'\[\[spoiler:.+\]\]',article) != None:
  32.     tmp1 = re.search(r'\[\[spoiler:.+\]\]',article)
  33.     tmp2 = tmp1.group()
  34.     tmp3 = re.sub(r'^\[\[spoiler:',"",tmp2,count=1)
  35.     tmp4 = re.sub(r'\]\]$',"",tmp3,count=1)
  36.     article = re.sub(r'\[\[spoiler:.+\]\]',tmp4,article,count=1)
  37.  
  38. # pull our other bracketed expressions out of the list and re-insert them
  39. theList.reverse()
  40. while re.search(pl1,article) != None:
  41.     article = re.sub(pl1,theList.pop(),article,count=1)
  42.  
  43.  
  44. #Not bothering to write to a file "properly". You can just capture the standard output if you need to.
  45. #e.g. on a linux/unix shell you would do 'python3 purge_spoilers.py >> output.txt'
  46. print(article)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement