コード例 #1
0
        private void PatchCData(XCData cdata, string address, string text, string pageUri)
        {
            var wrapper = cdata.GetWrapper();

            wrapper.Elements("a")
            .FirstOrDefault(e =>
                            e.Attribute("href").Value == address &&
                            e.Value == text)?
            .SetAttributeValue("href", pageUri);

            cdata.Value = wrapper.GetInnerXml();
        }
コード例 #2
0
        /*
         * <a href="..."><span style='vertical-align:super'>[2]</span></a>
         */

        private static void RemoveReference(XCData data, int label)
        {
            var wrapper = data.GetWrapper();

            var a = wrapper.Elements("a").Elements("span")
                    .Where(e =>
                           e.Attribute("style").Value.Contains("vertical-align:super") &&
                           e.Value.Equals($"[{label}]"))
                    .Select(e => e.Parent)
                    .FirstOrDefault();

            if (a != null)
            {
                a.Remove();
                data.Value = wrapper.GetInnerXml();
            }
        }
コード例 #3
0
        private static bool RestyleText(XCData cdata, bool completed)
        {
            var modified = false;
            var wrapper  = cdata.GetWrapper();
            var span     = wrapper.Elements("span").FirstOrDefault(e => e.Attribute("style") != null);

            if (completed)
            {
                if (span == null)
                {
                    wrapper.FirstNode.ReplaceWith(
                        new XElement("span",
                                     new XAttribute("style", "text-decoration:line-through"),
                                     cdata.Value
                                     ));

                    modified = true;
                }
                else
                {
                    var style = new Style(span.Attribute("style").Value);
                    if (!style.IsStrikethrough)
                    {
                        style.IsStrikethrough = true;
                        var css = style.ToCss(false);
                        if (string.IsNullOrEmpty(css))
                        {
                            wrapper.Value = span.Value;
                        }
                        else
                        {
                            span.SetAttributeValue("style", style.ToCss(false));
                        }
                        modified = true;
                    }
                }
            }
            else
            {
                if (span != null)
                {
                    var style = new Style(span.Attribute("style").Value);
                    if (style.IsStrikethrough)
                    {
                        style.IsStrikethrough = false;
                        var css = style.ToCss(false);
                        if (string.IsNullOrEmpty(css))
                        {
                            wrapper.Value = span.Value;
                        }
                        else
                        {
                            span.SetAttributeValue("style", style.ToCss(false));
                        }
                        modified = true;
                    }
                }
            }

            cdata.Value = wrapper.GetInnerXml();

            return(modified);
        }
コード例 #4
0
ファイル: MarkdownWriter.cs プロジェクト: stevencohn/OneMore
        private void WriteText(XCData cdata, bool startParagraph)
        {
            cdata.Value = cdata.Value
                          .Replace("<br>", "  ")       // usually followed by NL so leave it there
                          .Replace("[", "\\[")         // escape to prevent confusion with md links
                          .TrimEnd();

            var wrapper = cdata.GetWrapper();

            foreach (var span in wrapper.Descendants("span").ToList())
            {
                var text = span.Value;
                var att  = span.Attribute("style");
                // span might only have a lang attribute
                if (att != null)
                {
                    var style = new Style(span.Attribute("style").Value);
                    if (style.IsStrikethrough)
                    {
                        text = $"~~{text}~~";
                    }
                    if (style.IsItalic)
                    {
                        text = $"*{text}*";
                    }
                    if (style.IsBold)
                    {
                        text = $"**{text}**";
                    }
                }
                span.ReplaceWith(new XText(text));
            }

            foreach (var anchor in wrapper.Elements("a"))
            {
                var href = anchor.Attribute("href")?.Value;
                if (!string.IsNullOrEmpty(href))
                {
                    if (href.StartsWith("onenote:") || href.StartsWith("onemore:"))
                    {
                        // removes the hyperlink but preserves the text
                        anchor.ReplaceWith(anchor.Value);
                    }
                    else
                    {
                        anchor.ReplaceWith(new XText($"[{anchor.Value}]({href})"));
                    }
                }
            }

            // escape directives
            var raw = wrapper.GetInnerXml()
                      .Replace("&lt;", "\\<")
                      .Replace("|", "\\|");

            if (startParagraph && raw.Length > 0 && raw.StartsWith("#"))
            {
                writer.Write("\\");
            }

            writer.Write(raw);
        }