|
| 1 | +/* |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Neo4j is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | +package org.neo4j.gds.doc.syntax; |
| 21 | + |
| 22 | +import org.asciidoctor.ast.Document; |
| 23 | +import org.asciidoctor.extension.IncludeProcessor; |
| 24 | +import org.asciidoctor.extension.PreprocessorReader; |
| 25 | + |
| 26 | +import java.io.BufferedReader; |
| 27 | +import java.io.File; |
| 28 | +import java.io.FileReader; |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.UncheckedIOException; |
| 31 | +import java.nio.charset.StandardCharsets; |
| 32 | +import java.nio.file.Paths; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.stream.Collectors; |
| 35 | + |
| 36 | +final class PartialsIncludeProcessor extends IncludeProcessor { |
| 37 | + |
| 38 | + @Override |
| 39 | + public boolean handles(String target) { |
| 40 | + return target.contains("partial$"); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void process(Document document, PreprocessorReader reader, String target, Map<String, Object> attributes) { |
| 45 | + var base_dir = document.getOptions().get("base_dir").toString(); |
| 46 | + var relativePathToPartialFile = target.replace("partial$", "partials"); |
| 47 | + var partialFile = Paths.get(base_dir, relativePathToPartialFile).toFile(); |
| 48 | + try(var partialFileReader = new BufferedReader(new FileReader(partialFile, StandardCharsets.UTF_8))) { |
| 49 | + var partialFileContent = partialFileReader |
| 50 | + .lines() |
| 51 | + .collect(Collectors.joining(System.lineSeparator())); |
| 52 | + |
| 53 | + reader.push_include( |
| 54 | + partialFileContent, |
| 55 | + target, |
| 56 | + new File(".").getAbsolutePath(), |
| 57 | + 1, |
| 58 | + attributes |
| 59 | + ); |
| 60 | + |
| 61 | + } catch (IOException e) { |
| 62 | + throw new UncheckedIOException(e); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments