Commit 469a7b3b authored by Gabriela Bittencourt's avatar Gabriela Bittencourt
Browse files

Add link to related_objects to each -help file

For the related objects with help files it was add a link to the file
-help.pd. This was done in steps:

- First the indexing was changed to be indexed all the pd objects in doc
  and extra folders with name and path and in a second moment the
  selected folder (that can be only the doc or all the lib folder) will
  be detailed: the objects-help files with be read and it will be
  searched keywords, description and related objects

- Secondly, after finding the name of the related object, it's path will
  be searched in the pre-existed indexing and if it was not already
  added as a related object it will be added now (it adds robustness to
  the code)

- At least, in the exibition of the related objects, the objects with
  found help, will have a link to the patch-help.pd and the objects
  without a help, will be presented in a non-clickable black font.
parent 134df600
Pipeline #3675 canceled with stage
......@@ -837,7 +837,18 @@ function display_doc(doc) {
}
if (doc.related_objects) {
var p_rel_objs = document.createElement("p");
p_rel_objs.innerHTML = "Related objects: ".bold() + doc.related_objects.toString();
var ref_rel_objs = doc.ref_related_objects.split("\,");
p_rel_objs.innerHTML = "Related objects: ".bold();
doc.related_objects.split(" ").forEach(function (rel_obj, i, a) {
let link_rel_obj = rel_obj;
if (ref_rel_objs[i]) {
let ref_rel_obj = "javascript: pdgui.doc_open('" +
pdgui.defunkify_windows_path(path.dirname(ref_rel_objs[i])) + "', '" +
pdgui.defunkify_windows_path(path.basename(ref_rel_objs[i])) + "');";
link_rel_obj = rel_obj.link(ref_rel_obj);
}
p_rel_objs.innerHTML += " " + link_rel_obj;
});
p_rel_objs.style.marginTop = "-10px";
div.appendChild(p_rel_objs);
}
......
......@@ -13,6 +13,14 @@ exports.get_pwd = function() {
return pwd;
}
function funkify_windows_path(s) {
var ret = s;
if (process.platform === "win32") {
ret = ret.replace(/\//g, "\\");
}
return ret;
}
function defunkify_windows_path(s) {
var ret = s;
if (process.platform === "win32") {
......@@ -25,6 +33,8 @@ exports.set_pd_engine_id = function (id) {
pd_engine_id = id;
}
exports.funkify_windows_path = funkify_windows_path;
exports.defunkify_windows_path = defunkify_windows_path;
function gui_set_browser_config(doc_flag, path_flag, init_flag, helppath) {
......@@ -103,6 +113,7 @@ var fs = require("fs");
var path = require("path");
var dive = require("./dive.js"); // small module to recursively search dirs
var elasticlunr = require("./elasticlunr.js"); // lightweight full-text search engine in JavaScript, cf. https://github.com/weixsong/elasticlunr.js/
elasticlunr.clearStopWords();
function init_elasticlunr()
{
......@@ -111,8 +122,8 @@ function init_elasticlunr()
index.addField("keywords");
index.addField("description");
index.addField("related_objects");
//index.addField("body");
index.addField("path");
index.addField("ref_related_objects");
index.addField("dir");
index.setRef("id");
return index;
}
......@@ -121,6 +132,12 @@ var index = init_elasticlunr();
var index_cache = new Array();
var index_manif = new Set();
function regex_dir(dir) {
let str_regex = funkify_windows_path(dir).replace(/\\/g, "\\\\").replace(/\//g, "\\\/")
+ "\\" + path.sep + "?([\\w]*)\\" + path.sep + "?([\\w|\\.|\\-|\\~]*)\\" + path.sep + "?([\\S]*)";
return (RegExp(str_regex.toString()));
}
function index_entry_esc(s) {
if (s) {
var t = s.replace(/\\/g, "\\\\").replace(/:/g, "\\:");
......@@ -130,8 +147,13 @@ function index_entry_esc(s) {
}
}
function add_doc_to_index(filename, data) {
var rel_objs;
function add_doc_details_to_index(filename, data) {
var title = path.basename(filename, "-help.pd"),
big_line = data.replace("\n", " "),
keywords,
desc,
rel_objs,
ref_rel_objs;
const machine_related_objects = {
found_objects: [],
ref_found_objs: [],
......@@ -146,38 +168,55 @@ function add_doc_to_index(filename, data) {
},
SEARCHING_OBJS: {
search_regex: function(line_search) {
var rel_obj = line_search.match(/#X obj \-?\d+ \-?\d+ (pddp\/helplink )?(\w*\/)?([\w|\~]*);/i);
var rel_obj = line_search.match(/#X obj \-?\d+ \-?\d+ (pddp\/helplink )?(\w*\/)?([\w|\~]+);/i);
if (rel_obj != null) {
this.found_objects.push(rel_obj[3]);
this.ref_found_objs.push(rel_obj);
this.adds(rel_obj[3]);
} else if (/#X restore \-?\d+ \-?\d+ pd Related_objects;/i.test(line_search)) {
this.state = "SEARCHING_CANVAS";
} else if ((/#N canvas \-?\d+ \-?\d+ \-?\d+ \-?\d+ [\S]* \-?\d+;/i.test(line_search))) {
if (title != "random") {
this.adds("pd");
}
this.state = "PD_AS_REL_OBJ";
}
},
},
PD_AS_REL_OBJ: {
search_regex: function(line_search) {
if (/#X restore \-?\d+ \-?\d+ pd;/i.test(line_search)) {
this.state = "SEARCHING_OBJS";
}
}
}
},
dispatch(actionName, ...line_string) {
dispatch(actionName, line_string) {
const action = this.transitions[this.state][actionName];
if (action) {
action.call(this, ...line_string);
action.call(this, line_string);
}
},
adds(obj_title) {
if (!this.found_objects.includes(obj_title)) {
let obj_path = index.search(obj_title, {fields: {title: {}}});
if (obj_path.length > 0) obj_path = obj_path[0].ref;
this.found_objects.push(obj_title);
this.ref_found_objs.push(obj_path);
}
},
};
rel_objs = Object.create(machine_related_objects);
let eval_string = data.split("\n");
eval_string.forEach(function(l,a,i) {
rel_objs.dispatch('search_regex', l);
rel_objs.dispatch('search_regex', l.toString().replace(/\(/g, "\\\(")
.replace(/\)/, "\\\)"));
});
ref_rel_objs = rel_objs.ref_found_objs.toString();
rel_objs = rel_objs.found_objects;
rel_objs = rel_objs ? rel_objs.toString().replace( /,/g, " ") : null;
rel_objs = rel_objs ? rel_objs.toString().replace(/\,/g, " ") : null;
var title = path.basename(filename, ".pd"),
big_line = data.replace("\n", " "),
keywords,
desc;
// We use [\s\S] to match across multiple lines...
keywords = big_line
.match(/#X text \-?[0-9]+ \-?[0-9]+ KEYWORDS ([\s\S]*?);/i),
.match(/#X text \-?[0-9]+ \-?[0-9]+ KEYWORDS ([\s\S]*?);/i);
desc = big_line
.match(/#X text \-?[0-9]+ \-?[0-9]+ DESCRIPTION ([\s\S]*?);/i);
keywords = keywords && keywords.length > 1 ? keywords[1].trim() : null;
......@@ -189,34 +228,47 @@ function add_doc_to_index(filename, data) {
desc = desc.replace(" \\,", ",");
}
if (title.slice(-5) === "-help") {
title = title.slice(0, -5);
}
index_cache[index_cache.length] = [filename, title, keywords, desc, rel_objs]
index_cache[index_cache.length] = [filename, title, keywords, desc, rel_objs, ref_rel_objs]
.map(index_entry_esc).join(":");
var d = path.dirname(filename);
index_manif.add(d);
// Also add the parent directory to catch additions of siblings.
index_manif.add(path.dirname(d));
index.addDoc({
index.update({
"id": filename,
"title": title,
"keywords": keywords,
"description": desc,
"related_objects": rel_objs
"related_objects": rel_objs,
"ref_related_objects": ref_rel_objs
//"body": big_line,
});
}
function read_file(err, filename, stat) {
function make_cache(filename) {
index_cache[index_cache.length] = [filename, path.basename(filename, "-help.pd"), null, null, null, null]
.map(index_entry_esc).join(":");
var d = path.dirname(filename);
index_manif.add(d);
// Also add the parent directory to catch additions of siblings.
index_manif.add(path.dirname(d));
}
function add_doc_to_fast_index(err, filename, stat) {
if (!err) {
if (filename.slice(-3) === ".pd") {
// AG: We MUST read the files synchronously here. This might be a
// performance issue on some systems, but if we don't do this then
// we may open a huge number of files simultaneously, causing the
// process to run out of file handles.
if (filename.slice(-8) === "-help.pd") {
try {
var data = fs.readFileSync(filename, { encoding: "utf8", flag: "r" });
add_doc_to_index(filename, data);
let title = path.basename(filename, "-help.pd");
let regex_dir_compare = filename.match(regex_home_dir);
var dir = regex_dir_compare[1];
if (dir=="extra" && regex_dir_compare[3]) {
dir = dir + "\/" + regex_dir_compare[2];
}
index.addDoc({
"id": filename,
"title": title,
"dir": dir
})
} catch (read_err) {
post("err: " + read_err);
}
......@@ -230,6 +282,7 @@ function read_file(err, filename, stat) {
var index_done = false;
var index_started = false;
var index_start_time;
var regex_home_dir;
// Filenames for the index cache, relative to the user's homedir.
const cache_basename = nw_os_is_windows
......@@ -311,6 +364,36 @@ function make_index() {
var doc_path = browser_doc?path.join(lib_dir, "doc"):lib_dir;
var i = 0;
var l = help_path.length;
function detail_files () {
post("adding details to files in " + doc_path);
let dir = doc_path.match(regex_home_dir);
let all_indexed_files = Object.keys(index.documentStore.docs);
let files_not_to_detail = all_indexed_files;
var files_to_detail, data;
if (!dir) {
files_to_detail = all_indexed_files;
files_not_to_detail = null;
} else {
dir = (dir[1]=="extra" && dir[3])?(dir[1]+"\/"+dir[2]):dir[1];
files_to_detail = index.search(dir,{fields: {dir: {}}}).map(obj => obj.ref);
files_not_to_detail = files_not_to_detail.filter(doc => !files_to_detail.includes(doc));
}
files_to_detail.forEach(function(filename,i,a) {
// AG: We MUST read the files synchronously here. This might be a
// performance issue on some systems, but if we don't do this then
// we may open a huge number of files simultaneously, causing the
// process to run out of file handles.
try {
data = fs.readFileSync(filename, { encoding: "utf8", flag: "r" });
add_doc_details_to_index(filename, data);
} catch (read_err) {
post("err: " + read_err);
}
});
if (browser_path) make_index_cont();
if (files_not_to_detail) files_not_to_detail.forEach(file => make_cache(file));
finish_index();
}
function make_index_cont() {
if (i < l) {
var doc_path = help_path[i++];
......@@ -320,13 +403,13 @@ function make_index() {
fs.lstat(full_path, function(err, stat) {
if (!err) {
post("building help index in " + doc_path);
dive(full_path, read_file, make_index_cont);
detail_files();
} else {
make_index_cont();
}
});
} else {
finish_index();
// finish_index();
}
}
pdsend("pd gui-busy 1");
......@@ -358,12 +441,14 @@ function make_index() {
var keywords = e[2] ? e[2] : null;
var desc = e[3] ? e[3] : null;
var rel_obj = e[4] ? e[4] : null;
var ref_rel_obj = e[5] ? e[5] : null;
index.addDoc({
"id": filename,
"title": title,
"keywords": keywords,
"description": desc,
"related_objects": rel_obj
"related_objects": rel_obj,
"ref_related_objects": ref_rel_obj
});
}
}
......@@ -371,8 +456,9 @@ function make_index() {
} else {
// no index cache, or it is out of date, so (re)build it now, and
// save the new cache along the way
post("building help index in " + doc_path);
dive(doc_path, read_file, browser_path?make_index_cont:finish_index);
regex_home_dir = regex_dir(lib_dir);
post("building help index in " + lib_dir);
dive(lib_dir, add_doc_to_fast_index, detail_files);
}
pdsend("pd gui-busy 0");
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment