JS几乎不用考虑时间复杂度太高。
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// 监听键入回车事件
var a=new Array(),k=0;
rl.on('line', (str) => {
// str即为输入的内容
if (str === 'close') {
// 关闭逐行读取流 会触发关闭事件
rl.close()
}
a[k++]=str;//保存读入数据
});
rl.on('close', () => { //输入完毕
//输入完后处理。
//输入完后变量为a。
var ul=false;
for(var i=0;i<a.length;i++){
var em=false,url=false,bold=false;
var nowline=a[i];
if(nowline===""){
if(ul===true){
console.log("</ul>");
ul=false;
}
continue;
}
var cnt=0;
var j=0;
if(nowline[j]=='*'){
if(nowline[j+1]==' '){
if(ul===false){
console.log("<ul>");
}
ul=true;
}
}else{
if(ul===true){
console.log("</ul>");
}
ul=false;
}
j=0;
while(j<nowline.length){
if(nowline[j]=='_'){
var be=nowline.substr(0,j);
var af=nowline.substr(j+1,nowline.length-j-1);
nowline=be+"<"+(em?"/":"")+"em>"+af;
em=!em;
}
if(nowline[j]=='['){
var afk=-1;
var m1=-1;
for(var k=j+1;k<nowline.length;k++){
if(nowline[k]==']' && afk==-1) afk=k;
else if(nowline[k]==')' && m1==-1){
m1=k;
break;
}
}
try{
if(m1==-1 || afk==-1){
}else{
//console.log(afk,":",m1-afk-2-1,":",m1,":");
var be=nowline.substr(0,j);
var af=nowline.substr(m1+1,nowline.length-m1-1);
var url=nowline.substr(afk+2,m1-afk-2);
var value=nowline.substr(j+1,afk-j-1);
nowline=be+"<a href=\""+url+"\">"+value+"</a>"+af;
j++;
}
}catch(e){
}
//console.log(be,":",af,":",url,":",value);
}
j++;
}
for(var j=0;j<=6;j++){
if(nowline[j]!='#'){
if(nowline[j]!=' ') cnt=0;
break;
}else{
cnt++;
}
}
if(cnt!==0){
nowline="<h"+cnt+">"+nowline.substr(cnt+1,nowline.length-cnt-1)+"</h"+cnt+">"
}else{
if(!ul){
if(i==0) nowline="<p>"+nowline;
else if(a[i-1]==""){
nowline="<p>"+nowline;
}
if(i==a.length-1) nowline=nowline+"</p>";
else if(a[i+1]==""){
nowline=nowline+"</p>";
}
}
else
nowline="<li>"+nowline.substr(2,nowline.length-2)+"</li>";
}
var out="";
for(var j=0;j<nowline.length;j++){
if(nowline[j]=='>'){
for(var k=j+1;k<nowline.length;k++){
if(nowline[k]!=' '){
nowline=nowline.substr(0,j+1)+nowline.substr(k,nowline.length-k);
break;
}
}
break;
}
}
console.log(nowline);
if(ul===true && i==a.length-1){
console.log("</ul>");
}
}
});
//你快乐吗?
img