修复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+)οnlοad="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+)οnlοad="init\(\)">)/igm;
var path;
var console;

// get element by id
function get(id) {
    return document.getElementById(id);
}

// If the file is a html document
function isHTMLDocument(filename) {
        return /(\.html$)|(\.htm$)/i.test(filename);
}

// formate datetime
function 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;
}
// log
function log(level, msg) {
    var line = document.createElement('<div class="' + level + ' log"></div>');
    line.innerHTML = '[' + formateDate(new Date()) + '] ' + msg;
    console.appendChild(line);
    console.doScroll();
    line = null;
}

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);
    }
}

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 fixTutorials() {
    path = get('path').value;
    setTimeout(function() {
        travel(path);
        log('high', 'all works are done.');
    }, 0);
}

// initialization
function init() {
    console = get('console');
}

// show errors.
function showError(msg, url, line) {
    log('warn', 'Error: ' + msg + ' (line:' + line + ')');
    return true;
}

window.onerror = showError;
window.onload = init;
</script>
</head>
<body>
Path: <input type="text" id="path" size="74"/><button οnclick="fixTutorials()">Fix Tutorials</button>
<fieldset>
    <legend>Console</legend>
    <div id="console"></div>
</fieldset>
</body>
</html>

 

附上已经修复好的文档:

struts2-tutorials.part1.rar

struts2-tutorials.part2.rar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值