Recently wrote a bootstrap page ... Because the function needs to decide a page to solve all the problems, and then use jquery to dynamically display features .... However, the page would be quite large, with a stack of hidden modal windows and feature div all stacked together.
And then I wrote a small script with Python to support <include> tags, which is used to combine external HTML files to force a single large HTML page to be written.
Use the next feeling pretty good, share to everyone
How to use:
Use the <include src= "" > tag in HTML to import additional HTML code. Supports nested substitutions (such as a page nested b page, b page nesting c page). However, be careful to loop nesting (a page nested b page, b page nested a page), which causes a dead loop
The main page is index.html for the default processing page, and the merged page is generated newhtml.html
The specific code is as follows
Import codecs import webbrowser import sys charset = "Utf-8" #文件编码 files in the #读取text里的 <include> label and SRC attribute, replace the original label Def Repla
Ceinclude (filename,text): Try:posa = Text.find ("<include") while posa!= -1:posc = Text.find (">", Posa) Tag = text[posa:posc+1] Posa = Text.find ("src=", posa) Posa + = 5 PosB = text.find ("\", posa) file = Text[posa:p OsB] #获取src中的文件名 print ("Processing:", file) tmpfile = Codecs.open (file, "R", CharSet) Tmptext = Tmpfile.read () tmptext = Replaceinclude (file,tmptext) #递归处理文件嵌套后的include标签 text = Text.replace (tag,tmptext) tmpfile.close () Posa = Text.f
IND ("<include") return text; Except Exception as E:print ("Error: File", filename, "in", File, "Processing failed!") Error message: \ n ", E) sys.exit (1) readFile = Codecs.open (" index.html "," R ", CharSet) WriteFile = Codecs.open (" newhtml.html "," W " , charset) Try:text = Readfile.read () Text = Replaceinclude ("index.html", Text) writefile.write (text) Webbrowser.open (" Newhtml.html ") Finally:readFile.close () WRITefile.close () </pre>
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.