private static void FixSpacesInCodeElements(HtmlDocument doc)
        {
            // Replaces HTML content similar to the following:
            //
            //   ...the<code style="COLOR: #2b91af"> ExpectedException </code>attribute...
            //
            // with:
            //
            //   ...the <code style="COLOR: #2b91af">ExpectedException</code> attribute...
            //
            // This is necessary to avoid issues where whitespace within <code>
            // elements is removed by ReverseMarkdown.

            var nodes = doc.DocumentNode.SelectNodes("//code");

            if (nodes != null)
            {
                foreach (var node in nodes)
                {
                    // No need to fix spaces anywhere within <pre>
                    if (node.Ancestors("pre").Any() == true)
                    {
                        continue;
                    }

                    HtmlDocumentHelper.MoveLeadingWhitespaceToParent(node);
                    HtmlDocumentHelper.MoveTrailingWhitespaceToParent(node);
                }
            }
        }
        private static void FixSpacesInInlineElements(
            HtmlNodeCollection nodes)
        {
            // Replaces HTML content similar to the following:
            //
            //   ...click the<b> Advanced </b>button...
            //
            // with:
            //
            //   ...click the <b>Advanced</b> button...

            foreach (var node in nodes)
            {
                Debug.Assert(
                    node.Name == "a" ||
                    node.Name == "b" ||
                    node.Name == "em" ||
                    node.Name == "i" ||
                    node.Name == "strong");

                HtmlDocumentHelper.MoveLeadingWhitespaceToParent(node);

                HtmlDocumentHelper.MoveTrailingWhitespaceToParent(node);
            }
        }
        private static void FixSpacesInCodeSpanElements(HtmlDocument doc)
        {
            // Replaces HTML content similar to the following:
            //
            //   ...changing<code><span style="COLOR: #2b91af"> ReportingService </span></code>references...
            //
            // with:
            //
            //   ...changing <code><span style="COLOR: #2b91af">ReportingService</span></code> references...
            //
            // This is necessary to avoid issues where leading and trailing
            // whitespace within <span> elements inside <code> elements is removed by
            // ReverseMarkdown.

            var nodes = doc.DocumentNode.SelectNodes("//code/span");

            if (nodes != null)
            {
                foreach (var node in nodes)
                {
                    // No need to fix spaces anywhere within <pre>
                    if (node.Ancestors("pre").Any() == true)
                    {
                        continue;
                    }

                    var spanNode = node;
                    var codeNode = node.ParentNode;

                    HtmlDocumentHelper.MoveLeadingWhitespaceToParent(spanNode);
                    HtmlDocumentHelper.MoveTrailingWhitespaceToParent(spanNode);

                    HtmlDocumentHelper.MoveLeadingWhitespaceToParent(codeNode);
                    HtmlDocumentHelper.MoveTrailingWhitespaceToParent(codeNode);
                }
            }
        }
Exemple #4
0
        public static void MoveLeadingWhitespaceToParent(
            HtmlNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (node.FirstChild != null &&
                node.FirstChild.Name == "#text")
            {
                var textNode = node.FirstChild;

                var trimmedText = textNode.InnerText.TrimStart();

                if (trimmedText != textNode.InnerText)
                {
                    HtmlDocumentHelper.EnsureWhitespaceBeforeElement(
                        node);

                    textNode.InnerHtml = trimmedText;
                }
            }
        }
        private static void AppendHugoShortcodeParameterValue(
            StringBuilder sb,
            string parameterName,
            string parameterValue,
            bool omitIfNullOrWhitespace = true,
            bool appendNewLine          = true)
        {
            if (omitIfNullOrWhitespace == true &&
                string.IsNullOrWhiteSpace(parameterValue) == true)
            {
                return;
            }

            // If the parameter value contains a backslash and a double quote,
            // then simply escaping the double quote(s) causes the backslashes
            // to be omitted from the HTML generated by Hugo. To avoid this,
            // use a string literal by using back quotes instead of double
            // quotes (https://golang.org/ref/spec#String_literals)

            bool valueContainsBackslashesAndQuotes =
                parameterValue.Contains(@"\") && parameterValue.Contains("\"");

            var encodedParameterValue =
                HtmlDocumentHelper.NormalizeWhitespace(parameterValue)
                .Replace("\"", "&quot;")
                .Replace("&quot;", "\\&quot;")
                .Trim();

            var quote = "\"";

            if (valueContainsBackslashesAndQuotes == true)
            {
                if (parameterValue.Contains("`") == true)
                {
                    throw new ArgumentException(
                              "The parameter value cannot contain a backslash, a"
                              + " double quote, and a back quote.",
                              nameof(parameterValue));
                }

                quote = "`";

                // Do not escape double quotes when using string literals
                encodedParameterValue = encodedParameterValue.Replace(
                    "\\&quot;",
                    "\"");
            }

            if (string.IsNullOrWhiteSpace(parameterName) == false)
            {
                sb.Append($" {parameterName}={quote}{encodedParameterValue}{quote}");
            }
            else
            {
                sb.Append($" {quote}{encodedParameterValue}{quote}");
            }

            if (appendNewLine == true)
            {
                sb.AppendLine();
            }
        }