/// <summary>
        /// Takes action on the selected string in the editor using
        /// predefined commands.
        /// </summary>
        /// <param name="action"></param>
        /// <param name="input"></param>
        /// <param name="style"></param>
        /// <returns></returns>
        public string MarkupMarkdown(string action, string input, string style = null)
        {
            if (string.IsNullOrEmpty(action))
            {
                return(input);
            }

            action = action.ToLower();

            if (string.IsNullOrEmpty(input) && !StringUtils.Inlist(action, new string[] { "image", "href", "code" }))
            {
                return(null);
            }

            string html = input;

            if (action == "bold")
            {
                html = "**" + input + "**";
            }
            else if (action == "italic")
            {
                html = "*" + input + "*";
            }
            else if (action == "small")
            {
                html = "<small>" + input + "</small>";
            }
            else if (action == "underline")
            {
                html = "<u>" + input + "</u>";
            }
            else if (action == "strikethrough")
            {
                html = "~~" + input + "~~";
            }
            else if (action == "inlinecode")
            {
                html = "`" + input + "`";
            }
            else if (action == "h1")
            {
                html = "# " + input;
            }
            else if (action == "h2")
            {
                html = "## " + input;
            }
            else if (action == "h3")
            {
                html = "### " + input;
            }
            else if (action == "h4")
            {
                html = "#### " + input;
            }
            else if (action == "h5")
            {
                html = "##### " + input;
            }

            else if (action == "quote")
            {
                StringBuilder sb    = new StringBuilder();
                var           lines = StringUtils.GetLines(input);
                foreach (var line in lines)
                {
                    sb.AppendLine("> " + line);
                }
                html = sb.ToString();
            }
            else if (action == "list")
            {
                StringBuilder sb    = new StringBuilder();
                var           lines = StringUtils.GetLines(input);
                foreach (var line in lines)
                {
                    sb.AppendLine("* " + line);
                }
                html = sb.ToString();
            }
            else if (action == "numberlist")
            {
                StringBuilder sb    = new StringBuilder();
                var           lines = StringUtils.GetLines(input);
                int           ct    = 0;
                foreach (var line in lines)
                {
                    ct++;
                    sb.AppendLine($"{ct}. " + line);
                }
                html = sb.ToString();
            }
            else if (action == "href")
            {
                var form = new PasteHref()
                {
                    Owner        = Window,
                    LinkText     = input,
                    MarkdownFile = MarkdownDocument.Filename
                };

                // check for links in input or on clipboard
                string link = input;
                if (string.IsNullOrEmpty(link))
                {
                    link = Clipboard.GetText();
                }

                if (!(input.StartsWith("http:") || input.StartsWith("https:") || input.StartsWith("mailto:") || input.StartsWith("ftp:")))
                {
                    link = string.Empty;
                }
                form.Link = link;

                bool?res = form.ShowDialog();
                if (res != null && res.Value)
                {
                    if (form.IsExternal)
                    {
                        html = $"<a href=\"{form.Link}\" target=\"top\">{form.LinkText}</a>";
                    }
                    else
                    {
                        html = $"[{form.LinkText}]({form.Link})";
                    }
                }
            }
            else if (action == "image")
            {
                var form = new PasteImage
                {
                    Owner        = Window,
                    ImageText    = input,
                    MarkdownFile = MarkdownDocument.Filename
                };


                // check for links in input or on clipboard
                string link = input;
                if (string.IsNullOrEmpty(link))
                {
                    link = Clipboard.GetText();
                }

                if (!(input.StartsWith("http:") || input.StartsWith("https:") || input.StartsWith("mailto:") || input.StartsWith("ftp:")))
                {
                    link = string.Empty;
                }

                if (input.Contains(".png") || input.Contains(".jpg") || input.Contains(".gif"))
                {
                    link = input;
                }

                form.Image = link;

                bool?res = form.ShowDialog();
                if (res != null && res.Value)
                {
                    var image = form.Image;
                    html = $"![{form.ImageText}]({form.Image})";
                }
            }
            else if (action == "code")
            {
                var form = new PasteCode();
                form.Owner        = Window;
                form.Code         = input;
                form.CodeLanguage = "csharp";

                bool?res = form.ShowDialog();
                if (res != null && res.Value)
                {
                    html = "```" + form.CodeLanguage + "\r\n" +
                           form.Code.Trim() + "\r\n" +
                           "```\r\n";
                }
            }
            else
            {
                // allow addins to handle custom actions
                string addinAction = AddinManager.Current.RaiseOnEditorCommand(action, input);
                if (!string.IsNullOrEmpty(addinAction))
                {
                    html = addinAction;
                }
            }

            return(html);
        }
