Skip to content

Commit 9c67d38

Browse files
committed
Add Markdown support to Save Snippet Information dlg
Updated TSaveInfoMgr in USaveInfoMgr to add support for rendering, previewing and outputting snippet information in Markdown format.
1 parent 61d53b2 commit 9c67d38

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

Src/USaveInfoMgr.pas

+45-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
*
66
* Copyright (C) 2025, Peter Johnson (gravatar.com/delphidabbler).
77
*
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.
1010
}
1111

1212

@@ -55,6 +55,12 @@ TSaveInfoMgr = class(TNoPublicConstructObject)
5555
/// information about the snippet represented by the given view.</summary>
5656
function GeneratePlainText: TEncodedData;
5757

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+
5864
/// <summary>Returns type of file selected in the associated save dialogue
5965
/// box.</summary>
6066
function SelectedFileType: TSourceFileType;
@@ -127,11 +133,13 @@ implementation
127133
SysUtils,
128134
Dialogs,
129135
// Project
136+
DB.USnippetKind,
130137
FmPreviewDlg,
131138
Hiliter.UAttrs,
132139
Hiliter.UFileHiliter,
133140
Hiliter.UGlobals,
134141
UIOUtils,
142+
UMarkdownSnippetDoc,
135143
UOpenDialogHelper,
136144
UPreferences,
137145
URTFSnippetDoc,
@@ -171,7 +179,6 @@ procedure TSaveInfoMgr.DoExecute;
171179
if fSaveDlg.Execute then
172180
begin
173181
FileType := SelectedFileType;
174-
FileContent := GenerateOutput(FileType).ToString;
175182
Encoding := TEncodingHelper.GetEncoding(fSaveDlg.SelectedEncoding);
176183
try
177184
FileContent := GenerateOutput(FileType).ToString;
@@ -221,6 +228,22 @@ function TSaveInfoMgr.GenerateHTML(const AUseHiliting: Boolean;
221228
end;
222229
end;
223230

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+
224247
function TSaveInfoMgr.GenerateOutput(const FileType: TSourceFileType):
225248
TEncodedData;
226249
var
@@ -233,6 +256,7 @@ function TSaveInfoMgr.GenerateOutput(const FileType: TSourceFileType):
233256
sfText: Result := GeneratePlainText;
234257
sfHTML5: Result := GenerateHTML(UseHiliting, THTML5SnippetDoc);
235258
sfXHTML: Result := GenerateHTML(UseHiliting, TXHTMLSnippetDoc);
259+
sfMarkdown: Result := GenerateMarkdown;
236260
end;
237261
end;
238262

@@ -298,6 +322,7 @@ constructor TSaveInfoMgr.InternalCreate(AView: IView);
298322
sTextDesc = 'Plain text file';
299323
sHTML5Desc = 'HTML 5 file';
300324
sXHTMLDesc = 'XHTML file';
325+
sMarkdownDesc = 'Markdown file';
301326
begin
302327
inherited InternalCreate;
303328
fView := AView;
@@ -336,6 +361,17 @@ constructor TSaveInfoMgr.InternalCreate(AView: IView);
336361
]
337362
);
338363
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;
339375

340376
fSaveDlg := TSaveSourceDlg.Create(nil);
341377
fSaveDlg.Title := sDlgCaption;
@@ -379,6 +415,12 @@ procedure TSaveInfoMgr.PreviewHandler(Sender: TObject);
379415
PreviewDocType := dtHTML;
380416
PreviewFileType := sfXHTML;
381417
end;
418+
sfMarkdown:
419+
begin
420+
// Markdown is previewed as plain text
421+
PreviewDocType := dtPlainText;
422+
PreviewFileType := sfMarkdown;
423+
end;
382424
else
383425
raise Exception.Create(
384426
ClassName + '.PreviewHandler: unsupported file type'

0 commit comments

Comments
 (0)