Skip to content

Instantly share code, notes, and snippets.

@VincentLoy
Created July 9, 2015 16:55
Show Gist options
  • Save VincentLoy/81455a64386005c9cee5 to your computer and use it in GitHub Desktop.
Save VincentLoy/81455a64386005c9cee5 to your computer and use it in GitHub Desktop.
# vim: fileencoding=utf-8 tw=100 expandtab ts=4 sw=4 :
#
# GoodMorningPlanet.com
# author : Vincent Loy <[email protected]>
# (c) 2015 ActivKonnect
"""
Repair bad formatted Markdown Files - replace **foo** by #foo
only when a line start and end by strong tag
"""
import re
import codecs
file_list = ['history', 'politician', 'politics', 'geography', 'money',
'language', 'culture', 'transport', 'administrativ',
'health', 'religion', 'festivals', 'food', 'dailylife',
'luggage', 'mustsee', 'sport']
file_suffix = '_{locale}.md'.format(locale='en')
file_prefix = 'info_'
strong_regex = re.compile(ur'(^\*{2}([aA-zZ0-9_\s]+)\*{2}[\s]?$)', re.MULTILINE)
for file in file_list:
file_name = '{pref}{name}{suf}'.format(pref=file_prefix, suf=file_suffix, name=file)
with codecs.open(file_name, 'r', encoding='UTF-8') as chapt:
original = chapt.read()
with codecs.open(file_name, 'w+', encoding='UTF-8') as chapt:
match = re.findall(strong_regex, original)
if match:
print("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*")
print(file_name)
for m in match:
n = '#' + m[1]
original = original.replace(m[0], n)
print(m)
# print(original)
chapt.write(u'' + original)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment