|
| 1 | +using RegExpressLibrary; |
| 2 | +using RegExpressLibrary.Matches; |
| 3 | +using RegExpressLibrary.SyntaxColouring; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Text.Json; |
| 7 | +using System.Windows.Controls; |
| 8 | + |
| 9 | + |
| 10 | +namespace HtmlAgilityPackPlugin |
| 11 | +{ |
| 12 | + class Engine : IRegexEngine, IAIEngine |
| 13 | + |
| 14 | + |
| 15 | + { |
| 16 | + string IAIEngine.AIPatternType => Options.SelectorMode == SelectorMode.XPath ? "html xpath" : "html css query selector"; |
| 17 | + string IAIEngine.AIPatternCodeblockType => Options.SelectorMode == SelectorMode.XPath ? "xpath" : "css"; |
| 18 | + string IAIEngine.AIAdditionalSystemPrompt => ""; |
| 19 | + static readonly Lazy<string?> LazyVersion = new( Matcher.GetVersion ); |
| 20 | + |
| 21 | + Options mOptions = new( ); |
| 22 | + readonly Lazy<UCOptions> mOptionsControl; |
| 23 | + |
| 24 | + public Engine( ) |
| 25 | + { |
| 26 | + mOptionsControl = new Lazy<UCOptions>( ( ) => |
| 27 | + { |
| 28 | + UCOptions oc = new( ); |
| 29 | + oc.SetOptions( Options ); |
| 30 | + oc.Changed += OptionsControl_Changed; |
| 31 | + |
| 32 | + return oc; |
| 33 | + } ); |
| 34 | + } |
| 35 | + |
| 36 | + public Options Options |
| 37 | + { |
| 38 | + get |
| 39 | + { |
| 40 | + return mOptions; |
| 41 | + } |
| 42 | + set |
| 43 | + { |
| 44 | + mOptions = value; |
| 45 | + |
| 46 | + if( mOptionsControl.IsValueCreated ) mOptionsControl.Value.SetOptions( mOptions ); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + #region IRegexEngine |
| 51 | + |
| 52 | + public string Kind => "HtmlAgilityPack"; |
| 53 | + |
| 54 | + public string? Version => LazyVersion.Value; |
| 55 | + |
| 56 | + public string Name => "HtmlAgilityPack"; |
| 57 | + |
| 58 | + public string Subtitle => mOptions.SelectorMode == SelectorMode.XPath ? "XPath" : "CSS"; |
| 59 | + |
| 60 | + public RegexEngineCapabilityEnum Capabilities => RegexEngineCapabilityEnum.Default;// | RegexEngineCapabilityEnum.NoCaptures; |
| 61 | + |
| 62 | + public string? NoteForCaptures => "This engine uses XPath or CSS selectors to select HTML nodes, not regex patterns."; |
| 63 | + |
| 64 | + public event RegexEngineOptionsChanged? OptionsChanged; |
| 65 | +#pragma warning disable 0067 |
| 66 | + public event EventHandler? FeatureMatrixReady; |
| 67 | +#pragma warning restore 0067 |
| 68 | + |
| 69 | + public Control GetOptionsControl( ) |
| 70 | + { |
| 71 | + return mOptionsControl.Value; |
| 72 | + } |
| 73 | + |
| 74 | + public string? ExportOptions( ) |
| 75 | + { |
| 76 | + string json = JsonSerializer.Serialize( Options, JsonUtilities.JsonOptions ); |
| 77 | + |
| 78 | + return json; |
| 79 | + } |
| 80 | + |
| 81 | + public void ImportOptions( string? json ) |
| 82 | + { |
| 83 | + if( string.IsNullOrWhiteSpace( json ) ) |
| 84 | + { |
| 85 | + Options = new Options( ); |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + try |
| 90 | + { |
| 91 | + Options = JsonSerializer.Deserialize<Options>( json, JsonUtilities.JsonOptions )!; |
| 92 | + } |
| 93 | + catch( Exception ex ) |
| 94 | + { |
| 95 | + // ignore versioning errors, for example |
| 96 | + if( InternalConfig.HandleException( ex ) ) |
| 97 | + throw; |
| 98 | + |
| 99 | + Options = new Options( ); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public RegexMatches GetMatches( ICancellable cnc, string pattern, string text ) |
| 105 | + { |
| 106 | + return Matcher.GetMatches( cnc, pattern, text, Options ); |
| 107 | + } |
| 108 | + |
| 109 | + public SyntaxOptions GetSyntaxOptions( ) |
| 110 | + { |
| 111 | + // XPath and CSS selectors have their own syntax, not regex syntax |
| 112 | + // Return Literal mode so no regex syntax highlighting is applied |
| 113 | + return new SyntaxOptions |
| 114 | + { |
| 115 | + Literal = true, |
| 116 | + XLevel = XLevelEnum.none, |
| 117 | + FeatureMatrix = new FeatureMatrix( ) |
| 118 | + }; |
| 119 | + } |
| 120 | + |
| 121 | + public IReadOnlyList<FeatureMatrixVariant> GetFeatureMatrices( ) |
| 122 | + { |
| 123 | + // This engine doesn't use regex, so return empty feature matrix |
| 124 | + return []; |
| 125 | + } |
| 126 | + |
| 127 | + public void SetIgnoreCase( bool yes ) |
| 128 | + { |
| 129 | + // XPath/CSS selectors don't have a direct ignore case option |
| 130 | + // but we could potentially add this in the future |
| 131 | + } |
| 132 | + |
| 133 | + public void SetIgnorePatternWhitespace( bool yes ) |
| 134 | + { |
| 135 | + // Not applicable for XPath/CSS selectors |
| 136 | + } |
| 137 | + |
| 138 | + #endregion |
| 139 | + |
| 140 | + private void OptionsControl_Changed( object? sender, RegexEngineOptionsChangedArgs args ) |
| 141 | + { |
| 142 | + OptionsChanged?.Invoke( this, args ); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments