1: public void ParseText (ref string text)
2: {
3: if (string.IsNullOrEmpty (text))
4: {
5: return;
6: }
7:
8: if ((!ContainsOpenTag (text)) && (!ContainsCloseTag (text)))
9: {
10: // if there's no formatting, then just return the whole string as the only TextBlock Run
11: AddNewRun (text);
12: text = string.Empty;
13: return;
14: }
15:
16: if ((ContainsOpenTag (text)) && (!ContainsCloseTag (text)))
17: {
18: AddNewRun (text);
19: text = string.Empty;
20: return;
21: }
22:
23: int closeTagPosition;
24:
25: if ((!ContainsOpenTag (text)) && (ContainsCloseTag (text)))
26: {
27: closeTagPosition = FindFirstCloseTagPosition (text);
28:
29: var ft = GetFirstCloseTagType (text);
30:
31: if (((ft == FormattingType.Underline) && (_underlinePass))
32: || ((ft == FormattingType.Quote) && (_quotePass))
33: || ((ft == FormattingType.Bold) && (_fontWeight != FontWeights.Normal))
34: || ((ft == FormattingType.Italics) && (_fontStyle != FontStyles.Normal))
35: )
36: {
37: AddNewRun (text.Substring (0, closeTagPosition));
38: }
39: else
40: {
41: AddNewRun (text.Substring (0, closeTagPosition + 4)); // corresponding open tag not being processed, so output closing tag
42: }
43:
44: text = text.Substring (closeTagPosition + 4);
45:
46: return;
47: }
48:
49: // find any untagged text from the start of the string to the first open tag
50: var openTagPosition = FindFirstOpenTagPosition (text);
51: if (openTagPosition > 0)
52: {
53: AddNewRun (text.Substring (0, openTagPosition));
54: text = text.Substring (openTagPosition);
55: }
56:
57: openTagPosition = 0;
58:
59: var openTag = text.Substring (0, 3); // should be guaranteed to always have an opening tag as the first characters
60:
61: if (openTag == _openTagCollection[(int) FormattingType.Bold])
62: {
63: var foundClosingTag = true; // assume there is a corresponding closing tag for now
64:
65: closeTagPosition = text.IndexOf (_closeTagCollection[(int) FormattingType.Bold], openTagPosition + 3); // find the corresponding closing tag
66:
67: // only display with formatting if there is a corresponding closing tag
68: if (closeTagPosition != -1)
69: {
70: _fontWeight = FontWeights.Bold;
71: }
72:
73: if (closeTagPosition == -1)
74: {
75: foundClosingTag = false;
76:
77: // use end of string as closing tag location if we can't find one
78: closeTagPosition = text.Length;
79: }
80:
81: // look for nested open tag
82: if (ContainsOpenTag (text.Substring (openTagPosition + 3, closeTagPosition - openTagPosition - 3)))
83: {
84: if (foundClosingTag)
85: {
86: text = text.Substring (openTagPosition + 3);
87: }
88: else
89: {
90: // get the position of the nested open tag
91: var nestedOpenTagPosition = FindFirstOpenTagPosition (text.Substring (openTagPosition + 3));
92: AddNewRun (text.Substring (openTagPosition, nestedOpenTagPosition - openTagPosition + 3)); // add the section up to the nested open tag
93: text = text.Substring (nestedOpenTagPosition + 3);
94: }
95:
96: ParseText (ref text);
97: }
98:
99: if ((closeTagPosition = text.IndexOf (_closeTagCollection[(int) FormattingType.Bold], openTagPosition)) != -1)
100: {
101: text = GenerateAndAddNewRun (text, openTagPosition, closeTagPosition, _openTagCollection[(int) FormattingType.Bold]);
102: _fontWeight = FontWeights.Normal;
103: ParseText (ref text);
104: }
105:
106: _fontWeight = FontWeights.Normal;
107: }
108: else if (openTag == _openTagCollection[(int) FormattingType.Italics])
109: {
110: var foundClosingTag = true; // assume there is a corresponding closing tag for now
111:
112: closeTagPosition = text.IndexOf (_closeTagCollection[(int) FormattingType.Italics], openTagPosition + 3); // find the corresponding closing tag
113:
114: // only display with formatting if there is a corresponding closing tag
115: if (closeTagPosition != -1)
116: {
117: _fontStyle = FontStyles.Italic;
118: }
119:
120: if (closeTagPosition == -1)
121: {
122: foundClosingTag = false;
123:
124: // use end of string as closing tag location if we can't find one
125: closeTagPosition = text.Length;
126: }
127:
128: // look for nested open tag
129: if (ContainsOpenTag (text.Substring (openTagPosition + 3, closeTagPosition - openTagPosition - 3)))
130: {
131: if (foundClosingTag)
132: {
133: text = text.Substring (openTagPosition + 3);
134: }
135: else
136: {
137: // get the position of the nested open tag
138: var nestedOpenTagPosition = FindFirstOpenTagPosition (text.Substring (openTagPosition + 3));
139: AddNewRun (text.Substring (openTagPosition, nestedOpenTagPosition - openTagPosition + 3)); // add the section up to the nested open tag
140: text = text.Substring (nestedOpenTagPosition + 3);
141: }
142:
143: ParseText (ref text);
144: }
145:
146: if ((closeTagPosition = text.IndexOf (_closeTagCollection[(int) FormattingType.Italics], openTagPosition)) != -1)
147: {
148: text = GenerateAndAddNewRun (text, openTagPosition, closeTagPosition, _openTagCollection[(int) FormattingType.Italics]);
149: _fontStyle = FontStyles.Normal;
150: ParseText (ref text);
151: }
152:
153: _fontStyle = FontStyles.Normal;
154: }
155: else if (openTag == _openTagCollection[(int) FormattingType.Underline])
156: {
157: var foundClosingTag = true; // assume there is a corresponding closing tag for now
158:
159: closeTagPosition = text.IndexOf (_closeTagCollection[(int) FormattingType.Underline], openTagPosition + 3); // find the corresponding closing tag
160:
161: // only display with formatting if there is a corresponding closing tag
162: if (closeTagPosition != -1)
163: {
164: _underlinePass = true;
165: }
166:
167: if (closeTagPosition == -1)
168: {
169: foundClosingTag = false;
170:
171: // use end of string as closing tag location if we can't find one
172: closeTagPosition = text.Length;
173: }
174:
175: // look for nested open tag
176: if (ContainsOpenTag (text.Substring (openTagPosition + 3, closeTagPosition - openTagPosition - 3)))
177: {
178: if (foundClosingTag)
179: {
180: text = text.Substring (openTagPosition + 3);
181: }
182: else
183: {
184: // get the position of the nested open tag
185: var nestedOpenTagPosition = FindFirstOpenTagPosition (text.Substring (openTagPosition + 3));
186: AddNewRun (text.Substring (openTagPosition, nestedOpenTagPosition - openTagPosition + 3)); // add the section up to the nested open tag
187: text = text.Substring (nestedOpenTagPosition + 3);
188: }
189:
190: ParseText (ref text);
191: }
192:
193: if ((closeTagPosition = text.IndexOf (_closeTagCollection[(int) FormattingType.Underline], openTagPosition)) != -1)
194: {
195: text = GenerateAndAddNewRun (text, openTagPosition, closeTagPosition, _openTagCollection[(int) FormattingType.Underline]);
196: _underlinePass = false;
197: ParseText (ref text);
198: }
199:
200: _underlinePass = false;
201: }
202: else if (openTag == _openTagCollection[(int) FormattingType.Quote])
203: {
204: var foundClosingTag = true; // assume there is a corresponding closing tag for now
205:
206: closeTagPosition = text.IndexOf (_closeTagCollection[(int) FormattingType.Quote], openTagPosition + 3); // find the corresponding closing tag
207:
208: // only display with formatting if there is a corresponding closing tag
209: if (closeTagPosition != -1)
210: {
211: _quotePass = true;
212: }
213:
214: if (closeTagPosition == -1)
215: {
216: foundClosingTag = false;
217:
218: // use end of string as closing tag location if we can't find one
219: closeTagPosition = text.Length;
220: }
221:
222: // look for nested open tag
223: if (ContainsOpenTag (text.Substring (openTagPosition + 3, closeTagPosition - openTagPosition - 3)))
224: {
225: if (foundClosingTag)
226: {
227: text = text.Substring (openTagPosition + 3);
228: }
229: else
230: {
231: // get the position of the nested open tag
232: var nestedOpenTagPosition = FindFirstOpenTagPosition (text.Substring (openTagPosition + 3));
233: AddNewRun (text.Substring (openTagPosition, nestedOpenTagPosition - openTagPosition + 3)); // add the section up to the nested open tag
234: text = text.Substring (nestedOpenTagPosition + 3);
235: }
236:
237: ParseText (ref text);
238: }
239:
240: if ((closeTagPosition = text.IndexOf (_closeTagCollection[(int) FormattingType.Quote], openTagPosition)) != -1)
241: {
242: text = GenerateAndAddNewRun (text, openTagPosition, closeTagPosition, _openTagCollection[(int) FormattingType.Quote]);
243: _quotePass = false;
244: ParseText (ref text);
245: }
246:
247: _quotePass = false;
248: }
249:
250: return;
251: }