Skip to content

Commit 7897b6b

Browse files
committed
Remove dupe logic
1 parent d9c344e commit 7897b6b

File tree

1 file changed

+3
-53
lines changed

1 file changed

+3
-53
lines changed

src/Elastic.Markdown/Myst/InlineParsers/Substitution/SubstitutionParser.cs

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -66,61 +66,11 @@ protected override void Write(HtmlRenderer renderer, SubstitutionLeaf leaf)
6666
return;
6767
}
6868

69-
foreach (var mutation in leaf.Mutations)
70-
{
71-
var (success, update) = mutation switch
72-
{
73-
SubstitutionMutation.MajorComponent => TryGetVersion(replacement, v => $"{v.Major}"),
74-
SubstitutionMutation.MajorX => TryGetVersion(replacement, v => $"{v.Major}.x"),
75-
SubstitutionMutation.MajorMinor => TryGetVersion(replacement, v => $"{v.Major}.{v.Minor}"),
76-
SubstitutionMutation.IncreaseMajor => TryGetVersion(replacement, v => $"{v.Major + 1}.0.0"),
77-
SubstitutionMutation.IncreaseMinor => TryGetVersion(replacement, v => $"{v.Major}.{v.Minor + 1}.0"),
78-
SubstitutionMutation.LowerCase => (true, replacement.ToLowerInvariant()),
79-
SubstitutionMutation.UpperCase => (true, replacement.ToUpperInvariant()),
80-
SubstitutionMutation.Capitalize => (true, Capitalize(replacement)),
81-
SubstitutionMutation.KebabCase => (true, ToKebabCase(replacement)),
82-
SubstitutionMutation.CamelCase => (true, ToCamelCase(replacement)),
83-
SubstitutionMutation.PascalCase => (true, ToPascalCase(replacement)),
84-
SubstitutionMutation.SnakeCase => (true, ToSnakeCase(replacement)),
85-
SubstitutionMutation.TitleCase => (true, TitleCase(replacement)),
86-
SubstitutionMutation.Trim => (true, Trim(replacement)),
87-
_ => throw new Exception($"encountered an unknown mutation '{mutation.ToStringFast(true)}'")
88-
};
89-
if (!success)
90-
{
91-
_ = renderer.Write(leaf.Content);
92-
return;
93-
}
94-
replacement = update;
95-
}
69+
// Apply mutations using shared utility
70+
var mutationStrings = leaf.Mutations.Select(m => m.ToStringFast(true)).ToArray();
71+
replacement = SubstitutionMutationHelper.ApplyMutations(replacement, mutationStrings);
9672
_ = renderer.Write(replacement);
9773
}
98-
99-
private static string ToCamelCase(string str) => JsonNamingPolicy.CamelCase.ConvertName(str.Replace(" ", string.Empty));
100-
private static string ToSnakeCase(string str) => JsonNamingPolicy.SnakeCaseLower.ConvertName(str).Replace(" ", string.Empty);
101-
private static string ToKebabCase(string str) => JsonNamingPolicy.KebabCaseLower.ConvertName(str).Replace(" ", string.Empty);
102-
private static string ToPascalCase(string str) => TitleCase(str).Replace(" ", string.Empty);
103-
104-
private static string TitleCase(string str) => CultureInfo.InvariantCulture.TextInfo.ToTitleCase(str);
105-
106-
private static string Trim(string str) =>
107-
str.AsSpan().Trim(['!', ' ', '\t', '\r', '\n', '.', ',', ')', '(', ':', ';', '<', '>', '[', ']']).ToString();
108-
109-
private static string Capitalize(string input) =>
110-
input switch
111-
{
112-
null => string.Empty,
113-
"" => string.Empty,
114-
_ => string.Concat(input[0].ToString().ToUpper(), input.AsSpan(1))
115-
};
116-
117-
private (bool, string) TryGetVersion(string version, Func<SemVersion, string> mutate)
118-
{
119-
if (!SemVersion.TryParse(version, out var v) && !SemVersion.TryParse(version + ".0", out v))
120-
return (false, string.Empty);
121-
122-
return (true, mutate(v));
123-
}
12474
}
12575

12676
public class SubstitutionParser : InlineParser

0 commit comments

Comments
 (0)