public static SnippetDocumentControl Open(string filePath) { SnippetDocumentControl snippet = null; string fileExt = System.IO.Path.GetExtension(filePath); if (new[] { ".html", ".htm" }.Contains(fileExt, StringComparer.OrdinalIgnoreCase)) { snippet = Parsing.HTMLToSnippet.Parse(filePath); } else if (string.Equals(fileExt, ".web", StringComparison.OrdinalIgnoreCase)) { snippet = ParseSnippetFromWebPadFile(filePath); } else { throw new Exception($"File extension [ext={fileExt}] is not a supported file type"); } // if there are no references add in the defaults if (!snippet.References.Any()) { snippet.References.AddDefaults(); } // we just opened it so it isn't modified snippet.IsModified = false; return(snippet); }
private void ExecuteOpen(object sender, ExecutedRoutedEventArgs e) { try { // determine if we have a file open already var currentDoc = GetSelectedTabSnippetDocumentControl(); SnippetDocumentControl snippet = File.OpenHandler.Open(currentDoc); if (snippet != null) { handleAddingRecentFile(new Models.RecentFileModel { Path = snippet.SaveFilePath, FileName = snippet.SaveFileName }); AddSnippetTab(snippet); } else { log.Info("Didn't open webpad file, no snippet was found in file"); } } catch (Exception ex) { System.Windows.MessageBox.Show(string.Format("Failed to open file. Exception: {0}", ex), "Failed to Open Web File", MessageBoxButton.OK, MessageBoxImage.Error); } }
private SnippetDocumentControl AddSnippetTab(SnippetDocumentControl bodyCtrl = null, string tabName = null) { var headerTemplate = this.FindResource("docTabHeader") as DataTemplate; var tabItem = new TabItem(); DocumentTab.Items.Insert(0, tabItem); DocumentTab.SelectedItem = tabItem; var headerCtrl = headerTemplate.LoadContent() as StackPanel; if (bodyCtrl == null) { var contentTemplate = this.FindResource("docTabContent") as DataTemplate; bodyCtrl = contentTemplate.LoadContent() as SnippetDocumentControl; } tabItem.Header = headerCtrl; tabItem.Content = bodyCtrl; UpdateHeaderOfSnippetDocumentControl(bodyCtrl, tabName); bodyCtrl.HtmlEditorCaretPositionChangeDelayedEvent += CurrentSnippetDocument_HtmlEditorCaretPositionChangeDelayedEvent; bodyCtrl.HtmlEditorTextChangeDelayedEvent += CurrentSnippetDocument_HtmlEditorTextChangeDelayedEvent; return(bodyCtrl); }
private static void AppendSnippetReferences(SnippetDocumentControl snippet, HtmlAgilityPack.HtmlDocument doc, HtmlNode head) { // take care of javascript references foreach (var reference in snippet.References) { if (reference.Type == ReferenceTypes.Css) { var s = doc.CreateElement("link"); s.SetAttributeValue("rel", "stylesheet"); s.SetAttributeValue("href", reference.Url); head.AppendChild(s); } else if (reference.Type == ReferenceTypes.Javascript) { var s = doc.CreateElement("script"); s.SetAttributeValue("src", reference.Url); s.SetAttributeValue("type", "text/javascript"); head.AppendChild(s); } else { throw new Exception($"Unsupported snippet reference type: {reference.Type}"); } } }
public static System.Xml.Linq.XDocument SaveToXDocument(SnippetDocumentControl snippetControl) { var snippetData = new Snippet(snippetControl); var doc = Utilities.XmlSerialization.Serialize <Snippet>(snippetData); return(doc); }
private SnippetDocumentControl CreateNewDocument(SnippetDocumentControl bodyCtrl = null) { // may need to use a deserialized snippet control as a clone for a new document so there are things we need to do to clean it up if (bodyCtrl != null) { bodyCtrl.SaveFilePath = ""; } return(AddSnippetTab(bodyCtrl: bodyCtrl, tabName: $"New Query {newQueryCount++}")); }
public void OpenRecentFile(Models.RecentFileModel file) { SnippetDocumentControl snippet = File.OpenHandler.Open(file.Path); if (snippet != null) { AddSnippetTab(snippet); } }
private static string GetNewFilePath(SnippetDocumentControl snippet) { SaveFileDialog dialog = new SaveFileDialog { AddExtension = true, DefaultExt = "html", Filter = "HTML (*.html)|*.html|HTML (*.htm)|*.htm|WebPad Snippet (*.web)|*.web" }; if (dialog.ShowDialog() != DialogResult.OK) { return(null); } return(dialog.FileName); }
public Snippet(SnippetDocumentControl snippetControl) { this.CSS = snippetControl.CSS; if (string.IsNullOrWhiteSpace(snippetControl.ExternalHtmlPath)) { this.Html = snippetControl.Html; } else { this.Html = ""; // clear it out if it somehow had something this.externalHtmlFile = snippetControl.ExternalHtmlPath; } this.Javascript = snippetControl.Javascript; this.References = snippetControl.References; this.baseHref = snippetControl.BaseHref; }
private void UpdateHeaderOfSnippetDocumentControl(SnippetDocumentControl ctrl, string tabName = null) { //.SaveFileName TabItem tab = ctrl.FindVisualParent <TabItem>(); var headerCtrl = tab.Header as FrameworkElement; var queryNameTB = headerCtrl.FindNameDownTree("queryNameTextBlock") as TextBlock; if (tabName == null) { // don't blank out the name if there is no save file if (!string.IsNullOrWhiteSpace(ctrl.SaveFilePath)) { queryNameTB.Text = ctrl.SaveFileName; } } else { queryNameTB.Text = tabName; } }
public static void Save(SnippetDocumentControl snippet, string filePath) { var doc = new HtmlAgilityPack.HtmlDocument(); // use newlines with Stringbuilder so the document formats better than using @"" var node = HtmlAgilityPack.HtmlNode.CreateNode( new System.Text.StringBuilder() .AppendLine("<html>") .AppendLine("<head></head>") .AppendLine($"<body>{snippet.Html}</body>") .AppendLine("</html>") .ToString() ); doc.DocumentNode.AppendChild(node); var head = doc.DocumentNode.SelectSingleNode("//head"); var body = doc.DocumentNode.SelectSingleNode("//body"); // save CSS if (!string.IsNullOrWhiteSpace(snippet.CSS)) { var s = doc.CreateElement("style"); s.SetAttributeValue("type", "text/css"); s.AppendChild(doc.CreateTextNode(snippet.CSS)); head.AppendChild(s); } // save javascript if (!string.IsNullOrWhiteSpace(snippet.Javascript)) { var s = doc.CreateElement("script"); s.SetAttributeValue("type", "text/javascript"); s.AppendChild(doc.CreateTextNode(snippet.Javascript)); body.AppendChild(s); } AppendSnippetReferences(snippet, doc, head); doc.Save(filename: filePath); }
/// <summary> /// /// </summary> /// <param name="snippet"></param> /// <param name="type"></param> /// <param name="saveAs">If false an attempt will be made to save the file back to it's original name or a new name if the file doesn't exist. /// If true it will save it to a new name no matter what /// This should enable save as functionality</param> public static void Save(SnippetDocumentControl snippet, bool saveAs) { string filePath; bool newFile = !System.IO.File.Exists(snippet.SaveFilePath); if (newFile || saveAs) { filePath = GetNewFilePath(snippet); } else { filePath = snippet.SaveFilePath; } // make sure the filepath is not null or empty, because that might indicate the user canceled out of saving and we want to go back if (!string.IsNullOrEmpty(filePath)) { log.Info($"Saving file to {filePath}"); string fileExt = System.IO.Path.GetExtension(filePath); if (new[] { ".html", ".htm" }.Contains(fileExt, StringComparer.OrdinalIgnoreCase)) { File.SaveToHtmlHandler.Save(snippet, filePath); } else if (string.Equals(fileExt, ".web", StringComparison.OrdinalIgnoreCase)) { SaveToWebFile(snippet, filePath); } else { throw new Exception($"File extension [ext={fileExt}] is not a supported file type"); } snippet.SaveFilePath = filePath; // this filepath may have changed snippet.IsModified = false; log.Info("Saving file success"); } }
private static SnippetDocumentControl GetSnippetControlFromSnippet(Snippet snippetData, string webPadFilePath = null) { SnippetDocumentControl snippet = null; if (snippetData != null) { snippet = new SnippetDocumentControl { CSS = snippetData.CSS, Html = snippetData.Html, Javascript = snippetData.Javascript, References = snippetData.References, ExternalHtmlPath = snippetData.externalHtmlFile, SaveFilePath = webPadFilePath, BaseHref = snippetData.baseHref }; if (System.IO.File.Exists(webPadFilePath) && !string.IsNullOrWhiteSpace(snippetData.externalHtmlFile)) { var fullPath = Utilities.PathUtilities.MakeAbsolutePath(webPadFilePath, snippetData.externalHtmlFile); if (System.IO.File.Exists(fullPath)) { snippet.Html = System.IO.File.ReadAllText(fullPath); } else { log.Error($"External html file {fullPath} does not exist"); } } } else { throw new Exception(string.Format("Unable to parse data from {0}", webPadFilePath)); } return(snippet); }
private static void SaveToWebFile(SnippetDocumentControl snippetControl, string filePath) { var doc = SaveToXDocument(snippetControl); log.Info($"Saving to {filePath}"); doc.Save(filePath); if (!string.IsNullOrWhiteSpace(snippetControl.ExternalHtmlPath)) { var fullExtHtmlPath = Utilities.PathUtilities.MakeAbsolutePath(filePath, snippetControl.ExternalHtmlPath); log.Info($"Saving html to external document: [relative={snippetControl.ExternalHtmlPath},full={fullExtHtmlPath}]"); if (System.IO.File.Exists(fullExtHtmlPath)) { // save all the html to it System.IO.File.WriteAllText(fullExtHtmlPath, snippetControl.Html); } else { log.Error($"External html file: [{fullExtHtmlPath}] does not exist"); } } }
public static SnippetDocumentControl Open(SnippetDocumentControl currentDoc) { OpenFileDialog dialog = new OpenFileDialog { AddExtension = true, DefaultExt = "html", Filter = "HTML (*.html)|*.html|HTML (*.htm)|*.htm|WebPad Snippet (*.web)|*.web" }; // set initial folder to be where our current file is, if we have one if (System.IO.File.Exists(currentDoc.SaveFilePath)) { var folderPath = System.IO.Path.GetDirectoryName(currentDoc.SaveFilePath); dialog.InitialDirectory = folderPath; } if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) { return(null); } return(Open(dialog.FileName)); }