/*
 * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
 * 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 &quot;AS IS&quot; 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.asciidoctor.maven.site;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

import org.apache.maven.doxia.module.xhtml.XhtmlParser;
import org.apache.maven.doxia.parser.ParseException;
import org.apache.maven.doxia.parser.Parser;
import org.apache.maven.doxia.sink.Sink;
import org.asciidoctor.Asciidoctor;
import org.asciidoctor.OptionsBuilder;
import org.asciidoctor.AttributesBuilder;
import org.asciidoctor.SafeMode;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.util.IOUtil;

/**
 * This class is used by the Doxia framework to handle the actual parsing of the
 * AsciiDoc input files into HTML to be consumed/wrapped by the site generation
 * process.
 *
 * @author jdlee
 */
@Component(role = Parser.class, hint = AsciidoctorParser.ROLE_HINT)
public class AsciidoctorParser extends XhtmlParser {

    /**
     * The role hint for the {@link AsciiDoctorParser} Plexus component.
     */
    public static final String ROLE_HINT = "asciidoc";
    protected final Asciidoctor asciidoctorInstance = Asciidoctor.Factory.create();

    /**
     * {@inheritDoc}
     */
    @Override
    public void parse(Reader source, Sink sink) throws ParseException {
        try {
            // NOTE we have to generate a full document, but we unset stylesheet to keep framing minimal
            String result = asciidoctorInstance.render(IOUtil.toString(source),
                        OptionsBuilder.options().headerFooter(true).safe(SafeMode.UNSAFE).backend("xhtml")
                        .attributes(AttributesBuilder.attributes().unsetStyleSheet().attribute("idprefix", "a_").asMap()).asMap());
            // prevent site plugin from breaking font-based icon syntax
            result = result.replaceAll("<i class=\"fa icon-([^\"]+)\"([^>]*)></i>", "<span class=\"fa icon-$1\"$2></span>");
            super.parse(new StringReader(result), sink);
        } catch (IOException ex) {
            getLog().error(ex.getLocalizedMessage());
        }
    }
}

@@ -606,89 +606,8 @@
 e;%0A%0A
-import java.io.IOException;%0Aimport java.io.Reader;%0Aimport java.io.StringReader;%0A%0A
 impo
@@ -650,32 +650,32 @@
 ml.XhtmlParser;%0A
+
 import org.apach
@@ -837,47 +837,8 @@
 or;%0A
-import org.asciidoctor.OptionsBuilder;%0A
 impo
@@ -871,24 +871,63 @@
 tesBuilder;%0A
+import org.asciidoctor.OptionsBuilder;%0A
 import org.a
@@ -1048,16 +1048,68 @@
 OUtil;%0A%0A
+import java.io.IOException;%0Aimport java.io.Reader;%0A%0A
 /**%0A * T
@@ -1567,16 +1567,17 @@
 iidoc%22;%0A
+%0A
     prot
@@ -1975,16 +1975,47 @@
 ource),%0A
+                               
         
@@ -2072,11 +2072,12 @@
 ter(
-tru
+fals
 e).s
@@ -2113,17 +2113,33 @@
 %22xhtml%22)
-%0A
+.attributes(%0A    
         
@@ -2154,28 +2154,59 @@
         
-.attributes(
+                                           
 Attribut
@@ -2271,285 +2271,195 @@
 ix%22,
- %22a_%22).asMap()).asMap());%0A            // prevent site plugin from breaking font-based icon syntax%0A            result = result.replaceAll(%22%3Ci class=%5C%22fa icon-(%5B%5E%5C%22%5D+)%5C%22(%5B%5E%3E%5D*)%3E%3C/i%3E%22, %22%3Cspan class=%5C%22fa icon-$1%5C%22$2%3E%3C/span%3E%22);%0A            super.parse(new StringReader(result), sink
+%0A                                                                                                                                  %22a_%22).asMap()).asMap());%0A            sink.rawText(result
 );%0A 

Remove the super call to the xhtml parser, because that one will call doxia for every node which will cut out most of the attributes of different tags and will result in corrupt html. Instead we call sink.rawText() which will pass the already generated html