Beispiel #1
0
        protected override void Write(MamlRenderer renderer, ListBlock listBlock)
        {
            renderer.EnsureLine();

            if (listBlock.IsOrdered)
            {
                renderer.Push(MamlElements.list, new Dictionary <string, string> {
                    ["class"] = "ordered"
                });
            }
            else
            {
                renderer.Push(MamlElements.list, new Dictionary <string, string> {
                    ["class"] = "bullet"
                });
            }
            foreach (var item in listBlock)
            {
                var listItem = (ListItemBlock)item;

                renderer.EnsureLine();
                renderer.Push(MamlElements.listItem);
                renderer.WriteChildren(listItem);
                renderer.PopTo(MamlElements.listItem);
            }
            renderer.PopTo(MamlElements.list);
        }
 protected override void Write(MamlRenderer renderer, AutolinkInline obj)
 {
     renderer.Push(MamlElements.externalLink);
     renderer.Push(MamlElements.linkUri);
     renderer.Write(obj.Url);
     renderer.PopTo(MamlElements.linkUri);
     renderer.PopTo(MamlElements.externalLink);
 }
Beispiel #3
0
        protected override void Write(MamlRenderer renderer, MathBlock obj)
        {
            string formulaText = string.Empty; // obj.Content.Text.Substring(obj.Content.Start, obj.Content.Length);

            for (int i = 0; i < obj.Lines.Count; ++i)
            {
                var l = obj.Lines.Lines[i];
                formulaText += l.Slice.Text.Substring(l.Slice.Start, l.Slice.Length);
            }

            if (string.IsNullOrEmpty(formulaText))
            {
                return;
            }

            var formulaService = Current.GetRequiredService <ILaTeXFormulaImageStreamProvider>();

            var(stream, placement, offset, width, height) = formulaService.Parse(formulaText, renderer.BodyTextFontFamily, renderer.BodyTextFontSize, 192, renderer.IsIntendedForHelp1File);

            if (null == stream)
            {
                return;
            }

            stream.Seek(0, SeekOrigin.Begin);
            var streamHash = MemoryStreamImageProxy.ComputeStreamHash(stream);

            stream.Seek(0, SeekOrigin.Begin);

            try
            {
                renderer.StorePngImageFile(stream, streamHash);
                stream.Close();
            }
            finally
            {
                stream.Dispose();
            }

            // now render to Maml file

            string localUrl = "../media/" + streamHash + ".png";

            var attributes = new Dictionary <string, string>
            {
                { "src", localUrl },
                { "width", System.Xml.XmlConvert.ToString(width) },
                { "height", System.Xml.XmlConvert.ToString(height) }
            };

            renderer.Push(MamlElements.markup);

            renderer.Push(MamlElements.a, new[] { new KeyValuePair <string, string>("href", renderer.ImageTopicFileGuid + ".htm#" + streamHash) });

            renderer.Push(MamlElements.img, attributes);

            renderer.PopTo(MamlElements.markup);
        }
Beispiel #4
0
        protected override void Write(MamlRenderer renderer, HeadingBlock obj)
        {
            bool newFileWasStarted = renderer.TryStartNewMamlFile(obj);

            if (newFileWasStarted)
            {
            }
            else
            {
                // Ensure we have the sections element somewhere down the line ...
                // we need (obj.Level - renderer.SplitLevel) section elements down the stack
                int numberOfSectionsElementsRequired = obj.Level - renderer.SplitLevel - 1;
                int numberOfSectionsElementsOnStack  = renderer.NumberOfElementsOnStack(MamlElements.sections);

                // Push sections element if required
                for (int i = 0; i < numberOfSectionsElementsRequired - numberOfSectionsElementsOnStack; ++i)
                {
                    renderer.Push(MamlElements.sections);
                }

                if (numberOfSectionsElementsOnStack > 0 && numberOfSectionsElementsRequired > 0)
                {
                    // Or pop sections elements if required
                    for (int i = 0; i < numberOfSectionsElementsOnStack - numberOfSectionsElementsRequired; ++i)
                    {
                        renderer.PopToBefore(MamlElements.sections);
                    }
                }

                if (numberOfSectionsElementsRequired == 0)
                {
                    renderer.PopToBefore(MamlElements.developerConceptualDocument);
                }

                // Find a unique address in order for AutoOutline to work
                var    attr          = (Markdig.Renderers.Html.HtmlAttributes)obj.GetData(typeof(Markdig.Renderers.Html.HtmlAttributes));
                string uniqueAddress = attr?.Id; // this header has a user defined address
                if (string.IsNullOrEmpty(uniqueAddress))
                {
                    renderer.HeaderGuids.TryGetValue(obj.Span.Start, out uniqueAddress); // use the guid generated from the hierarchy of titles
                }
                if (string.IsNullOrEmpty(uniqueAddress))
                {
                    uniqueAddress = Guid.NewGuid().ToString(); // fallback (should not happen): Create Guid
                }
                renderer.Push(MamlElements.section, new[] { new KeyValuePair <string, string>("address", uniqueAddress) });

                renderer.Push(MamlElements.title);
                renderer.WriteLeafInline(obj);
                renderer.EnsureLine();
                renderer.PopTo(MamlElements.title);
                renderer.Push(MamlElements.content);
            }
        }
