Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created February 16, 2023 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/9a07d85a7c26b6bec8b12fd4300928ba to your computer and use it in GitHub Desktop.
Save bjoerntx/9a07d85a7c26b6bec8b12fd4300928ba to your computer and use it in GitHub Desktop.
[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