node.js 读目录.txt文件,用 xml2js 转换为json数据,生成jstree所需的文件

请参阅:java : pdfbox 读取 PDF文件内书签

请注意:书的目录.txt 编码:UTF-8,推荐用 Notepad++ 转换编码。

npm install elementtree ;

npm install xml2js ;

node.js 用 elementtree读目录.txt文件,用 xml2js 转换为json数据,ejs 生成 jstree模板所需的文件。

编写 txt_etree_json.js 如下

javascript 复制代码
// 读目录.txt文件,用xml2js 转换为json数据,生成jstree所需的文件
let fs = require('fs');
let process = require('process');
let path = require('path');
let readline = require('readline');
let et = require('elementtree');
let XML = et.XML;
let ElementTree = et.ElementTree;
//  element = et.Element;
let subElement = et.SubElement;
let xml2js = require('xml2js');
let ejs = require('ejs');

if (process.argv.length <3){
    console.error("usage: node txt_etree_json.js file1.txt");
    return 1;
}
let file1 = process.argv[2];
if (! fs.statSync(file1).isFile()) {
    console.error("it is not File.");
    return 2;
}
let ext = path.extname(file1);
if (ext.toLowerCase() != '.txt'){
    console.error(ext +" is not .txt");
    return 3;
}

let file2 = file1.replace('.txt', '.htm');
console.log(file2);
let fRead = fs.createReadStream(file1);
let fWrite = fs.createWriteStream(file2);
// 创建readline接口实例
let rline = readline.createInterface({
    input: fRead,
//  output: fWrite,
    terminal: true
});

// 用缩排表现层级关系,假设最多5个层级
const indent1 ="  ";
const indent2 ="    ";
const indent3 ="      ";
const indent4 ="        ";

var txt, title, root, edge, p_node, node1, node2, node3, node4, node5;
// line 事件
let ln =1;
rline.on('line', function(line){
    txt = line.trim();
    if (ln ==1){
        // 读取第一行:书名
        title = txt;
        // 创建主题节点
        root = et.Element("node");
        root.set('id', '1');
        root.set('text', txt);
        // 定义状态:
        state = subElement(root, "state")
        state.set("opened", 'true')
        state.set("disabled", 'true')
    }
    txt = txt.slice(0,-3); // 去掉行尾的页数
    if (txt.length ==0 || ln==1) ;
    else if (txt.length >0 && line.slice(0,1) !=' '){
        // 创建主题的子节点(1级节点)
        node1 = subElement(root, "children");
        node1.set('id', String(ln));
        node1.set('text', txt);
        p_node = node1; // 寄存父节点
    }
    else if (line.startsWith(indent1) && line.slice(2,3) !=' '){
        // 创建node1的子节点(2级节点)
        if (node1) node2 = subElement(node1, "children");
        else node2 = subElement(root, "children");
        node2.set('id', String(ln));
        node2.set('text', txt);
        p_node = node2;
    }
    else if (line.startsWith(indent2) && line.slice(4,5) !=' '){    
        // 创建node2的子节点(3级节点)
        if (node2) node3 = subElement(node2, "children");
        else if(node1) node3 = subElement(node1, "children");
        else node3 = subElement(root, "children");
        node3.set('id', String(ln));
        node3.set('text', txt);
        p_node = node3;
    }
    else if (line.startsWith(indent3) && line.slice(6,7) !=' '){
        // 创建node3的子节点(4级节点)
        if (node3) node4 = subElement(node3, "children");
        else if (node2) node4 = subElement(node2, "children");
        else if (node1) node4 = subElement(node1, "children");
        else node4 = subElement(root, "children");
        node4.set('id', String(ln));
        node4.set('text', txt);
        p_node = node4;
    }
    else if (line.startsWith(indent4) && line.slice(8,9) !=' '){
        // 创建node4的子节点(5级节点)
        if (node4) node5 = subElement(node4, "children");
        else if (p_node) node5 = subElement(p_node, "children");
        else node5 = subElement(root, "children");
        node5.set('id', String(ln));
        node5.set('text', txt);
    }
    else {
        console.log(txt);
    }
    ln++;
});
// 官网 www.npmjs.com/package/xml2js 查找 Options
//  Parser 用于解析xml为json对象 
let parser = new xml2js.Parser({explicitArray:false, mergeAttrs:true});
var etree, xml, json_str;
let filename ="jstree_template.htm";
rline.on('close', function(){
    etree = new ElementTree(root);
    xml = etree.write({'xml_declaration': false});
    parser.parseString(xml, function(err,res){
        if (err) throw err;
        //console.dir(res);
        json_str = '['+ JSON.stringify(res['node'], null,2) +']';
    });
    // 官网 ejs.bootcss.com/#docs  <%- 输出非转义的数据到模板 %>
  //ejs.renderFile(filename, data, options, function(err, str){
    ejs.renderFile(filename, {title:title, mydir:json_str}, {}, function(err, str){
        // str => 输出渲染后的 HTML 字符串
        fWrite.write(str);
    });    
    console.log("line number: "+ln);
});

https://gitee.com/ 搜索 jstree 下载

https://gitee.com/mirrors/jstree?_from=gitee_search

git clone https://gitee.com/mirrors/jstree.git

编写 jstree_template.htm 如下

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title><%= title%></title>
  <script src="../js/jquery-3.2.1.min.js"></script>
  <link rel="stylesheet" href="../js/jstree/dist/themes/default/style.css" />
  <script src="../js/jstree/dist/jstree.min.js"></script>
</head>
<body>
  <!-- 搜索框 -->
  <div class="search_input">
  <input type="text" id="search_a" />
  <img src="../js/jstree/dist/search.png" />
  </div>
  <div id="treeview1" class="treeview">
  </div>
<script type="text/javascript">
  var mydir = <%- mydir%>;
  $("#treeview1").jstree({
    'core' : {
    "multiple" : false,
    'data' : mydir,
    'dblclick_toggle': true
    },
    "plugins" : ["search"]
  });
  //输入框输入时自动搜索
  var tout = false;
  $('#search_a').keyup(function(){
    if (tout) clearTimeout(tout);    
    tout = setTimeout(function(){
      $('#treeview1').jstree(true).search($('#search_a').val());   
    }, 250);
  });   
</script> 
</body>
</html>

运行 node txt_etree_json.js your_pdf_dir.txt

生成 your_pdf_dir.htm

相关推荐
酷酷的威朗普42 分钟前
医院绩效考核系统
javascript·css·vue.js·typescript·node.js·echarts·html5
前端李易安13 小时前
Webpack 热更新(HMR)详解:原理与实现
前端·webpack·node.js
黎明晓月18 小时前
PostgreSQL提取JSON格式的数据(包含提取list指定索引数据)
postgresql·json·list
Ztiddler1 天前
【npm设置代理-解决npm网络连接error network失败问题】
前端·后端·npm·node.js·vue
前端青山1 天前
webpack进阶(一)
前端·javascript·webpack·前端框架·node.js
老攀呀1 天前
安装多个nodejs版本(nvm)
node.js
佚名程序员1 天前
【Node.js】全面解析 Node.js 安全最佳实践:保护您的应用
安全·node.js
心死翼未伤1 天前
python从入门到精通:pyspark实战分析
开发语言·数据结构·python·spark·json
Mephisto.java1 天前
【大数据学习 | flume】flume Sink Processors与拦截器Interceptor
大数据·sql·oracle·sqlite·json·flume
zxg_神说要有光2 天前
快速入门 AI:调用 AI 接口生成 React 组件
前端·javascript·node.js