Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion TriasDev.Templify.Gui/Services/ITemplifyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,26 @@ public interface ITemplifyService
/// </summary>
/// <param name="templatePath">Path to the template file (.docx).</param>
/// <param name="jsonPath">Optional path to JSON data file for validation.</param>
/// <param name="enableHtmlEntityReplacement">Enable HTML entity replacement (e.g., &lt;br&gt; to line break).</param>
/// <returns>Validation result with errors and warnings.</returns>
Task<ValidationResult> ValidateTemplateAsync(string templatePath, string? jsonPath = null);
Task<ValidationResult> ValidateTemplateAsync(
string templatePath,
string? jsonPath = null,
bool enableHtmlEntityReplacement = false);

/// <summary>
/// Processes a template with JSON data and generates output.
/// </summary>
/// <param name="templatePath">Path to the template file (.docx).</param>
/// <param name="jsonPath">Path to JSON data file.</param>
/// <param name="outputPath">Path for the output file.</param>
/// <param name="enableHtmlEntityReplacement">Enable HTML entity replacement (e.g., &lt;br&gt; to line break).</param>
/// <param name="progress">Optional progress reporter.</param>
/// <returns>Processing result with statistics and any errors.</returns>
Task<UiProcessingResult> ProcessTemplateAsync(
string templatePath,
string jsonPath,
string outputPath,
bool enableHtmlEntityReplacement = false,
IProgress<double>? progress = null);
}
15 changes: 11 additions & 4 deletions TriasDev.Templify.Gui/Services/TemplifyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using TriasDev.Templify.Core;
using TriasDev.Templify.Gui.Models;
using TriasDev.Templify.Replacements;
using TriasDev.Templify.Utilities;

