Skip to content

Commit 859662c

Browse files
committed
Avoid hashing the same image within the same page.
1 parent 619a203 commit 859662c

1 file changed

Lines changed: 32 additions & 10 deletions

File tree

converter/generator/DocTransformer.cs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal abstract partial class DocTransformer
3030

3131
private static Dictionary<string, string> SharedImages => field ??= GetSharedImages();
3232

33-
private readonly Dictionary<string, (long size, ulong hash, string url)> VisitedImages = new(StringComparer.OrdinalIgnoreCase);
33+
private readonly Dictionary<string, (long size, ulong hash, string url)> EnglishImages = new(StringComparer.OrdinalIgnoreCase);
3434

3535
private readonly Dictionary<string, INode[]> LayoutNodes = [];
3636
private readonly Dictionary<string, List<(string file, string? details, TextPosition? position)>> Problems = [];
@@ -191,9 +191,10 @@ void Transform(IHtmlDocument document, string sourceFile, string language, in Na
191191
TransformAnchor(a, sourceFile, language, sourceDir);
192192
}
193193

194+
var pageImages = new Dictionary<string, string>();
194195
foreach (var img in document.Descendants<IHtmlImageElement>())
195196
{
196-
TransformImage(img, sourceFile, language, sourceDir);
197+
TransformImage(img, sourceFile, language, sourceDir, pageImages);
197198
}
198199

199200
var navDataDiv = CreateNavDataDiv(document, nav, sourceDir, language);
@@ -381,7 +382,7 @@ private bool TryResolveHref(string sourceDir, string language, string href, out
381382
return false;
382383
}
383384

384-
protected virtual void TransformImage(IHtmlImageElement img, string sourceFile, string language, string sourceDir)
385+
protected virtual void TransformImage(IHtmlImageElement img, string sourceFile, string language, string sourceDir, Dictionary<string, string> pageImages)
385386
{
386387
if (img.GetAttribute("src") is not string srcFull)
387388
{
@@ -411,17 +412,38 @@ protected virtual void TransformImage(IHtmlImageElement img, string sourceFile,
411412
{
412413
url = $"/{BookUrlName}/{language}/{srcFull.AsSpan("../".Length)}";
413414

414-
if (!VisitedImages.TryGetValue(src, out var visited))
415+
if (language == "en")
415416
{
416-
var size = srcImg.Length;
417-
var hash = FileHash.UInt64FromFile(srcImg.FullName);
417+
if (!EnglishImages.TryGetValue(src, out var visited))
418+
{
419+
var size = srcImg.Length;
420+
var hash = FileHash.UInt64FromFile(srcImg.FullName);
418421

419-
VisitedImages.Add(src, (size, hash, url));
422+
EnglishImages.Add(src, (size, hash, url));
423+
}
424+
else
425+
{
426+
url = visited.url;
427+
copy = false;
428+
}
420429
}
421-
else if (srcImg.Length == visited.size && FileHash.UInt64FromFile(srcImg.FullName) == visited.hash)
430+
else
422431
{
423-
url = visited.url;
424-
copy = false;
432+
if (pageImages.TryGetValue(src, out var prevUrl))
433+
{
434+
url = prevUrl;
435+
copy = false;
436+
}
437+
else
438+
{
439+
pageImages.Add(src, url);
440+
441+
if (EnglishImages.TryGetValue(src, out var visited) && srcImg.Length == visited.size && FileHash.UInt64FromFile(srcImg.FullName) == visited.hash)
442+
{
443+
url = pageImages[src] = visited.url;
444+
copy = false;
445+
}
446+
}
425447
}
426448
}
427449
else

0 commit comments

Comments
 (0)