33// See the LICENSE file in the project root for more information
44
55using System . Diagnostics . CodeAnalysis ;
6- using System . Globalization ;
6+ using System . Text . Json ;
77using System . Text . RegularExpressions ;
8+ using Elastic . Documentation ;
89using Elastic . Documentation . Diagnostics ;
910using Elastic . Markdown . Myst ;
11+ using Elastic . Markdown . Myst . InlineParsers . Substitution ;
1012
1113namespace Elastic . Markdown . Helpers ;
1214
@@ -83,7 +85,7 @@ private static bool ReplaceSubstitutions(
8385 // Apply mutations if present
8486 if ( components . Length > 1 )
8587 {
86- value = ApplyMutations ( value , components [ 1 ..] ) ;
88+ value = ApplyMutationsUsingExistingSystem ( value , components [ 1 ..] ) ;
8789 }
8890
8991 replacement ??= span . ToString ( ) ;
@@ -95,62 +97,68 @@ private static bool ReplaceSubstitutions(
9597 return replaced ;
9698 }
9799
98- private static string ApplyMutations ( string value , string [ ] mutations )
100+ private static string ApplyMutationsUsingExistingSystem ( string value , string [ ] mutations )
99101 {
100102 var result = value ;
101- foreach ( var mutation in mutations )
103+ foreach ( var mutationStr in mutations )
102104 {
103- var mutationStr = mutation . Trim ( ) ;
104- result = mutationStr switch
105+ var trimmedMutation = mutationStr . Trim ( ) ;
106+ if ( SubstitutionMutationExtensions . TryParse ( trimmedMutation , out var mutation , true , true ) )
105107 {
106- "M" => TryGetVersionMajor ( result ) ,
107- "M.M" => TryGetVersionMajorMinor ( result ) ,
108- "M.x" => TryGetVersionMajorX ( result ) ,
109- "M+1" => TryGetVersionIncreaseMajor ( result ) ,
110- "M.M+1" => TryGetVersionIncreaseMinor ( result ) ,
111- "lc" => result . ToLowerInvariant ( ) ,
112- "uc" => result . ToUpperInvariant ( ) ,
113- "tc" => CultureInfo . CurrentCulture . TextInfo . ToTitleCase ( result . ToLowerInvariant ( ) ) ,
114- "c" => char . ToUpperInvariant ( result [ 0 ] ) + result [ 1 ..] . ToLowerInvariant ( ) ,
115- "trim" => result . Trim ( ) ,
116- _ => result // Unknown mutation, return unchanged
117- } ;
108+ // Use the same logic as SubstitutionRenderer.Write
109+ var ( success , update ) = mutation switch
110+ {
111+ SubstitutionMutation . MajorComponent => TryGetVersion ( result , v => $ "{ v . Major } ") ,
112+ SubstitutionMutation . MajorX => TryGetVersion ( result , v => $ "{ v . Major } .x") ,
113+ SubstitutionMutation . MajorMinor => TryGetVersion ( result , v => $ "{ v . Major } .{ v . Minor } ") ,
114+ SubstitutionMutation . IncreaseMajor => TryGetVersion ( result , v => $ "{ v . Major + 1 } .0.0") ,
115+ SubstitutionMutation . IncreaseMinor => TryGetVersion ( result , v => $ "{ v . Major } .{ v . Minor + 1 } .0") ,
116+ SubstitutionMutation . LowerCase => ( true , result . ToLowerInvariant ( ) ) ,
117+ SubstitutionMutation . UpperCase => ( true , result . ToUpperInvariant ( ) ) ,
118+ SubstitutionMutation . Capitalize => ( true , Capitalize ( result ) ) ,
119+ SubstitutionMutation . KebabCase => ( true , ToKebabCase ( result ) ) ,
120+ SubstitutionMutation . CamelCase => ( true , ToCamelCase ( result ) ) ,
121+ SubstitutionMutation . PascalCase => ( true , ToPascalCase ( result ) ) ,
122+ SubstitutionMutation . SnakeCase => ( true , ToSnakeCase ( result ) ) ,
123+ SubstitutionMutation . TitleCase => ( true , TitleCase ( result ) ) ,
124+ SubstitutionMutation . Trim => ( true , Trim ( result ) ) ,
125+ _ => ( false , result )
126+ } ;
127+ if ( success )
128+ {
129+ result = update ;
130+ }
131+ }
118132 }
119133 return result ;
120134 }
121135
122- private static string TryGetVersionMajor ( string version )
136+ private static ( bool Success , string Result ) TryGetVersion ( string version , Func < SemVersion , string > transform )
123137 {
124- if ( Version . TryParse ( version , out var v ) )
125- return v . Major . ToString ( ) ;
126- return version ;
138+ if ( ! SemVersion . TryParse ( version , out var v ) && ! SemVersion . TryParse ( version + ".0" , out v ) )
139+ return ( false , version ) ;
140+ return ( true , transform ( v ) ) ;
127141 }
128142
129- private static string TryGetVersionMajorMinor ( string version )
130- {
131- if ( Version . TryParse ( version , out var v ) )
132- return $ "{ v . Major } .{ v . Minor } ";
133- return version ;
134- }
143+ // These methods match the exact implementation in SubstitutionRenderer
144+ private static string Capitalize ( string input ) =>
145+ input switch
146+ {
147+ null => string . Empty ,
148+ "" => string . Empty ,
149+ _ => string . Concat ( input [ 0 ] . ToString ( ) . ToUpper ( ) , input . AsSpan ( 1 ) )
150+ } ;
135151
136- private static string TryGetVersionMajorX ( string version )
137- {
138- if ( Version . TryParse ( version , out var v ) )
139- return $ "{ v . Major } .x";
140- return version ;
141- }
152+ private static string ToKebabCase ( string str ) => JsonNamingPolicy . KebabCaseLower . ConvertName ( str ) . Replace ( " " , string . Empty ) ;
142153
143- private static string TryGetVersionIncreaseMajor ( string version )
144- {
145- if ( Version . TryParse ( version , out var v ) )
146- return $ "{ v . Major + 1 } .0.0";
147- return version ;
148- }
154+ private static string ToCamelCase ( string str ) => JsonNamingPolicy . CamelCase . ConvertName ( str ) . Replace ( " " , string . Empty ) ;
149155
150- private static string TryGetVersionIncreaseMinor ( string version )
151- {
152- if ( Version . TryParse ( version , out var v ) )
153- return $ "{ v . Major } .{ v . Minor + 1 } .0";
154- return version ;
155- }
156+ private static string ToPascalCase ( string str ) => TitleCase ( str ) . Replace ( " " , string . Empty ) ;
157+
158+ private static string ToSnakeCase ( string str ) => JsonNamingPolicy . SnakeCaseLower . ConvertName ( str ) . Replace ( " " , string . Empty ) ;
159+
160+ private static string TitleCase ( string str ) => System . Globalization . CultureInfo . InvariantCulture . TextInfo . ToTitleCase ( str ) ;
161+
162+ private static string Trim ( string str ) =>
163+ str . AsSpan ( ) . Trim ( [ '!' , ' ' , '\t ' , '\r ' , '\n ' , '.' , ',' , ')' , '(' , ':' , ';' , '<' , '>' , '[' , ']' ] ) . ToString ( ) ;
156164}
0 commit comments