Add support for code appendices: enable code blocks with language labels, line-number gutter, and directive-based integration.

This commit is contained in:
Sebastian Unterschütz
2026-05-14 21:00:58 +02:00
parent 3afdc3bf1e
commit 2d3e544d4f
7 changed files with 76 additions and 21 deletions
+21 -9
View File
@@ -44,15 +44,17 @@ func ParseMarkdown(mdPath string) (Config, ast.Node, []byte, error) {
// parserState tracks transient state during the AST walk.
type parserState struct {
nextCodeIsAppendix bool
nextAppendixLandscape bool // set by @AnhangUMLQuer: — landscape for diagram appendix
appendixTitle string
nextTableCaption string // set by @Tabelle: directive
nextTableIsAppendix bool // set by @TabelleAnhang: or @TabelleAnhangQuer:
nextTableIsLandscape bool // set by @TabelleAnhangQuer:
nextDiagramLandscape bool // set by @DiagrammQuer: directive
nextDiagramCaption string // caption for the landscape diagram page
listStack []listFrame // stack for nested list tracking
nextCodeIsAppendix bool
nextAppendixLandscape bool // set by @AnhangUMLQuer: — landscape for diagram appendix
appendixTitle string
nextCodeBlockAppendix bool // set by @AnhangCode: — next non-diagram code block → appendix
codeBlockAppendixTitle string
nextTableCaption string // set by @Tabelle: directive
nextTableIsAppendix bool // set by @TabelleAnhang: or @TabelleAnhangQuer:
nextTableIsLandscape bool // set by @TabelleAnhangQuer:
nextDiagramLandscape bool // set by @DiagrammQuer: directive
nextDiagramCaption string // caption for the landscape diagram page
listStack []listFrame // stack for nested list tracking
}
// listFrame tracks the type and item counter for one list nesting level.
@@ -142,6 +144,12 @@ func RenderAST(doc ast.Node, content []byte, r *IHKRenderer) error {
}
// Fall through: render as plain code block on error
}
if state.nextCodeBlockAppendix {
r.AddCodeAppendix(state.codeBlockAppendixTitle, lang, code)
state.nextCodeBlockAppendix = false
state.codeBlockAppendixTitle = ""
return ast.WalkSkipChildren, nil
}
// Render as a numbered code block (gutter + monospace body).
r.RenderCodeBlock(lang, code)
return ast.WalkSkipChildren, nil
@@ -264,6 +272,10 @@ func handleDirectives(text string, state *parserState, r *IHKRenderer) bool {
case strings.HasPrefix(line, "@Quelle:"):
r.AddSource(strings.TrimSpace(strings.TrimPrefix(line, "@Quelle:")))
handled = true
case strings.HasPrefix(line, "@AnhangCode:"):
state.nextCodeBlockAppendix = true
state.codeBlockAppendixTitle = strings.TrimSpace(strings.TrimPrefix(line, "@AnhangCode:"))
handled = true
case strings.HasPrefix(line, "@Anhang:"):
r.AddAppendix(strings.TrimSpace(strings.TrimPrefix(line, "@Anhang:")))
handled = true