Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
fix ruby replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
herring101 committed Feb 25, 2024
1 parent bdaade5 commit 01372e7
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions KoeBook.Core/Services/AnalyzerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,18 @@ public async ValueTask<BookScripts> AnalyzeAsync(BookProperties bookProperties,
{
var line = paragraph.Text;
// rubyタグがあればルビのdictionaryに登録
var matches = MyRegex().Matches(input: line);
foreach (Match match in matches)
var rubyDict = ExtractRuby(line);

Check warning on line 42 in KoeBook.Core/Services/AnalyzerService.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference argument for parameter 'text' in 'Dictionary<string, string> AnalyzerService.ExtractRuby(string text)'.

Check warning on line 42 in KoeBook.Core/Services/AnalyzerService.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'text' in 'Dictionary<string, string> AnalyzerService.ExtractRuby(string text)'.

Check warning on line 42 in KoeBook.Core/Services/AnalyzerService.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'text' in 'Dictionary<string, string> AnalyzerService.ExtractRuby(string text)'.

foreach (var ruby in rubyDict)
{
var key = match.Groups[1].Value;
var value = match.Groups[2].Value;
if (!_rubyReplacements.ContainsKey(key))
if (!_rubyReplacements.ContainsKey(ruby.Key))
{
_rubyReplacements.Add(key, value);
_rubyReplacements.Add(ruby.Key, ruby.Value);
}
}
// ルビを置換
foreach (var replacement in _rubyReplacements)
{
line = line.Replace(replacement.Key, replacement.Value);
}
line = ReplaceBaseTextWithRuby(line, rubyDict);

scriptLines.Add(new ScriptLine(paragraph, line, "", ""));
}
}
Expand All @@ -80,6 +77,32 @@ public async ValueTask<BookScripts> AnalyzeAsync(BookProperties bookProperties,
return bookScripts;
}

[GeneratedRegex("<ruby>(.*?)<rt>(.*?)</rt></ruby>")]
private static partial Regex MyRegex();
private static Dictionary<string, string> ExtractRuby(string text)
{
var rubyDict = new Dictionary<string, string>();
var rubyRegex = new Regex("<ruby><rb>(.*?)</rb><rp>(</rp><rt>(.*?)</rt><rp>)</rp></ruby>");

foreach (Match match in rubyRegex.Matches(text))
{
if (!rubyDict.ContainsKey(match.Groups[1].Value))
{
rubyDict.Add(match.Groups[1].Value, match.Groups[2].Value);
}
}

return rubyDict;
}

private static string ReplaceBaseTextWithRuby(string text, Dictionary<string, string> rubyDict)
{
// 元のテキストからルビタグをすべてルビテキストに置き換える
var resultText = text;
foreach (var pair in rubyDict)
{
var rubyTag = $"<ruby><rb>{pair.Key}</rb><rp>(</rp><rt>{pair.Value}</rt><rp>)</rp></ruby>";
resultText = resultText.Replace(rubyTag, pair.Value);
}

return resultText;
}
}

0 comments on commit 01372e7

Please sign in to comment.