Exemple #2
0
        /// <summary>
        /// Takes action on the selected string in the editor using
        /// predefined commands.
        /// </summary>
        /// <param name="action"></param>
        /// <param name="input"></param>
        /// <param name="style"></param>
        /// <returns></returns>
        public string MarkupMarkdown(string action, string input, string style = null)
        {
            if (string.IsNullOrEmpty(action))
            {
                return(input);
            }

            action = action.ToLower();

            // check for insert actions that don't require a pre selection
            if (string.IsNullOrEmpty(input) && !StringUtils.Inlist(action, new string[] { "image", "href", "code", "emoji" }))
            {
                return(null);
            }

            string html = input;

            if (action == "bold")
            {
                html = "**" + input + "**";
            }
            else if (action == "italic")
            {
                html = "*" + input + "*";
            }
            else if (action == "small")
            {
                html = "<small>" + input + "</small>";
            }
            else if (action == "underline")
            {
                html = "<u>" + input + "</u>";
            }
            else if (action == "strikethrough")
            {
                html = "~~" + input + "~~";
            }
            else if (action == "inlinecode")
            {
                html = "`" + input + "`";
            }
            else if (action == "h1")
            {
                html = "# " + input;
            }
            else if (action == "h2")
            {
                html = "## " + input;
            }
            else if (action == "h3")
            {
                html = "### " + input;
            }
            else if (action == "h4")
            {
                html = "#### " + input;
            }
            else if (action == "h5")
            {
                html = "##### " + input;
            }

            else if (action == "quote")
            {
                StringBuilder sb    = new StringBuilder();
                var           lines = StringUtils.GetLines(input);
                foreach (var line in lines)
                {
                    sb.AppendLine("> " + line);
                }
                html = sb.ToString();
            }
            else if (action == "list")
            {
                StringBuilder sb    = new StringBuilder();
                var           lines = StringUtils.GetLines(input);
                foreach (var line in lines)
                {
                    sb.AppendLine("* " + line);
                }
                html = sb.ToString();
            }
            else if (action == "numberlist")
            {
                StringBuilder sb    = new StringBuilder();
                var           lines = StringUtils.GetLines(input);
                int           ct    = 0;
                foreach (var line in lines)
                {
                    ct++;
                    sb.AppendLine($"{ct}. " + line);
                }
                html = sb.ToString();
            }
            else if (action == "emoji")
            {
                var form = new EmojiWindow();
                form.Owner = Window;
                form.ShowDialog();

                if (form.Cancelled)
                {
                    return(input);
                }

                html = form.EmojiString;
            }
            else if (action == "href")
            {
                var form = new PasteHref()
                {
                    Owner        = Window,
                    LinkText     = input,
                    MarkdownFile = MarkdownDocument.Filename
                };

                // check for links in input or on clipboard
                string link = input;
                if (string.IsNullOrEmpty(link))
                {
                    link = Clipboard.GetText();
                }

                if (!(input.StartsWith("http:") || input.StartsWith("https:") || input.StartsWith("mailto:") || input.StartsWith("ftp:")))
                {
                    link = string.Empty;
                }
                form.Link = link;

                bool?res = form.ShowDialog();
                if (res != null && res.Value)
                {
                    if (form.IsExternal)
                    {
                        html = $"<a href=\"{form.Link}\" target=\"_blank\">{form.LinkText}</a>";
                    }
                    else
                    {
                        html = $"[{form.LinkText}]({form.Link})";
                    }
                }
            }
            else if (action == "image")
            {
                var form = new PasteImageWindow(Window)
                {
                    ImageText    = input,
                    MarkdownFile = MarkdownDocument.Filename
                };


                // check for links in input or on clipboard
                string link = input;
                if (string.IsNullOrEmpty(link))
                {
                    link = Clipboard.GetText();
                }

                if (!(input.StartsWith("http:") || input.StartsWith("https:") || input.StartsWith("mailto:") || input.StartsWith("ftp:")))
                {
                    link = string.Empty;
                }

                if (input.Contains(".png") || input.Contains(".jpg") || input.Contains(".gif"))
                {
                    link = input;
                }

                form.Image = link;

                bool?res = form.ShowDialog();
                if (res != null && res.Value)
                {
                    var image = form.Image;
                    if (!image.StartsWith("data:image/"))
                    {
                        html = $"![{form.ImageText}]({form.Image})";
                    }
                    else
                    {
                        var id = "image_ref_" + DataUtils.GenerateUniqueId();

                        dynamic pos    = AceEditor.getCursorPosition(false);
                        dynamic scroll = AceEditor.getscrolltop(false);

                        // the ID tag
                        html = $"\r\n\r\n[{id}]: {image}\r\n";

                        // set selction position to bottom of document
                        AceEditor.gotoBottom(false);
                        SetSelection(html);

                        // reset the selection point
                        AceEditor.setcursorposition(pos); //pos.column,pos.row);

                        if (scroll != null)
                        {
                            AceEditor.setscrolltop(scroll);
                        }

                        WindowUtilities.DoEvents();
                        html = $"![{form.ImageText}][{id}]";
                    }
                }
            }
            else if (action == "code")
            {
                var form = new PasteCode();
                form.Owner        = Window;
                form.Code         = input;
                form.CodeLanguage = "csharp";
                bool?res = form.ShowDialog();

                if (res != null && res.Value)
                {
                    html = "```" + form.CodeLanguage + "\r\n" +
                           form.Code.Trim() + "\r\n" +
                           "```\r\n";
                }
            }
            else
            {
                // allow addins to handle custom actions
                string addinAction = AddinManager.Current.RaiseOnEditorCommand(action, input);
                if (!string.IsNullOrEmpty(addinAction))
                {
                    html = addinAction;
                }
            }

            return(html);
        }