Skip to content

Instantly share code, notes, and snippets.

@shellexy
Created March 26, 2012 06:39
Show Gist options
  • Save shellexy/2203475 to your computer and use it in GitHub Desktop.
Save shellexy/2203475 to your computer and use it in GitHub Desktop.
py 给 html 添加适当换行,以便版本管理
# -*- coding: UTF-8 -*-
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
import re
def format_html(html):
'''给 html 添加适当换行,以便版本管理
'''
html = re.sub('\n?\ ?<(address|blockquote|center|dir|div|dl|fieldset|form|h1|h2|h3|h4|h5|h6|hr|isindex|menu|noframes|noscript|ol|p|pre|table|ul|dd|dt|frameset|li|tbody|td|tfoot|th|thead|tr)([^>]*?)>', '\n<\\1\\2>', html)
html = re.sub('</(address|blockquote|center|dir|div|dl|fieldset|form|h1|h2|h3|h4|h5|h6|hr|isindex|menu|noframes|noscript|ol|p|pre|table|ul|dd|dt|frameset|li|tbody|td|tfoot|th|thead|tr)([^>]*?)>\ ?\n?', '</\\1\\2>\n', html)
html = re.sub('\n?<(img|hr|br)([^>]*?)>\n?', '\n<\\1\\2>\n', html)
return html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment