这段时间需要为公司编辑公众号,需要做一些效果漂亮的布局(图片,按钮,标题),但是默认微信官方并不能用原生的HTML编写公众号,所以需要借助第三方来编辑,壹伴还不错,可以直接把html 源代码放到微信公众号网页里。但是微信公众号内的HTML是没有像原生那样,有style标签的,只有包含在body标签里面的代码。而且body标签里面的代码样式是内联样式,这个还特意查看了网页链接的公众号,进行调试,发现确实只能是内联样式,(PS:可能更容易让用户图形化操作吧)。内联样式看着维护很难,很难去区分那个标签是什么作用的。
解决方法:所以可以用python(PS:或者其他代码,例如nodejs,个人喜欢python的风格,write less code do more thing)来读取一个html文件,然后提取style标签里面的内容 和 body标签里面的内容,再进行类型匹配,替代为内联样式,下面是python的源代码:
import sys
def is_space(s):
return s=='\r' or s=='\n' or s=='\t' or s==' '
class ConvertInnerStyle:
"""this class is to convert the inner style into the inline style
generate the html code which only contain the source code within the body
it could assist the people who want to make the customize web page in the wechat subscription"""
def __init__(self,origin):
"""make the constructor to be clear"""
self.total_content=''
self.style_dict={}
self._fg_init(origin)