@@ -20,6 +20,7 @@ interface
2020 UEncodings,
2121 UHTMLSnippetDoc,
2222 USaveSourceDlg,
23+ USnippetDoc,
2324 USourceFileInfo,
2425 UView;
2526
@@ -39,32 +40,6 @@ TSaveInfoMgr = class(TNoPublicConstructObject)
3940 // / <c>ExpectedStr</c> doesn't match <c>EncodedStr</c>.</summary>
4041 class procedure WarnIfDataLoss (const ExpectedStr, EncodedStr: string);
4142
42- // / <summary>Returns encoded data containing a RTF representation of
43- // / information about the snippet represented by the given view.</summary>
44- class function GenerateRichText (View : IView; const AUseHiliting: Boolean):
45- TEncodedData; static;
46-
47- // / <summary>Returns encoded data containing a HTML representation of the
48- // / required snippet information.</summary>
49- // / <param name="AUseHiliting"><c>Boolean</c> [in] Determines whether
50- // / source code is syntax highlighted or not.</param>
51- // / <param name="GeneratorClass"><c>THTMLSnippetDocClass</c> [in] Class of
52- // / object used to generate the required flavour of HTML.</param>
53- // / <returns><c>TEncodedData</c>. Required HTML document, encoded as UTF-8.
54- // / </returns>
55- function GenerateHTML (const AUseHiliting: Boolean;
56- const GeneratorClass: THTMLSnippetDocClass): TEncodedData;
57-
58- // / <summary>Returns encoded data containing a plain text representation of
59- // / information about the snippet represented by the given view.</summary>
60- function GeneratePlainText : TEncodedData;
61-
62- // / <summary>Returns encoded data containing a Markdown representation of
63- // / information about the snippet represented by the given view.</summary>
64- // / <returns><c>TEncodedData</c>. Required Markdown document, encoded as
65- // / UTF-16.</returns>
66- function GenerateMarkdown : TEncodedData;
67-
6843 // / <summary>Returns type of file selected in the associated save dialogue
6944 // / box.</summary>
7045 function SelectedFileType : TSourceFileType;
@@ -96,6 +71,14 @@ TSaveInfoMgr = class(TNoPublicConstructObject)
9671 procedure EncodingQueryHandler (Sender: TObject;
9772 var Encodings: TSourceFileEncodings);
9873
74+ // / <summary>Returns an instance of the document generator object for the
75+ // / desired file type.</summary>
76+ // / <param name="FileType"><c>TSourceFileType</c> [in] The type of file to
77+ // / be generated.</param>
78+ // / <returns><c>TSnippetDoc</c>. The required document generator object.
79+ // / The caller MUST free this object.</returns>
80+ function GetDocGenerator (const FileType: TSourceFileType): TSnippetDoc;
81+
9982 // / <summary>Generates the required snippet information in the requested
10083 // / format.</summary>
10184 // / <param name="FileType"><c>TSourceFileType</c> [in] Type of file to be
@@ -142,6 +125,7 @@ implementation
142125 Hiliter.UAttrs,
143126 Hiliter.UFileHiliter,
144127 Hiliter.UGlobals,
128+ UExceptions,
145129 UIOUtils,
146130 UMarkdownSnippetDoc,
147131 UMessageBox,
@@ -215,106 +199,62 @@ class procedure TSaveInfoMgr.Execute(View: IView);
215199 end ;
216200end ;
217201
218- function TSaveInfoMgr.GenerateHTML (const AUseHiliting: Boolean;
219- const GeneratorClass: THTMLSnippetDocClass): TEncodedData;
220- var
221- Doc: THTMLSnippetDoc; // object that generates RTF document
222- HiliteAttrs: IHiliteAttrs; // syntax highlighter formatting attributes
223- begin
224- if (fView as ISnippetView).Snippet.HiliteSource and AUseHiliting then
225- HiliteAttrs := THiliteAttrsFactory.CreateUserAttrs
226- else
227- HiliteAttrs := THiliteAttrsFactory.CreateNulAttrs;
228- Doc := GeneratorClass.Create(HiliteAttrs);
229- try
230- Result := Doc.Generate((fView as ISnippetView).Snippet);
231- finally
232- Doc.Free;
233- end ;
234- end ;
235-
236- function TSaveInfoMgr.GenerateMarkdown : TEncodedData;
237- var
238- Doc: TMarkdownSnippetDoc;
239- ExpectedMarkown: string;
240- begin
241- Assert(Supports(fView, ISnippetView),
242- ClassName + ' .GenerateMarkdown: View is not a snippet view' );
243- Doc := TMarkdownSnippetDoc.Create(
244- (fView as ISnippetView).Snippet.Kind <> skFreeform
245- );
246- try
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);
253- finally
254- Doc.Free;
255- end ;
256- end ;
257-
258202function TSaveInfoMgr.GenerateOutput (const FileType: TSourceFileType):
259203 TEncodedData;
260204var
261- UseHiliting: Boolean;
205+ Doc: TSnippetDoc;
206+ DocData: TEncodedData;
207+ ExpectedText: string;
262208begin
263- UseHiliting := fSaveDlg.UseSyntaxHiliting and
264- TFileHiliter.IsHilitingSupported(FileType);
265- case FileType of
266- sfRTF: Result := GenerateRichText(fView, UseHiliting);
267- sfText: Result := GeneratePlainText;
268- sfHTML5: Result := GenerateHTML(UseHiliting, THTML5SnippetDoc);
269- sfXHTML: Result := GenerateHTML(UseHiliting, TXHTMLSnippetDoc);
270- sfMarkdown: Result := GenerateMarkdown;
271- end ;
272- end ;
273-
274- function TSaveInfoMgr.GeneratePlainText : TEncodedData;
275- var
276- Doc: TTextSnippetDoc; // object that generates plain text document
277- HiliteAttrs: IHiliteAttrs; // syntax highlighter formatting attributes
278- ExpectedText: string; // expected plain text
279- begin
280- Assert(Supports(fView, ISnippetView),
281- ClassName + ' .GeneratePlainText: View is not a snippet view' );
282- HiliteAttrs := THiliteAttrsFactory.CreateNulAttrs;
283- Doc := TTextSnippetDoc.Create;
209+ // Create required type of document generator
210+ Doc := GetDocGenerator(FileType);
284211 try
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
288- Result := TEncodedData.Create(
289- ExpectedText, fSaveDlg.SelectedEncoding
290- );
291- // Check for data loss in required encoding
292- WarnIfDataLoss(ExpectedText, Result.ToString);
212+ Assert(Assigned(Doc), ClassName + ' .GenerateOutput: unknown file type' );
213+ // Generate text
214+ DocData := Doc.Generate((fView as ISnippetView).Snippet);
215+ if DocData.EncodingType <> fSaveDlg.SelectedEncoding then
216+ begin
217+ // Required encoding is different to that used to generate document, so
218+ // we need to convert to the desired encoding
219+ ExpectedText := DocData.ToString;
220+ // Convert encoding to that selected in save dialogue box
221+ Result := TEncodedData.Create(
222+ ExpectedText, fSaveDlg.SelectedEncoding
223+ );
224+ // Check for data loss in desired encoding
225+ WarnIfDataLoss(ExpectedText, Result.ToString);
226+ end
227+ else
228+ // Required encoding is same as that used to generate the document
229+ Result := DocData;
293230 finally
294231 Doc.Free;
295232 end ;
296233end ;
297234
298- class function TSaveInfoMgr.GenerateRichText ( View : IView;
299- const AUseHiliting: Boolean): TEncodedData ;
235+ function TSaveInfoMgr.GetDocGenerator ( const FileType: TSourceFileType):
236+ TSnippetDoc ;
300237var
301- Doc: TRTFSnippetDoc; // object that generates RTF document
238+ UseHiliting: Boolean;
239+ IsPascalSnippet: Boolean;
302240 HiliteAttrs: IHiliteAttrs; // syntax highlighter formatting attributes
303241begin
304- Assert(Supports(View , ISnippetView),
305- ' TSaveInfoMgr.GenerateRichText: View is not a snippet view' );
306- if (View as ISnippetView).Snippet.HiliteSource and AUseHiliting then
242+ IsPascalSnippet := (fView as ISnippetView).Snippet.Kind <> skFreeform;
243+ UseHiliting := fSaveDlg.UseSyntaxHiliting
244+ and TFileHiliter.IsHilitingSupported(FileType)
245+ and (fView as ISnippetView).Snippet.HiliteSource;
246+ if UseHiliting then
307247 HiliteAttrs := THiliteAttrsFactory.CreateUserAttrs
308248 else
309249 HiliteAttrs := THiliteAttrsFactory.CreateNulAttrs;
310- Doc := TRTFSnippetDoc. Create(HiliteAttrs);
311- try
312- // TRTFSnippetDoc generates stream of ASCII bytes
313- Result := Doc.Generate(( View as ISnippetView).Snippet) ;
314- Assert( Result.EncodingType = etASCII,
315- ' TSaveInfoMgr.GenerateRichText: ASCII encoded data expected ' );
316- finally
317- Doc.Free ;
250+ // Create required type of document generator
251+ case FileType of
252+ sfRTF: Result := TRTFSnippetDoc.Create(HiliteAttrs);
253+ sfText: Result := TTextSnippetDoc.Create ;
254+ sfHTML5: Result := THTML5SnippetDoc.Create(HiliteAttrs);
255+ sfXHTML: Result := TXHTMLSnippetDoc.Create(HiliteAttrs );
256+ sfMarkdown: Result := TMarkdownSnippetDoc.Create(IsPascalSnippet);
257+ else Result := nil ;
318258 end ;
319259end ;
320260
0 commit comments