use of com.vladsch.flexmark.parser.PostProcessor in project flexmark-java by vsch.
the class PostProcessorManager method postProcess.
public Document postProcess(Document document) {
// first initialize node tracker if
ClassifyingNodeTracker classifyingNodeTracker;
classifyingNodeTracker = null;
for (PostProcessorDependencyStage stage : postProcessorDependencies.getDependentStages()) {
// idiosyncrasy of post processors the last dependency can be global, in which case it processes the whole document and no ancestry info is
// provided
// new ClassifyingNodeTracker()
boolean hadGlobal = false;
for (PostProcessorFactory dependent : stage.dependents) {
if (dependent.affectsGlobalScope()) {
document = dependent.create(document).processDocument(document);
hadGlobal = true;
// assume it no longer reflects reality;
classifyingNodeTracker = null;
} else {
if (hadGlobal) {
int tmp = 0;
}
assert !hadGlobal;
if (classifyingNodeTracker == null) {
// build the node type information by traversing the document tree
classifyingNodeTracker = new NodeClassifierVisitor(stage.myNodeMap).classify(document);
}
Map<Class<?>, Set<Class<?>>> dependentNodeTypes = dependent.getNodeTypes();
PostProcessor postProcessor = dependent.create(document);
BitSet exclusionSet = new BitSet();
for (Set<Class<?>> excluded : dependentNodeTypes.values()) {
BitSet mapped = classifyingNodeTracker.getExclusionSet().indexBitSet(excluded);
exclusionSet.or(mapped);
}
ReversibleIterable<Node> nodes = classifyingNodeTracker.getCategoryItems(Node.class, dependentNodeTypes.keySet());
for (Node node : nodes) {
// was already removed
if (node.getParent() == null)
continue;
// now we need to get the bitset for the excluded ancestors of the node, then intersect it with the actual ancestors of this factory
int index;
BitSet nodeAncestors;
BitSet nodeExclusions;
Set<Class<?>> excluded = dependentNodeTypes.get(node.getClass());
if (excluded != null) {
index = classifyingNodeTracker.getItems().indexOf(node);
if (index != -1) {
nodeAncestors = classifyingNodeTracker.getNodeAncestryMap().get(index);
if (nodeAncestors != null) {
nodeExclusions = classifyingNodeTracker.getExclusionSet().indexBitSet(excluded);
nodeExclusions.and(nodeAncestors);
if (!nodeExclusions.isEmpty()) {
// has excluded ancestor
continue;
}
}
}
}
postProcessor.process(classifyingNodeTracker, node);
}
}
}
}
return document;
}
Aggregations