/// <summary>
 /// Builds and launches the Jekyll site using the `LaunchCommand`
 /// </summary>
 public void BuildAndLaunchSite()
 {
     //mmFileUtils.OpenTerminal(weblogInfo.ApiUrl);
     if (WeblogInfo.LaunchCommand != null)
     {
         ShellUtils.ExecuteCommandLine(WeblogInfo.LaunchCommand);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Shows external browser that's been configured in the MM Configuration.
        /// Defaults to Chrome
        /// </summary>
        /// <param name="url"></param>
        public static void ShowExternalBrowser(string url)
        {
            try
            {
                if (string.IsNullOrEmpty(mmApp.Configuration.WebBrowserPreviewExecutable) ||
                    !File.Exists(mmApp.Configuration.WebBrowserPreviewExecutable))
                {
                    mmApp.Configuration.WebBrowserPreviewExecutable = null;

                    //ShellUtils.GoUrl(url);

                    WindowsUtils.TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice", "ProgId", out dynamic value, Registry.CurrentUser);
                    var progId = value as string;
                    Console.WriteLine(progId);

                    WindowsUtils.TryGetRegistryKey(@"MSEdgeHTM\shell\open\command", "", out value, Registry.ClassesRoot);

                    var exe = value as string;
                    if (exe == null)
                    {
                        ShellUtils.GoUrl(url); // truy
                        return;
                    }
                    ShellUtils.ExecuteCommandLine(exe);

                    //exe = exe.Replace("%1", $"{url}");

                    //var tokens = exe.Split(new string[] { "\" " },StringSplitOptions.RemoveEmptyEntries);


                    //tokens[0] = tokens[0].Replace("\"","");
                    //Process.Start(tokens[0],tokens[1]);
                }
                else
                {
                    ShellUtils.ExecuteProcess(mmApp.Configuration.WebBrowserPreviewExecutable, $"\"{url}\"");
                }
            }
            catch (Exception ex)
            {
                mmApp.Log($"External Preview failed: {url}", ex, logLevel: LogLevels.Warning);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Builds and launches the Jekyll site using the `LaunchCommand`
        /// </summary>
        public bool BuildAndLaunchSite()
        {
            //mmFileUtils.OpenTerminal(weblogInfo.ApiUrl);
            if (string.IsNullOrEmpty(WeblogInfo.LaunchCommand))
            {
                SetError("Can't build and launch: Missing Jekyll Launch Command.");
                return(false);
            }

            try
            {
                ShellUtils.ExecuteCommandLine(WeblogInfo.LaunchCommand);
            }
            catch
            {
                SetError("Failed to launch Jekyll Build and Launch Command: " + WeblogInfo.LaunchCommand);
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Saves a Markdown Document to file with Save UI.
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="saveAsEncrypted"></param>
        /// <returns></returns>
        public static bool SaveMarkdownDocumentToFile(MarkdownDocument doc = null, bool saveAsEncrypted = false)
        {
            if (doc == null)
            {
                doc = mmApp.Model.ActiveDocument;
            }

            string filename = Path.GetFileName(doc.Filename);
            string folder   = Path.GetDirectoryName(doc.Filename);

            if (filename == null)
            {
                filename = "untitled";
            }

            if (folder == null)
            {
                folder = mmApp.Configuration.LastFolder;
            }

            if (filename == "untitled")
            {
                folder = mmApp.Configuration.LastFolder;

                var match = Regex.Match(doc.CurrentText, @"^# (\ *)(?<Header>.+)", RegexOptions.Multiline);

                if (match.Success)
                {
                    filename = match.Groups["Header"].Value;
                    if (!string.IsNullOrEmpty(filename))
                    {
                        filename = FileUtils.SafeFilename(filename);
                    }
                }
            }

            if (string.IsNullOrEmpty(folder) || !Directory.Exists(folder))
            {
                folder = mmApp.Configuration.LastFolder;
                if (string.IsNullOrEmpty(folder) || !Directory.Exists(folder))
                {
                    folder = KnownFolders.GetPath(KnownFolder.Libraries);
                }
            }

            if (doc.HasFileCrcChanged())
            {
                if (MessageBox.Show(mmApp.Model.Window,
                                    "The underlying file has been changed by another application and if you save as is it will overwrite those changes.\n\n" +
                                    "Do you want to compare files?\n\n" +
                                    "If you reply 'Yes' your preferred Diff tool is opened for you to review changes. Please save changes in the diff tool to apply changes."
                                    , "File on Disk has changed", MessageBoxButton.YesNo) == MessageBoxResult.No)
                {
                    return(false);
                }

                doc.Save(doc.Filename + ".diff");

                var diff = mmApp.Configuration.Git.GitDiffExecutable;
                ShellUtils.ExecuteCommandLine(diff, "\"" + doc.Filename + "Ours.diff" + "\" \"" + doc.Filename + "\"", 999999999);

                // reload the doc
                doc.Load(doc.Filename);
                return(true);
            }


            SaveFileDialog sd = new SaveFileDialog
            {
                FilterIndex      = 1,
                InitialDirectory = folder,
                FileName         = filename,
                CheckFileExists  = false,
                OverwritePrompt  = true,
                CheckPathExists  = true,
                RestoreDirectory = true
            };


            bool?result = null;

            try
            {
                result = sd.ShowDialog();
            }
            catch (Exception ex)
            {
                mmApp.Log("Unable to save file: " + doc.Filename, ex);
                MessageBox.Show(
                    $@"Unable to open file:\r\n\r\n" + ex.Message,
                    "An error occurred trying to open a file",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
            }

            if (!saveAsEncrypted)
            {
                doc.Password = null;
            }
            else
            {
                var pwdDialog = new FilePasswordDialog(doc, false)
                {
                    Owner = mmApp.Model.Window
                };
                bool?pwdResult = pwdDialog.ShowDialog();
            }

            if (result == null || !result.Value)
            {
                return(false);
            }

            doc.Filename = sd.FileName;

            if (!doc.Save())
            {
                MessageBox.Show(mmApp.Model.Window,
                                $"{sd.FileName}\r\n\r\nThis document can't be saved in this location. The file is either locked or you don't have permissions to save it. Please choose another location to save the file.",
                                "Unable to save Document", MessageBoxButton.OK, MessageBoxImage.Warning);
                return(false);
            }
            mmApp.Configuration.LastFolder = Path.GetDirectoryName(sd.FileName);
            return(true);
        }