/// <summary>
        /// If this is a reference-style link, attempts to converts it to a regular link.
        /// </summary>
        /// <param name="document"> The document containing the list of references. </param>
        public void ResolveReference(MarkdownDocument document)
        {
            if (document == null)
                throw new ArgumentNullException("document");
            if (ReferenceId == null)
                return;

            // Look up the reference ID.
            var reference = document.LookUpReference(ReferenceId);
            if (reference == null)
                return;

            // The reference was found. Check the URL is valid.
            if (!IsUrlValid(reference.Url))
                return;

            // Everything is cool when you're part of a team.
            Url = reference.Url;
            Tooltip = reference.Tooltip;
            ReferenceId = null;
        }
Example #2
0
        /// <summary>
        /// Generates XAML for the markdown element.
        /// </summary>
        /// <param name="Output">XAML will be output here.</param>
        /// <param name="Settings">XAML settings.</param>
        /// <param name="TextAlignment">Alignment of text in element.</param>
        /// <param name="Items">Multimedia items.</param>
        /// <param name="ChildNodes">Child nodes.</param>
        /// <param name="AloneInParagraph">If the element is alone in a paragraph.</param>
        /// <param name="Document">Markdown document containing element.</param>
        public override void GenerateXAML(XmlWriter Output, XamlSettings Settings, TextAlignment TextAlignment, MultimediaItem[] Items,
                                          IEnumerable <MarkdownElement> ChildNodes, bool AloneInParagraph, MarkdownDocument Document)
        {
            Variables Variables = Document.Settings.Variables;

            if (Variables != null)
            {
                Variables.Push();
            }

            try
            {
                foreach (MultimediaItem Item in Items)
                {
                    MarkdownDocument Markdown = this.GetMarkdown(Item, Document.URL);
                    Markdown.GenerateXAML(Output, Settings, true);
                }
            }
            finally
            {
                if (Variables != null)
                {
                    Variables.Pop();
                }
            }
        }
Example #3
0
 /// <summary>
 /// Represents a task item in a task list.
 /// </summary>
 /// <param name="Document">Markdown document.</param>
 /// <param name="IsChecked">If the item is checked or not.</param>
 /// <param name="CheckPosition">Position of the checkmark in the original markdown text document.</param>
 /// <param name="Child">Child element.</param>
 public TaskItem(MarkdownDocument Document, bool IsChecked, int CheckPosition, MarkdownElement Child)
     : base(Document, Child)
 {
     this.isChecked     = IsChecked;
     this.checkPosition = CheckPosition;
 }
Example #4
0
 /// <summary>
 /// Represents a bullet list in a markdown document.
 /// </summary>
 /// <param name="Document">Markdown document.</param>
 /// <param name="Children">Child elements.</param>
 public BulletList(MarkdownDocument Document, params MarkdownElement[] Children)
     : base(Document, Children)
 {
 }
Example #5
0
 /// <summary>
 /// Sends a chat message.
 /// </summary>
 /// <param name="Message">Text message to send.</param>
 /// <param name="Markdown">Markdown document, if any, or null if plain text.</param>
 /// <exception cref="NotSupportedException">If the feature is not supported by the node.</exception>
 public virtual void SendChatMessage(string Message, MarkdownDocument Markdown)
 {
     MainWindow.ErrorBox("You are not allowed to chat with this entity.");
 }
Example #6
0
 /// <summary>
 /// Creates an object of the same type, and meta-data, as the current object,
 /// but with content defined by <paramref name="Children"/>.
 /// </summary>
 /// <param name="Children">New content.</param>
 /// <param name="Document">Document that will contain the element.</param>
 /// <returns>Object of same type and meta-data, but with new content.</returns>
 public override MarkdownElementChildren Create(IEnumerable <MarkdownElement> Children, MarkdownDocument Document)
 {
     return(new DefinitionList(Document, Children));
 }
 public ParsingResult(ParsingResult parsingResult)
 {
     Source           = parsingResult.Source;
     SyntaxTree       = parsingResult.SyntaxTree;
     LineStartIndexes = parsingResult.LineStartIndexes;
 }
