I am working on a WPF open source markdown editor and I am at the point where I need to save and load documents.
A \"document\" contains 3 files:
If anyone is wondering how I applied Ed Plunkett's answer, here is the code:
var saveDialog = new SaveFileDialog
{
CreatePrompt = true,
OverwritePrompt = true,
Filter = "Project Markdown File | *.pmd"
};
var result = saveDialog.ShowDialog();
if (result != null)
{
if (result == true)
{
if (!Directory.Exists(saveDialog.FileName + "_temp"))
{
var parentFolder = Directory.CreateDirectory(saveDialog.FileName + "_temp").FullName;
var mp = new MarkdownParser();
// Generate HTML
var html = mp.Parse(document.Markdown.Markdown);
var markdownFilePath = parentFolder + "\\" + saveDialog.SafeFileName + ".md";
var htmlFilePath = parentFolder + "\\" + saveDialog.SafeFileName + ".html";
var metadataFilePath = parentFolder + "\\" + saveDialog.SafeFileName + ".xml";
// Generate MD file
using (var sw = new StreamWriter(markdownFilePath))
{
sw.Write(document.Markdown.Markdown);
}
// Generate HTML file
using (var sw = new StreamWriter(htmlFilePath))
{
sw.Write(html);
}
// Generate XML file
document.Metadata.FileName = saveDialog.SafeFileName;
var gxs = new GenericXmlSerializer();
gxs.Serialize(document.Metadata, metadataFilePath);
// Generate style
var cssFilePath = AppDomain.CurrentDomain.BaseDirectory + "Styles\\github-markdown.css";
if (!Directory.Exists(parentFolder + "\\Styles"))
{
Directory.CreateDirectory(parentFolder + "\\Styles");
}
if (!File.Exists(parentFolder + "\\Styles\\github-markdown.css"))
{
File.Copy(cssFilePath, parentFolder + "\\Styles\\github-markdown.css");
}
// Generate the package
ZipFile.CreateFromDirectory(parentFolder, saveDialog.FileName);
// Update the view
var saveResult = new SaveResult
{
FileName = saveDialog.SafeFileName,
Source = htmlFilePath.ToUri(),
TempFile = saveDialog.FileName + "_temp"
};
return saveResult;
}
}
}