5
5
*
6
6
* Copyright (C) 2025, Peter Johnson (gravatar.com/delphidabbler).
7
7
*
8
- * Saves information about a snippet to disk in rich text format. Only routine
9
- * snippet kinds are supported.
8
+ * Saves information about a snippet to disk in various, user specifed, formats.
9
+ * Only routine snippet kinds are supported.
10
10
}
11
11
12
12
@@ -55,6 +55,12 @@ TSaveInfoMgr = class(TNoPublicConstructObject)
55
55
// / information about the snippet represented by the given view.</summary>
56
56
function GeneratePlainText : TEncodedData;
57
57
58
+ // / <summary>Returns encoded data containing a Markdown representation of
59
+ // / information about the snippet represented by the given view.</summary>
60
+ // / <returns><c>TEncodedData</c>. Required Markdown document, encoded as
61
+ // / UTF-16.</returns>
62
+ function GenerateMarkdown : TEncodedData;
63
+
58
64
// / <summary>Returns type of file selected in the associated save dialogue
59
65
// / box.</summary>
60
66
function SelectedFileType : TSourceFileType;
@@ -127,11 +133,13 @@ implementation
127
133
SysUtils,
128
134
Dialogs,
129
135
// Project
136
+ DB.USnippetKind,
130
137
FmPreviewDlg,
131
138
Hiliter.UAttrs,
132
139
Hiliter.UFileHiliter,
133
140
Hiliter.UGlobals,
134
141
UIOUtils,
142
+ UMarkdownSnippetDoc,
135
143
UOpenDialogHelper,
136
144
UPreferences,
137
145
URTFSnippetDoc,
@@ -171,7 +179,6 @@ procedure TSaveInfoMgr.DoExecute;
171
179
if fSaveDlg.Execute then
172
180
begin
173
181
FileType := SelectedFileType;
174
- FileContent := GenerateOutput(FileType).ToString;
175
182
Encoding := TEncodingHelper.GetEncoding(fSaveDlg.SelectedEncoding);
176
183
try
177
184
FileContent := GenerateOutput(FileType).ToString;
@@ -221,6 +228,22 @@ function TSaveInfoMgr.GenerateHTML(const AUseHiliting: Boolean;
221
228
end ;
222
229
end ;
223
230
231
+ function TSaveInfoMgr.GenerateMarkdown : TEncodedData;
232
+ var
233
+ Doc: TMarkdownSnippetDoc;
234
+ begin
235
+ Assert(Supports(fView, ISnippetView),
236
+ ClassName + ' .GeneratePlainText: View is not a snippet view' );
237
+ Doc := TMarkdownSnippetDoc.Create(
238
+ (fView as ISnippetView).Snippet.Kind <> skFreeform
239
+ );
240
+ try
241
+ Result := Doc.Generate((fView as ISnippetView).Snippet);
242
+ finally
243
+ Doc.Free;
244
+ end ;
245
+ end ;
246
+
224
247
function TSaveInfoMgr.GenerateOutput (const FileType: TSourceFileType):
225
248
TEncodedData;
226
249
var
@@ -233,6 +256,7 @@ function TSaveInfoMgr.GenerateOutput(const FileType: TSourceFileType):
233
256
sfText: Result := GeneratePlainText;
234
257
sfHTML5: Result := GenerateHTML(UseHiliting, THTML5SnippetDoc);
235
258
sfXHTML: Result := GenerateHTML(UseHiliting, TXHTMLSnippetDoc);
259
+ sfMarkdown: Result := GenerateMarkdown;
236
260
end ;
237
261
end ;
238
262
@@ -298,6 +322,7 @@ constructor TSaveInfoMgr.InternalCreate(AView: IView);
298
322
sTextDesc = ' Plain text file' ;
299
323
sHTML5Desc = ' HTML 5 file' ;
300
324
sXHTMLDesc = ' XHTML file' ;
325
+ sMarkdownDesc = ' Markdown file' ;
301
326
begin
302
327
inherited InternalCreate;
303
328
fView := AView;
@@ -336,6 +361,17 @@ constructor TSaveInfoMgr.InternalCreate(AView: IView);
336
361
]
337
362
);
338
363
fSourceFileInfo.DefaultFileName := sDefFileName;
364
+ fSourceFileInfo.FileTypeInfo[sfMarkdown] := TSourceFileTypeInfo.Create(
365
+ ' .md' ,
366
+ sMarkdownDesc,
367
+ [
368
+ TSourceFileEncoding.Create(etUTF8, sUTF8Encoding),
369
+ TSourceFileEncoding.Create(etUTF16LE, sUTF16LEEncoding),
370
+ TSourceFileEncoding.Create(etUTF16BE, sUTF16BEEncoding),
371
+ TSourceFileEncoding.Create(etSysDefault, sANSIDefaultEncoding)
372
+ ]
373
+ );
374
+ fSourceFileInfo.DefaultFileName := sDefFileName;
339
375
340
376
fSaveDlg := TSaveSourceDlg.Create(nil );
341
377
fSaveDlg.Title := sDlgCaption;
@@ -379,6 +415,12 @@ procedure TSaveInfoMgr.PreviewHandler(Sender: TObject);
379
415
PreviewDocType := dtHTML;
380
416
PreviewFileType := sfXHTML;
381
417
end ;
418
+ sfMarkdown:
419
+ begin
420
+ // Markdown is previewed as plain text
421
+ PreviewDocType := dtPlainText;
422
+ PreviewFileType := sfMarkdown;
423
+ end ;
382
424
else
383
425
raise Exception.Create(
384
426
ClassName + ' .PreviewHandler: unsupported file type'
0 commit comments