Example #8
0
 /// <summary>
 /// Represents a sequence of sections.
 /// </summary>
 /// <param name="Document">Markdown document.</param>
 /// <param name="NrColumns">Number of columns in first section.</param>
 /// <param name="Children">Child elements.</param>
 public Sections(MarkdownDocument Document, int NrColumns, IEnumerable <MarkdownElement> Children)
     : base(Document, Children)
 {
     this.nrColumns = NrColumns;
 }
Example #9
0
        /// <summary>
        /// Generates HTML for the markdown element.
        /// </summary>
        /// <param name="Output">HTML will be output here.</param>
        /// <param name="Items">Multimedia items.</param>
        /// <param name="ChildNodes">Child nodes.</param>
        /// <param name="AloneInParagraph">If the element is alone in a paragraph.</param>
        /// <param name="Document">Markdown document containing element.</param>
        public override void GenerateHTML(StringBuilder Output, MultimediaItem[] Items, IEnumerable <MarkdownElement> ChildNodes,
                                          bool AloneInParagraph, MarkdownDocument Document)
        {
            bool First = true;

            Output.Append("<video");

            if (Document.Settings.VideoAutoplay)
            {
                Output.Append(" autoplay=\"autoplay\"");
            }

            if (Document.Settings.VideoControls)
            {
                Output.Append(" controls=\"controls\"");
            }

            foreach (MultimediaItem Item in Items)
            {
                if (First)
                {
                    First = false;

                    if (Item.Width.HasValue)
                    {
                        Output.Append(" width=\"");
                        Output.Append(Item.Width.Value.ToString());
                        Output.Append("\"");
                    }

                    if (Item.Height.HasValue)
                    {
                        Output.Append(" height=\"");
                        Output.Append(Item.Height.Value.ToString());
                        Output.Append("\"");
                    }

                    Output.AppendLine(">");
                }

                Output.Append("<source src=\"");
                Output.Append(XML.HtmlAttributeEncode(Document.CheckURL(Item.Url, Document.URL)));
                Output.Append("\" type=\"");
                Output.Append(XML.HtmlAttributeEncode(Item.ContentType));

                Output.AppendLine("\"/>");
            }

            if (First)
            {
                Output.AppendLine(">");
            }

            foreach (MarkdownElement E in ChildNodes)
            {
                E.GenerateHTML(Output);
            }

            Output.Append("</video>");

            if (AloneInParagraph)
            {
                Output.AppendLine();
            }
        }
Example #10
0
        /// <summary>
        /// Generates Xamarin.Forms XAML for the markdown element.
        /// </summary>
        /// <param name="Output">XAML will be output here.</param>
        /// <param name="TextAlignment">Alignment of text in element.</param>
        /// <param name="Items">Multimedia items.</param>
        /// <param name="ChildNodes">Child nodes.</param>
        /// <param name="AloneInParagraph">If the element is alone in a paragraph.</param>
        /// <param name="Document">Markdown document containing element.</param>
        public override void GenerateXamarinForms(XmlWriter Output, TextAlignment TextAlignment, MultimediaItem[] Items,
                                                  IEnumerable <MarkdownElement> ChildNodes, bool AloneInParagraph, MarkdownDocument Document)
        {
            foreach (MultimediaItem Item in Items)
            {
                Output.WriteStartElement("MediaElement");
                Output.WriteAttributeString("Source", Document.CheckURL(Item.Url, Document.URL));

                if (Item.Width.HasValue)
                {
                    Output.WriteAttributeString("WidthRequest", Item.Width.Value.ToString());
                }

                if (Item.Height.HasValue)
                {
                    Output.WriteAttributeString("HeightRequest", Item.Height.Value.ToString());
                }

                // TODO: Tooltip

                Output.WriteEndElement();

                break;
            }
        }
