code: def space_references(document):
      
      for ref in document.xpath('.//a/sup/span[@class="sup_ref"]'):
          a = ref.getparent().getparent()
          if a is not None:
              atail = a.tail or ''
              if not atail.startswith(')') and not atail.startswith(',') and not atail.startswith(' '):
                  a.tail = ' ' + atail
      return document
docs: Ensure a space around reference links, so there's a gap when they are removed.