Skip to content

Commit 9e5d96d

Browse files
committed
Use action pattern
1 parent 62963b1 commit 9e5d96d

File tree

5 files changed

+87
-88
lines changed

5 files changed

+87
-88
lines changed

src/Elastic.Markdown/DocumentationGenerator.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -347,13 +347,10 @@ public async Task<string> RenderLlmMarkdown(MarkdownFile markdown, Cancel ctx)
347347
{
348348
await DocumentationSet.Tree.Resolve(ctx);
349349
var document = await markdown.ParseFullAsync(ctx);
350-
var subscription = DocumentationObjectPoolProvider.LlmMarkdownRendererPool.Get();
351-
subscription.SetBuildContext(DocumentationSet.Context);
352-
var renderer = subscription.LlmMarkdownRenderer;
353-
_ = renderer.Render(document);
354-
var result = subscription.RentedStringBuilder!.ToString().Trim();
355-
DocumentationObjectPoolProvider.LlmMarkdownRendererPool.Return(subscription);
356-
return result;
350+
return DocumentationObjectPoolProvider.UseLlmMarkdownRenderer(DocumentationSet.Context, renderer =>
351+
{
352+
_ = renderer.Render(document);
353+
}).Trim();
357354
}
358355

359356
public async Task<RenderResult> RenderLayout(MarkdownFile markdown, Cancel ctx)

src/Elastic.Markdown/Exporters/LlmMarkdownExporter.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,11 @@ await fileContext.SourceFile.SourceFile.FileSystem.File.WriteAllTextAsync(
5959
return true;
6060
}
6161

62-
public static string ConvertToLlmMarkdown(MarkdownDocument document, MarkdownExportFileContext context)
63-
{
64-
var subscription = DocumentationObjectPoolProvider.LlmMarkdownRendererPool.Get();
65-
subscription.SetBuildContext(context.BuildContext!);
66-
var renderer = subscription.LlmMarkdownRenderer;
67-
_ = renderer.Render(document);
68-
var result = subscription.RentedStringBuilder!.ToString();
69-
DocumentationObjectPoolProvider.LlmMarkdownRendererPool.Return(subscription);
70-
return result;
71-
}
62+
public static string ConvertToLlmMarkdown(MarkdownDocument document, MarkdownExportFileContext context) =>
63+
DocumentationObjectPoolProvider.UseLlmMarkdownRenderer(context.BuildContext!, renderer =>
64+
{
65+
_ = renderer.Render(document);
66+
});
7267

7368
private static IFileInfo GetLlmOutputFile(MarkdownExportFileContext fileContext)
7469
{

src/Elastic.Markdown/Helpers/DocumentationObjectPoolProvider.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,22 @@ internal static class DocumentationObjectPoolProvider
1919
public static readonly ObjectPool<StringBuilder> StringBuilderPool = PoolProvider.CreateStringBuilderPool(256, 4 * 1024);
2020
public static readonly ObjectPool<ReusableStringWriter> StringWriterPool = PoolProvider.Create(new ReusableStringWriterPooledObjectPolicy());
2121
public static readonly ObjectPool<HtmlRenderSubscription> HtmlRendererPool = PoolProvider.Create(new HtmlRendererPooledObjectPolicy());
22-
public static readonly ObjectPool<LlmMarkdownRenderSubscription> LlmMarkdownRendererPool = PoolProvider.Create(new LlmMarkdownRendererPooledObjectPolicy());
22+
private static readonly ObjectPool<LlmMarkdownRenderSubscription> LlmMarkdownRendererPool = PoolProvider.Create(new LlmMarkdownRendererPooledObjectPolicy());
2323

24+
public static string UseLlmMarkdownRenderer(BuildContext buildContext, Action<LlmMarkdownRenderer> action)
25+
{
26+
var subscription = LlmMarkdownRendererPool.Get();
27+
subscription.SetBuildContext(buildContext);
28+
try
29+
{
30+
action(subscription.LlmMarkdownRenderer);
31+
return subscription.RentedStringBuilder!.ToString();
32+
}
33+
finally
34+
{
35+
LlmMarkdownRendererPool.Return(subscription);
36+
}
37+
}
2438

2539
private sealed class ReusableStringWriterPooledObjectPolicy : IPooledObjectPolicy<ReusableStringWriter>
2640
{
@@ -39,14 +53,6 @@ public sealed class HtmlRenderSubscription
3953
public StringBuilder? RentedStringBuilder { get; internal set; }
4054
}
4155

42-
public sealed class LlmMarkdownRenderSubscription
43-
{
44-
public required LlmMarkdownRenderer LlmMarkdownRenderer { get; init; }
45-
public StringBuilder? RentedStringBuilder { get; internal set; }
46-
47-
public void SetBuildContext(BuildContext buildContext) => LlmMarkdownRenderer.BuildContext = buildContext;
48-
}
49-
5056
private sealed class HtmlRendererPooledObjectPolicy : IPooledObjectPolicy<HtmlRenderSubscription>
5157
{
5258
public HtmlRenderSubscription Create()
@@ -81,6 +87,14 @@ public bool Return(HtmlRenderSubscription subscription)
8187
}
8288
}
8389

