Skip to content

Commit 243f24c

Browse files
Copilotkzu
andcommitted
Multi-target netstandard2.0 and net8.0 with regex fallback
Agent-Logs-Url: https://github.com/devlooped/Schematron/sessions/f32a5ddd-99bd-47c6-b680-474fbf82aa8e Co-authored-by: kzu <169707+kzu@users.noreply.github.com>
1 parent 43e46ea commit 243f24c

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

src/Schematron/Schematron.csproj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<AssemblyName>Schematron</AssemblyName>
55
<RootNamespace>Schematron</RootNamespace>
6-
<TargetFramework>net8.0</TargetFramework>
6+
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<PackageId>Schematron.NET</PackageId>
99
<LangVersion>Latest</LangVersion>
@@ -13,4 +13,11 @@
1313
<InternalsVisibleTo Include="Schematron.Tests" />
1414
</ItemGroup>
1515

16+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
17+
<PackageReference Include="PolySharp" Version="1.15.0">
18+
<PrivateAssets>all</PrivateAssets>
19+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
20+
</PackageReference>
21+
</ItemGroup>
22+
1623
</Project>

src/Schematron/TagExpressions.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace Schematron;
55
/// <summary />
66
partial class TagExpressions
77
{
8+
#if NET8_0_OR_GREATER
89
/// <summary>
910
/// The compiled regular expression to replace the special <c>name</c> and <c>value</c> tags inside a message.
1011
/// </summary>
@@ -32,8 +33,42 @@ partial class TagExpressions
3233

3334
// Closing elements don't have an expanded xmlns so they will be matched too.
3435
// TODO: improve this to avoid removing non-schematron closing elements.
35-
// Pattern derived from Schema.LegacyNamespace and Schema.IsoNamespace (with Regex.Escape applied).
36+
// Pattern derived from Regex.Escape(Schema.LegacyNamespace) and Regex.Escape(Schema.IsoNamespace).
37+
// If those constants ever change, update the hardcoded escaped values here to match.
3638
[GeneratedRegex(@"<.*\bxmlns\b[^\s]*(?:http://www\.ascc\.net/xml/schematron|http://purl\.oclc\.org/dsdl/schematron)[^>]*>|</[^>]*>")]
3739
public static partial Regex AllSchematron();
40+
#else
41+
/// <summary>
42+
/// The compiled regular expression to replace the special <c>name</c> and <c>value</c> tags inside a message.
43+
/// </summary>
44+
/// <remarks>
45+
/// Replaces each instance of <c>name</c> and <c>value</c>tags with the value in the current context element.
46+
/// </remarks>
47+
// The element declarations can contain the namespace if expanded in a loaded document.
48+
static readonly Regex _nameValueOf = new Regex(@"<[^\s>]*\b(name|value-of)\b[^>]*/>", RegexOptions.Compiled);
49+
public static Regex NameValueOf() => _nameValueOf;
50+
51+
static readonly Regex _emph = new Regex(@"<[^\s>]*\bemph\b[^>]*>", RegexOptions.Compiled);
52+
public static Regex Emph() => _emph;
53+
54+
static readonly Regex _dir = new Regex(@"<[^\s]*\bdir\b[^>]*>", RegexOptions.Compiled);
55+
public static Regex Dir() => _dir;
56+
57+
static readonly Regex _span = new Regex(@"<[^\s]*\bspan\b[^>]*>", RegexOptions.Compiled);
58+
public static Regex Span() => _span;
59+
60+
static readonly Regex _para = new Regex(@"<[^\s]*\bp\b[^>]*>", RegexOptions.Compiled);
61+
public static Regex Para() => _para;
62+
63+
static readonly Regex _any = new Regex(@"<[^\s]*[^>]*>", RegexOptions.Compiled);
64+
public static Regex Any() => _any;
65+
66+
// Closing elements don't have an expanded xmlns so they will be matched too.
67+
// TODO: improve this to avoid removing non-schematron closing elements.
68+
static readonly Regex _allSchematron = new Regex(
69+
@"<.*\bxmlns\b[^\s]*(?:" + Regex.Escape(Schema.LegacyNamespace) + "|" + Regex.Escape(Schema.IsoNamespace) + @")[^>]*>|</[^>]*>",
70+
RegexOptions.Compiled);
71+
public static Regex AllSchematron() => _allSchematron;
72+
#endif
3873
}
3974

0 commit comments

Comments
 (0)