Example #11
0
        /// <summary>
        /// Generates WPF XAML for the markdown element.
        /// </summary>
        /// <param name="Output">XAML will be output here.</param>
        /// <param name="TextAlignment">Alignment of text in element.</param>
        /// <param name="Items">Multimedia items.</param>
        /// <param name="ChildNodes">Child nodes.</param>
        /// <param name="AloneInParagraph">If the element is alone in a paragraph.</param>
        /// <param name="Document">Markdown document containing element.</param>
        public override void GenerateXAML(XmlWriter Output, TextAlignment TextAlignment, MultimediaItem[] Items,
                                          IEnumerable <MarkdownElement> ChildNodes, bool AloneInParagraph, MarkdownDocument Document)
        {
            foreach (MultimediaItem Item in Items)
            {
                Output.WriteStartElement("MediaElement");
                Output.WriteAttributeString("Source", Document.CheckURL(Item.Url, Document.URL));

                if (Item.Width.HasValue)
                {
                    Output.WriteAttributeString("Width", Item.Width.Value.ToString());
                }

                if (Item.Height.HasValue)
                {
                    Output.WriteAttributeString("Height", Item.Height.Value.ToString());
                }

                if (!string.IsNullOrEmpty(Item.Title))
                {
                    Output.WriteAttributeString("ToolTip", Item.Title);
                }

                Output.WriteEndElement();

                break;
            }
        }
Example #12
0
 /// <summary>
 /// Strong text
 /// </summary>
 /// <param name="Document">Markdown document.</param>
 /// <param name="ChildElements">Child elements.</param>
 public Strong(MarkdownDocument Document, IEnumerable <MarkdownElement> ChildElements)
     : base(Document, ChildElements)
 {
 }
 ///DOLATER <summary></summary>
 /// <param name="Document"></param>
 /// <param name="stream"></param>
 public void Serialize(MarkdownDocument Document, Stream stream) {
     foreach ()
 }
Example #14
0
        /// <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);
                }
            }


            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);
        }
        protected override async Task <IEnumerable <IDocument> > ExecuteInputAsync(IDocument input, IExecutionContext context)
        {
            context.LogDebug(
                "Processing Markdown {0} for {1}",
                string.IsNullOrEmpty(_sourceKey) ? string.Empty : ("in" + _sourceKey),
                input.ToSafeDisplayString());

            string content;

            if (string.IsNullOrEmpty(_sourceKey))
            {
                content = await input.GetContentStringAsync();
            }
            else if (input.ContainsKey(_sourceKey))
            {
                content = input.GetString(_sourceKey) ?? string.Empty;
            }
            else
            {
                // Don't do anything if the key doesn't exist
                return(input.Yield());
            }

            MarkdownPipeline pipeline = CreateMarkdownPipeline();

            string result;

            using (StringWriter writer = new StringWriter())
            {
                HtmlRenderer htmlRenderer = new HtmlRenderer(writer);
                pipeline.Setup(htmlRenderer);

                if (_prependLinkRoot && context.Settings.ContainsKey(Keys.LinkRoot))
                {
                    htmlRenderer.LinkRewriter = (link) =>
                    {
                        if (string.IsNullOrEmpty(link))
                        {
                            return(link);
                        }

                        if (link[0] == '/')
                        {
                            // root-based url, must be rewritten by prepending the LinkRoot setting value
                            // ex: '/virtual/directory' + '/relative/abs/link.html' => '/virtual/directory/relative/abs/link.html'
                            link = context.Settings[Keys.LinkRoot] + link;
                        }

                        return(link);
                    };
                }

                MarkdownDocument document = MarkdownParser.Parse(content, pipeline);
                htmlRenderer.Render(document);
                writer.Flush();
                result = writer.ToString();
            }

            if (_escapeAt)
            {
                result = EscapeAtRegex.Replace(result, "&#64;");
                result = result.Replace("\\@", "@");
            }

            return(string.IsNullOrEmpty(_sourceKey)
                ? input.Clone(await context.GetContentProviderAsync(result)).Yield()
                : input
                   .Clone(new MetadataItems
            {
                { string.IsNullOrEmpty(_destinationKey) ? _sourceKey : _destinationKey, result }
            })
                   .Yield());
        }
