Search in sources :

Example 1 with PostProcessor

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;
}
Also used : ClassifyingNodeTracker(com.vladsch.flexmark.util.collection.ClassifyingNodeTracker) PostProcessorFactory(com.vladsch.flexmark.parser.PostProcessorFactory) OrderedSet(com.vladsch.flexmark.util.collection.OrderedSet) NodeClassifierVisitor(com.vladsch.flexmark.util.collection.NodeClassifierVisitor) Node(com.vladsch.flexmark.ast.Node) PostProcessor(com.vladsch.flexmark.parser.PostProcessor)

Aggregations

Node (com.vladsch.flexmark.ast.Node)1 PostProcessor (com.vladsch.flexmark.parser.PostProcessor)1 PostProcessorFactory (com.vladsch.flexmark.parser.PostProcessorFactory)1 ClassifyingNodeTracker (com.vladsch.flexmark.util.collection.ClassifyingNodeTracker)1 NodeClassifierVisitor (com.vladsch.flexmark.util.collection.NodeClassifierVisitor)1 OrderedSet (com.vladsch.flexmark.util.collection.OrderedSet)1