@@ -35,6 +35,10 @@ TSaveInfoMgr = class(TNoPublicConstructObject)
35
35
fSaveDlg: TSaveSourceDlg;
36
36
fSourceFileInfo: TSourceFileInfo;
37
37
38
+ // / <summary>Displays a warning message about data loss if
39
+ // / <c>ExpectedStr</c> doesn't match <c>EncodedStr</c>.</summary>
40
+ class procedure WarnIfDataLoss (const ExpectedStr, EncodedStr: string);
41
+
38
42
// / <summary>Returns encoded data containing a RTF representation of
39
43
// / information about the snippet represented by the given view.</summary>
40
44
class function GenerateRichText (View : IView; const AUseHiliting: Boolean):
@@ -140,6 +144,7 @@ implementation
140
144
Hiliter.UGlobals,
141
145
UIOUtils,
142
146
UMarkdownSnippetDoc,
147
+ UMessageBox,
143
148
UOpenDialogHelper,
144
149
UPreferences,
145
150
URTFSnippetDoc,
@@ -231,18 +236,20 @@ function TSaveInfoMgr.GenerateHTML(const AUseHiliting: Boolean;
231
236
function TSaveInfoMgr.GenerateMarkdown : TEncodedData;
232
237
var
233
238
Doc: TMarkdownSnippetDoc;
234
- GeneratedData: TEncodedData ;
239
+ ExpectedMarkown: string ;
235
240
begin
236
241
Assert(Supports(fView, ISnippetView),
237
- ClassName + ' .GeneratePlainText : View is not a snippet view' );
242
+ ClassName + ' .GenerateMarkdown : View is not a snippet view' );
238
243
Doc := TMarkdownSnippetDoc.Create(
239
244
(fView as ISnippetView).Snippet.Kind <> skFreeform
240
245
);
241
246
try
242
- GeneratedData := Doc.Generate((fView as ISnippetView).Snippet);
243
- Result := TEncodedData.Create(
244
- GeneratedData.ToString, fSaveDlg.SelectedEncoding
245
- );
247
+ // Generate Markdown using default UTF-16 encoding
248
+ ExpectedMarkown := Doc.Generate((fView as ISnippetView).Snippet).ToString;
249
+ // Convert Markdown to encoding to that selected in save dialogue box
250
+ Result := TEncodedData.Create(ExpectedMarkown, fSaveDlg.SelectedEncoding);
251
+ // Check for data loss in required encoding
252
+ WarnIfDataLoss(ExpectedMarkown, Result.ToString);
246
253
finally
247
254
Doc.Free;
248
255
end ;
@@ -266,19 +273,23 @@ function TSaveInfoMgr.GenerateOutput(const FileType: TSourceFileType):
266
273
267
274
function TSaveInfoMgr.GeneratePlainText : TEncodedData;
268
275
var
269
- Doc: TTextSnippetDoc; // object that generates RTF document
270
- HiliteAttrs: IHiliteAttrs; // syntax highlighter formatting attributes
271
- GeneratedData: TEncodedData;
276
+ Doc: TTextSnippetDoc; // object that generates plain text document
277
+ HiliteAttrs: IHiliteAttrs; // syntax highlighter formatting attributes
278
+ ExpectedText: string; // expected plain text
272
279
begin
273
280
Assert(Supports(fView, ISnippetView),
274
281
ClassName + ' .GeneratePlainText: View is not a snippet view' );
275
282
HiliteAttrs := THiliteAttrsFactory.CreateNulAttrs;
276
283
Doc := TTextSnippetDoc.Create;
277
284
try
278
- GeneratedData := Doc.Generate((fView as ISnippetView).Snippet);
285
+ // Generate text using default UTF-16 encoding
286
+ ExpectedText := Doc.Generate((fView as ISnippetView).Snippet).ToString;
287
+ // Convert encoding to that selected in save dialogue box
279
288
Result := TEncodedData.Create(
280
- GeneratedData.ToString , fSaveDlg.SelectedEncoding
289
+ ExpectedText , fSaveDlg.SelectedEncoding
281
290
);
291
+ // Check for data loss in required encoding
292
+ WarnIfDataLoss(ExpectedText, Result.ToString);
282
293
finally
283
294
Doc.Free;
284
295
end ;
@@ -448,4 +459,15 @@ function TSaveInfoMgr.SelectedFileType: TSourceFileType;
448
459
Result := fSourceFileInfo.FileTypeFromFilterIdx(fSaveDlg.FilterIndex);
449
460
end ;
450
461
462
+ class procedure TSaveInfoMgr.WarnIfDataLoss (const ExpectedStr,
463
+ EncodedStr: string);
464
+ resourcestring
465
+ sEncodingError = ' The selected snippet contains characters that can'' t be '
466
+ + ' represented in the chosen file encoding.' + sLineBreak + sLineBreak
467
+ + ' Please compare the output to the snippet displayed in the Details pane.' ;
468
+ begin
469
+ if ExpectedStr <> EncodedStr then
470
+ TMessageBox.Warning(nil , sEncodingError);
471
+ end ;
472
+
451
473
end .
0 commit comments