# -*- coding: utf-8 -*- import os, re rootdir = '.' def add_mobile_viewport_support(filepath): metastr = ' \n' lines = [] with open(filepath, 'r', encoding='UTF-8') as f: lines = f.readlines() if metastr in lines: return for i in range(len(lines)): if lines[i].strip(" ").startswith("

search | git repo

\n""" lines = [] newlines = [] skipping = False with open(filepath, 'r', encoding='UTF-8') as f: lines = f.readlines() for i in range(len(lines)): if "" in lines[i]: skipping = False continue if skipping: continue if lines[i].strip(" ").startswith(""): newlines.append(footerstr) newlines.append(lines[i]) with open(filepath, 'w', encoding='UTF-8') as f: f.writelines(newlines) def add_style(filepath): stylestr = """ \n""" # TODO: read style from css lines = [] newlines = [] skipping = False with open(filepath, 'r', encoding='UTF-8') as f: lines = f.readlines() for i in range(len(lines)): if "" in lines[i]: skipping = False newlines.append(stylestr) continue if skipping: continue newlines.append(lines[i]) with open(filepath, 'w', encoding='UTF-8') as f: f.writelines(newlines) def add_details(filepath): p0 = u'
(.*?)
' r0 = u'\\2' p1 = u'
  • (.*?)
    ' r1 = u'
  • \\1' p2 = u'
  • ' r2 = u'' lines = [] with open(filepath, 'r', encoding='UTF-8') as f: lines = f.readlines() newlines = ''.join(lines) newlines = re.sub('<(.*?)span(.*?)>', '<\\1div\\2>', newlines, flags=re.UNICODE|re.DOTALL) newlines = re.sub(p0, r0, newlines, flags=re.UNICODE|re.DOTALL) newlines = re.sub(p1, r1, newlines, flags=re.UNICODE|re.DOTALL) newlines = re.sub(p2, r2, newlines, flags=re.UNICODE|re.DOTALL) with open(filepath, 'w', encoding='UTF-8') as f: f.writelines(newlines) exclude_dirs = [".git", "src", "search", "node_modules", "flowy", "public"] def is_excluded(subdir): for dirname in exclude_dirs: if dirname in subdir: return True def main(): # TODO allow args for subdir, dirs, files in os.walk(rootdir): if is_excluded(subdir): continue for file in files: filepath = os.path.join(subdir, file) if file.endswith(".html"): print(filepath) add_mobile_viewport_support(filepath) add_style(filepath) add_details(filepath) # add_footer(filepath) if __name__ == "__main__": main()