import sys
import re

global html

pattern = r'[ ]?‘(?i)(\d\d|ad[n]{0,1}|app[yines]{0,5}|appen[eds]{0,2}|ard[er]{0,2}|arf|alf|ang|as|at|av[ein]{0,3}|bout|bye|cause|cept[ing]{0,3}|copter[s]{0,1}|cos|cross|cuz|couse|e[emr]{0,1}|ell|elp[edling]{0,5}|ere[abouts]{0,5}|eard|f|fraid|fore|id|igh[er]{0,2}|ighness|im|is|isself|gainst|kay|less|mongst|n|nd|neath|nough|nother|nuff|o[o]{0,1}|ood|ome|ow|op[eding]{0,3}|oney|orse[flesh]{0,5}|ouse[ds]{0,1}|pon|puter[edrs]{0,2}|round|scuse[ds]{0,1}|spect[sed]{0,2}|scaped|sides|tween|special[ly]{0,2}|stead|t|taint|til|tis|twas|twere|twould|twil l|ud|un|urt|vise)(\W?)'

replacement =  r' ’\1\2'

# pattern = r" (’|')(re|ve|t|m|d|s|ll) "
# replacement=r"\1\2 "

html='''
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta name="charset" content="UTF-8"/>
<meta name="viewport" content="width=-3, height=-4"/>
<title></title>
  <link href="../Styles/main.css" type="text/css" rel="stylesheet"/>
</head>
<body class="fullpage">
   <div class="cover">
      <img id="coverimage" src="../Images/mycoverimagehere.jpg" alt="cover image"/>
   </div>
</body>
</html>
'''

def doit(pat, repl):
    count = 0
    flags = 0
    newHTML, replacements = re.subn(pattern, repl, html)
    print(newHTML)

doit(pattern, replacement)

