Esempio n. 1
0
        public void Parse(
            Container container,
            IDocumentReader reader,
            Func <string, bool> predicate,
            ref List <string> buffer,
            ref AttributeList attributes)
        {
            var match = PatternMatcher.Anchor.Match(reader.Line);

            if (!match.Success)
            {
                throw new ArgumentException("not an anchor");
            }

            var id = match.Groups["id"].Value;

            var reference = !string.IsNullOrEmpty(match.Groups["reference"].Value)
                ? match.Groups["reference"].Value
                : null;

            var anchor = new Anchor(id, reference);

            if (attributes != null)
            {
                attributes.Add(anchor);
            }
            else
            {
                attributes = new AttributeList {
                    anchor
                };
            }

            reader.ReadLine();
        }
Esempio n. 2
0
        public override void InternalParse(Container container, IDocumentReader reader, Func <string, bool> predicate, ref List <string> buffer,
                                           ref AttributeList attributes)
        {
            var match = PatternMatcher.Title.Match(reader.Line);

            if (!match.Success)
            {
                throw new ArgumentException("not a block title");
            }

            var title = new Title(match.Groups["title"].Value);

            if (attributes != null)
            {
                attributes.Add(title);
            }
            else
            {
                attributes = new AttributeList {
                    title
                }
            };

            reader.ReadLine();
        }
    }
Esempio n. 3
0
        private static AttributeList ParseElementAttributesWithPosition(string input, int position)
        {
            var attributes = new AttributeList();

            input = input.Trim();
            var start = 0;

            for (var index = 0; index < input.Length; index++)
            {
                var currentChar = input[index];
                var last        = index == input.Length - 1;

                if (currentChar == '%')
                {
                    var options           = new List <string>();
                    var optionsStartIndex = index + 1;

                    for (int i = optionsStartIndex; i < input.Length; i++)
                    {
                        var lastChar = i == input.Length - 1;

                        if (input[i] == '%')
                        {
                            options.Add(input.Substring(optionsStartIndex, i - optionsStartIndex));
                            optionsStartIndex = i + 1;
                        }
                        else if (lastChar || input[i] == '#' || input[i] == '.' || input[i] == '=')
                        {
                            options.Add(lastChar
                                ? input.Substring(optionsStartIndex, i - (optionsStartIndex - 1))
                                : input.Substring(optionsStartIndex, i - 1));

                            attributes.Add(new OptionsAttribute(options.ToArray(), false));
                            index = i;
                            start = i;
                            break;
                        }
                    }
                }
                else if (currentChar == '#')
                {
                    var startIdIndex = index + 1;
                    for (var i = startIdIndex; i < input.Length; i++)
                    {
                        var lastChar = i == input.Length - 1;

                        if (lastChar)
                        {
                            var value = input.Substring(startIdIndex);
                            attributes.Add(new IdAttribute(value, false));
                            index = i;
                            start = i;
                            break;
                        }
                        if (input[i] == '.' || input[i] == '%' || input[i] == '=')
                        {
                            var value = input.Substring(startIdIndex, i - 1);
                            attributes.Add(new IdAttribute(value, false));
                            index = i - 1;
                            start = i - 1;
                            break;
                        }
                    }
                }
                else if (currentChar == '.')
                {
                    var options        = new List <string>();
                    var roleStartIndex = index + 1;

                    for (var i = roleStartIndex; i < input.Length; i++)
                    {
                        var lastChar = i == input.Length - 1;

                        if (input[i] == '.')
                        {
                            options.Add(input.Substring(roleStartIndex, i - roleStartIndex));
                            roleStartIndex = i + 1;
                        }
                        else if (lastChar || input[i] == '#' || input[i] == '%' || input[i] == '=')
                        {
                            options.Add(lastChar
                                ? input.Substring(roleStartIndex, i - (roleStartIndex - 1))
                                : input.Substring(roleStartIndex, i - 1));

                            attributes.Add(new RoleAttribute(options, false));
                            index = i;
                            start = i;
                            break;
                        }
                    }
                }
                else if (currentChar == '=')
                {
                    var name = input.Substring(start, index);
                    for (int i = index + 1; i < input.Length; i++)
                    {
                        var lastChar = i == input.Length - 1;

                        if (lastChar || input[i] == '#' || input[i] == '%' || input[i] == '.')
                        {
                            var singleQuoted = input[index + 1] == '\'';

                            var value = singleQuoted || input[index + 1] == '"'
                                ? input.Substring(index + 2, i - (name.Length + 2))
                                : input.Substring(index + 1, i - name.Length);

                            // TODO: handle known named elements
                            switch (name.ToLowerInvariant())
                            {
                            case "id":
                                if (value.IndexOf(",", StringComparison.OrdinalIgnoreCase) > -1)
                                {
                                    var valueParts = value.Split(',');
                                    attributes.Add(new IdAttribute(valueParts[0], valueParts[1], singleQuoted));
                                }
                                else
                                {
                                    attributes.Add(new IdAttribute(value, singleQuoted));
                                }
                                break;

                            case "role":
                                attributes.Add(new RoleAttribute(value, singleQuoted));
                                break;

                            case "options":
                            case "opts":
                                attributes.Add(new OptionsAttribute(value, singleQuoted));
                                break;

                            case "subs":
                                attributes.Add(new SubstitutionsAttribute(value));
                                break;

                            default:
                                attributes.Add(new NamedAttribute(name, value, singleQuoted));
                                break;
                            }

                            index = i;
                            start = i;
                            break;
                        }
                    }
                }
                else if (index == 0 && position == 0)
                {
                    var hashIndex    = input.IndexOf("#", StringComparison.OrdinalIgnoreCase);
                    var dotIndex     = input.IndexOf(".", StringComparison.OrdinalIgnoreCase);
                    var percentIndex = input.IndexOf("%", StringComparison.OrdinalIgnoreCase);

                    if (hashIndex > -1 || dotIndex > -1 || percentIndex > -1)
                    {
                        var minIndex = new[] { hashIndex, dotIndex, percentIndex }.Where(i => i != -1).Min();
                        if (minIndex > 1)
                        {
                            var name = input.Substring(0, minIndex);
                            attributes.Add(new Attribute(name));
                            index = minIndex - 1;
                            start = minIndex - 1;
                        }
                    }
                }
                else if (last)
                {
                    var name = input.Substring(start);
                    attributes.Add(new Attribute(name));
                }
            }

            return(attributes);
        }
