拆分前的代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>index首页</title>
<style>
html,
body {
margin: 0;
padding: 0;
height: 100%;
background-image: linear-gradient(to bottom right, red, gold);
}
.box {
width: 400px;
height: 250px;
background-color: rgba(255, 255, 255, 0.6);
border-radius: 6px;
position: absolute;
left: 50%;
top: 40%;
transform: translate(-50%, -50%);
box-shadow: 1px 1px 10px #fff;
text-shadow: 0px 1px 30px white;
display: flex;
justify-content: space-around;
align-items: center;
font-size: 70px;
user-select: none;
padding: 0 20px;
/* 盒子投影 */
-webkit-box-reflect: below 0px -webkit-gradient(linear, left top, left bottom, from(transparent), color-stop(0%, transparent), to(rgba(250, 250, 250, .2)));
}
</style>
</head>
<body>
<div class="box">
<div id="HH">00</div>
<div>:</div>
<div id="mm">00</div>
<div>:</div>
<div id="ss">00</div>
</div>
<script>
window.onload = function () {
// 定时器,每隔 1 秒执行 1 次
setInterval(() => {
var dt = new Date()
var HH = dt.getHours()
var mm = dt.getMinutes()
var ss = dt.getSeconds()
// 为页面上的元素赋值
document.querySelector('#HH').innerHTML = padZero(HH)
document.querySelector('#mm').innerHTML = padZero(mm)
document.querySelector('#ss').innerHTML = padZero(ss)
}, 1000)
}
// 补零函数
function padZero(n) {
return n > 9 ? n : '0' + n
}
</script>
</body>
</html>
1.调用fs.readFile()方法读取文件
2.使用正则表达式提取需要的内容
3.再调用fs.writeFile()方法,将提取出来的内容写入文件里面
代码实现
// 1.导入fs模块,来操作文件
const fs = require('fs')
// 2. 导入path模块
const path = require("path")
// 3.定义正则表达式,分别匹配<style></style>,<script></script>标签
const regStyle = /<style>[\s\S]*<\/style>/
const regScript = /<script>[\s\S]*<\/script>/
// 4.调用fs.readFile()方法读取文件 路径使用path.join()进行拼接
const a = fs.readFile(path.join(__dirname + '/files/index.html'),"utf8",function(err,data){
if(err){
return console.log("读取html文件失败",err.message);
}
resolveCss(data)
resolveJs(data)
resolveHTML(data)
// console.log(data);
})
// 5.1 定义处理css样式的方法
function resolveCss(htmlStr){
// 5.2 使用正则表达式提取需要的内容
const r1 = regStyle.exec(htmlStr)
// 5.3 将提取的样式字符串,进行字符串的replace替换操作
const newCss = r1[0].replace('<style>','').replace('</style>','')
// 5.4 调用fs.writeFile()方法,将提取的样式,写入到clock目录中index.css文件里面
fs.writeFile(path.join(__dirname,"/clock/index.css"),newCss,function(err){
if(err){
return console.log("写入css文件失败",err.message);
}
console.log("写入css文件成功");
})
}
// 6.1 定义处理js脚本的方法
function resolveJs(htmlStr) {
// 6.2 使用正则表达式提取需要的内容
const r2 = regScript.exec(htmlStr)
// 6.3 将提取的样式和字符串,进行字符串的replace替换操作
const newJs = r2[0].replace("<script>",'').replace("</script>",'')
// 6.4 调用fs.writeFile()方法,将提取的js,写入到clock目录中的index.js文件里
fs.writeFile(path.join(__dirname,"/clock/index.js"),newJs,function(err){
if(err){
return console.log("写入js文件失败",err.message);
}
console.log("写入js文件成功");
})
}
// 7.1 定义处理html结构的方法
function resolveHTML(htmlStr){
// 7.2 将字符串调用replace方法,把内嵌的style和script标签,替换未外联的link和script标签
const newHtml = htmlStr.replace(regStyle,'<link rel="stylesheet" href="./index.css">').
replace(regScript,'<script src="./index.js"></script>')
// 7.3 调用fs.writeFile()方法,将提取的html,写入到clock目录中的index.html文件里
fs.writeFile(path.join(__dirname,"/clock/index.html"),newHtml,function(err){
if(err){
return console.log("写入html文件失败",err.message);
}
console.log("写入html文件成功");
})
}
处理后的代码
html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>index首页</title>
<link rel="stylesheet" href="./index.css">
</head>
<body>
<div class="box">
<div id="HH">00</div>
<div>:</div>
<div id="mm">00</div>
<div>:</div>
<div id="ss">00</div>
</div>
<script src="./index.js"></script>
</body>
</html>
css文件
html,
body {
margin: 0;
padding: 0;
height: 100%;
background-image: linear-gradient(to bottom right, red, gold);
}
.box {
width: 400px;
height: 250px;
background-color: rgba(255, 255, 255, 0.6);
border-radius: 6px;
position: absolute;
left: 50%;
top: 40%;
transform: translate(-50%, -50%);
box-shadow: 1px 1px 10px #fff;
text-shadow: 0px 1px 30px white;
display: flex;
justify-content: space-around;
align-items: center;
font-size: 70px;
user-select: none;
padding: 0 20px;
/* 盒子投影 */
-webkit-box-reflect: below 0px -webkit-gradient(linear, left top, left bottom, from(transparent), color-stop(0%, transparent), to(rgba(250, 250, 250, .2)));
}
js文件
window.onload = function () {
// 定时器,每隔 1 秒执行 1 次
setInterval(() => {
var dt = new Date()
var HH = dt.getHours()
var mm = dt.getMinutes()
var ss = dt.getSeconds()
// 为页面上的元素赋值
document.querySelector('#HH').innerHTML = padZero(HH)
document.querySelector('#mm').innerHTML = padZero(mm)
document.querySelector('#ss').innerHTML = padZero(ss)
}, 1000)
}
// 补零函数
function padZero(n) {
return n > 9 ? n : '0' + n
}