Skip to content

Commit f3588df

Browse files
committed
Refactor extracting TryResolveSrc.
1 parent 7c913d3 commit f3588df

4 files changed

Lines changed: 105 additions & 73 deletions

File tree

converter/generator/DocBookTransformer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private IHtmlDivElement CreateNavDataDiv(IHtmlDocument document, in Nav nav, str
150150

151151
if (!files.Parent.IsEmpty)
152152
{
153-
if (TryResolveHref(sourceDir, "../" + files.Parent, out var url, out var _))
153+
if (TryResolveHref("../" + files.Parent, sourceDir, out var url, out var _))
154154
{
155155
navDataDiv.SetAttribute("data-parent-link", url);
156156
}

converter/generator/DocToStaticPagesTransformer.cs

Lines changed: 42 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Buffers;
2+
using System.Diagnostics;
23
using System.Net;
34
using AngleSharp.Dom;
45
using AngleSharp.Html.Dom;
@@ -13,7 +14,6 @@ internal abstract partial class DocToStaticPagesTransformer : DocTransformer, ID
1314

1415
private readonly bool UseWebp;
1516

16-
1717
#region Language specific members
1818

1919
protected string Language { get; private set; } = null!;
@@ -24,7 +24,6 @@ internal abstract partial class DocToStaticPagesTransformer : DocTransformer, ID
2424

2525
#endregion
2626

27-
2827
protected DocToStaticPagesTransformer(DocToStaticPagesTransformerArgs args, ProblemRecorder problems) : base(args, problems)
2928
{
3029
UseWebp = args.UseWebp;
@@ -88,7 +87,7 @@ protected internal override void Transform(IHtmlDocument document, IHtmlHeadElem
8887
body.AppendNodes(loading, mainContent);
8988
}
9089

91-
protected override bool TryResolveHref(string sourceDir, string href, out string result, out string? titleEn)
90+
protected override bool TryResolveHref(string href, string sourceDir, out string result, out string? titleEn)
9291
{
9392
titleEn = null;
9493

@@ -130,35 +129,21 @@ protected override bool TryResolveHref(string sourceDir, string href, out string
130129
return false;
131130
}
132131

133-
protected override void TransformImage(IHtmlImageElement img, string sourceFile, string sourceDir)
132+
protected override bool TryResolveSrc(string src, string sourceDir, out string result, out (string src, string dst)? copy)
134133
{
135-
if (img.GetAttribute("src") is not string srcFull)
136-
{
137-
return;
138-
}
139-
140-
var src = srcFull;
141-
var sep = srcFull.AsSpan().IndexOfAny("?#");
142-
if (sep > -1)
143-
{
144-
src = srcFull[..sep];
145-
}
146-
147134
if (src.StartsWith("../images/"))
148135
{
149-
img.SetAttribute("loading", "lazy");
150-
151136
var srcImg = new FileInfo(Path.GetFullPath(src, sourceDir));
152-
var copy = true;
137+
var needsCopy = true;
153138

154-
var fileName = Path.GetFileName(src.AsSpan());
155-
if (SharedImages.GetAlternateLookup<ReadOnlySpan<char>>().TryGetValue(fileName, out var url))
139+
var fileName = Path.GetFileName(src);
140+
if (SharedImages.TryGetValue(fileName, out result!))
156141
{
157-
copy = false;
142+
needsCopy = false;
158143
}
159144
else if (srcImg.Exists)
160145
{
161-
url = '/'.TryPrefixEach(BookUrlName, Language, srcFull["../".Length..]);
146+
result = '/'.TryPrefixEach(BookUrlName, Language, src["../".Length..]);
162147

163148
if (Language == "en")
164149
{
@@ -167,29 +152,29 @@ protected override void TransformImage(IHtmlImageElement img, string sourceFile,
167152
var size = srcImg.Length;
168153
var hash = FileHash.UInt64FromFile(srcImg.FullName);
169154

170-
EnglishImages.Add(src, (size, hash, url));
155+
EnglishImages.Add(src, (size, hash, result));
171156
}
172157
else
173158
{
174-
url = visited.url;
175-
copy = false;
159+
result = visited.url;
160+
needsCopy = false;
176161
}
177162
}
178163
else
179164
{
180165
if (VisitedImages.TryGetValue(src, out var prevUrl))
181166
{
182-
url = prevUrl;
183-
copy = false;
167+
result = prevUrl;
168+
needsCopy = false;
184169
}
185170
else
186171
{
187-
VisitedImages.Add(src, url);
172+
VisitedImages.Add(src, result);
188173

189174
if (EnglishImages.TryGetValue(src, out var visited) && srcImg.Length == visited.size && FileHash.UInt64FromFile(srcImg.FullName) == visited.hash)
190175
{
191-
url = VisitedImages[src] = visited.url;
192-
copy = false;
176+
result = VisitedImages[src] = visited.url;
177+
needsCopy = false;
193178
}
194179
}
195180
}
@@ -200,44 +185,45 @@ protected override void TransformImage(IHtmlImageElement img, string sourceFile,
200185

201186
if (!File.Exists(srcImgEn))
202187
{
203-
ReportProblem(sourceFile, "Image src not found", src, img.SourceReference?.Position);
188+
result = "Image src not found";
189+
copy = null;
190+
return false;
204191
}
205192

206-
url = '/'.TryPrefixEach(BookUrlName, "en", srcFull["../".Length..]);
207-
copy = false;
193+
result = '/'.TryPrefixEach(BookUrlName, "en", src["../".Length..]);
194+
needsCopy = false;
208195
}
209196

210197
if (UseWebp)
211198
{
212-
sep = url.AsSpan().IndexOfAny("?#");
199+
var resultDir = Path.GetDirectoryName(result.AsSpan());
200+
var resultFileName = Path.GetFileNameWithoutExtension(result.AsSpan());
213201

214-
if (sep > -1)
215-
{
216-
var dot = url.AsSpan(..sep).LastIndexOf('.');
217-
url = $"{url.AsSpan(..dot)}.webp{url.AsSpan(sep)}";
218-
}
219-
else
220-
{
221-
var dot = url.AsSpan().LastIndexOf('.');
222-
url = $"{url.AsSpan(..dot)}.webp";
223-
}
202+
result = $"{resultDir}/{resultFileName}.webp";
224203
}
225204

226-
img.SetAttribute("src", url);
227-
228-
if (copy)
205+
if (!needsCopy)
206+
{
207+
copy = null;
208+
}
209+
else
229210
{
230211
var dstImg = Path.Combine(OutputFolder, Language, src["../".Length..]);
231-
232-
Directory.CreateDirectory(Path.GetDirectoryName(dstImg)!);
233-
234-
File.Copy(srcImg.FullName, dstImg, overwrite: true);
212+
copy = (srcImg.FullName, dstImg);
235213
}
214+
215+
return true;
236216
}
237-
else if (!Uri.IsWellFormedUriString(srcFull, UriKind.Absolute))
238-
{
239-
ReportProblem(sourceFile, "Unrecognized src", src, img.SourceReference?.Position);
240-
}
241217

218+
result = "Unrecognized src";
219+
copy = null;
220+
return false;
221+
}
222+
223+
protected override void TransformImage(IHtmlImageElement img, string sourceFile, string sourceDir)
224+
{
225+
base.TransformImage(img, sourceFile, sourceDir);
226+
227+
img.SetAttribute("loading", "lazy");
242228
}
243229
}

converter/generator/DocTransformer.cs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Buffers;
2+
using System.Diagnostics.CodeAnalysis;
23
using System.Text.Json;
34
using System.Text.RegularExpressions;
45
using System.Xml.Linq;
@@ -145,27 +146,17 @@ internal protected virtual void Transform(IHtmlDocument document, IHtmlHeadEleme
145146
}
146147
}
147148

148-
protected abstract bool TryResolveHref(string sourceDir, string href, out string result, out string? titleEn);
149+
protected abstract bool TryResolveHref(string href, string sourceDir, out string result, out string? titleEn);
149150

150151
protected virtual void TransformAnchor(IHtmlAnchorElement a, string sourceFile, string sourceDir)
151152
{
152-
if (a.GetAttribute("href") is string strHref && !strHref.IsBlank)
153+
if (a.GetAttribute("href") is string href && !href.IsBlank)
153154
{
154-
ReadOnlySpan<char> href = strHref, hash = "";
155-
var hashIndex = strHref.IndexOf('#');
156-
if (hashIndex == 0)
157-
{
158-
return;
159-
}
160-
else if (hashIndex > 0)
161-
{
162-
hash = strHref.AsSpan(hashIndex);
163-
href = strHref.AsSpan(..hashIndex);
164-
}
155+
var parts = new UrlParts(href);
165156

166-
if (TryResolveHref(sourceDir, hashIndex > 0 ? href.ToString() : strHref, out var result, out var title))
157+
if (TryResolveHref(sourceDir, parts.File.Length == href.Length ? href : parts.File.ToString(), out var result, out var title))
167158
{
168-
a.SetAttribute("href", $"{result}{hash}");
159+
a.SetAttribute("href", $"{result}{parts.Query}{parts.Hash}");
169160

170161
if (a.Title.IsBlank && !title.IsEmpty)
171162
{
@@ -179,7 +170,32 @@ protected virtual void TransformAnchor(IHtmlAnchorElement a, string sourceFile,
179170
}
180171
}
181172

182-
protected abstract void TransformImage(IHtmlImageElement img, string sourceFile, string sourceDir);
173+
protected abstract bool TryResolveSrc(string src, string sourceDir, out string result, out (string src, string dst)? copy);
174+
175+
protected virtual void TransformImage(IHtmlImageElement img, string sourceFile, string sourceDir)
176+
{
177+
if (img.GetAttribute("src") is not string src)
178+
{
179+
return;
180+
}
181+
182+
var parts = new UrlParts(src);
183+
184+
if (TryResolveSrc(parts.File.ToString(), sourceDir, out var result, out var copy))
185+
{
186+
img.SetAttribute("src", $"{result}{parts.Query}{parts.Hash}");
187+
188+
if (copy is (string srcImg, string dstImg))
189+
{
190+
Directory.CreateDirectory(Path.GetDirectoryName(dstImg)!);
191+
File.Copy(srcImg, dstImg, overwrite: true);
192+
}
193+
}
194+
else
195+
{
196+
ReportProblem(sourceFile, result, parts.File.ToString(), img.SourceReference?.Position);
197+
}
198+
}
183199

184200
private static void CleanUp(IHtmlDocument document)
185201
{

converter/generator/UrlParts.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace OriginLab.DocumentGeneration;
2+
3+
internal readonly ref struct UrlParts
4+
{
5+
public ReadOnlySpan<char> File { get; }
6+
public ReadOnlySpan<char> Query { get; }
7+
public ReadOnlySpan<char> Hash { get; }
8+
9+
public UrlParts(string url)
10+
{
11+
var span = url.AsSpan();
12+
int sep;
13+
14+
sep = span.LastIndexOf('#');
15+
if (sep > -1)
16+
{
17+
Hash = span[sep..];
18+
span = span[..sep];
19+
}
20+
21+
sep = span.LastIndexOf('?');
22+
if (sep > -1)
23+
{
24+
Query = span[sep..];
25+
span = span[..sep];
26+
}
27+
28+
File = sep > -1 ? span[..sep] : span;
29+
}
30+
}

0 commit comments

Comments
 (0)