Example #16
0
 /// <summary>
 /// Inline HTML.
 /// </summary>
 /// <param name="Document">Markdown document.</param>
 /// <param name="EMail">Automatic e-Mail link.</param>
 public AutomaticLinkMail(MarkdownDocument Document, string EMail)
     : base(Document)
 {
     this.eMail = EMail;
 }
 public SampleAppMarkdownRenderer(MarkdownDocument document, ILinkRegister linkRegister, IImageResolver imageResolver, ICodeBlockResolver codeBlockResolver)
     : base(document, linkRegister, imageResolver, codeBlockResolver)
 {
     LanguageRequested += SampleAppMarkdownRenderer_LanguageRequested;
 }
Example #18
0
 /// <summary>
 /// Represents a code block in a markdown document.
 /// </summary>
 /// <param name="Document">Markdown document.</param>
 /// <param name="Rows">Rows</param>
 /// <param name="Start">Start index of code.</param>
 /// <param name="End">End index of code.</param>
 /// <param name="Indent">Additional indenting.</param>
 public CodeBlock(MarkdownDocument Document, string[] Rows, int Start, int End, int Indent)
     : this(Document, Rows, Start, End, Indent, null)
 {
 }
Example #19
0
 /// <summary>
 /// Represents a task item in a task list.
 /// </summary>
 /// <param name="Document">Markdown document.</param>
 /// <param name="IsChecked">If the item is checked or not.</param>
 /// <param name="Child">Child element.</param>
 public TaskItem(MarkdownDocument Document, bool IsChecked, MarkdownElement Child)
     : base(Document, Child)
 {
     this.isChecked = IsChecked;
 }
Example #20
0
 /// <summary>
 /// Represents a definition list in a markdown document.
 /// </summary>
 /// <param name="Document">Markdown document.</param>
 /// <param name="Children">Child elements.</param>
 public DefinitionList(MarkdownDocument Document, IEnumerable <MarkdownElement> Children)
     : base(Document, Children)
 {
 }
Example #21
0
 public static bool HasAbbreviations(this MarkdownDocument document)
 {
     return(document.GetAbbreviations() != null);
 }
Example #22
0
 /// <summary>
 /// Represents a definition list in a markdown document.
 /// </summary>
 /// <param name="Document">Markdown document.</param>
 /// <param name="Children">Child elements.</param>
 public DefinitionList(MarkdownDocument Document, params MarkdownElement[] Children)
     : base(Document, Children)
 {
 }
Example #23
0
 public static Dictionary <string, Abbreviation> GetAbbreviations(this MarkdownDocument document)
 {
     return(document.GetData(DocumentKey) as Dictionary <string, Abbreviation>);
 }
Example #24
0
 /// <summary>
 /// Represents a bullet list in a markdown document.
 /// </summary>
 /// <param name="Document">Markdown document.</param>
 /// <param name="Children">Child elements.</param>
 public BulletList(MarkdownDocument Document, IEnumerable <MarkdownElement> Children)
     : base(Document, Children)
 {
 }
Example #25
0
 /// <summary>
 /// Sends a chat message.
 /// </summary>
 /// <param name="Message">Text message to send.</param>
 /// <param name="Markdown">Markdown document, if any, or null if plain text.</param>
 /// <exception cref="NotSupportedException">If the feature is not supported by the node.</exception>
 public virtual void SendChatMessage(string Message, MarkdownDocument Markdown)
 {
     throw new NotSupportedException();
 }
