Esempio n. 1
0
        public static bool SaveFileAs(Editor editor, string defaultFilter = "markdown")
        {
            const int markdown = 1;
            const int html     = 2;
            const int pdf      = 3;
            const int docx     = 4;

            var filterIndex = markdown;

            if (defaultFilter == "html" || defaultFilter == "html-with-template")
            {
                filterIndex = html;
            }
            if (defaultFilter == "pdf")
            {
                filterIndex = pdf;
            }
            if (defaultFilter == "docx")
            {
                filterIndex = docx;
            }

            var dialog = new SaveFileDialog
            {
                FilterIndex      = filterIndex,
                OverwritePrompt  = true,
                RestoreDirectory = true,
                FileName         = Markdown.SuggestFilenameFromTitle(editor.EditBox.Text),
                Filter           = "Markdown files (*.md)|*.md|"
                                   + "HTML files (*.html)|*.html|"
                                   + "PDF files (*.pdf)|*.pdf|"
                                   + "Docx files (*.docx)|*.docx|"
                                   + "All files (*.*)|*.*"
            };

            if (dialog.ShowDialog() == false)
            {
                return(false);
            }

            var filename = dialog.FileNames[0];

            if (dialog.FilterIndex == html)
            {
                return(SaveAsHtml(editor.Text, filename, "html-with-template"));
            }
            if (dialog.FilterIndex == pdf)
            {
                return(SaveAsPdf(editor.Text, filename));
            }
            if (dialog.FilterIndex == docx)
            {
                return(SaveAsDocx(editor.Text, filename));
            }

            var currentFileName = editor.FileName;

            editor.FileName = filename;
            var offset = editor.EditBox.SelectionStart;

            if (!Save(editor) || !LoadFile(editor, filename, false))
            {
                editor.FileName = currentFileName;
                return(false);
            }
            var max = editor.EditBox.Text.Length;

            editor.EditBox.SelectionStart = Math.Min(max, offset);
            return(true);
        }
Esempio n. 2
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 isWordDoc = pathExtension.Equals(".docx", StringComparison.OrdinalIgnoreCase);

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

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

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

                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);
            }
        }
Esempio n. 3
0
 private static bool SaveAsDocx(string markdown, string filename)
 {
     Markdown.ToMicrosoftWord(Markdown.RemoveYamlFrontMatter(markdown), filename);
     return(true);
 }