90+
private sealed class LlmMarkdownRenderSubscription
91+
{
92+
public required LlmMarkdownRenderer LlmMarkdownRenderer { get; init; }
93+
public StringBuilder? RentedStringBuilder { get; internal set; }
94+
95+
public void SetBuildContext(BuildContext buildContext) => LlmMarkdownRenderer.BuildContext = buildContext;
96+
}
97+
8498
private sealed class LlmMarkdownRendererPooledObjectPolicy : IPooledObjectPolicy<LlmMarkdownRenderSubscription>
8599
{
86100
public LlmMarkdownRenderSubscription Create()
@@ -90,7 +104,7 @@ public LlmMarkdownRenderSubscription Create()
90104
stringWriter.SetStringBuilder(stringBuilder);
91105
var renderer = new LlmMarkdownRenderer(stringWriter)
92106
{
93-
BuildContext = null
107+
BuildContext = null!
94108
};
95109
return new LlmMarkdownRenderSubscription { LlmMarkdownRenderer = renderer, RentedStringBuilder = stringBuilder };
96110
}

src/Elastic.Markdown/Myst/Renderers/LlmMarkdown/LlmBlockRenderers.cs

Lines changed: 53 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public static class LlmRenderingHelpers
2323
{
2424
public static void RenderBlockWithIndentation(LlmMarkdownRenderer renderer, MarkdownObject block, string indentation = " ")
2525
{
26-
var subscription = DocumentationObjectPoolProvider.LlmMarkdownRendererPool.Get();
27-
subscription.SetBuildContext(renderer.BuildContext!);
28-
var tempRenderer = subscription.LlmMarkdownRenderer;
29-
_ = tempRenderer.Render(block);
30-
var content = subscription.RentedStringBuilder!.ToString();
26+
var content = DocumentationObjectPoolProvider.UseLlmMarkdownRenderer(renderer.BuildContext, tempRenderer =>
27+
{
28+
_ = tempRenderer.Render(block);
29+
});
30+
3131
if (string.IsNullOrEmpty(content))
3232
return;
3333
var lines = content.Split(['\n', '\r'], StringSplitOptions.RemoveEmptyEntries);
@@ -36,7 +36,6 @@ public static void RenderBlockWithIndentation(LlmMarkdownRenderer renderer, Mark
3636
renderer.Write(indentation);
3737
renderer.WriteLine(line);
3838
}
39-
DocumentationObjectPoolProvider.LlmMarkdownRendererPool.Return(subscription);
4039
}
4140

4241
/// <summary>
@@ -46,7 +45,7 @@ public static void RenderBlockWithIndentation(LlmMarkdownRenderer renderer, Mark
4645
{
4746
if (
4847
string.IsNullOrEmpty(url)
49-
|| renderer.BuildContext!.CanonicalBaseUrl == null
48+
|| renderer.BuildContext.CanonicalBaseUrl == null
5049
|| Uri.IsWellFormedUriString(url, UriKind.Absolute)
5150
|| !Uri.IsWellFormedUriString(url, UriKind.Relative))
5251
return url;
@@ -191,11 +190,10 @@ private static string GetContinuationIndent(string baseIndent, bool isOrdered) =
191190

192191
private static void RenderBlockWithIndentation(LlmMarkdownRenderer renderer, Block block, string baseIndent, bool isOrdered)
193192
{
194-
var subscription = DocumentationObjectPoolProvider.LlmMarkdownRendererPool.Get();
195-
subscription.SetBuildContext(renderer.BuildContext!);
196-
var tempRenderer = subscription.LlmMarkdownRenderer;
197-
_ = tempRenderer.Render(block);
198-
var blockOutput = subscription.RentedStringBuilder!.ToString();
193+
var blockOutput = DocumentationObjectPoolProvider.UseLlmMarkdownRenderer(renderer.BuildContext, tempRenderer =>
194+
{
195+
_ = tempRenderer.Render(block);
196+
});
199197

200198
var continuationIndent = GetContinuationIndent(baseIndent, isOrdered);
201199
var lines = blockOutput.Split('\n');
@@ -213,7 +211,6 @@ private static void RenderBlockWithIndentation(LlmMarkdownRenderer renderer, Blo
213211
else if (i < lines.Length - 1)
214212
renderer.WriteLine();
215213
}
216-
DocumentationObjectPoolProvider.LlmMarkdownRendererPool.Return(subscription);
217214
}
218215

219216
/// <summary>
@@ -290,19 +287,19 @@ protected override void Write(LlmMarkdownRenderer renderer, Table table)
290287
renderer.Writer.Write(" ");
291288

292289
// Capture cell content
293-
var subscription = DocumentationObjectPoolProvider.LlmMarkdownRendererPool.Get();
294-
subscription.SetBuildContext(renderer.BuildContext!);
295-
var tempRenderer = subscription.LlmMarkdownRenderer;
296-
// Render cell content to temporary writer
297-
foreach (var inline in cell.Descendants().OfType<Inline>())
298-
tempRenderer.Write(inline);
290+
var content = DocumentationObjectPoolProvider.UseLlmMarkdownRenderer(renderer.BuildContext, tempRenderer =>
291+
{
292+
// Render cell content to temporary writer
293+
foreach (var inline in cell.Descendants().OfType<Inline>())
294+
{
295+
tempRenderer.Write(inline);
296+
}
297+
});
299298

300299
// Write padded content
301-
var content = subscription.RentedStringBuilder!.ToString();
302300
renderer.Writer.Write(content.PadRight(columnWidths[cellIndex]));
303301
renderer.Writer.Write(" |");
304302
cellIndex++;
305-
DocumentationObjectPoolProvider.LlmMarkdownRendererPool.Return(subscription);
306303
}
307304

308305
renderer.WriteLine();
@@ -328,19 +325,19 @@ protected override void Write(LlmMarkdownRenderer renderer, Table table)
328325
renderer.Writer.Write(" ");
329326

330327
// Capture cell content
331-
var subscription = DocumentationObjectPoolProvider.LlmMarkdownRendererPool.Get();
332-
subscription.SetBuildContext(renderer.BuildContext!);
333-
var tempRenderer = subscription.LlmMarkdownRenderer;
334-
// Render cell content to temporary writer
335-
foreach (var inline in cell.Descendants().OfType<Inline>())
336-
tempRenderer.Write(inline);
328+
var content = DocumentationObjectPoolProvider.UseLlmMarkdownRenderer(renderer.BuildContext, tempRenderer =>
329+
{
330+
// Render cell content to temporary writer
331+
foreach (var inline in cell.Descendants().OfType<Inline>())
332+
{
333+
tempRenderer.Write(inline);
334+
}
335+
});
337336

338337
// Write padded content
339-
var content = subscription.RentedStringBuilder!.ToString();
340338
renderer.Writer.Write(content.PadRight(columnWidths[cellIndex]));
341339
renderer.Writer.Write(" |");
342340
cellIndex++;
343-
DocumentationObjectPoolProvider.LlmMarkdownRendererPool.Return(subscription);
344341
}
345342

346343
renderer.WriteLine();
@@ -367,20 +364,18 @@ private static int[] CalculateColumnWidths(LlmMarkdownRenderer renderer, Table t
367364
foreach (var cell in row.Cast<TableCell>())
368365
{
369366
// Capture cell content
370-
var subscription = DocumentationObjectPoolProvider.LlmMarkdownRendererPool.Get();
371-
subscription.SetBuildContext(renderer.BuildContext!);
372-
var tempRenderer = subscription.LlmMarkdownRenderer;
373-
// Render cell content to temporary writer
374-
foreach (var inline in cell.Descendants().OfType<Inline>())
367+
var content = DocumentationObjectPoolProvider.UseLlmMarkdownRenderer(renderer.BuildContext, tempRenderer =>
375368
{
376-
tempRenderer.Write(inline);
377-
}
369+
// Render cell content to temporary writer
370+
foreach (var inline in cell.Descendants().OfType<Inline>())
371+
{
372+
tempRenderer.Write(inline);
373+
}
374+
});
378375

379376
// Update width if this cell is wider
380-
var content = subscription.RentedStringBuilder!.ToString();
381377
widths[cellIndex] = Math.Max(widths[cellIndex], content.Length);
382378
cellIndex++;
383-
DocumentationObjectPoolProvider.LlmMarkdownRendererPool.Return(subscription);
384379
}
385380
}
386381

