Skip to content

Commit b2e92de

Browse files
committed
rollback multi-line if statement
1 parent f6566a3 commit b2e92de

6 files changed

Lines changed: 49 additions & 7 deletions

File tree

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ MiniWord.SaveAsByTemplate(path, templatePath, value);
237237

238238
### If statement inside template
239239

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

242243
##### Example
243244

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

266-
##### Template
267+
##### Template For Multi Paragraph
268+
269+
![before_if](https://user-images.githubusercontent.com/38832863/220125429-7dd6ce94-35c6-478e-8903-064f9cf9361a.PNG)
270+
271+
##### Result Of Multi Paragraph
272+
273+
![after_if](https://user-images.githubusercontent.com/38832863/220125435-72ea24b4-2412-45de-961a-ad4b2134417b.PNG)
274+
275+
##### Template For Single Paragraph
276+
267277
<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">
268278

269-
##### Result
279+
##### Result Of Single Paragraph
270280

271281
<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">
272282

release-note/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

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

2929
### 0.7.0
3030
- [New] Add support to List inside List via `IEnumerable<MiniWordForeach>` and `{{foreach`/`endforeach}}` tags (via @eynarhaji)

samples/docx/TestIfStatement.docx

408 Bytes
Binary file not shown.

src/MiniWord/MiniWord.Implment.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ private static void Generate(this OpenXmlElement xmlElement, WordprocessingDocum
8585
dic.Add(dicKey, e.Value);
8686
}
8787

88+
ReplaceStatements(newTr, tags: dic);
89+
8890
ReplaceText(newTr, docx, tags: dic);
8991
table.Append(newTr);
9092
}
@@ -95,6 +97,8 @@ private static void Generate(this OpenXmlElement xmlElement, WordprocessingDocum
9597
}
9698
}
9799

100+
ReplaceStatements(xmlElement, tags);
101+
98102
ReplaceText(xmlElement, docx, tags);
99103
}
100104

@@ -490,6 +494,35 @@ private static void ReplaceText(OpenXmlElement xmlElement, WordprocessingDocumen
490494
}
491495
}
492496
}
497+
498+
private static void ReplaceStatements(OpenXmlElement xmlElement, Dictionary<string, object> tags)
499+
{
500+
var paragraphs = xmlElement.Descendants<Paragraph>().ToList();
501+
502+
while (paragraphs.Any(s => s.InnerText.Contains("@if")))
503+
{
504+
var ifIndex = paragraphs.FindIndex(0, s => s.InnerText.Contains("@if"));
505+
var endIfFinalIndex = paragraphs.FindIndex(ifIndex, s => s.InnerText.Contains("@endif"));
506+
507+
var statement = paragraphs[ifIndex].InnerText.Split(' ');
508+
509+
var tagValue = tags[statement[1]];
510+
var checkStatement = statement.Length == 4 ? EvaluateStatement(tagValue.ToString(), statement[2], statement[3]) : !bool.Parse(tagValue.ToString());
511+
512+
if (!checkStatement)
513+
{
514+
for (int i = ifIndex + 1; i <= endIfFinalIndex - 1; i++)
515+
{
516+
paragraphs[i].Remove();
517+
}
518+
}
519+
520+
paragraphs[ifIndex].Remove();
521+
paragraphs[endIfFinalIndex].Remove();
522+
523+
paragraphs = xmlElement.Descendants<Paragraph>().ToList();
524+
}
525+
}
493526

494527
private static string EvaluateIfStatement(string text)
495528
{

src/MiniWord/MiniWord.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>net45;netstandard2.0;net5.0</TargetFrameworks>
4-
<Version>0.7.0</Version>
5-
<Version>0.6.3</Version>
4+
<Version>0.7.1</Version>
65
</PropertyGroup>
76
<PropertyGroup>
87
<AssemblyName>MiniWord</AssemblyName>

tests/MiniWordTests/MiniWordTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net5.0;</TargetFrameworks>
4+
<TargetFrameworks>net5.0;net6.0;</TargetFrameworks>
55

66
<IsPackable>false</IsPackable>
77
</PropertyGroup>

0 commit comments

Comments
 (0)