This PR addresses #1625. Basically, it parses the resulting html after markdown2 runs, and uses BeautifulSoup to look at all the hrefs as well as the text inside and immediately after the link.
- If it looks like a parenthesis was dropped from the end of the link, it will stick it back on the href and link text while stripping it from the trailing text.
- If it looks like a trailing parenthesis was added accidentally onto the link, it will be stripped from the link href and text and then added to the trailing text.
This determination is made with the assumption that a url with parentheses are likely to have a balanced number of ( and ) characters. As far as I can tell, this is a decent assumption. Basically the only place I have systematically seen this be an issue is with wikipedia links since they love to use parentheses (and their urls are going to be well-formed).
I added some new tests and made sure that all the tests for both markdown_to_html and allowlist_html pass. I did have to make one tweak in allowlist_html to properly handle <hr/> tags that result from the analysis I do with BeautifulSoup when the html has a horizontal rule in it.