kwin commented on code in PR #180:
URL: https://github.com/apache/maven-doxia/pull/180#discussion_r1408134053


##########
doxia-core/src/main/java/org/apache/maven/doxia/sink/impl/NamesForIndexEntriesSinkWrapper.java:
##########
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.doxia.sink.impl;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.maven.doxia.index.IndexEntry;
+import org.apache.maven.doxia.index.SectionLocation;
+import org.apache.maven.doxia.macro.toc.TocMacro;
+import org.apache.maven.doxia.parser.SinkWrapper;
+import org.apache.maven.doxia.sink.Sink;
+import org.apache.maven.doxia.sink.SinkEventAttributes;
+
+/**
+ * Sink wrapper which emits anchors for each section represented by the given 
{@link IndexEntry}.
+ * @see TocMacro
+ */
+public class NamesForIndexEntriesSinkWrapper extends SinkWrapper {
+
+    private final Map<SectionLocation, IndexEntry> entryByLocation;
+    private SectionLocation currentLocation;
+
+    public NamesForIndexEntriesSinkWrapper(Sink delegate, IndexEntry index) {
+        super(delegate);
+        entryByLocation = new HashMap<>();
+        populateMap(entryByLocation, index);
+        this.currentLocation = new SectionLocation();
+    }
+
+    private static void populateMap(Map<SectionLocation, IndexEntry> 
entryByLocation, IndexEntry entry) {
+        entryByLocation.put(entry.getLocation(), entry);
+        entry.getChildEntries().forEach(e -> populateMap(entryByLocation, e));
+    }
+
+    @Override
+    public void section(int level, SinkEventAttributes attributes) {
+        onStartSection(level);
+        super.section(level, attributes);
+    }
+
+    @Override
+    public void section_(int level) {
+        super.section_(level);
+        onEndSection();
+    }
+
+    @Override
+    public void section1() {
+        super.section1();
+        onStartSection(1);
+    }
+
+    @Override
+    public void section1_() {
+        super.section1_();
+        onEndSection();
+    }
+
+    @Override
+    public void section2() {
+        super.section2();
+        onStartSection(2);
+    }
+
+    @Override
+    public void section2_() {
+        super.section2_();
+        onEndSection();
+    }
+
+    @Override
+    public void section3() {
+        super.section3();
+        onStartSection(3);
+    }
+
+    @Override
+    public void section3_() {
+        super.section3_();
+        onEndSection();
+    }
+
+    @Override
+    public void section4() {
+        super.section4();
+        onStartSection(4);
+    }
+
+    @Override
+    public void section4_() {
+        super.section4_();
+        onEndSection();
+    }
+
+    @Override
+    public void section5() {
+        super.section5();
+        onStartSection(5);
+    }
+
+    @Override
+    public void section5_() {
+        super.section5_();
+        onEndSection();
+    }
+
+    private void onStartSection(int level) {
+        currentLocation = currentLocation.createChildSection(level);
+        IndexEntry entry = entryByLocation.get(currentLocation);
+        if (entry != null) {

Review Comment:
   Probably a guard similar to 
https://github.com/apache/maven-doxia-sitetools/pull/58/files#diff-dabe16ceeba5dc7cb0a15e30dcb7c204a78f4763346f016ab7639ab2f59e78d4L250
 needs to be introduced to at least prevent double anchors for APT format.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@maven.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to