知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X(jué)表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
您當(dāng)前位置>首頁(yè) » 新聞資訊 » 公眾號(hào)相關(guān) >
如何用HTML編輯微信公眾號(hào),正常的網(wǎng)頁(yè)HTML轉(zhuǎn)化為內(nèi)聯(lián)樣式的代碼解決方案(jackson)
發(fā)表時(shí)間:2020-10-19
發(fā)布人:葵宇科技
瀏覽次數(shù):175
這段時(shí)間需要為公司編輯公眾號(hào),需要做一些效果漂亮的布局(圖片,按鈕,標(biāo)題),但是默認(rèn)微信官方并不能用原生的HTML編寫公眾號(hào),所以需要借助第三方來(lái)編輯,壹伴還不錯(cuò),可以直接把html 源代碼放到微信公眾號(hào)網(wǎng)頁(yè)里。但是微信公眾號(hào)內(nèi)的HTML是沒(méi)有像原生那樣,有style標(biāo)簽的,只有包含在body標(biāo)簽里面的代碼。而且body標(biāo)簽里面的代碼樣式是內(nèi)聯(lián)樣式,這個(gè)還特意查看了網(wǎng)頁(yè)鏈接的公眾號(hào),進(jìn)行調(diào)試,發(fā)現(xiàn)確實(shí)只能是內(nèi)聯(lián)樣式,(PS:可能更容易讓用戶圖形化操作吧)。內(nèi)聯(lián)樣式看著維護(hù)很難,很難去區(qū)分那個(gè)標(biāo)簽是什么作用的。
解決方法:所以可以用python(PS:或者其他代碼,例如nodejs,個(gè)人喜歡python的風(fēng)格,write less code do more thing)來(lái)讀取一個(gè)html文件,然后提取style標(biāo)簽里面的內(nèi)容 和 body標(biāo)簽里面的內(nèi)容,再進(jìn)行類型匹配,替代為內(nèi)聯(lián)樣式,下面是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)
def _fg_init(self,origin):
"""initialize the resouces that we provided """
f=open(origin,'r',encoding="UTF-8")
self.totalContent=f.read()
f.close()
self._process_style()
def _fg_find_style(self,tag):
"""this is the method that can generate the substring which match the tag name of the html"""
preffix='<'+tag+'>'
suffix=r'</'+tag+'>'
s=self.totalContent.find(preffix)
e=self.totalContent.find(suffix)
return self.totalContent[s+len(preffix)-1:e]
def _process_style(self):
"""process the source code within the style tag ,generate the content which match the class name,(optional it can process the multiple classes which has the same styles)"""
#get the source code within the style tag of the html
inner_styles=self._fg_find_style('style')
#print(inner_styles)
#for loop that find the index of the } character
end_index=inner_styles.find('}')
print(end_index)
while end_index!=-1:
#get the substring of the single styles block
#which contain .xxx{xxxx} .xxx,.yyy,.uuu {xxx;xxxx;}
single_styles=inner_styles[:end_index+1] #because the end_index is the forward element of }
print(single_styles)
#first find the index of {
index=single_styles.find('{')
ii=index-1
#the variable to check whether it is the last class before the style block
is_valid=True
class_name=''
class_name_list=[]
#from the end to the start loop through each charater find the class name list which can contain at least one class
while ii>=0:
#if it is the space skip it
if is_space(single_styles[ii]):
ii-=1
continue
c=single_styles[ii]
if c==',':
is_valid=True
ii-=1
continue
if is_valid:
if c=='.':
is_valid=False
#because this is the reverse class name so make it forward
l=list(class_name)
l.reverse()
class_name=''.join(l)
class_name_list.append(class_name)
#empty the class name in order to reuse next loop
class_name=''
else:
class_name=class_name+c
ii-=1
#find the style block and strip the empty the \r\n charater
style_block=single_styles[index+1:end_index]
style_block=style_block.strip().replace('\r','').replace('\n','')
#put the element of the class name list to the dictionary
for tmp in class_name_list:
self.style_dict[tmp]=style_block
inner_styles=inner_styles[end_index+1:]
end_index=inner_styles.find('}')
for key in self.style_dict:
print(key+' : '+self.style_dict[key])
def generate(self,filename):
#find the body
body=self._fg_find_style('body')
for key in self.style_dict:
body=body.replace('class="'+key+'"','style="'+self.style_dict[key]+'"')
f=open(filename,'w',encoding='UTF-8')
f.write(body)
f.close()
def main():
t=ConvertInnerStyle(str(sys.argv[1]))
t.generate(str(sys.argv[2]))
if __name__ == "__main__":
main()
使用方法:
1、通過(guò)命令行工具 python converter.py 需要轉(zhuǎn)換的文件 最后生成的內(nèi)聯(lián)樣式文件
這個(gè)工具還不支持跳過(guò)注釋,有興趣的同學(xué)可以自己去修正
.header .hr,
.header .content-wrap .content,
.header .content-wrap .icon,
.header,
.header .content-wrap {
width: 100%;
height: 40px;
display: flex;
align-items: flex-end;
justify-content: flex-start;
}
原始html里面的內(nèi)部樣式
content-wrap : width: 100%; height: 40px; display: flex; align-items: flex-end; justify-content: flex-start;
header : width: 100%; height: 40px; display: flex; align-items: flex-end; justify-content: flex-start;
icon : width: 100%; height: 40px; display: flex; align-items: flex-end; justify-content: flex-start;
hr : width: 100%; height: 40px; display: flex; align-items: flex-end; justify-content: flex-start;
通過(guò)python代碼提取的對(duì)應(yīng)類名 : 樣式的字典
相關(guān)案例查看更多
相關(guān)閱讀
- 云南網(wǎng)站建設(shè)選
- 前端技術(shù)
- 定制小程序開(kāi)發(fā)
- 文山小程序開(kāi)發(fā)
- 云南小程序開(kāi)發(fā)費(fèi)用
- 云南網(wǎng)站建設(shè)公司哪家好
- 網(wǎng)站建設(shè)價(jià)格
- 云南網(wǎng)站建設(shè)方法
- 小程序分銷商城
- 云南小程序被騙
- 小程序開(kāi)發(fā)平臺(tái)前十名
- 海報(bào)插件
- 報(bào)廢車管理系統(tǒng)
- 云南網(wǎng)站維護(hù)
- 政府網(wǎng)站建設(shè)服務(wù)
- 昆明軟件定制
- 云南省住房建設(shè)廳網(wǎng)站
- 人口普查小程序
- 報(bào)廢車回收管理軟件
- 昆明小程序公司
- 云南小程序開(kāi)發(fā)推薦
- 搜索引擎自然排名
- 網(wǎng)站優(yōu)化
- 小程序模板開(kāi)發(fā)公司
- 搜索排名
- 小程序開(kāi)發(fā)費(fèi)用
- 云南網(wǎng)站建設(shè)百度
- 云南網(wǎng)站建設(shè)專家
- 北京小程序制作
- typescript