Files
QWERTYkez.OpenXmlProcessors/QWERTYkez.WordProcessor/MultiReplace.cs
2026-06-05 15:58:03 +07:00

494 lines
20 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace QWERTYkez.WordProcessor;
/// <summary>
/// Выполняет замену всех вхождений ключей из словаря на соответствующие массивы значений.
/// Каждое значение из массива помещается в отдельный параграф, причём первое значение
/// остаётся в текущем параграфе, а последующие создают новые.
/// Текст между вхождениями и после последнего сохраняется в соответствующих параграфах.
/// </summary>
internal static class MultiReplaceExt
{
// ---------- ПУБЛИЧНЫЕ МЕТОДЫ (СИГНАТУРЫ НЕИЗМЕННЫ) ----------
#region Body.Replace с одним ключом
internal static void Replace(this Body body, string oldValue, IEnumerable<string> newValues, StringComparison comparisonType)
{
if (body is null || string.IsNullOrEmpty(oldValue) || newValues is null) return;
var dict = new Dictionary<string, IEnumerable<string>> { { oldValue, newValues } };
body.Replace(dict, comparisonType);
}
internal static void Replace(this Body body, string oldValue, IEnumerable<ReplaceItem> newValues, StringComparison comparisonType)
{
if (body is null || string.IsNullOrEmpty(oldValue) || newValues is null) return;
var dict = new Dictionary<string, IEnumerable<ReplaceItem>> { { oldValue, newValues } };
body.Replace(dict, comparisonType);
}
#endregion
#region Body.Replace со словарём массивов
internal static void Replace(this Body body, IEnumerable<KeyValuePair<string, IEnumerable<string>>> replacements, StringComparison comparisonType)
{
if (body is null || replacements is null) return;
var paragraphs = body.Elements<Paragraph>().ToList();
for (int i = paragraphs.Count - 1; i >= 0; i--)
{
var p = paragraphs[i];
if (p?.Parent is null) continue;
var newParas = ProcessMultiReplacements(p, replacements, null, comparisonType);
if (newParas is not null && newParas.Count > 0)
ParagraphReplacer.ReplaceParagraph(p, newParas);
}
}
internal static void Replace(this Body body, IEnumerable<KeyValuePair<string, IEnumerable<ReplaceItem>>> replacements, StringComparison comparisonType)
{
if (body is null || replacements is null) return;
var paragraphs = body.Elements<Paragraph>().ToList();
for (int i = paragraphs.Count - 1; i >= 0; i--)
{
var p = paragraphs[i];
if (p?.Parent is null) continue;
var newParas = ProcessMultiReplacements(p, null, replacements, comparisonType);
if (newParas is not null && newParas.Count > 0)
ParagraphReplacer.ReplaceParagraph(p, newParas);
}
}
#endregion
#region Paragraph.ReplaceWithMultiple (один ключ)
internal static bool ReplaceWithMultiple(this Paragraph? paragraph, string oldValue, IEnumerable<string> newValues, StringComparison comparisonType)
{
if (paragraph is null || string.IsNullOrEmpty(oldValue) || newValues is null || newValues.Count() == 0)
return false;
var dict = new Dictionary<string, IEnumerable<string>> { { oldValue, newValues } };
var newParas = ProcessMultiReplacements(paragraph, dict, null, comparisonType);
if (newParas is null || newParas.Count == 0) return false;
if (paragraph.Parent is not null)
{
ParagraphReplacer.ReplaceParagraph(paragraph, newParas);
return true;
}
return false;
}
internal static bool ReplaceWithMultiple(this Paragraph? paragraph, string oldValue, IEnumerable<ReplaceItem> newValues, StringComparison comparisonType)
{
if (paragraph is null || string.IsNullOrEmpty(oldValue) || newValues is null || newValues.Count() == 0)
return false;
var dict = new Dictionary<string, IEnumerable<ReplaceItem>> { { oldValue, newValues } };
var newParas = ProcessMultiReplacements(paragraph, null, dict, comparisonType);
if (newParas is null || newParas.Count == 0) return false;
if (paragraph.Parent is not null)
{
ParagraphReplacer.ReplaceParagraph(paragraph, newParas);
return true;
}
return false;
}
#endregion
#region ProcessParagraphWithAllReplacements (для обратной совместимости)
internal static List<Paragraph>? ProcessParagraphWithAllReplacements(
Paragraph paragraph,
IEnumerable<KeyValuePair<string, IEnumerable<string>>> replacements,
StringComparison comparisonType)
{
return ProcessMultiReplacements(paragraph, replacements, null, comparisonType);
}
internal static List<Paragraph>? ProcessParagraphWithAllReplacements(
Paragraph paragraph,
IEnumerable<KeyValuePair<string, IEnumerable<ReplaceItem>>> replacements,
StringComparison comparisonType)
{
return ProcessMultiReplacements(paragraph, null, replacements, comparisonType);
}
internal static List<Paragraph>? ProcessParagraphWithAllReplacements(
Paragraph paragraph,
IEnumerable<KeyValuePair<string, string>> replacements,
StringComparison comparisonType)
{
Dictionary<string, IEnumerable<string>> dict = replacements
.Where(kvp => !string.IsNullOrEmpty(kvp.Key))
.ToDictionary(kvp => kvp.Key, kvp => (IEnumerable<string>)[kvp.Value]);
return ProcessMultiReplacements(paragraph, dict, null, comparisonType);
}
#endregion
// ---------- ВНУТРЕННЯЯ РЕАЛИЗАЦИЯ ----------
private class MatchDefinition(string key, IEnumerable<ReplaceItem> values)
{
public string Key { get; } = key;
public IEnumerable<ReplaceItem> Values { get; } = values;
}
private class Match
{
public MatchDefinition Definition { get; set; } = null!;
public int Start { get; set; }
public int End { get; set; }
}
private class RunSegment(Run run, string text, int start, int end)
{
public Run Run { get; } = run;
public string Text { get; } = text;
public int Start { get; } = start;
public int End { get; } = end;
}
private class ParagraphStructure(string fullText, List<MultiReplaceExt.RunSegment> segments)
{
public string FullText { get; } = fullText;
public List<RunSegment> Segments { get; } = segments;
}
private static ParagraphStructure AnalyzeParagraphStructure(List<Run> runs)
{
var segments = new List<RunSegment>();
var sb = new StringBuilder();
int pos = 0;
foreach (var run in runs)
{
string text = GetRunText(run);
if (string.IsNullOrEmpty(text)) continue;
segments.Add(new RunSegment(run, text, pos, pos + text.Length));
sb.Append(text);
pos += text.Length;
}
return new ParagraphStructure(sb.ToString(), segments);
}
private static string GetRunText(Run run)
{
var sb = new StringBuilder();
foreach (var text in run.Elements<Text>())
sb.Append(text.Text);
return sb.ToString();
}
private static Paragraph CloneParagraphProperties(Paragraph original)
{
var newPara = new Paragraph();
if (original.ParagraphProperties is not null)
newPara.ParagraphProperties = (ParagraphProperties)original.ParagraphProperties.CloneNode(true);
return newPara;
}
/// <summary>
/// Строит параграф, содержащий копии всех элементов исходного параграфа,
/// попадающих в текстовый диапазон [start, end).
/// </summary>
private static Paragraph? BuildRangeParagraph(Paragraph original, ParagraphStructure structure, int start, int end)
{
if (start >= end) return null;
var newPara = CloneParagraphProperties(original);
foreach (var child in original.ChildElements)
{
if (child is Run run)
{
var seg = structure.Segments.FirstOrDefault(s => s.Run == run);
if (seg is null)
{
// Run без текста (разрыв, поле) копируем целиком, т.к. не можем привязать к позиции
newPara.AppendChild(run.CloneNode(true));
continue;
}
if (seg.End <= start || seg.Start >= end)
continue;
if (seg.Start >= start && seg.End <= end)
{
// Полностью внутри диапазона
newPara.AppendChild(run.CloneNode(true));
}
else
{
// Частичное пересечение обрезаем текст
var runClone = (Run)run.CloneNode(true);
foreach (var t in runClone.Elements<Text>().ToList())
t.Remove();
int cutStart = Math.Max(start, seg.Start) - seg.Start;
int cutEnd = Math.Min(end, seg.End) - seg.Start;
string newText = seg.Text.Substring(cutStart, cutEnd - cutStart);
runClone.AppendChild(new Text(newText));
newPara.AppendChild(runClone);
}
}
else
{
// Не Run копируем всегда (закладки, поля и т.п.), т.к. не можем определить позицию
newPara.AppendChild(child.CloneNode(true));
}
}
// Удаляем пустые Run
foreach (var run in newPara.Descendants<Run>().Where(r => !r.HasChildren).ToList())
run.Remove();
if (!newPara.ChildElements.OfType<Run>().Any() && newPara.ParagraphProperties is null)
return null;
return newPara;
}
/// <summary>
/// Строит параграф, содержащий все элементы исходного параграфа,
/// которые находятся строго после указанной текстовой позиции,
/// пропуская нетекстовые элементы до первого текстового сегмента.
/// </summary>
private static Paragraph? BuildAfterParagraph(Paragraph original, ParagraphStructure structure, int position)
{
if (position >= structure.FullText.Length) return null;
var newPara = CloneParagraphProperties(original);
// Находим первый текстовый сегмент, который начинается на или после position
var firstTextSeg = structure.Segments.FirstOrDefault(s => s.Start >= position);
bool passedFirstText = false;
foreach (var child in original.ChildElements)
{
if (child is Run run)
{
var seg = structure.Segments.FirstOrDefault(s => s.Run == run);
if (seg is null)
{
// Run без текста добавляем только если уже прошли первый текстовый сегмент
if (passedFirstText)
newPara.AppendChild(run.CloneNode(true));
continue;
}
if (seg.Start >= position)
{
// Полностью после позиции
newPara.AppendChild(run.CloneNode(true));
if (seg == firstTextSeg)
passedFirstText = true;
}
else if (seg.End > position)
{
// Частично пересекает обрезаем текст
var runClone = (Run)run.CloneNode(true);
foreach (var t in runClone.Elements<Text>().ToList())
t.Remove();
int offset = position - seg.Start;
string newText = seg.Text.Substring(offset);
runClone.AppendChild(new Text(newText));
newPara.AppendChild(runClone);
passedFirstText = true;
}
// seg.End <= position игнорируем
}
else
{
// Не Run добавляем только если уже прошли первый текстовый сегмент
if (passedFirstText)
newPara.AppendChild(child.CloneNode(true));
}
}
foreach (var run in newPara.Descendants<Run>().Where(r => !r.HasChildren).ToList())
run.Remove();
if (!newPara.ChildElements.OfType<Run>().Any() && newPara.ParagraphProperties is null)
return null;
return newPara;
}
/// <summary>
/// Вставляет в параграф новый Run с текстом из ReplaceItem,
/// копируя форматирование из сегмента, содержащего указанную позицию.
/// Если BreakPage == true, добавляет отдельный Run с разрывом страницы.
/// </summary>
private static void InsertFormattedRun(Paragraph para, ReplaceItem item, ParagraphStructure structure, int position)
{
var seg = structure.Segments.FirstOrDefault(s => position >= s.Start && position < s.End);
if (seg is null) return;
var textRun = new Run();
if (seg.Run.RunProperties is not null)
textRun.RunProperties = (RunProperties)seg.Run.RunProperties.CloneNode(true);
textRun.AppendChild(new Text(item.Text ?? string.Empty));
para.AppendChild(textRun);
if (item.BreakPage)
{
var breakRun = new Run(new Break() { Type = BreakValues.Page });
if (seg.Run.RunProperties is not null)
breakRun.RunProperties = (RunProperties)seg.Run.RunProperties.CloneNode(true);
para.AppendChild(breakRun);
}
}
/// <summary>
/// Добавляет содержимое одного параграфа в другой (клонируя элементы).
/// </summary>
private static void MergeParagraph(Paragraph target, Paragraph source)
{
foreach (var child in source.ChildElements)
target.AppendChild(child.CloneNode(true));
}
/// <summary>
/// Основной алгоритм: обрабатывает все вхождения всех ключей из предоставленных словарей.
/// </summary>
private static List<Paragraph>? ProcessMultiReplacements(
Paragraph original,
IEnumerable<KeyValuePair<string, IEnumerable<string>>>? stringReplacements,
IEnumerable<KeyValuePair<string, IEnumerable<ReplaceItem>>>? itemReplacements,
StringComparison comparisonType)
{
// 1. Собираем определения замен
var definitions = new List<MatchDefinition>();
if (stringReplacements is not null)
{
foreach (var kvp in stringReplacements)
{
if (string.IsNullOrEmpty(kvp.Key) || kvp.Value is null || kvp.Value.Count() == 0) continue;
definitions.Add(new MatchDefinition(kvp.Key, [.. kvp.Value.Select(v => new ReplaceItem(v, false))]));
}
}
if (itemReplacements is not null)
{
foreach (var kvp in itemReplacements)
{
if (string.IsNullOrEmpty(kvp.Key) || kvp.Value is null || kvp.Value.Count() == 0) continue;
definitions.Add(new MatchDefinition(kvp.Key, kvp.Value));
}
}
if (definitions.Count == 0) return null;
// 2. Анализ структуры параграфа
var runs = original.Descendants<Run>().ToList();
if (runs.Count == 0) return null;
var structure = AnalyzeParagraphStructure(runs);
string fullText = structure.FullText;
if (fullText.Length == 0) return null;
// 3. Находим все вхождения всех ключей
var matches = new List<Match>();
foreach (var def in definitions)
{
int pos = 0;
while ((pos = fullText.IndexOf(def.Key, pos, comparisonType)) != -1)
{
matches.Add(new Match
{
Definition = def,
Start = pos,
End = pos + def.Key.Length
});
pos += def.Key.Length;
}
}
if (matches.Count == 0) return null;
// 4. Сортируем по позиции
matches.Sort((a, b) => a.Start.CompareTo(b.Start));
// 5. Построение результирующих параграфов
var resultParas = new List<Paragraph>();
Paragraph? currentPara = null;
int currentPos = 0;
for (int i = 0; i < matches.Count; i++)
{
var match = matches[i];
// Текст перед текущим совпадением (от currentPos до match.Start)
if (currentPos < match.Start)
{
var textPart = BuildRangeParagraph(original, structure, currentPos, match.Start);
if (textPart is not null)
{
if (currentPara is null)
{
currentPara = textPart;
resultParas.Add(currentPara);
}
else
{
MergeParagraph(currentPara, textPart);
}
}
}
// Обрабатываем значения замены для этого совпадения
var values = match.Definition.Values;
// Первое значение в текущий параграф (или создаём новый)
if (currentPara is null)
{
currentPara = CloneParagraphProperties(original);
resultParas.Add(currentPara);
}
values.ForFirstNext(first =>
{
InsertFormattedRun(currentPara, first, structure, match.Start);
},
next =>
{
// Остальные значения в новые параграфы
var newPara = CloneParagraphProperties(original);
InsertFormattedRun(newPara, next, structure, match.Start);
resultParas.Add(newPara);
currentPara = newPara; // теперь текущий параграф последний созданный
});
currentPos = match.End;
}
// Текст после последнего совпадения используем BuildAfterParagraph, чтобы пропустить лишние разрывы
if (currentPos < fullText.Length)
{
var textPart = BuildAfterParagraph(original, structure, currentPos);
if (textPart is not null)
{
if (currentPara is null)
{
currentPara = textPart;
resultParas.Add(currentPara);
}
else
{
MergeParagraph(currentPara, textPart);
}
}
}
// Удаляем пустые параграфы
for (int i = resultParas.Count - 1; i >= 0; i--)
{
if (!resultParas[i].ChildElements.OfType<Run>().Any() && resultParas[i].ParagraphProperties is null)
resultParas.RemoveAt(i);
}
return resultParas.Count > 0 ? resultParas : null;
}
}