private static bool Save(Editor editor)
        {
            try
            {
                if (App.UserSettings.FormatOnSave)
                {
                    Editor.FormatCommand.Execute(true, editor);
                }

                var lineEnd = "\r\n";
                if (App.UserSettings.LineEnding.Equals("cr", StringComparison.OrdinalIgnoreCase))
                {
                    lineEnd = "\r";
                }
                if (App.UserSettings.LineEnding.Equals("lf", StringComparison.OrdinalIgnoreCase))
                {
                    lineEnd = "\n";
                }

                var text = string.Join(
                    lineEnd,
                    editor.EditBox.Document.Lines.Select(line => editor.EditBox.Document.GetText(line).Trim('\r', '\n')));

                File.WriteAllText(editor.FileName, text);
                RecentFilesDialog.UpdateRecentFiles(editor.FileName, editor.EditBox.SelectionStart);
                Settings.Default.LastOpenFile = editor.FileName.AddOffsetToFileName(editor.EditBox.SelectionStart);
                editor.IsModified             = false;
                return(true);
            }
            catch (Exception ex)
            {
                Notify.Alert(ex.Message);
                return(false);
            }
        }
Beispiel #2
0
 public static string ResultFromExecuteProcess(string text, ProcessStartInfo startInfo)
 {
     using (var process = Process.Start(startInfo))
     {
         if (process == null)
         {
             Notify.Alert("Error starting process");
             return(text);
         }
         if (text != null)
         {
             var utf8 = new StreamWriter(process.StandardInput.BaseStream, Encoding.UTF8);
             utf8.Write(text);
             utf8.Close();
         }
         var result = process.StandardOutput.ReadToEnd();
         process.WaitForExit();
         if (process.ExitCode != 0)
         {
             var msg = process.StandardError.ReadToEnd();
             result = IsNullOrWhiteSpace(msg) ? "empty error response" : msg;
         }
         return(result);
     }
 }
        public static bool LoadFile(Editor editor, string file, bool updateCursorPosition = true)
        {
            if (string.IsNullOrWhiteSpace(file))
            {
                return(false);
            }
            try
            {
                var parts         = file.Split(new[] { '|' }, 2);
                var filename      = parts[0] ?? "";
                var offset        = ConvertToOffset(parts.Length == 2 ? parts[1] : "0");
                var pathExtension = Path.GetExtension(filename);

                var isWordDoc = pathExtension.Equals(".docx", StringComparison.OrdinalIgnoreCase);

                if (isWordDoc)
                {
                    NewFile(editor);
                    editor.EditBox.Text = Markdown.FromMicrosoftWord(filename);
                    return(true);
                }

                var isHtmlFile = pathExtension.Equals(".html", StringComparison.OrdinalIgnoreCase) ||
                                 pathExtension.Equals(".htm", StringComparison.OrdinalIgnoreCase);

                if (isHtmlFile)
                {
                    NewFile(editor);
                    editor.EditBox.Text = Markdown.FromHtml(filename);
                    return(true);
                }

                editor.EditBox.Text = File.ReadAllText(filename);

                if (updateCursorPosition)
                {
                    if (App.UserSettings.EditorOpenLastCursorPosition)
                    {
                        editor.EditBox.ScrollToLine(editor.EditBox.Document.GetLineByOffset(offset)?.LineNumber ?? 0);
                        editor.EditBox.SelectionStart = offset;
                    }
                    else
                    {
                        editor.EditBox.ScrollToHome();
                    }
                }

                Settings.Default.LastOpenFile = file;
                RecentFilesDialog.UpdateRecentFiles(filename, offset);
                editor.IsModified = false;
                editor.FileName   = filename;
                return(true);
            }
            catch (Exception ex)
            {
                Notify.Alert($"{ex.Message} {file}");
                return(false);
            }
        }
Beispiel #4
0
 private static bool SaveAsDocx(string markdown, string filename)
 {
     try
     {
         Markdown.ToMicrosoftWord(Markdown.RemoveYamlFrontMatter(markdown), filename);
         return(true);
     }
     catch (Exception ex)
     {
         Notify.Alert(ex.Message);
         return(false);
     }
 }
