-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
183 lines (156 loc) · 6.34 KB
/
Program.cs
File metadata and controls
183 lines (156 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection.Metadata;
using System.Threading.Channels;
using Xceed.Document.NET;
using Xceed.Words.NET;
namespace ConsoleApp2
{
internal class Program
{
static string STUFFED_STRING = "[_]";
static void Main(string[] args)
{
/*
* *
* Neste exemplo foi inserido uma sentença com duas palavras delimitados pela tag 'strong', esse delimitador STUFFED_STRING garante que a expressão será analisada dentro do requerimento da função
* posteriormente o mesmo delimitador STUFFED_STRING é removido para a impressão da sentença
*/
string frase = "Nas <strong>culturas[_]africanas</strong>, o <i>zimbro</i> africano é considerado <b>sagrado</b> e usado em rituais religiosos e cerimônias de <em>purificação</em>.";
var obj = new ChangeText();
var changes = obj.CreateChanges(frase);
/*
* Lista as possiveis mudanças
*/
foreach (ChangeWords item in changes)
{
Console.WriteLine($"palavra : {item.Text} tipo: {item.Type}");
}
/*
* Executa as substituições
*/
using (var document = DocX.Create("NewDocument.docx"))
{
var p = document.InsertParagraph();
/*
* Remove todas as tags
*/
frase = obj.ReplaceTag(new string[] { "b", "strong", "i", "em" }, frase);
/*
* Remove o delimitador, neste momento, a string ja foi apendada ao documento
*/
p.Append(frase.Replace(STUFFED_STRING, " "));
foreach (ChangeWords item in changes)
{
Formatting formatting = new Formatting();
formatting.Bold = true;
var options = new StringReplaceTextOptions
{
SearchValue = item.Text.Replace(STUFFED_STRING, " "),
NewValue = item.Text.Replace(STUFFED_STRING, " "),
NewFormatting = new Formatting()
{
Bold = (item.Type == 1 || item.Type == 3 || item.Type == 4) ? true : false,
Italic = item.Type == 2 ? true : false,
FontColor = item.Type == 4 ? Xceed.Drawing.Color.Blue : Xceed.Drawing.Color.Black
}
};
/*
* Executa a substituição
*/
document.ReplaceText(options);
}
document.Save();
}
Console.WriteLine("Pressione uma tecla qualquer para continuar");
Console.ReadKey();
}
}
/// <summary>
/// Retêm os verbetes com marcação especial
/// </summary>
public class ChangeWords
{
/// <summary>
/// Verbete
/// </summary>
public string Text { get; set; } = "";
/// <summary>
/// Tipo
/// </summary>
/// <list type="=bullet">
///<listheader>
///<term>Tipo</term>
///<description>Descrição</description>
///</listheader>
///<item><term>1</term><description>Bold</description></item>
///<item><term>2</term><description>Itálico</description></item>
///<item><term>3</term><description>Strong</description></item>
///<item><term>4</term><description>Enfático</description></item>
///</list>
public byte Type { get; set; } = 0;
}
/// <summary>
/// Retem o processamento de substituição de Tags
/// </summary>
public class ChangeText
{
/// <summary>
/// Anota as Tag a serem substituidas de acordo com o seu Tipo
/// </summary>
List<ChangeWords> Changes { get; set; } = new List<ChangeWords>();
/// <summary>
/// Efetua o parse de cada tag envolvida e anota para substituição
/// </summary>
/// <param name="frase">String contendo as marcações</param>
/// <returns>List of ChangeWords</returns>
public List<ChangeWords> CreateChanges(string frase)
{
string[] palavras = frase.Split(' ');
foreach (string palavra in palavras)
{
if (palavra.Contains("<b>") && palavra.Contains("</b>"))
this.Changes.Add(new ChangeWords { Text = ReplaceTag("b", palavra), Type = 1 });
if (palavra.Contains("<i>") && palavra.Contains("</i>"))
this.Changes.Add(new ChangeWords { Text = ReplaceTag("i", palavra), Type = 2 });
if (palavra.Contains("<strong>") && palavra.Contains("</strong>"))
this.Changes.Add(new ChangeWords { Text = ReplaceTag("strong", palavra), Type = 3 });
if (palavra.Contains("<em>") && palavra.Contains("</em>"))
this.Changes.Add(new ChangeWords { Text = ReplaceTag("em", palavra), Type = 4 });
}
return this.Changes;
}
/// <summary>
/// Efetua a substituilçao de uma array de Tags
/// </summary>
/// <param name="tag">Array de Tags</param>
/// <param name="text">String alvo de substituição</param>
/// <returns>String com substituições</returns>
public string ReplaceTag(string[] tag, string text)
{
if (tag.Length > 0)
{
foreach (string s in tag)
{
text = ReplaceTag(s, text);
}
}
return text;
}
/// <summary>
/// Efetua a substituilçao de uma Tag
/// </summary>
/// <param name="tag">Tag</param>
/// <param name="text">String alvo de substituição</param>
/// <returns>String com substituições</returns>
public string ReplaceTag(string tag, string text)
{
if (!string.IsNullOrEmpty(tag))
{
text = text.Replace(string.Format("<{0}>", tag), "");
text = text.Replace(string.Format("</{0}>", tag), "");
}
return text;
}
}
}