diff --git a/CHANGES.md b/CHANGES.md index 7ca7a5b2..b9537335 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,6 +17,8 @@ This file describes changes in the AutoDoc package. AutoDoc marker at the start of a line (ignoring leading whitespace) - Warn if a chunk is defined (via `@BeginChunk`/`@BeginCode`) but never inserted (via `@InsertChunk`/`@InsertCode`) + - Allow using Markdown-style headings `#`/`##`/`###` as aliases for + `@Chapter`/`@Section`/`@Subsection` in `.autodoc` files and doc comments 2025.12.19 - Don't replace empty lines in `@BeginCode` blocks by `
` diff --git a/gap/Parser.gi b/gap/Parser.gi index 5fa90bbf..f22f27ec 100644 --- a/gap/Parser.gi +++ b/gap/Parser.gi @@ -19,10 +19,50 @@ BindGlobal( "AUTODOC_PositionPrefixShebang", return fail; end ); +## +BindGlobal( "AUTODOC_SplitMarkdownHeading", + function( line ) + local trimmed, level, first_non_hash; + trimmed := StripBeginEnd( line, " \t\r\n" ); + if trimmed = "" or trimmed[ 1 ] <> '#' then + return fail; + fi; + + level := 1; + while level < Length( trimmed ) and trimmed[ level + 1 ] = '#' do + level := level + 1; + od; + if level > 3 then + return fail; + fi; + + first_non_hash := level + 1; + if first_non_hash <= Length( trimmed ) and + not ( trimmed[ first_non_hash ] in " \t\r\n" ) then + return fail; + fi; + + while first_non_hash <= Length( trimmed ) and + trimmed[ first_non_hash ] in " \t\r\n" do + first_non_hash := first_non_hash + 1; + od; + + if level = 1 then + return [ "@Chapter", trimmed{ [ first_non_hash .. Length( trimmed ) ] } ]; + elif level = 2 then + return [ "@Section", trimmed{ [ first_non_hash .. Length( trimmed ) ] } ]; + fi; + return [ "@Subsection", trimmed{ [ first_non_hash .. Length( trimmed ) ] } ]; +end ); + ## BindGlobal( "AUTODOC_SplitCommandAndArgument", function( line ) - local command_start, argument_start, command, argument; + local command_start, argument_start, command, argument, heading; + heading := AUTODOC_SplitMarkdownHeading( line ); + if heading <> fail then + return heading; + fi; command_start := PositionSublist( line, "@" ); if command_start = fail then return [ "STRING", line ]; diff --git a/tst/misc.tst b/tst/misc.tst index 07cfe8fc..a6eaac98 100644 --- a/tst/misc.tst +++ b/tst/misc.tst @@ -95,6 +95,14 @@ gap> Scan_for_AutoDoc_Part( " @NoArg", true ); [ "@NoArg", "" ] gap> Scan_for_AutoDoc_Part( "no command here", true ); [ "STRING", "no command here" ] +gap> Scan_for_AutoDoc_Part( "# Heading chapter", true ); +[ "@Chapter", "Heading chapter" ] +gap> Scan_for_AutoDoc_Part( "## Heading section", true ); +[ "@Section", "Heading section" ] +gap> Scan_for_AutoDoc_Part( "### Heading subsection", true ); +[ "@Subsection", "Heading subsection" ] +gap> Scan_for_AutoDoc_Part( " #! ## Comment section", false ); +[ "@Section", "Comment section" ] # # AutoDoc_Parser_ReadFiles: multiline InstallMethod parsing diff --git a/tst/worksheets/autoplain.expected/_Chapter_Test.xml b/tst/worksheets/autoplain.expected/_Chapter_Test.xml index 3683a9c4..8e2799b1 100644 --- a/tst/worksheets/autoplain.expected/_Chapter_Test.xml +++ b/tst/worksheets/autoplain.expected/_Chapter_Test.xml @@ -6,6 +6,12 @@ Does AutoDoc have a way to allow that header block not to appear in the documentation generated from a