Markdown ATX Headers Require a Space after Hashes

I just upgraded to Anaconda 2.4, which includes Python 3.5. To update, just run the following code:

conda update conda
conda install anaconda=2.4 

Almost everything seems to have gone smoothly. The exception was that all the markdown headers in my IPython notebooks lost their formatting. They started showing up like this:
Broken Markdown Headers

It turns out it was my fault. I didn’t know that “The opening sequence of # characters cannot be followed directly by a non-space character” in ATX (hash-style) headers. Earlier versions of Anaconda were more lenient and formatted the header correctly even you (read I) didn’t put a space after the hash marks. So, I had dozens of notebooks with the offending #headers.

Python to the Rescue

Luckily, the fix isn’t too bad. I used a regular expression to find series of hash marks that weren’t followed by a space:

pattern = re.compile(r'(\n\s*"#+)([^\s#])')

And then I looped through folders containing IPython notebooks finding and replacing the pattern above with the two substrings in the matches separated by a space.

pattern = re.compile(r'(\n\s*"#+)([^\s#])')

Here is the full code on gist.

I didn’t test it to thoroughly, so use at your own risk!

About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and much more. Check out our complete course catalog.