Example #26
0
 /// <summary>
 /// Horizontal rule
 /// </summary>
 /// <param name="Document">Markdown document.</param>
 /// <param name="Row">Markdown definition.</param>
 public HorizontalRule(MarkdownDocument Document, string Row)
     : base(Document)
 {
     this.row = Row;
 }
Example #27
0
 /// <summary>
 /// Creates an object of the same type, and meta-data, as the current object,
 /// but with content defined by <paramref name="Children"/>.
 /// </summary>
 /// <param name="Children">New content.</param>
 /// <param name="Document">Document that will contain the element.</param>
 /// <returns>Object of same type and meta-data, but with new content.</returns>
 public override MarkdownElementChildren Create(IEnumerable <MarkdownElement> Children, MarkdownDocument Document)
 {
     return(new Paragraph(Document, Children));
 }
Example #28
0
 /// <summary>
 /// Creates an object of the same type, and meta-data, as the current object,
 /// but with content defined by <paramref name="Child"/>.
 /// </summary>
 /// <param name="Child">New content.</param>
 /// <param name="Document">Document that will contain the element.</param>
 /// <returns>Object of same type and meta-data, but with new content.</returns>
 public override MarkdownElementSingleChild Create(MarkdownElement Child, MarkdownDocument Document)
 {
     return(new TaskItem(Document, this.isChecked, this.checkPosition, Child));
 }
Example #29
0
 /// <summary>
 /// Represents a paragraph in a markdown document.
 /// </summary>
 /// <param name="Document">Markdown document.</param>
 /// <param name="Children">Child elements.</param>
 public Paragraph(MarkdownDocument Document, IEnumerable <MarkdownElement> Children)
     : base(Document, Children)
 {
 }
Example #30
0
        private MarkdownDocument GetMarkdown(MultimediaItem Item, string ParentURL)
        {
            int    i = Item.Url.IndexOf('?');
            string Query;
            string FileName;

            if (i < 0)
            {
                Query    = null;
                FileName = Item.Url;
            }
            else
            {
                Query    = Item.Url.Substring(i + 1);
                FileName = Item.Url.Substring(0, i);
            }

            if (!string.IsNullOrEmpty(ParentURL))
            {
                if (Uri.TryCreate(new Uri(ParentURL), FileName, out Uri NewUri))
                {
                    ParentURL = NewUri.ToString();
                }
            }

            FileName = Path.Combine(Path.GetDirectoryName(Item.Document.FileName), FileName);

            if (!string.IsNullOrEmpty(Query))
            {
                Variables Variables = Item.Document.Settings.Variables;
                string    Value;

                if (Variables != null)
                {
                    foreach (string Part in Query.Split('&'))
                    {
                        i = Part.IndexOf('=');
                        if (i < 0)
                        {
                            Variables[Part] = string.Empty;
                        }
                        else
                        {
                            Value = Part.Substring(i + 1);

                            if (double.TryParse(Value.Replace(".", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator), out double d))
                            {
                                Variables[Part.Substring(0, i)] = d;
                            }
                            else if (bool.TryParse(Value, out bool b))
                            {
                                Variables[Part.Substring(0, i)] = b;
                            }
                            else
                            {
                                Variables[Part.Substring(0, i)] = Value;
                            }
                        }
                    }
                }
            }

            string           MarkdownText = File.ReadAllText(FileName);
            MarkdownDocument Markdown     = new MarkdownDocument(MarkdownText, Item.Document.Settings, FileName, string.Empty, ParentURL)
            {
                Master = Item.Document
            };

            MarkdownDocument Loop = Item.Document;

            while (Loop != null)
            {
                if (Loop.FileName == FileName)
                {
                    throw new Exception("Circular reference detected.");
                }

                Loop = Loop.Master;
            }

            return(Markdown);
        }
Example #31
0
 /// <summary>
 /// Represents an atom of editable text (i.e. typed character).
 /// </summary>
 public Atom(MarkdownDocument Document, IEditableText Source, char Character)
     : base(Document)
 {
     this.source    = Source;
     this.character = Character;
 }