@npmstuff/argdown-core
Version:
A pluggable parser for the Argdown argumentation syntax
168 lines • 6.24 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GroupPlugin = void 0;
const ArgdownPluginError_1 = require("../ArgdownPluginError");
const model_1 = require("../model/model");
const utils_1 = require("../utils");
const lodash_defaultsdeep_1 = __importDefault(require("lodash.defaultsdeep"));
const defaultSettings = {
groupDepth: 2,
sections: utils_1.ensure.object({}),
regroup: utils_1.ensure.array([])
};
class GroupPlugin {
constructor(config) {
this.name = "GroupPlugin";
this.getSettings = (request) => {
if ((0, utils_1.isObject)(request.group)) {
return request.group;
}
else {
request.group = {};
return request.group;
}
};
this.prepare = (request, response) => {
(0, ArgdownPluginError_1.checkResponseFields)(this, response, [
"statements",
"arguments",
"relations"
]);
let settings = this.getSettings(request);
(0, utils_1.mergeDefaults)(settings, this.defaults);
};
this.run = (request, response) => {
(0, ArgdownPluginError_1.checkResponseFields)(this, response, ["map"]);
const settings = this.getSettings(request);
const minGroupLevel = (response.maxSectionLevel || 0) - settings.groupDepth + 1;
if (response.sections) {
response.sections = response.sections.map(s => setIsGroupRecursive(s, minGroupLevel));
}
response.map.nodes = createMapNodeTree(response, response.map.nodes);
};
this.defaults = (0, lodash_defaultsdeep_1.default)({}, config, defaultSettings);
}
}
exports.GroupPlugin = GroupPlugin;
const createMapNodeTree = (response, nodes) => {
const groupMap = createGroups(response, nodes);
[...groupMap.values()].reduce(createAncestorGroups, groupMap);
const groups = [...groupMap.values()];
const topLevelGroups = groups.filter(g => !g.parent);
for (let group of topLevelGroups) {
setGroupLevelsRecursive(group);
}
const nodesWithoutSection = nodes.filter(n => !findSection(response, n));
return [...topLevelGroups, ...nodesWithoutSection];
};
const setIsGroupRecursive = (section, minGroupLevel) => {
if (section.isGroup === undefined) {
section.isGroup = section.level >= minGroupLevel;
}
if (section.children) {
for (let child of section.children) {
setIsGroupRecursive(child, minGroupLevel);
}
}
return section;
};
const createGroups = (response, nodes) => {
const groupMap = new Map();
for (let node of nodes) {
let section = findSection(response, node);
if (section) {
let group = groupMap.get(section.id);
if (!group) {
group = {
type: model_1.ArgdownTypes.GROUP_MAP_NODE,
id: section.id,
title: section.title,
labelTitle: section.title,
children: [],
level: section.level,
section: section,
isClosed: section.isClosed
};
groupMap.set(section.id, group);
let parentSection = section.parent;
while (parentSection && parentSection.isGroup === false) {
parentSection = parentSection.parent;
}
if (parentSection) {
group.parent = parentSection.id;
}
}
group.children.push(node);
}
}
return groupMap;
};
const findSection = (response, node) => {
let section = null;
if (node.type == model_1.ArgdownTypes.ARGUMENT_MAP_NODE) {
let argument = response.arguments[node.title];
section = argument.section;
}
else {
let equivalenceClass = response.statements[node.title];
section = equivalenceClass.section;
}
while (section && section.isGroup === false) {
section = section.parent;
}
return section;
};
const setGroupLevelsRecursive = (currentGroup, parentLevel = 0) => {
currentGroup.level = parentLevel + 1;
if (currentGroup.children) {
for (let child of currentGroup.children) {
if ((0, model_1.isGroupMapNode)(child)) {
setGroupLevelsRecursive(child, currentGroup.level);
}
}
}
return currentGroup;
};
const createAncestorGroups = (groupMap, group) => {
let currentGroup = group;
let parentSection = currentGroup.section.parent;
while (currentGroup.parent) {
let parentGroup = groupMap.get(currentGroup.parent);
if (parentGroup) {
parentGroup.children.push(currentGroup);
break;
}
if (parentSection) {
if (parentSection.isGroup || parentSection.isGroup === undefined) {
parentGroup = {
type: model_1.ArgdownTypes.GROUP_MAP_NODE,
id: parentSection.id,
title: parentSection.title,
labelTitle: parentSection.title,
children: [currentGroup],
level: parentSection.level,
section: parentSection
};
if (parentSection.parent) {
parentGroup.parent = parentSection.parent.id;
}
groupMap.set(currentGroup.parent, parentGroup);
currentGroup = parentGroup;
parentSection = currentGroup.section.parent;
}
else if (parentSection.parent) {
currentGroup.parent = parentSection.parent.id;
parentSection = parentSection.parent;
}
else {
currentGroup.parent = undefined;
break;
}
}
}
return groupMap;
};
//# sourceMappingURL=GroupPlugin.js.map
;