Created
February 16, 2023 15:00
-
-
Save bjoerntx/9a07d85a7c26b6bec8b12fd4300928ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPost] | |
public bool Create(string document, string name) { | |
var snippetName = _applicationSettings.SnippetDirectory + | |
"/" + Path.GetInvalidFileNameChars().Aggregate(name, (f, c) => f.Replace(c, '_').Replace(' ', '_')); | |
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) { | |
tx.Create(); | |
// load the snippet | |
tx.Load(Convert.FromBase64String(document), TXTextControl.BinaryStreamType.InternalUnicodeFormat); | |
// select 200 chars as a preview | |
tx.Select(0, 200); | |
// save the body of the snippet | |
TXTextControl.SaveSettings saveSettings = new TXTextControl.SaveSettings() { | |
CssSaveMode = TXTextControl.CssSaveMode.Inline | |
}; | |
tx.Selection.Save(out string htmlString, TXTextControl.StringStreamType.HTMLFormat, saveSettings); | |
TextReader reader = new StringReader(htmlString); | |
HtmlDocument doc = new HtmlDocument(); | |
doc.Load(reader); | |
HtmlNode bodyNode = doc.DocumentNode.SelectSingleNode("/html/body"); | |
System.IO.File.WriteAllText(snippetName + ".htm", bodyNode.InnerHtml); | |
} | |
// store the actual snippet in the Text Control format | |
System.IO.File.WriteAllBytes(snippetName + ".tx", Convert.FromBase64String(document)); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment