Exemplo n.º 1
0
        public static string[] ToValues(string html, string objectSelector, string propertySelector)
        {
            var htmlObjectSelector   = SelectorCache.GetOrAddObject(objectSelector);
            var htmlNodes            = htmlObjectSelector.Execute(html);
            var htmlPropertySelector = SelectorCache.GetOrAddProperty(propertySelector);

            return(htmlNodes.Select(htmlPropertySelector.Execute).ToArray());
        }
Exemplo n.º 2
0
 protected internal void SetInternal <TProperty>(PropertyInfo propertyInfo, string propertySelector, Func <string, TProperty> propertyValueParser)
 {
     AddSetter((htmlNode, currentObject) =>
     {
         var htmlPropertySelector  = SelectorCache.GetOrAddProperty(propertySelector);
         var executedPropertyValue = htmlPropertySelector.Execute(htmlNode);
         var parsedPropertyValue   = propertyValueParser(executedPropertyValue);
         propertyInfo.SetValue(currentObject, parsedPropertyValue);
     });
 }
Exemplo n.º 3
0
        internal TObject[] ParseInternal(HtmlNode htmlNode, int?count = null)
        {
            var returnObjects      = new List <TObject>();
            var htmlObjectSelector = SelectorCache.GetOrAddObject(objectSelector);
            var nodes = htmlObjectSelector.Execute(htmlNode);

            foreach (var node in nodes)
            {
                var obj = objectInitializer();
                foreach (var propertySetter in propertySetters)
                {
                    propertySetter(node, obj);
                }
                returnObjects.Add(obj);
                if (count.HasValue && returnObjects.Count == count.Value)
                {
                    break;
                }
            }
            return(returnObjects.ToArray());
        }
Exemplo n.º 4
0
        public static string ToValue(string html, string selector)
        {
            var htmlSelector = SelectorCache.GetOrAddProperty(selector);

            return(htmlSelector.Execute(html));
        }
Exemplo n.º 5
0
        public string Execute(HtmlNode htmlNode)
        {
            if (!RegexUtils.TryMatch(selector, pattern, out var match))
            {
                throw new ArgumentException("Property selector did not matched");
            }

            var prefixGroup = match.Groups["prefix"];
            var suffixGroup = match.Groups["suffix"];

            var objectGroup = match.Groups["object"];
            var valueGroup  = match.Groups["value"];

            if (!valueGroup.Success)
            {
                throw new ArgumentException("Value group in property pattern did not matched");
            }
            var valuePropGroup = match.Groups["valueProp"];

            if (objectGroup.Success)
            {
                htmlNode = SelectorCache.GetOrAddObject(objectGroup.Value).Execute(htmlNode).FirstOrDefault();
            }

            if (htmlNode == null)
            {
                return(null);
            }

            string r()
            {
                switch (valueGroup.Value)
                {
                case "text":
                    if (valuePropGroup.Success)
                    {
                        switch (valuePropGroup.Value)
                        {
                        case "decoded": return(htmlNode.DecodedInnerText());

                        case "purified": return(htmlNode.Purify().InnerText);

                        case "purified-decoded": return(htmlNode.Purify().DecodedInnerText());
                        }
                    }
                    return(htmlNode.DecodedInnerText());

                case "html":
                    if (valuePropGroup.Success)
                    {
                        switch (valuePropGroup.Value)
                        {
                        case "inner": return(htmlNode.InnerHtml);

                        case "outer": return(htmlNode.OuterHtml);
                        }
                    }
                    return(htmlNode.OuterHtml);

                case "attr":
                    if (valuePropGroup.Success)
                    {
                        return(htmlNode.Attributes[valuePropGroup.Value]?.Value);
                    }
                    return(null);

                case "name":
                    return(htmlNode.Name);

                default:
                    throw new ArgumentException("Value selector must be one of 'text, html, attr, name'");
                }
            }

            if (prefixGroup.Success && suffixGroup.Success)
            {
                return(prefixGroup.Value.TrimEnd('{') + r() + suffixGroup.Value.TrimStart('}'));
            }
            else
            {
                return(r());
            }
        }