Search in sources :

Example 1 with PostProcessorFactory

use of com.vladsch.flexmark.parser.PostProcessorFactory in project flexmark-java by vsch.

the class PostProcessorManager method calculatePostProcessors.

public static PostProcessorDependencies calculatePostProcessors(DataHolder options, List<PostProcessorFactory> postProcessorFactories) {
    List<PostProcessorFactory> list = new ArrayList<PostProcessorFactory>();
    // By having the custom factories come first, extensions are able to change behavior of core syntax.
    list.addAll(postProcessorFactories);
    // list.addAll(CORE_POST_PROCESSORS.keySet().stream().filter(options::get).map(key -> CORE_POST_PROCESSORS.get(key)).collect(Collectors.toList()));
    for (DataKey<Boolean> processorDataKey : CORE_POST_PROCESSORS.keySet()) {
        if (processorDataKey.getFrom(options)) {
            PostProcessorFactory preProcessorFactory = CORE_POST_PROCESSORS.get(processorDataKey);
            list.add(preProcessorFactory);
        }
    }
    PostProcessDependencyHandler resolver = new PostProcessDependencyHandler();
    return resolver.resolveDependencies(list);
}
Also used : PostProcessorFactory(com.vladsch.flexmark.parser.PostProcessorFactory)

Example 2 with PostProcessorFactory

use of com.vladsch.flexmark.parser.PostProcessorFactory 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

PostProcessorFactory (com.vladsch.flexmark.parser.PostProcessorFactory)2 Node (com.vladsch.flexmark.ast.Node)1 PostProcessor (com.vladsch.flexmark.parser.PostProcessor)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