|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to you under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.logging.log4j.docgen.processor; |
| 18 | + |
| 19 | +import static org.apache.commons.lang3.StringUtils.substringBefore; |
| 20 | + |
| 21 | +import com.sun.source.doctree.DocTree; |
| 22 | +import com.sun.source.doctree.EndElementTree; |
| 23 | +import com.sun.source.doctree.LinkTree; |
| 24 | +import com.sun.source.doctree.LiteralTree; |
| 25 | +import com.sun.source.doctree.StartElementTree; |
| 26 | +import com.sun.source.doctree.TextTree; |
| 27 | +import com.sun.source.util.SimpleDocTreeVisitor; |
| 28 | +import java.util.ArrayList; |
| 29 | +import org.apache.commons.lang3.StringUtils; |
| 30 | +import org.apache.logging.log4j.docgen.processor.internal.BlockImpl; |
| 31 | +import org.apache.logging.log4j.docgen.processor.internal.CellImpl; |
| 32 | +import org.apache.logging.log4j.docgen.processor.internal.ListImpl; |
| 33 | +import org.apache.logging.log4j.docgen.processor.internal.ListItemImpl; |
| 34 | +import org.apache.logging.log4j.docgen.processor.internal.RowImpl; |
| 35 | +import org.apache.logging.log4j.docgen.processor.internal.TableImpl; |
| 36 | +import org.asciidoctor.ast.Block; |
| 37 | +import org.asciidoctor.ast.Cell; |
| 38 | +import org.asciidoctor.ast.Document; |
| 39 | +import org.asciidoctor.ast.List; |
| 40 | +import org.asciidoctor.ast.ListItem; |
| 41 | +import org.asciidoctor.ast.Row; |
| 42 | +import org.asciidoctor.ast.Section; |
| 43 | +import org.asciidoctor.ast.StructuralNode; |
| 44 | +import org.asciidoctor.ast.Table; |
| 45 | + |
| 46 | +class AbstractAsciidocTreeVisitor extends SimpleDocTreeVisitor<Void, AsciidocData> { |
| 47 | + |
| 48 | + private static void appendSentences(final String text, final AsciidocData data) { |
| 49 | + final String body = StringUtils.normalizeSpace(text); |
| 50 | + final String[] sentences = body.split("(?<=\\w{2}[.!?])", -1); |
| 51 | + // Full sentences |
| 52 | + for (int i = 0; i < sentences.length - 1; i++) { |
| 53 | + data.appendWords(sentences[i].strip()); |
| 54 | + data.newLine(); |
| 55 | + } |
| 56 | + // Partial sentence |
| 57 | + data.appendWords(sentences[sentences.length - 1].strip()); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public Void visitStartElement(final StartElementTree node, final AsciidocData data) { |
| 62 | + final String elementName = node.getName().toString(); |
| 63 | + switch (elementName) { |
| 64 | + case "p": |
| 65 | + data.newParagraph(); |
| 66 | + break; |
| 67 | + case "ol": |
| 68 | + // Nested list without a first paragraph |
| 69 | + if (data.getCurrentNode() instanceof ListItem) { |
| 70 | + data.newParagraph(); |
| 71 | + } |
| 72 | + data.pushChildNode(ListImpl::new).setContext(ListImpl.ORDERED_LIST_CONTEXT); |
| 73 | + break; |
| 74 | + case "ul": |
| 75 | + // Nested list without a first paragraph |
| 76 | + if (data.getCurrentNode() instanceof ListItem) { |
| 77 | + data.newParagraph(); |
| 78 | + } |
| 79 | + data.pushChildNode(ListImpl::new).setContext(ListImpl.UNORDERED_LIST_CONTEXT); |
| 80 | + break; |
| 81 | + case "li": |
| 82 | + if (!(data.getCurrentNode() instanceof List)) { |
| 83 | + throw new IllegalArgumentException("A <li> tag must be a child of a <ol> or <ul> tag."); |
| 84 | + } |
| 85 | + data.pushChildNode(ListItemImpl::new); |
| 86 | + break; |
| 87 | + case "h1": |
| 88 | + case "h2": |
| 89 | + case "h3": |
| 90 | + case "h4": |
| 91 | + case "h5": |
| 92 | + case "h6": |
| 93 | + // Flush the current paragraph |
| 94 | + data.newParagraph(); |
| 95 | + StructuralNode currentNode; |
| 96 | + // Remove other types of nodes from stack |
| 97 | + while ((currentNode = data.getCurrentNode()) != null |
| 98 | + && !(currentNode instanceof Section || currentNode instanceof Document)) { |
| 99 | + data.popNode(); |
| 100 | + } |
| 101 | + break; |
| 102 | + case "table": |
| 103 | + data.pushChildNode(TableImpl::new); |
| 104 | + break; |
| 105 | + case "tr": |
| 106 | + break; |
| 107 | + case "th": |
| 108 | + data.pushChildNode(CellImpl::new).setContext(CellImpl.HEADER_CONTEXT); |
| 109 | + break; |
| 110 | + case "td": |
| 111 | + data.pushChildNode(CellImpl::new); |
| 112 | + break; |
| 113 | + case "pre": |
| 114 | + data.newParagraph(); |
| 115 | + final Block currentParagraph = data.getCurrentParagraph(); |
| 116 | + currentParagraph.setContext(BlockImpl.LISTING_CONTEXT); |
| 117 | + currentParagraph.setStyle(BlockImpl.SOURCE_STYLE); |
| 118 | + break; |
| 119 | + default: |
| 120 | + } |
| 121 | + return super.visitStartElement(node, data); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public Void visitEndElement(final EndElementTree node, final AsciidocData data) { |
| 126 | + final String elementName = node.getName().toString(); |
| 127 | + switch (elementName) { |
| 128 | + case "p": |
| 129 | + // Ignore closing tags. |
| 130 | + break; |
| 131 | + case "ol": |
| 132 | + case "ul": |
| 133 | + case "li": |
| 134 | + case "table": |
| 135 | + case "th": |
| 136 | + case "td": |
| 137 | + data.popNode(); |
| 138 | + break; |
| 139 | + case "h1": |
| 140 | + case "h2": |
| 141 | + case "h3": |
| 142 | + case "h4": |
| 143 | + case "h5": |
| 144 | + case "h6": |
| 145 | + // Only flush the current line |
| 146 | + if (!data.getCurrentLine().isEmpty()) { |
| 147 | + data.newLine(); |
| 148 | + } |
| 149 | + // The current paragraph contains the title |
| 150 | + // We retrieve the text and empty the paragraph |
| 151 | + final Block currentParagraph = data.getCurrentParagraph(); |
| 152 | + final String title = StringUtils.normalizeSpace(currentParagraph.convert()); |
| 153 | + currentParagraph.setLines(new ArrayList<>()); |
| 154 | + |
| 155 | + // There should be no <h1> tags |
| 156 | + final int newLevel = "h1".equals(elementName) ? 2 : elementName.charAt(1) - '0'; |
| 157 | + data.setCurrentSectionLevel(newLevel); |
| 158 | + data.getCurrentNode().setTitle(title); |
| 159 | + break; |
| 160 | + case "pre": |
| 161 | + data.newParagraph(); |
| 162 | + break; |
| 163 | + case "tr": |
| 164 | + // We group the new cells into a row |
| 165 | + final Table table = (Table) data.getCurrentNode(); |
| 166 | + final java.util.List<StructuralNode> cells = table.getBlocks(); |
| 167 | + // First index of the row |
| 168 | + int idx = 0; |
| 169 | + for (final Row row : table.getHeader()) { |
| 170 | + idx += row.getCells().size(); |
| 171 | + } |
| 172 | + for (final Row row : table.getBody()) { |
| 173 | + idx += row.getCells().size(); |
| 174 | + } |
| 175 | + final Row row = new RowImpl(); |
| 176 | + String context = CellImpl.BODY_CONTEXT; |
| 177 | + for (int i = idx; i < table.getBlocks().size(); i++) { |
| 178 | + final StructuralNode cell = cells.get(i); |
| 179 | + context = cell.getContext(); |
| 180 | + if (cell instanceof Cell) { |
| 181 | + row.getCells().add((Cell) cell); |
| 182 | + } |
| 183 | + } |
| 184 | + if (CellImpl.HEADER_CONTEXT.equals(context)) { |
| 185 | + table.getHeader().add(row); |
| 186 | + } else { |
| 187 | + table.getBody().add(row); |
| 188 | + } |
| 189 | + break; |
| 190 | + default: |
| 191 | + } |
| 192 | + return super.visitEndElement(node, data); |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public Void visitLink(final LinkTree node, final AsciidocData data) { |
| 197 | + final String className = substringBefore(node.getReference().getSignature(), '#'); |
| 198 | + final String simpleName = StringUtils.substringAfterLast(className, '.'); |
| 199 | + if (!data.getCurrentLine().isEmpty()) { |
| 200 | + data.append(" "); |
| 201 | + } |
| 202 | + data.append("xref:") |
| 203 | + .append(className) |
| 204 | + .append(".adoc[") |
| 205 | + .append(simpleName) |
| 206 | + .append("]"); |
| 207 | + return super.visitLink(node, data); |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + public Void visitLiteral(final LiteralTree node, final AsciidocData data) { |
| 212 | + if (node.getKind() == DocTree.Kind.CODE) { |
| 213 | + if (!data.getCurrentLine().isEmpty()) { |
| 214 | + data.append(" "); |
| 215 | + } |
| 216 | + data.append("`").append(node.getBody().getBody()).append("`"); |
| 217 | + } else { |
| 218 | + node.getBody().accept(this, data); |
| 219 | + } |
| 220 | + return super.visitLiteral(node, data); |
| 221 | + } |
| 222 | + |
| 223 | + @Override |
| 224 | + public Void visitText(final TextTree node, final AsciidocData data) { |
| 225 | + final Block currentParagraph = data.getCurrentParagraph(); |
| 226 | + if (BlockImpl.PARAGRAPH_CONTEXT.equals(currentParagraph.getContext())) { |
| 227 | + appendSentences(node.getBody(), data); |
| 228 | + } else { |
| 229 | + data.append(node.getBody()); |
| 230 | + } |
| 231 | + return super.visitText(node, data); |
| 232 | + } |
| 233 | +} |
0 commit comments