namespace TriasDev.Templify.Gui.Services;
Expand All @@ -20,14 +21,18 @@ public class TemplifyService : ITemplifyService
/// <summary>
/// Validates a template file with optional JSON data.
/// </summary>
public async Task<ValidationResult> ValidateTemplateAsync(string templatePath, string? jsonPath = null)
public async Task<ValidationResult> ValidateTemplateAsync(
string templatePath,
string? jsonPath = null,
bool enableHtmlEntityReplacement = false)
{
return await Task.Run(() =>
{
PlaceholderReplacementOptions options = new PlaceholderReplacementOptions
{
MissingVariableBehavior = MissingVariableBehavior.LeaveUnchanged,
Culture = CultureInfo.InvariantCulture
Culture = CultureInfo.InvariantCulture,
TextReplacements = enableHtmlEntityReplacement ? TextReplacements.HtmlEntities : null
};

DocumentTemplateProcessor processor = new DocumentTemplateProcessor(options);
Expand Down Expand Up @@ -57,6 +62,7 @@ public async Task<UiProcessingResult> ProcessTemplateAsync(
string templatePath,
string jsonPath,
string outputPath,
bool enableHtmlEntityReplacement = false,
IProgress<double>? progress = null)
{
return await Task.Run(() =>
Expand All @@ -76,11 +82,12 @@ public async Task<UiProcessingResult> ProcessTemplateAsync(

progress?.Report(0.3);

// Validate template first
// Configure options with optional HTML entity replacement
PlaceholderReplacementOptions options = new PlaceholderReplacementOptions
{
MissingVariableBehavior = MissingVariableBehavior.LeaveUnchanged,
Culture = CultureInfo.InvariantCulture
Culture = CultureInfo.InvariantCulture,
TextReplacements = enableHtmlEntityReplacement ? TextReplacements.HtmlEntities : null
};

DocumentTemplateProcessor processor = new DocumentTemplateProcessor(options);
Expand Down
14 changes: 13 additions & 1 deletion TriasDev.Templify.Gui/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public partial class MainWindowViewModel : ViewModelBase
[ObservableProperty]
private ObservableCollection<string> _results = new();

/// <summary>
/// Gets or sets whether HTML entity replacement is enabled.
/// When enabled, HTML entities like &lt;br&gt;, &amp;nbsp;, etc. are converted
/// to their Word equivalents before processing.
/// </summary>
[ObservableProperty]
private bool _enableHtmlEntityReplacement;

public MainWindowViewModel(
ITemplifyService templifyService,
IFileDialogService fileDialogService)
Expand Down Expand Up @@ -103,7 +111,10 @@ private async Task ValidateTemplateAsync()

try
{
ValidationResult validation = await _templifyService.ValidateTemplateAsync(TemplatePath, JsonPath);
ValidationResult validation = await _templifyService.ValidateTemplateAsync(
TemplatePath,
JsonPath,
EnableHtmlEntityReplacement);

if (validation.IsValid)
{
Expand Down Expand Up @@ -177,6 +188,7 @@ private async Task ProcessTemplateAsync()
TemplatePath,
JsonPath,
OutputPath,
EnableHtmlEntityReplacement,
progressReporter);

if (result.Success)
Expand Down
19 changes: 13 additions & 6 deletions TriasDev.Templify.Gui/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<vm:MainWindowViewModel/>
</Design.DataContext>

<Grid Margin="20" RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto,*,Auto">
<Grid Margin="20" RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto,Auto,*,Auto">

<!-- Title -->
<TextBlock Grid.Row="0"
Expand Down Expand Up @@ -51,7 +51,7 @@
</Grid>

<!-- Output File Selection -->
<Grid Grid.Row="3" ColumnDefinitions="100,*,100" Margin="0,0,0,20">
<Grid Grid.Row="3" ColumnDefinitions="100,*,100" Margin="0,0,0,10">
<TextBlock Grid.Column="0" Text="Output File:" VerticalAlignment="Center"/>
<TextBox Grid.Column="1"
Text="{Binding OutputPath}"
Expand All @@ -63,8 +63,15 @@
Command="{Binding BrowseOutputCommand}"/>
</Grid>

<!-- Options Section -->
<StackPanel Grid.Row="4" Orientation="Horizontal" Margin="0,0,0,20">
<CheckBox Content="Enable HTML Entity Replacement"
IsChecked="{Binding EnableHtmlEntityReplacement}"
ToolTip.Tip="Convert HTML entities like &lt;br&gt;, &amp;nbsp;, &amp;lt;, etc. to Word equivalents"/>
</StackPanel>

<!-- Action Buttons -->
<StackPanel Grid.Row="4" Orientation="Horizontal" Spacing="10" Margin="0,0,0,20">
<StackPanel Grid.Row="5" Orientation="Horizontal" Spacing="10" Margin="0,0,0,20">
<Button Content="Validate Template"
Command="{Binding ValidateTemplateCommand}"
Width="150"/>
Expand All @@ -74,7 +81,7 @@
</StackPanel>

<!-- Status and Progress -->
<StackPanel Grid.Row="5" Margin="0,0,0,10">
<StackPanel Grid.Row="6" Margin="0,0,0,10">
<TextBlock Text="{Binding StatusMessage}"
FontWeight="SemiBold"
Margin="0,0,0,5"/>
Expand All @@ -85,7 +92,7 @@
</StackPanel>

<!-- Results Display -->
<Border Grid.Row="6"
<Border Grid.Row="7"
BorderBrush="{DynamicResource SystemControlForegroundBaseMediumBrush}"
BorderThickness="1"
Padding="10"
Expand All @@ -104,7 +111,7 @@
</Border>

<!-- Bottom Action Buttons -->
<StackPanel Grid.Row="7" Orientation="Horizontal" Spacing="10" HorizontalAlignment="Right">
<StackPanel Grid.Row="8" Orientation="Horizontal" Spacing="10" HorizontalAlignment="Right">
<Button Content="Open Output File"
Command="{Binding OpenOutputFileCommand}"
Width="130"/>
Expand Down
Loading