@@ -450,7 +445,7 @@ private void WriteIncludeBlock(LlmMarkdownRenderer renderer, IncludeBlock block)
450445
{
451446
if (!block.Found || block.IncludePath is null)
452447
{
453-
renderer.BuildContext!.Collector.EmitError(block.IncludePath ?? string.Empty, "File not found or invalid path");
448+
renderer.BuildContext.Collector.EmitError(block.IncludePath ?? string.Empty, "File not found or invalid path");
454449
return;
455450
}
456451

@@ -460,7 +455,7 @@ private void WriteIncludeBlock(LlmMarkdownRenderer renderer, IncludeBlock block)
460455
var snippet = block.Build.ReadFileSystem.FileInfo.New(block.IncludePath);
461456
if (!snippet.Exists)
462457
{
463-
renderer.BuildContext!.Collector.EmitError(block.IncludePath ?? string.Empty, "File not found or invalid path");
458+
renderer.BuildContext.Collector.EmitError(block.IncludePath ?? string.Empty, "File not found or invalid path");
464459
return;
465460
}
466461

@@ -489,7 +484,7 @@ private void WriteIncludeBlock(LlmMarkdownRenderer renderer, IncludeBlock block)
489484
}
490485
catch (Exception ex)
491486
{
492-
renderer.BuildContext!.Collector.EmitError(block.IncludePath ?? string.Empty, "Failed to parse included content", ex);
487+
renderer.BuildContext.Collector.EmitError(block.IncludePath ?? string.Empty, "Failed to parse included content", ex);
493488
}
494489
}
495490