Esempio n. 4
0
        private AttributeList ParseQuotedAttributes(string value)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return(null);
            }

            if (value.IndexOf("{", StringComparison.CurrentCultureIgnoreCase) > -1)
            {
                // TODO: Handle attribute references. Do nothing for now, let them fall through
            }

            value = value.Trim();

            if (value.IndexOf(",", StringComparison.OrdinalIgnoreCase) > -1)
            {
                value = value.Split(new[] { ',' }, 2)[0];
            }

            if (value.Length == 0)
            {
                return(new AttributeList());
            }

            if (value.StartsWith(".") || value.StartsWith("#"))
            {
                var              segments = value.Split(new[] { '#' }, 2);
                string           id       = null;
                HashSet <string> roles    = new HashSet <string>();
                if (segments.Length > 1)
                {
                    var subSegments = segments[1].Split('.');

                    if (subSegments.Length > 1)
                    {
                        id = subSegments[0];
                        for (int i = 1; i < subSegments.Length; i++)
                        {
                            roles.Add(subSegments[i]);
                        }
                    }
                }

                foreach (var role in segments[0].Split('.'))
                {
                    roles.Add(role);
                }

                var attributes = new AttributeList();
                if (id != null)
                {
                    attributes.Add(new IdAttribute(id, false));
                }
                if (roles.Any())
                {
                    attributes.Add(new RoleAttribute(roles, false));
                }

                return(attributes);
            }

            return(new AttributeList {
                new RoleAttribute(value, false)
            });
        }