|
@@ -7,6 +7,7 @@ using Sayaka.Common;
|
|
|
using Spire.Pdf.Annotations;
|
|
using Spire.Pdf.Annotations;
|
|
|
using Spire.Pdf.Exporting.XPS.Schema;
|
|
using Spire.Pdf.Exporting.XPS.Schema;
|
|
|
using System.Security.Cryptography;
|
|
using System.Security.Cryptography;
|
|
|
|
|
+using System.Text.Json;
|
|
|
using System.Text.RegularExpressions;
|
|
using System.Text.RegularExpressions;
|
|
|
using System.Web;
|
|
using System.Web;
|
|
|
using System.Xml.Linq;
|
|
using System.Xml.Linq;
|
|
@@ -227,7 +228,7 @@ namespace molilian.core
|
|
|
{
|
|
{
|
|
|
foreach (var resource in rule.resources)
|
|
foreach (var resource in rule.resources)
|
|
|
{
|
|
{
|
|
|
- Dictionary<string, string> resResult = await ResourceProcessing(match, resource);
|
|
|
|
|
|
|
+ Dictionary<string, string> resResult = await ResourceProcessing(match, resource, rule.replacements);
|
|
|
foreach (var e in resResult)
|
|
foreach (var e in resResult)
|
|
|
{
|
|
{
|
|
|
if (deeplink.Contains(e.Key))
|
|
if (deeplink.Contains(e.Key))
|
|
@@ -258,7 +259,7 @@ namespace molilian.core
|
|
|
}
|
|
}
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
- ReplaceProcessing(val, i, ref deeplink, ref prompt_text);
|
|
|
|
|
|
|
+ ReplaceProcessing(val, i, rule.replacements, ref deeplink, ref prompt_text);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (rule.replace != null && rule.replace.Count > 0)
|
|
if (rule.replace != null && rule.replace.Count > 0)
|
|
@@ -313,7 +314,7 @@ namespace molilian.core
|
|
|
/// <param name="i"></param>
|
|
/// <param name="i"></param>
|
|
|
/// <param name="deeplink"></param>
|
|
/// <param name="deeplink"></param>
|
|
|
/// <param name="prompt_text"></param>
|
|
/// <param name="prompt_text"></param>
|
|
|
- private static void ReplaceProcessing(string word, int i, ref string deeplink, ref string prompt_text)
|
|
|
|
|
|
|
+ private static void ReplaceProcessing(string word, int i, List<ReplacementRule> replacements, ref string deeplink, ref string prompt_text)
|
|
|
{
|
|
{
|
|
|
if (deeplink.Contains("{url:"))
|
|
if (deeplink.Contains("{url:"))
|
|
|
{
|
|
{
|
|
@@ -391,9 +392,60 @@ namespace molilian.core
|
|
|
}
|
|
}
|
|
|
deeplink = deeplink.Replace($"{{{i - 1}}}", word);
|
|
deeplink = deeplink.Replace($"{{{i - 1}}}", word);
|
|
|
prompt_text = prompt_text.Replace($"{{{i - 1}}}", word);
|
|
prompt_text = prompt_text.Replace($"{{{i - 1}}}", word);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // 新增 <<>> 格式处理
|
|
|
|
|
+ if (replacements != null)
|
|
|
|
|
+ {
|
|
|
|
|
+ deeplink = Regex.Replace(deeplink, @"<<(\w+):([^>]+)>>", match =>
|
|
|
|
|
+ {
|
|
|
|
|
+ var ruleName = match.Groups[1].Value;
|
|
|
|
|
+ var value = match.Groups[2].Value;
|
|
|
|
|
+
|
|
|
|
|
+ // 查找并应用替换规则
|
|
|
|
|
+ var rule = replacements.FirstOrDefault(r => r.Name == ruleName);
|
|
|
|
|
+ if (rule != null)
|
|
|
|
|
+ {
|
|
|
|
|
+ foreach (var r in rule.Rules)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (r.When.Evaluate(value))
|
|
|
|
|
+ {
|
|
|
|
|
+ return r.Then;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return rule.Default;
|
|
|
|
|
+ }
|
|
|
|
|
+ return value;
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ prompt_text = Regex.Replace(prompt_text, @"<<(\w+):([^>]+)>>", match =>
|
|
|
|
|
+ {
|
|
|
|
|
+ var ruleName = match.Groups[1].Value;
|
|
|
|
|
+ var value = match.Groups[2].Value;
|
|
|
|
|
+
|
|
|
|
|
+ // 查找并应用替换规则
|
|
|
|
|
+ var rule = replacements.FirstOrDefault(r => r.Name == ruleName);
|
|
|
|
|
+ if (rule != null)
|
|
|
|
|
+ {
|
|
|
|
|
+ foreach (var r in rule.Rules)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (r.When.Evaluate(value))
|
|
|
|
|
+ {
|
|
|
|
|
+ return r.Then;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return rule.Default;
|
|
|
|
|
+ }
|
|
|
|
|
+ return value;
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 最后处理普通的索引替换
|
|
|
|
|
+ deeplink = deeplink.Replace($"{{{i - 1}}}", word);
|
|
|
|
|
+ prompt_text = prompt_text.Replace($"{{{i - 1}}}", word);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private static async Task<Dictionary<string, string>> ResourceProcessing(Match baseMatch, DeeplinkResources resource)
|
|
|
|
|
|
|
+ private static async Task<Dictionary<string, string>> ResourceProcessing(Match baseMatch, DeeplinkResources resource, List<ReplacementRule> replacements)
|
|
|
{
|
|
{
|
|
|
Dictionary<string, string> result = new Dictionary<string, string>();
|
|
Dictionary<string, string> result = new Dictionary<string, string>();
|
|
|
string url = resource.url_pattern;
|
|
string url = resource.url_pattern;
|
|
@@ -401,7 +453,7 @@ namespace molilian.core
|
|
|
for (var i = 1; i < baseMatch.Groups.Count; i++)
|
|
for (var i = 1; i < baseMatch.Groups.Count; i++)
|
|
|
{
|
|
{
|
|
|
string val = baseMatch.Groups[i].Value;
|
|
string val = baseMatch.Groups[i].Value;
|
|
|
- ReplaceProcessing(val, i, ref url, ref prompt_text);
|
|
|
|
|
|
|
+ ReplaceProcessing(val, i, replacements, ref url, ref prompt_text);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
string content = string.Empty;
|
|
string content = string.Empty;
|
|
@@ -425,6 +477,7 @@ namespace molilian.core
|
|
|
}
|
|
}
|
|
|
break;
|
|
break;
|
|
|
case UrlAction.Body:
|
|
case UrlAction.Body:
|
|
|
|
|
+ case UrlAction.JsonBody:
|
|
|
{
|
|
{
|
|
|
var response = await new WebClientUtility
|
|
var response = await new WebClientUtility
|
|
|
{
|
|
{
|
|
@@ -449,19 +502,38 @@ namespace molilian.core
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(content)) { return result; }
|
|
if (string.IsNullOrEmpty(content)) { return result; }
|
|
|
|
|
|
|
|
- for (int i = 0; i < resource.pattern.Count; i++)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ try
|
|
|
{
|
|
{
|
|
|
- string val = string.Empty;
|
|
|
|
|
- var regex = new Regex(resource.pattern[i]);
|
|
|
|
|
- var match = regex.Match(content);
|
|
|
|
|
- if (match.Success)
|
|
|
|
|
|
|
+ JsonElement root = default;
|
|
|
|
|
+ if (UrlAction.JsonBody.Equals(resource.url_action))
|
|
|
{
|
|
{
|
|
|
- val = match.Groups[1].Value;
|
|
|
|
|
|
|
+ root = content.Convert2JsonElement();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- string key = $"{{{i}@{resource.name}}}";
|
|
|
|
|
- result.Add(key, val);
|
|
|
|
|
|
|
+ for (int i = 0; i < resource.pattern.Count; i++)
|
|
|
|
|
+ {
|
|
|
|
|
+ string val = string.Empty;
|
|
|
|
|
+
|
|
|
|
|
+ if (UrlAction.JsonBody.Equals(resource.url_action))
|
|
|
|
|
+ {
|
|
|
|
|
+ val = root.PathRead(resource.pattern[i], string.Empty);
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ var regex = new Regex(resource.pattern[i]);
|
|
|
|
|
+ var match = regex.Match(content);
|
|
|
|
|
+ if (match.Success)
|
|
|
|
|
+ {
|
|
|
|
|
+ val = match.Groups[1].Value;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ string key = $"{{{i}@{resource.name}}}";
|
|
|
|
|
+ result.Add(key, val);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
+ catch (Exception ex) { }
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
|
|
|