| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TextTemplatingExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Globalization; |
| | | 10 | | using MyNet.Text.Templating; |
| | | 11 | | |
| | | 12 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 13 | | namespace MyNet.Text; |
| | | 14 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 15 | | |
| | | 16 | | public static class TextTemplatingExtensions |
| | | 17 | | { |
| | | 18 | | extension(TextPipeline pipeline) |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Resolves the template using the provided transform. Transformations are applied in the provided order. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="transform">The template transform to apply.</param> |
| | | 24 | | /// <returns>The transformed TextPipeline instance.</returns> |
| | | 25 | | public TextPipeline ResolveTemplate(ITemplateTransform transform) |
| | | 26 | | { |
| | 0 | 27 | | ArgumentNullException.ThrowIfNull(transform); |
| | 0 | 28 | | return pipeline.Apply(transform); |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Resolves the template using the provided options. Transformations are applied in the provided order. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="options">The template options to apply.</param> |
| | | 35 | | /// <returns>The transformed TextPipeline instance.</returns> |
| | | 36 | | public TextPipeline ResolveTemplate(TextTemplateOptions options) |
| | | 37 | | { |
| | 0 | 38 | | ArgumentNullException.ThrowIfNull(options); |
| | 0 | 39 | | return pipeline.Apply(new TemplateTransform(options)); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Resolves the template using the provided configuration action. Transformations are applied in the provided o |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="configure">The configuration action to apply.</param> |
| | | 46 | | /// <returns>The transformed TextPipeline instance.</returns> |
| | | 47 | | public TextPipeline ResolveTemplate(Action<TextTemplateOptionsBuilder> configure) |
| | | 48 | | { |
| | 0 | 49 | | ArgumentNullException.ThrowIfNull(configure); |
| | 0 | 50 | | return pipeline.Apply(TextTemplating.Create(configure)); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Resolves the template using the provided arguments. Transformations are applied in the provided order. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="arguments">The arguments to apply.</param> |
| | | 57 | | /// <returns>The transformed TextPipeline instance.</returns> |
| | | 58 | | public TextPipeline ResolveTemplate(IEnumerable<KeyValuePair<string, object?>> arguments) |
| | | 59 | | { |
| | 0 | 60 | | ArgumentNullException.ThrowIfNull(arguments); |
| | 0 | 61 | | return pipeline.Apply(TextTemplating.Create(x => x.WithArguments(arguments))); |
| | | 62 | | } |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | extension(string input) |
| | | 66 | | { |
| | | 67 | | /// <summary> |
| | | 68 | | /// Resolves the template using the provided transform. Transformations are applied in the provided order. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="transform">The template transform to apply.</param> |
| | | 71 | | /// <param name="culture">The culture to use for the transformation.</param> |
| | | 72 | | /// <returns>The transformed string.</returns> |
| | | 73 | | public string ResolveTemplate(ITemplateTransform transform, CultureInfo? culture = null) |
| | | 74 | | { |
| | 0 | 75 | | ArgumentNullException.ThrowIfNull(transform); |
| | 0 | 76 | | return transform.Apply(input, culture ?? CultureInfo.CurrentCulture); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Resolves the template using the provided options. Transformations are applied in the provided order. |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="options">The template options to apply.</param> |
| | | 83 | | /// <param name="culture">The culture to use for the transformation.</param> |
| | | 84 | | /// <returns>The transformed string.</returns> |
| | | 85 | | public string ResolveTemplate(TextTemplateOptions options, CultureInfo? culture = null) |
| | | 86 | | { |
| | 0 | 87 | | ArgumentNullException.ThrowIfNull(options); |
| | 0 | 88 | | return new TemplateTransform(options).Apply(input, culture ?? CultureInfo.CurrentCulture); |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Resolves the template using the provided configuration action. Transformations are applied in the provided o |
| | | 93 | | /// </summary> |
| | | 94 | | /// <param name="configure">The configuration action to apply.</param> |
| | | 95 | | /// <param name="culture">The culture to use for the transformation.</param> |
| | | 96 | | /// <returns>The transformed string.</returns> |
| | | 97 | | public string ResolveTemplate(Action<TextTemplateOptionsBuilder> configure, CultureInfo? culture = null) |
| | | 98 | | { |
| | 0 | 99 | | ArgumentNullException.ThrowIfNull(configure); |
| | 0 | 100 | | return TextTemplating.Create(configure).Apply(input, culture ?? CultureInfo.CurrentCulture); |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Resolves the template using the provided arguments. Transformations are applied in the provided order. |
| | | 105 | | /// </summary> |
| | | 106 | | /// <param name="arguments">The arguments to apply.</param> |
| | | 107 | | /// <param name="culture">The culture to use for the transformation.</param> |
| | | 108 | | /// <returns>The transformed string.</returns> |
| | | 109 | | public string ResolveTemplate(IEnumerable<KeyValuePair<string, object?>> arguments, CultureInfo? culture = null) |
| | | 110 | | { |
| | 0 | 111 | | ArgumentNullException.ThrowIfNull(arguments); |
| | 0 | 112 | | return TextTemplating.Create(x => x.WithArguments(arguments)).Apply(input, culture ?? CultureInfo.CurrentCul |
| | | 113 | | } |
| | | 114 | | } |
| | | 115 | | } |
| | | 116 | | |