Exemplo n.º 1
0
        private string TagToYaml(YamlNode node, string defaultTag)
        {
            var tag = node.ShorthandTag();

            if (tag == YamlNode.ShorthandTag(defaultTag))
            {
                return("");
            }
            if (tag == YamlNode.ShorthandTag(GetPropertyOrNull(node, "expectedTag")))
            {
                return("");
            }
            if (config.DoNotUseVerbatimTag)
            {
                if (tag.StartsWith("!"))
                {
                    tag = tag.UriEscapeForTag();
                }
                else
                {
                    tag = "!<" + tag.UriEscape() + ">";
                }
            }
            else
            {
                tag = tag.UriEscape();
                if (!CanBeShorthand.IsMatch(tag))
                {
                    tag = "!<" + tag + ">";
                }
            }
            return(tag);
        }
Exemplo n.º 2
0
        private void ScalarToYaml(YamlScalar node, string pres, Context c)
        {
            var s = node.Value;

            // If tag can be resolved from the content, or tag is !!str,
            // no need to explicitly specify it.
            var auto_tag = YamlNode.ShorthandTag(AutoTagResolver.Resolve(s));
            var tag      = TagToYaml(node, auto_tag);

            if (tag != "" && tag != "!!str")
            {
                Write(tag + " ");
            }

            if (IsValidPlainText(s, c) && !(node.ShorthandTag() == "!!str" && auto_tag != null && !node.Properties.ContainsKey("plainText")))
            {
                // one line plain style
                Write(s);
                if (c != Context.NoBreak)
                {
                    WriteLine();
                }
            }
            else
            {
                if (ForbiddenChars.IsMatch(s) || OneLine.IsMatch(s) ||
                    (config.ExplicitlyPreserveLineBreaks &&
                     GetPropertyOrNull(node, "Don'tCareLineBreaks") == null))
                {
                    // double quoted
                    Write(DoubleQuotedString.Quote(s, pres, c));
                    if (c != Context.NoBreak)
                    {
                        WriteLine();
                    }
                }
                else
                {
                    // Literal style
                    if (s[s.Length - 1] == '\n' || s[s.Length - 1] == '\r')
                    {
                        WriteLine("|+2");
                    }
                    else
                    {
                        WriteLine("|-2");
                        s += "\r\n"; // guard
                    }
                    var press = pres + "  ";
                    for (int p = 0; p < s.Length;)
                    {
                        var m = UntilBreak.Match(s, p); // does not fail because of the guard
                        Write(pres + s.Substring(p, m.Length));
                        p += m.Length;
                    }
                }
            }
        }