@@ -499,25 +494,24 @@ private void WriteIncludeBlock(LlmMarkdownRenderer renderer, IncludeBlock block)
499494
private static void WriteChildrenWithIndentation(LlmMarkdownRenderer renderer, Block container, string indent)
500495
{
501496
// Capture output and manually add indentation
502-
var subscription = DocumentationObjectPoolProvider.LlmMarkdownRendererPool.Get();
503-
subscription.SetBuildContext(renderer.BuildContext!);
504-
var tempRenderer = subscription.LlmMarkdownRenderer;
505-
switch (container)
497+
var content = DocumentationObjectPoolProvider.UseLlmMarkdownRenderer(renderer.BuildContext, tempRenderer =>
506498
{
507-
case ContainerBlock containerBlock:
508-
tempRenderer.WriteChildren(containerBlock);
509-
break;
510-
case LeafBlock leafBlock:
511-
tempRenderer.WriteLeafInline(leafBlock);
512-
break;
513-
}
514-
var content = subscription.RentedStringBuilder!.ToString();
499+
switch (container)
500+
{
501+
case ContainerBlock containerBlock:
502+
tempRenderer.WriteChildren(containerBlock);
503+
break;
504+
case LeafBlock leafBlock:
505+
tempRenderer.WriteLeafInline(leafBlock);
506+
break;
507+
}
508+
});
509+
515510
if (string.IsNullOrEmpty(content))
516511
return;
517512
var reader = new StringReader(content);
518513
while (reader.ReadLine() is { } line)
519514
renderer.WriteLine(string.IsNullOrWhiteSpace(line) ? string.Empty : indent + line);
520-
DocumentationObjectPoolProvider.LlmMarkdownRendererPool.Return(subscription);
521515
}
522516
}
523517

@@ -544,12 +538,11 @@ protected override void Write(LlmMarkdownRenderer renderer, DefinitionItem obj)
544538

545539
private static string GetPlainTextFromLeafBlock(LlmMarkdownRenderer renderer, LeafBlock leafBlock)
546540
{
547-
var subscription = DocumentationObjectPoolProvider.LlmMarkdownRendererPool.Get();
548-
subscription.SetBuildContext(renderer.BuildContext!);
549-
var tempRenderer = subscription.LlmMarkdownRenderer;
550-
tempRenderer.WriteLeafInline(leafBlock);
551-
var markdownText = subscription.RentedStringBuilder!.ToString();
552-
DocumentationObjectPoolProvider.LlmMarkdownRendererPool.Return(subscription);
541+
var markdownText = DocumentationObjectPoolProvider.UseLlmMarkdownRenderer(renderer.BuildContext, tempRenderer =>
542+
{
543+
tempRenderer.WriteLeafInline(leafBlock);
544+
});
545+
553546
return markdownText.StripMarkdown();
554547
}
555548
}

src/Elastic.Markdown/Myst/Renderers/LlmMarkdown/LlmMarkdownRenderer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Elastic.Markdown.Myst.Renderers.LlmMarkdown;
1313
/// </summary>
1414
public class LlmMarkdownRenderer : TextRendererBase
1515
{
16-
public required BuildContext? BuildContext { get; set; }
16+
public required BuildContext BuildContext { get; set; }
1717
private bool _isAtLineStart = true;
1818

1919
/// <summary>

0 commit comments

Comments
 (0)