|
5 | 5 |
|
6 | 6 | package software.amazon.smithy.docgen.core.writers; |
7 | 7 |
|
| 8 | +import software.amazon.smithy.codegen.core.CodegenException; |
8 | 9 | import software.amazon.smithy.codegen.core.SymbolWriter; |
9 | 10 | import software.amazon.smithy.model.shapes.Shape; |
10 | 11 | import software.amazon.smithy.utils.SmithyUnstableApi; |
|
16 | 17 | */ |
17 | 18 | @SmithyUnstableApi |
18 | 19 | public abstract class DocWriter extends SymbolWriter<DocWriter, DocImportContainer> { |
| 20 | + private static final int MAX_HEADING_DEPTH = 6; |
| 21 | + |
| 22 | + private int headingDepth = 0; |
| 23 | + |
19 | 24 | public DocWriter(DocImportContainer importContainer) { |
20 | 25 | super(importContainer); |
21 | 26 | } |
@@ -47,12 +52,44 @@ public DocWriter(DocImportContainer importContainer) { |
47 | 52 | * @param content A string to use as the heading content. |
48 | 53 | * @return returns the writer. |
49 | 54 | */ |
50 | | - public abstract DocWriter openHeading(String content); |
| 55 | + public DocWriter openHeading(String content) { |
| 56 | + headingDepth++; |
| 57 | + if (headingDepth > MAX_HEADING_DEPTH) { |
| 58 | + throw new CodegenException(String.format( |
| 59 | + "Tried opening a heading nested more deeply than the max depth of %d.", |
| 60 | + MAX_HEADING_DEPTH |
| 61 | + )); |
| 62 | + } |
| 63 | + return openHeading(content, headingDepth); |
| 64 | + } |
51 | 65 |
|
52 | 66 | /** |
53 | | - * Closes the current heading, cleaning any context created for the current level. |
| 67 | + * Writes a heading of a given level with the given content. |
54 | 68 | * |
| 69 | + * <p>{@link #closeHeading} will be called to enable cleaning up any resources or |
| 70 | + * context this method creates. |
| 71 | + * |
| 72 | + * @param content A string to use as the heading content. |
| 73 | + * @param level The level of the heading to open. This corresponds to HTML heading |
| 74 | + * levels, and will only have values between 1 and 6. |
55 | 75 | * @return returns the writer. |
56 | 76 | */ |
57 | | - public abstract DocWriter closeHeading(); |
| 77 | + abstract DocWriter openHeading(String content, int level); |
| 78 | + |
| 79 | + /** |
| 80 | + * Closes the current heading, cleaning any context created for the current level, |
| 81 | + * then writes a blank line. |
| 82 | + * |
| 83 | + * @return returns the writer. |
| 84 | + */ |
| 85 | + public DocWriter closeHeading() { |
| 86 | + headingDepth--; |
| 87 | + if (headingDepth < 0) { |
| 88 | + throw new CodegenException( |
| 89 | + "Attempted to close a heading when at the base heading level." |
| 90 | + ); |
| 91 | + } |
| 92 | + write(""); |
| 93 | + return this; |
| 94 | + } |
58 | 95 | } |
0 commit comments