修复Sturts2发布包中的教程文档 | #struts2 #hta #regex
在Struts2的发行包中,有一份帮助文档,位于“struts-2.1.8.1.zip\struts-2.1.8.1\docs\docs”
?
该文档包含如下一些不错的资源:
?
Tutorials? Our tutorials are designed to help you get started with the framework ASAP. We offer an all-purpose "Bootstrap" tutorial as well as specialty tutorials on portlets and database access.? Guides? Our in-depth technical guides focus on specific components of the framework, such as the Core framework, Struts Tags, and optional Extensions, as well as migrating from Struts 1 or WebWork 2.? FAQs and Cookbook? Our FAQs and Cookbook examples provide a wide range of rapid-fire "HOWTOs" in question-and-answer format.? Security Bulletins? Our security bulletins explain any security issues and their solutions? Other Resources? Books, articles, and presentations about Struts 2.?这些资源对于初学者来说,是很好的学习资料,不过这些HTML文件中包含一以http://开头的外部样式表文件,每次在打开这些HTML文档时,页面都要很久时间才能显示。
?
<LINK type="text/css" rel="stylesheet" href="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shStyles.css">
?
只要去掉这句,页面就可以很快打开,不过逐个去除不太现实,作为程序员,自然要使用聪明的办法,其实网上有许多批量查找替换文件内容的软件,不过本着研究的精神,咱还是自己搞吧。

这里依然选择用HTA+JS开发,不过无论用什么语言,思路都是一样的。
?
最核心的东西当然是匹配要去掉的样式的正则表达式了:
?
var pattern = /(<LINK.+href="http:\/\/.*)|(<SCRIPT[\s\S]*<\/SCRIPT>)|((<BODY\s+)onload="init\(\)">)/igm;
?
这里我顺便把脚本和<body>标签也去掉了,因为这些脚本会导致在使用IE查看时,有安全提示,很讨厌。
?
然后就是指定一个目录,然后递归遍历之:
?
function travel(path) { log('high', 'scanning folder "' + path + '"'); var dir = fso.GetFolder(path); var folders = new Enumerator(dir.SubFolders); var files = new Enumerator(dir.Files); // travel files for (; !files.atEnd(); files.moveNext()) { var doc = files.item(); log('info', 'analyzing file "' + doc + '"'); if (isHTMLDocument(doc)) { fixTutorial(doc); } } // travel folders for (; !folders.atEnd(); folders.moveNext()) { travel(folders.item()); }}?
其次就是具体的替换操作了,先读取文件内容,替换后再重新写进去:
?
function fixTutorial(filename) { try { log('info', 'fixing file: ' + filename); var file = fso.OpenTextFile(filename, 1); var html = file.ReadAll().replace(pattern, ''); file.Close(); file = fso.OpenTextFile(filename, 2); file.Write(html); file.Close(); log('high', 'completed.'); } catch (e) { log('warn', 'failed fixing file "' + filename + '", ' + e.description); }}?完整代码:
?
<html><head><title>Struts2 Tutorial Fix</title><style type="text/css">#console { height: 400px; overflow: auto; }.log { word-break: break-all;word-wrap: break-word; }.warn { color: red; border-top: 1px solid #DDD; }.info { color: gray; border-top: 1px solid #DDD; }.high { color: green; border-top: 1px solid #DDD; }</style><script type="text/javascript">var fso = new ActiveXObject('Scripting.FileSystemObject');var pattern = /(<LINK.+href="http:\/\/.*)|(<SCRIPT[\s\S]*<\/SCRIPT>)|((<BODY\s+)onload="init\(\)">)/igm;var path;var console;// get element by idfunction get(id) { return document.getElementById(id);}// If the file is a html documentfunction isHTMLDocument(filename) { return /(\.html$)|(\.htm$)/i.test(filename);}// formate datetimefunction formateDate(datetime) { var hour = datetime.getHours(); var min = datetime.getMinutes(); var sec = datetime.getSeconds(); hour = (hour < 10 ? '0' : '') + hour; min = (min < 10 ? '0' : '') + min; sec = (sec < 10 ? '0' : '') + sec; return hour + ':' + min + ':' + sec;}// logfunction log(level, msg) { var line = document.createElement('<div id="path" size="74"/><button onclick="fixTutorials()">Fix Tutorials</button><fieldset> <legend>Console</legend> <div id="console"></div></fieldset></body></html>?
附上已经修复好的文档:
struts2-tutorials.part1.rar
struts2-tutorials.part2.rar