Beispiel #1
0
        // TODO: передавати одразу список посилань на зображення.
        static string telegraphParserMethod(HtmlNode imagesNode, HtmlNode htmlNode)
        {
            var listOfNodes = new List <Object>();

            foreach (var node in htmlNode.ChildNodes)
            {
                listOfNodes.Add(domToNode(node));
            }
            List <string> imageUrls = ParseImage(imagesNode);// Парсимо зображення методом "ParseImage"

            foreach (var item in imageUrls)
            {
                TelegraphNode imgNode = new TelegraphNode()
                {
                    tag = "img"
                };
                imgNode.attrs        = new Dictionary <string, string>();
                imgNode.attrs["src"] = item;
                listOfNodes.Add(imgNode);
            }
            string output = JsonConvert.SerializeObject(listOfNodes, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });

            return(output);//в результаті маємо контент зі всіма тегами і посиланнями для постингу.
        }
Beispiel #2
0
        //static List<string> TextDevidingOnParts(string text)
        //{
        //    int counter = 0;
        //    char newLine = '\n';
        //    string result = "";
        //    string resultCount = "";
        //    string[] arr = text.Split(newLine);
        //    List<string> resultList = new List<string>();
        //    while (resultCount.Length < text.Length)
        //    {
        //        if (result.Length < 4096 && counter < arr.Length)
        //        {
        //            result += arr[counter] + newLine;
        //            counter++;
        //        }
        //        else
        //        {
        //            resultList.Add(result.TrimStart(' '));
        //            resultCount += result;
        //            result = "";
        //        }
        //    }
        //    return resultList;
        //}
        //static List<TelegraphContent> TextOnParts(string text)
        //{
        //    int counter = 0;
        //    char newLine = '\n';
        //    string result = "";
        //    string resultCount = "";
        //    string[] arr = text.Split(newLine);
        //    List<TelegraphContent> resultList = new List<TelegraphContent>();
        //    while (resultCount.Length < text.Length)
        //    {
        //        if (result.Length < 4096 && counter < arr.Length)
        //        {
        //            result += arr[counter] + newLine;
        //            counter++;
        //        }
        //        else
        //        {
        //            resultList.Add(new TelegraphContent { tag = new Tag { Name = "p" }, content = new Content { ChildrenContent = result } });
        //            resultCount += result;
        //            result = "";
        //        }
        //        //return resultList;
        //    }
        //    return resultList;
        //}
        static object domToNode(HtmlNode node)
        {
            if (node.NodeType == HtmlNodeType.Text)
            {
                if (node.InnerText.Length == 0)
                {
                    return(null);
                }
                return(node.InnerText);
            }

            if (node.NodeType != HtmlNodeType.Element)
            {
                return(null);
            }

            var telegrahpNode = new TelegraphNode();

            telegrahpNode.tag = node.OriginalName.ToLower();

            var hrefValue = node.GetAttributeValue("href", null);
            var srcValue  = node.GetAttributeValue("src", null);

            if ((hrefValue != null || srcValue != null) && telegrahpNode.attrs == null)
            {
                telegrahpNode.attrs = new Dictionary <string, string>();
            }
            if (hrefValue != null)
            {
                telegrahpNode.attrs["href"] = hrefValue;
            }
            if (srcValue != null)
            {
                telegrahpNode.attrs["src"] = srcValue;
            }

            if (node.ChildNodes.Count > 0)
            {
                telegrahpNode.children = new List <object>();
                foreach (var childNode in node.ChildNodes)
                {
                    telegrahpNode.children.Add(domToNode(childNode));
                }
            }
            return(telegrahpNode);
        }