Skip to content

Commit

Permalink
rollback multi-line if statement
Browse files Browse the repository at this point in the history
  • Loading branch information
eynarhaji committed Aug 11, 2023
1 parent f6566a3 commit b2e92de
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 7 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ MiniWord.SaveAsByTemplate(path, templatePath, value);

### If statement inside template

Adding `{{if` and `endif}}` tags to template is required.
For multip paragraph, use @if and @endif tags.
For single paragraph and inside foreach, use `{{if` and `endif}}` tags to template is required.

##### Example

Expand All @@ -263,10 +264,19 @@ var value = new Dictionary<string, object>()
MiniWord.SaveAsByTemplate(path, templatePath, value);
```

##### Template
##### Template For Multi Paragraph

![before_if](https://user-images.githubusercontent.com/38832863/220125429-7dd6ce94-35c6-478e-8903-064f9cf9361a.PNG)

##### Result Of Multi Paragraph

![after_if](https://user-images.githubusercontent.com/38832863/220125435-72ea24b4-2412-45de-961a-ad4b2134417b.PNG)

##### Template For Single Paragraph

<img width="931" alt="Screenshot 2023-08-08 at 17 55 46" src="https://github.com/mini-software/MiniWord/assets/38832863/2adea468-a9c1-422f-a270-167086bc4ba3">

##### Result
##### Result Of Single Paragraph

<img width="536" alt="Screenshot 2023-08-08 at 17 56 47" src="https://github.com/mini-software/MiniWord/assets/38832863/01f71c0f-eee0-4189-8510-abe063126514">

Expand Down
2 changes: 1 addition & 1 deletion release-note/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

### 0.7.1
- [New] Add support to if statement inside foreach statement inside templates. Please refer to samples. (via @eynarhaji)
- [Breaking change] Change tags for if statements from @if to {{if and @endif to endif}} inside templates. Please refer to samples. (via @eynarhaji)
- [New] Change tags for if statements for single paragraph if statement {{if and endif}} inside templates. Please refer to samples. (via @eynarhaji)

### 0.7.0
- [New] Add support to List inside List via `IEnumerable<MiniWordForeach>` and `{{foreach`/`endforeach}}` tags (via @eynarhaji)
Expand Down
Binary file modified samples/docx/TestIfStatement.docx
Binary file not shown.
33 changes: 33 additions & 0 deletions src/MiniWord/MiniWord.Implment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ private static void Generate(this OpenXmlElement xmlElement, WordprocessingDocum
dic.Add(dicKey, e.Value);
}

ReplaceStatements(newTr, tags: dic);

ReplaceText(newTr, docx, tags: dic);
table.Append(newTr);
}
Expand All @@ -95,6 +97,8 @@ private static void Generate(this OpenXmlElement xmlElement, WordprocessingDocum
}
}

ReplaceStatements(xmlElement, tags);

ReplaceText(xmlElement, docx, tags);
}

Expand Down Expand Up @@ -490,6 +494,35 @@ private static void ReplaceText(OpenXmlElement xmlElement, WordprocessingDocumen
}
}
}

private static void ReplaceStatements(OpenXmlElement xmlElement, Dictionary<string, object> tags)
{
var paragraphs = xmlElement.Descendants<Paragraph>().ToList();

while (paragraphs.Any(s => s.InnerText.Contains("@if")))
{
var ifIndex = paragraphs.FindIndex(0, s => s.InnerText.Contains("@if"));
var endIfFinalIndex = paragraphs.FindIndex(ifIndex, s => s.InnerText.Contains("@endif"));

var statement = paragraphs[ifIndex].InnerText.Split(' ');

var tagValue = tags[statement[1]];
var checkStatement = statement.Length == 4 ? EvaluateStatement(tagValue.ToString(), statement[2], statement[3]) : !bool.Parse(tagValue.ToString());

if (!checkStatement)
{
for (int i = ifIndex + 1; i <= endIfFinalIndex - 1; i++)
{
paragraphs[i].Remove();
}
}

paragraphs[ifIndex].Remove();
paragraphs[endIfFinalIndex].Remove();

paragraphs = xmlElement.Descendants<Paragraph>().ToList();
}
}

private static string EvaluateIfStatement(string text)
{
Expand Down
3 changes: 1 addition & 2 deletions src/MiniWord/MiniWord.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net45;netstandard2.0;net5.0</TargetFrameworks>
<Version>0.7.0</Version>
<Version>0.6.3</Version>
<Version>0.7.1</Version>
</PropertyGroup>
<PropertyGroup>
<AssemblyName>MiniWord</AssemblyName>
Expand Down
2 changes: 1 addition & 1 deletion tests/MiniWordTests/MiniWordTests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net5.0;</TargetFrameworks>
<TargetFrameworks>net5.0;net6.0;</TargetFrameworks>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down

0 comments on commit b2e92de

Please sign in to comment.