Beispiel #5
0
 protected override void Write(MamlRenderer renderer, Markdig.Extensions.Figures.FigureCaption obj)
 {
     renderer.Push(MamlElements.para);
     renderer.WriteLeafInline(obj);
     renderer.EnsureLine();
     renderer.PopTo(MamlElements.para);
 }
Beispiel #6
0
        protected override void Write(MamlRenderer renderer, QuoteBlock obj)
        {
            renderer.EnsureLine();

            renderer.Push(MamlElements.quote);
            renderer.WriteChildren(obj);
            renderer.PopTo(MamlElements.quote);
        }
Beispiel #7
0
        protected override void Write(MamlRenderer renderer, CodeBlock obj)
        {
            renderer.EnsureLine();

            renderer.Push(MamlElements.code);
            renderer.WriteLeafRawLines(obj, true, true);
            renderer.PopTo(MamlElements.code);
        }
Beispiel #8
0
        protected override void Write(MamlRenderer renderer, ThematicBreakBlock obj)
        {
            // since there is no direct equivalent to a thematic break in Maml,
            // here we use a paragraph with 80 underlines

            renderer.Push(MamlElements.markup);
            renderer.Write("<hr/>");
            renderer.PopTo(MamlElements.markup);
        }
        protected override void Write(MamlRenderer renderer, EmphasisInline obj)
        {
            MamlElement mamlElement = null;

            switch (obj.DelimiterChar)
            {
            case '*':
            case '_':
                if (obj.IsDouble)
                {
                    mamlElement = MamlElements.legacyBold;
                }
                else
                {
                    mamlElement = MamlElements.legacyItalic;
                }
                break;

            case '~':
                if (obj.IsDouble)
                {
                    mamlElement = MamlElements.legacyStrikethrough;
                }
                else
                {
                    mamlElement = MamlElements.subscript;
                }

                break;

            case '^':
                mamlElement = MamlElements.superscript;
                break;

            case '+':
                // Inserted style
                mamlElement = MamlElements.legacyUnderline;
                break;

            case '=':
                // Marked style
                break;
            }

            if (null != mamlElement)
            {
                renderer.Push(mamlElement);
            }
            renderer.WriteChildren(obj);
            if (null != mamlElement)
            {
                renderer.PopTo(mamlElement);
            }
        }
Beispiel #10
0
        protected override void Write(MamlRenderer renderer, ParagraphBlock obj)
        {
            if (!renderer.IsFirstInContainer)
            {
                renderer.EnsureLine();
            }

            renderer.Push(MamlElements.para);
            renderer.WriteLeafInline(obj);
            renderer.EnsureLine();
            renderer.PopTo(MamlElements.para);
        }
Beispiel #11
0
        /// <inheritdoc/>
        protected override void Write(MamlRenderer renderer, LinkInline link)
        {
            var url = link.GetDynamicUrl != null?link.GetDynamicUrl() ?? link.Url : link.Url;

            if (link.IsImage)
            {
                RenderImage(renderer, link, url);
            }
            else // link is not an image
            {
                if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
                {
                    renderer.Push(MamlElements.externalLink);
                    renderer.Push(MamlElements.linkText);
                    renderer.WriteChildren(link);
                    renderer.PopTo(MamlElements.linkText);

                    renderer.Push(MamlElements.linkUri);
                    renderer.Write(url);
                    renderer.PopTo(MamlElements.linkUri);
                    renderer.PopTo(MamlElements.externalLink);
                }
                else // not a well formed Uri String - then it is probably a fragment reference
                {
                    // the challenge here is to find out where (in which file) our target is. The file might even not be defined in the moment
                    var(fileGuid, localUrl) = renderer.FindFragmentLink(url);
                    string totalAddress = string.Empty;
                    if (null != fileGuid && null != localUrl)
                    {
                        totalAddress = fileGuid + "#" + localUrl;
                    }

                    renderer.Push(MamlElements.link, new[] { new KeyValuePair <string, string>("xlink:href", totalAddress) });
                    renderer.WriteChildren(link);
                    renderer.PopTo(MamlElements.link);
                }
            }
        }