Beispiel #5
0
 private static bool SaveAsPdf(string markdown, string filename)
 {
     try
     {
         var html = UserTemplate.InsertContent(Markdown.ToHtml(Markdown.RemoveYamlFrontMatter(markdown)));
         var pdf  = Markdown.HtmlToPdf(html);
         File.WriteAllBytes(filename, pdf);
         return(true);
     }
     catch (Exception ex)
     {
         Notify.Alert(ex.Message);
         return(false);
     }
 }
Beispiel #6
0
        public static bool LoadFile(Editor editor, string file, bool updateCursorPosition = true)
        {
            if (string.IsNullOrWhiteSpace(file))
            {
                return(false);
            }
            try
            {
                var parts         = file.Split(new[] { '|' }, 2);
                var filename      = parts[0] ?? "";
                var offset        = ConvertToOffset(parts.Length == 2 ? parts[1] : "0");
                var pathExtension = Path.GetExtension(filename);

                var editorEncoding = App.UserSettings.EditorEncoding;

                var encoding = MyEncodingInfo.IsAutoDetectEncoding(editorEncoding)
                    ? MyEncodingInfo.DetectEncoding(filename)
                    : Encoding.GetEncoding(editorEncoding.CodePage);

                editor.EditBox.Text     = File.ReadAllText(filename, encoding);
                editor.EditBox.Encoding = encoding;

                if (updateCursorPosition)
                {
                    if (App.UserSettings.EditorOpenLastCursorPosition)
                    {
                        editor.EditBox.ScrollToLine(editor.EditBox.Document.GetLineByOffset(offset)?.LineNumber ?? 0);
                        editor.EditBox.SelectionStart = offset;
                    }
                    else
                    {
                        editor.EditBox.ScrollToHome();
                    }
                }

                Settings.Default.LastOpenFile = file;
                RecentFilesDialog.UpdateRecentFiles(filename, offset);
                editor.IsModified = false;
                editor.FileName   = filename;
                return(true);
            }
            catch (Exception ex)
            {
                Notify.Alert($"{ex.Message} {file}");
                return(false);
            }
        }
Beispiel #7
0
 private static bool SaveAsHtml(string markdown, string filename, string filter)
 {
     try
     {
         var html = Markdown.ToHtml(Markdown.RemoveYamlFrontMatter(markdown));
         if (filter == "html-with-template")
         {
             html = UserTemplate.InsertContent(html);
         }
         File.WriteAllText(filename, html);
         return(true);
     }
     catch (Exception ex)
     {
         Notify.Alert(ex.Message);
         return(false);
     }
 }
Beispiel #8
0
 public static UserSettings Load()
 {
     try
     {
         if (File.Exists(SettingsFile) == false)
         {
             var defaultSettings = new UserSettings {
                 Theme = new Theme()
             };
             defaultSettings.Save();
         }
         return(JsonConvert.DeserializeObject <UserSettings>(SettingsFile.ReadAllTextRetry()));
     }
     catch (Exception ex)
     {
         Notify.Alert($"{ex.Message} in {SettingsFile}");
         return(null);
     }
 }
 public static void InsertFile(Editor editor, string file)
 {
     try
     {
         if (string.IsNullOrWhiteSpace(file))
         {
             var dialog = new OpenFileDialog();
             var result = dialog.ShowDialog();
             if (result == false)
             {
                 return;
             }
             file = dialog.FileNames[0];
         }
         var text = File.ReadAllText(file);
         editor.EditBox.Document.Insert(editor.EditBox.SelectionStart, text);
     }
     catch (Exception ex)
     {
         Notify.Alert(ex.Message);
     }
 }
Beispiel #10
0
        public static string InsertContent(string content)
        {
            try
            {
                var template = new HtmlDocument();
                template.Load(TemplateFile);
                var contenthtml = new HtmlDocument();
                contenthtml.LoadHtml(content);

                template.GetElementbyId("content").AppendChildren(contenthtml.DocumentNode.ChildNodes);

                using (var stream = new MemoryStream())
                {
                    template.Save(stream);
                    stream.Position = 0;
                    return(new StreamReader(stream).ReadToEnd());
                }
            }
            catch (Exception ex)
            {
                Notify.Alert(ex.Message);
                return(content);
            }
        }