Skip to content

Commit

Permalink
Implement suggestions from PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
kwin committed Dec 16, 2023
1 parent 0bebc8c commit 610c5f9
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public class IndexEntry {
*/
private List<IndexEntry> childEntries = new ArrayList<>();

/** The type of the entry, one of the types defined by {@link IndexingSink} */
private int type;

/**
* System-dependent EOL.
*/
Expand All @@ -74,12 +77,23 @@ public IndexEntry(String newId) {
* @param newId The id. May be null.
*/
public IndexEntry(IndexEntry newParent, String newId) {
this(newParent, newId, 0);
}

/**
* Constructor.
*
* @param newParent The parent. May be null.
* @param newId The id. May be null.
*/
public IndexEntry(IndexEntry newParent, String newId, int type) {
this.parent = newParent;
this.id = newId;

if (parent != null) {
parent.childEntries.add(this);
}
this.type = type;
}

/**
Expand Down Expand Up @@ -110,6 +124,21 @@ protected void setId(String id) {
this.id = id;
}

/** Set if the entry's id already has an anchor in the underlying document.
*
* @param hasAnchor {@true} if the id already has an anchor.
* @since 2.0.0
*/
public void setAnchor(boolean hasAnchor) {
this.hasAnchor = hasAnchor;
}

/**
* Returns if the entry's id already has an anchor in the underlying document.
* @return {@code true} if the id already has an anchor otherwise {@code false}.
*
* @since 2.0.0
*/
public boolean hasAnchor() {
return hasAnchor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
import org.apache.maven.doxia.util.DoxiaUtils;

/**
* A sink wrapper for populating a section index.
* A sink wrapper for populating an index tree for particular elements in a document.
* Currently this only generates {@link IndexEntry} objects for sections.
*
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
* @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
Expand Down Expand Up @@ -82,6 +83,7 @@ public class IndexingSink extends SinkWrapper {

private final IndexEntry rootEntry;

private boolean isComplete;
/**
* @deprecated legacy constructor, use {@link #IndexingSink(Sink)} with {@link SinkAdapter} as argument and call {@link #getRootEntry()} to retrieve the index tree afterwards.
*/
Expand All @@ -104,15 +106,21 @@ private IndexingSink(IndexEntry rootEntry, Sink delegate) {
stack.push(rootEntry);
usedIds = new HashMap<>();
usedIds.put(rootEntry.getId(), new AtomicInteger());
init();
this.type = 0;
this.title = null;
}

/**
* This should only be called once the sink is closed.
* Before that the tree might not be complete.
* @return the tree of entries starting from the root
* @throws IllegalStateException in case the sink was not closed yet
*/
public IndexEntry getRootEntry() {
if (!isComplete) {
throw new IllegalStateException(
"The sink has not been closed yet, i.e. the index tree is not complete yet");
}
return rootEntry;
}
/**
Expand Down Expand Up @@ -228,6 +236,7 @@ private boolean parseAnchor(String name) {
case TYPE_SECTION_4:
case TYPE_SECTION_5:
IndexEntry entry = stack.lastElement();
entry.setAnchor(true);
setEntryId(entry, name);
break;
default:
Expand Down Expand Up @@ -321,13 +330,6 @@ public IndexEntry peek() {
@Override
public void close() {
super.close();

init();
}

@Override
protected void init() {
this.type = 0;
this.title = null;
isComplete = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,12 @@ public void execute(Sink sink, MacroRequest request) throws MacroExecutionExcept
}

IndexingSink tocSink = new IndexingSink(new SinkAdapter());

try {
parser.parse(new StringReader(source), tocSink);
} catch (ParseException e) {
throw new MacroExecutionException(e);
} finally {
tocSink.close();
}

IndexEntry index = tocSink.getRootEntry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ protected boolean isSecondParsing() {
}

@Override
public void registerSinkWrapperFactory(SinkWrapperFactory factory) {
public void addSinkWrapperFactory(SinkWrapperFactory factory) {
manuallyRegisteredSinkWrapperFactories.add(factory);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static BufferingSink castAsBufferingSink(Sink sink) {
}

@Override
public int getRank() {
public int getPriority() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public interface Parser {
* @param factory the factory to create the sink wrapper
* @since 2.0.0
*/
void registerSinkWrapperFactory(SinkWrapperFactory factory);
void addSinkWrapperFactory(SinkWrapperFactory factory);

/**
*
Expand All @@ -101,7 +101,7 @@ public interface Parser {
Collection<SinkWrapperFactory> getSinkWrapperFactories();

/**
* Determines whether to automatically generate anchors for each section found by {@link IndexingSink} or not.
* Determines whether to automatically generate anchors for each index entry found by {@link IndexingSink} or not.
* By default no anchors are generated.
*
* @param emitAnchors {@code true} to emit anchors otherwise {@code false} (the default)
Expand All @@ -110,7 +110,7 @@ public interface Parser {
void setEmitAnchors(boolean emitAnchors);

/**
* Returns whether anchors are automatically generated for each section found by {@link IndexingSink} or not.
* Returns whether anchors are automatically generated for each index entry found by {@link IndexingSink} or not.
* @return {@code true} if anchors are emitted otherwise {@code false}
* @since 2.0.0
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public interface SinkWrapperFactory {
Sink createWrapper(Sink sink);

/**
* Determines the order of sink wrappers. The highest ranked wrapper factory is called first.
* @return the rank of this factory
* Determines the order of sink wrappers. The wrapper factory with the highest priority is called first.
* @return the priority of this factory
*/
int getRank();
int getPriority();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Sink createWrapper(Sink sink) {
}

@Override
public int getRank() {
public int getPriority() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public class SinkWrapperFactoryComparator implements Comparator<SinkWrapperFacto
@Override
public int compare(SinkWrapperFactory o1, SinkWrapperFactory o2) {

return Integer.compare(o2.getRank(), o1.getRank());
return Integer.compare(o2.getPriority(), o1.getPriority());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Sink createWrapper(Sink sink) {
}

@Override
public int getRank() {
public int getPriority() {
// should come last (after potential preprocessing/modification of anchor names)
return -1000;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public void testGenerateAnchors() throws ParseException, MacroExecutionException
// check html output as well (without the actual TOC)
StringWriter out = new StringWriter();
Sink sink2 = new Xhtml5BaseSink(out);
parser.registerSinkWrapperFactory(new CreateAnchorsForIndexEntriesFactory());
parser.addSinkWrapperFactory(new CreateAnchorsForIndexEntriesFactory());
parser.parse(sourceContent, sink2);
assertEquals(
"<section><a id=\"" + actualLinkTarget.substring(1) + "\"></a>\n" + "<h1>1 Headline</h1>",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public final void testDocument() throws IOException, ParseException {
public final void testSinkWrapper() throws ParseException, IOException {
Parser parser = createParser();

parser.registerSinkWrapperFactory(new SinkWrapperFactory() {
parser.addSinkWrapperFactory(new SinkWrapperFactory() {

@Override
public Sink createWrapper(Sink sink) {
Expand All @@ -116,12 +116,12 @@ public void text(String text, SinkEventAttributes attributes) {
}

@Override
public int getRank() {
public int getPriority() {
return 0;
}
});

parser.registerSinkWrapperFactory(new SinkWrapperFactory() {
parser.addSinkWrapperFactory(new SinkWrapperFactory() {

@Override
public Sink createWrapper(Sink sink) {
Expand All @@ -134,7 +134,7 @@ public void text(String text, SinkEventAttributes attributes) {
}

@Override
public int getRank() {
public int getPriority() {
return 1;
}
});
Expand Down

0 comments on commit 610c5f9

Please sign in to comment.