Beispiel #12
0
        protected override void Write(MamlRenderer renderer, Table table)
        {
            renderer.Push(MamlElements.table);
            renderer.Push(MamlElements.tableHeader);

            foreach (TableRow row in table)
            {
                if (row.IsHeader)
                {
                    renderer.Push(MamlElements.row);

                    foreach (TableCell cell in row)
                    {
                        renderer.Push(MamlElements.entry);
                        renderer.Write(cell);
                        renderer.PopTo(MamlElements.entry);
                    }

                    renderer.PopTo(MamlElements.row);
                }
            }

            renderer.PopTo(MamlElements.tableHeader);

            foreach (TableRow row in table)
            {
                if (!row.IsHeader)
                {
                    renderer.Push(MamlElements.row);

                    foreach (TableCell cell in row)
                    {
                        renderer.Push(MamlElements.entry);
                        renderer.Write(cell);
                        renderer.PopTo(MamlElements.entry);
                    }

                    renderer.PopTo(MamlElements.row);
                }
            }

            renderer.PopTo(MamlElements.table);
        }
Beispiel #13
0
        private void RenderImage(MamlRenderer renderer, LinkInline link, string url)
        {
            double?width = null, height = null;

            if (link.ContainsData(typeof(Markdig.Renderers.Html.HtmlAttributes)))
            {
                var htmlAttributes = (Markdig.Renderers.Html.HtmlAttributes)link.GetData(typeof(Markdig.Renderers.Html.HtmlAttributes));
                if (null != htmlAttributes.Properties)
                {
                    foreach (var entry in htmlAttributes.Properties)
                    {
                        switch (entry.Key.ToLowerInvariant())
                        {
                        case "width":
                            width = GetLength(entry.Value);
                            break;

                        case "height":
                            height = GetLength(entry.Value);
                            break;
                        }
                    }
                }
            }

            if (null != renderer.OldToNewImageUris && renderer.OldToNewImageUris.ContainsKey(url))
            {
                url = renderer.OldToNewImageUris[url];
            }

            if (width == null && height == null) // if we include the image in its native resolution, we do not need a link to the native resolution image
            {
                string localUrl = System.IO.Path.GetFileNameWithoutExtension(url);

                renderer.Push(MamlElements.mediaLinkInline);

                renderer.Push(MamlElements.image, new[] { new KeyValuePair <string, string>("xlink:href", localUrl) });

                renderer.PopTo(MamlElements.image);

                renderer.PopTo(MamlElements.mediaLinkInline);
            }
            else // width or height or both specified
            {
                string localUrl = "../media/" + System.IO.Path.GetFileName(url);

                var attributes = new Dictionary <string, string>
                {
                    { "src", localUrl }
                };
                if (width.HasValue)
                {
                    attributes.Add("width", System.Xml.XmlConvert.ToString(Math.Round(width.Value)));
                }
                if (height.HasValue)
                {
                    attributes.Add("height", System.Xml.XmlConvert.ToString(height.Value));
                }

                renderer.Push(MamlElements.markup);

                renderer.Push(MamlElements.a, new[] { new KeyValuePair <string, string>("href", renderer.ImageTopicFileGuid + ".htm#" + System.IO.Path.GetFileNameWithoutExtension(url)) });

                renderer.Push(MamlElements.img, attributes);

                renderer.PopTo(MamlElements.markup);
            }
        }
Beispiel #14
0
 protected override void Write(MamlRenderer renderer, CodeInline obj)
 {
     renderer.Push(MamlElements.codeInline);
     renderer.WriteEscape(obj.Content);
     renderer.PopTo(MamlElements.codeInline);
 }
Beispiel #15
0
        protected override void Write(MamlRenderer renderer, MathInline obj)
        {
            var formulaText = obj.Content.Text.Substring(obj.Content.Start, obj.Content.Length);

            if (string.IsNullOrEmpty(formulaText))
            {
                return;
            }

            var formulaService = Current.GetRequiredService <ILaTeXFormulaImageStreamProvider>();

            var(stream, placement, offset, width, height) = formulaService.Parse(formulaText, renderer.BodyTextFontFamily, renderer.BodyTextFontSize, 192, renderer.IsIntendedForHelp1File);

            if (null == stream)
            {
                return;
            }

            stream.Seek(0, SeekOrigin.Begin);
            var streamHash = MemoryStreamImageProxy.ComputeStreamHash(stream);

            stream.Seek(0, SeekOrigin.Begin);

            try
            {
                renderer.StorePngImageFile(stream, streamHash);
                stream.Close();
            }
            finally
            {
                stream.Dispose();
            }

            // now render to Maml file

            string localUrl = "../media/" + streamHash + ".png";

            var attributes = new Dictionary <string, string>
            {
                { "src", localUrl },
                { "width", System.Xml.XmlConvert.ToString(width) },
                { "height", System.Xml.XmlConvert.ToString(height) }
            };

            if (renderer.IsIntendedForHelp1File)
            {
                attributes.Add("align", placement);
            }
            else
            {
                attributes.Add("style", string.Format(System.Globalization.CultureInfo.InvariantCulture, "vertical-align:{0}px;", offset));
            }

            renderer.Push(MamlElements.markup);

            renderer.Push(MamlElements.a, new[] { new KeyValuePair <string, string>("href", renderer.ImageTopicFileGuid + ".htm#" + streamHash) });

            renderer.Push(MamlElements.img, attributes);

            renderer.PopTo(MamlElements.markup);
        }