initi.doc/templates/initi/search_inheritance.js

223 lines
5.3 KiB
JavaScript
Raw Normal View History

2018-12-10 08:04:43 +00:00
/**
* Created by Aleksey Chichenkov <a.chichenkov@initi.ru> on 12/6/18.
*/
var fs = require("fs");
var path = require('jsdoc/path');
var rx_class_declarator_std = /var *(.*?)(?:| )=(?:| ) std.class\(\[(.*?)\]/im;
var rx_requires = /var {1,}?(.*?) {1,}?= {1,}?require\("(.*?)"\)/i;
var __tree_list = Object.create(null);
var __root;
var map = {};
var destination;
2018-12-21 15:19:36 +00:00
var output;
var helper;
var logger;
var generate;
2018-12-10 08:04:43 +00:00
2018-12-21 15:19:36 +00:00
var find = function (_conf, _helper, _logger, _generate) {
helper = _helper;
logger = _logger;
generate = _generate;
2018-12-10 08:04:43 +00:00
__root = new Node();
destination = path.normalize(_conf.source.include_2[0]);
2018-12-21 15:19:36 +00:00
output = path.normalize(_conf.opts.destination);
2018-12-10 08:04:43 +00:00
recursive(destination);
generate_tree();
2018-12-21 15:19:36 +00:00
generate_files();
var struct = __root.get_struct();
return struct;
2018-12-10 08:04:43 +00:00
};
var recursive = function(_path){
if(fs.lstatSync(_path).isDirectory()) {
var arr = fs.readdirSync(_path);
for (var a = 0; a < arr.length; a++) {
var name = arr[a];
recursive(_path + "/" + name);
}
} else {
parse_file(_path);
}
};
var parse_file = function(_path){
let info = Object.create(null);
info.requires = Object.create(null);
info.classes = Object.create(null);
var file = fs.readFileSync(_path, "utf8");
var requires = exec_all(rx_requires, file);
if(requires.length > 0) {
for(var a = 0; a < requires.length; a++){
info.requires[requires[a].subs[0]] = update_link_file(requires[a].subs[1])
}
}
var classes = exec_all(rx_class_declarator_std, file);
if(classes.length > 0) {
for(var a = 0; a < classes.length; a++){
info.classes[classes[a].subs[0]] = classes[a].subs[1]
}
}
map[_path] = info;
};
var exec_all = function(_rx, _string){
var out = [];
var result;
while(result = _string.match(_rx)){
result = convert_result(result);
// console.log("result", result);
_string = _string.substring(result.index + result.match.length, _string.length);
out.push(result);
}
return out
};
var convert_result = function(_exec_result) {
var info = Object.create(null);
info.index = _exec_result.index;
info.match = _exec_result[0];
info.subs = [];
for (var a = 1; a < _exec_result.length; a++) {
info.subs.push(_exec_result[a]);
}
return info;
};
var update_link_file = function (_path) {
var arr = destination.split("/");
arr.pop();
return arr.join("/") + "/" + _path + ".js"
};
var generate_tree = function() {
2018-12-21 15:19:36 +00:00
__root.name = "std.class";
__root.path = destination + "/std/class";
2018-12-10 08:04:43 +00:00
for (var path in map) {
var file_info = map[path];
recursive_tree(path, file_info);
}
for(var name in __tree_list){
var n = __tree_list[name];
if(n.inherit === "base_class"){
__root.children.push(n);
}
}
};
var recursive_tree = function (_file_name, _file_info, _child) {
if(!_file_info){
2018-12-19 10:22:04 +00:00
// console.log("ERROR: ", _file_name);
2018-12-10 08:04:43 +00:00
return;
}
var classes = _file_info.classes;
var requires = _file_info.requires;
for(var name in classes){
var path = _file_name + ":" + name;
var node = __tree_list[path];
if(!node){
node = new Node();
if(name == "data"){
// console.log(_file_name);
}
node.name = name;
2018-12-21 15:19:36 +00:00
node.path = _file_name;
2018-12-10 08:04:43 +00:00
__tree_list[path] = node;
}
var inheritance = classes[name];
var req = requires[inheritance];
if(req){
if(_child && node.children.indexOf(_child) === -1){
_child.inherit = name;
node.children.push(_child);
}
var fi = map[req];
recursive_tree(req, fi, node);
}
}
};
var Node = function(){
this.children = [];
this.name = "";
2018-12-21 15:19:36 +00:00
this.path = "no_path";
this.link = "#no_link";
2018-12-10 08:04:43 +00:00
this.inherit = "base_class";
};
Node.prototype = {
get_struct: function () {
var children = [];
for(var a = 0; a < this.children.length; a++){
children.push(this.children[a].get_struct());
}
2018-12-21 15:19:36 +00:00
// console.log(this.path);
2018-12-10 08:04:43 +00:00
return {
name: this.name,
2018-12-21 15:19:36 +00:00
path: this.path,
link: this.link,
2018-12-10 08:04:43 +00:00
children: children
}
}
};
2018-12-21 15:19:36 +00:00
var generate_files = function(){
if(!fs.existsSync(output)){
fs.mkdirSync(output);
}
for(var key in __tree_list){
var node = __tree_list[key];
var name = node.path.split("/").splice(6).join("_");
2019-03-15 07:50:42 +00:00
// console.log("out_name:", name)
2018-12-21 15:19:36 +00:00
var out_path = output + "/" + name + ".html";
node.link = name + ".html";
2019-03-15 07:50:42 +00:00
// console.log(node.link, node.name, node.path);
2018-12-21 15:19:36 +00:00
try {
var source = {
kind: 'source',
code: helper.htmlsafe( fs.readFileSync(node.path, 'utf8') )
};
}
catch (e) {
logger.error('Error while generating source file %s: %s', name, e.message);
}
2019-03-15 07:50:42 +00:00
// out_path = out_path.replace("//", "/");
// console.log("out_path:", out_path);
generate('Source: ' + node.name, [source], node.link, false);
2018-12-21 15:19:36 +00:00
}
};
2018-12-10 08:04:43 +00:00
module.exports.find = find;