All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.greenpepper.shaded.com.vladsch.flexmark.ext.gfm.tasklist.internal.TaskListNodeFormatter Maven / Gradle / Ivy
package com.greenpepper.shaded.com.vladsch.flexmark.ext.gfm.tasklist.internal;
import com.greenpepper.shaded.com.vladsch.flexmark.ast.*;
import com.greenpepper.shaded.com.vladsch.flexmark.ext.gfm.tasklist.TaskListItem;
import com.greenpepper.shaded.com.vladsch.flexmark.ext.gfm.tasklist.TaskListItemPlacement;
import com.greenpepper.shaded.com.vladsch.flexmark.formatter.CustomNodeFormatter;
import com.greenpepper.shaded.com.vladsch.flexmark.formatter.internal.*;
import com.greenpepper.shaded.com.vladsch.flexmark.parser.ListOptions;
import com.greenpepper.shaded.com.vladsch.flexmark.util.options.DataHolder;
import com.greenpepper.shaded.com.vladsch.flexmark.util.sequence.BasedSequence;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
@SuppressWarnings("WeakerAccess")
public class TaskListNodeFormatter implements NodeFormatter {
private final FormatOptions myOptions;
private final ListOptions listOptions;
public TaskListNodeFormatter(DataHolder options) {
myOptions = new FormatOptions(options);
listOptions = ListOptions.getFrom(options);
}
@Override
public Set> getNodeFormattingHandlers() {
//noinspection unchecked
return new HashSet>(Arrays.asList(
new NodeFormattingHandler(TaskListItem.class, new CustomNodeFormatter() {
@Override
public void render(TaskListItem node, NodeFormatterContext context, MarkdownWriter markdown) {
TaskListNodeFormatter.this.render(node, context, markdown);
}
}),
new NodeFormattingHandler(BulletList.class, new CustomNodeFormatter() {
@Override
public void render(BulletList node, NodeFormatterContext context, MarkdownWriter markdown) {
TaskListNodeFormatter.this.render(node, context, markdown);
}
}),
new NodeFormattingHandler(OrderedList.class, new CustomNodeFormatter() {
@Override
public void render(OrderedList node, NodeFormatterContext context, MarkdownWriter markdown) {
TaskListNodeFormatter.this.render(node, context, markdown);
}
})
));
}
@Override
public Set> getNodeClasses() {
return null;
}
private void render(final TaskListItem node, final NodeFormatterContext context, final MarkdownWriter markdown) {
BasedSequence markerSuffix = node.getMarkerSuffix();
switch (myOptions.taskListItemCase) {
case AS_IS:
break;
case LOWERCASE:
markerSuffix = markerSuffix.toLowerCase();
break;
case UPPERCASE:
markerSuffix = markerSuffix.toUpperCase();
break;
default:
throw new IllegalStateException("Missing case for TaskListItemCase " + myOptions.taskListItemCase.name());
}
if (node.isItemDoneMarker()) {
switch (myOptions.taskListItemPlacement) {
case AS_IS:
case INCOMPLETE_FIRST:
case INCOMPLETE_NESTED_FIRST:
break;
case COMPLETE_TO_NON_TASK:
case COMPLETE_NESTED_TO_NON_TASK:
markerSuffix = BasedSequence.NULL;
break;
default:
throw new IllegalStateException("Missing case for ListItemPlacement " + myOptions.taskListItemPlacement.name());
}
}
CoreNodeFormatter.renderListItem(node, context, markdown, listOptions, markerSuffix.isEmpty() ? markerSuffix : markerSuffix.append(" "));
}
private void render(final BulletList node, final NodeFormatterContext context, MarkdownWriter markdown) {
renderList(node, context, markdown, myOptions);
}
private void render(final OrderedList node, final NodeFormatterContext context, MarkdownWriter markdown) {
renderList(node, context, markdown, myOptions);
}
public static boolean hasIncompleteDescendants(Node node) {
Node item = node.getFirstChild();
while (item != null) {
if (item instanceof TaskListItem) {
if (!((TaskListItem) item).isItemDoneMarker()) {
return true;
}
}
if (item instanceof Block && !(item instanceof Paragraph) && hasIncompleteDescendants(item)) {
return true;
}
item = item.getNext();
}
return false;
}
public static void renderList(
final ListBlock node,
final NodeFormatterContext context,
MarkdownWriter markdown,
FormatOptions formatOptions
) {
ArrayList itemList = new ArrayList();
TaskListItemPlacement taskListItemPlacement = formatOptions.taskListItemPlacement;
if (taskListItemPlacement != TaskListItemPlacement.AS_IS) {
ArrayList incompleteTasks = new ArrayList();
ArrayList completeItems = new ArrayList();
boolean incompleteDescendants = taskListItemPlacement == TaskListItemPlacement.INCOMPLETE_NESTED_FIRST || taskListItemPlacement == TaskListItemPlacement.COMPLETE_NESTED_TO_NON_TASK;
Node item = node.getFirstChild();
while (item != null) {
if (item instanceof TaskListItem) {
TaskListItem taskItem = (TaskListItem) item;
if (!taskItem.isItemDoneMarker() || (incompleteDescendants && hasIncompleteDescendants(item))) {
incompleteTasks.add(item);
} else {
completeItems.add(item);
}
} else {
if (incompleteDescendants && hasIncompleteDescendants(item)) {
incompleteTasks.add(item);
} else {
completeItems.add(item);
}
}
item = item.getNext();
}
itemList.addAll(incompleteTasks);
itemList.addAll(completeItems);
} else {
Node item = node.getFirstChild();
while (item != null) {
itemList.add(item);
item = item.getNext();
}
}
CoreNodeFormatter.renderList(node, context, markdown, itemList);
}
public static class Factory implements NodeFormatterFactory {
@Override
public NodeFormatter create(final DataHolder options) {
return new TaskListNodeFormatter(options);
}
}
}