Last active
November 6, 2015 15:08
-
-
Save ndunn219/1d7fd5edd94d8a05bb8b to your computer and use it in GitHub Desktop.
Adds a space after series of hash marks at the beginning of lines. The purpose is to fix markdown headers that didn't include a space after heading hash marks. USE AT YOUR OWN RISK.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import os\n", | |
"import re\n", | |
"\n", | |
"searchdir='.'\n", | |
"os.chdir(searchdir)\n", | |
"\n", | |
"pattern = re.compile(r'(\\n\\s*\"#+)([^\\s#])')\n", | |
"\n", | |
"ext_list = ['ipynb']\n", | |
"exclude_dirs = ['.ipynb_checkpoints']\n", | |
"\n", | |
"fixed_files = []\n", | |
"for dirpath, dirnames, filenames in os.walk('.'):\n", | |
" dirnames[:] = [d for d in dirnames if d not in exclude_dirs]\n", | |
" filtered_filenames = [fname for fname in filenames if fname.split('.')[-1] in ext_list]\n", | |
" \n", | |
" for fname in filtered_filenames:\n", | |
" file = dirpath + '/' + fname\n", | |
" try:\n", | |
" with open(file, encoding=\"utf-8\") as f:\n", | |
" content = f.read()\n", | |
" if pattern.search(content):\n", | |
" fixed_content = pattern.sub(r'\\1 \\2',content)\n", | |
" with open(file,'w', encoding=\"utf-8\") as f2:\n", | |
" f2.write(fixed_content)\n", | |
" fixed_files.append(file)\n", | |
" except Exception as e:\n", | |
" print(file, e)\n", | |
"\n", | |
"print( '{} files fixed.'.format(len(fixed_files) ) )\n", | |
" \n", | |
"for i, file in enumerate(fixed_files,1):\n", | |
" print( '{}. {}'.format(i,file) )" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.4.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment