Exemplo n.º 1
0
        public void TestFormNodeShouldBeNode()
        {
            JsonizeConfiguration jsonizeConfiguration = new JsonizeConfiguration();

            Jsonize jsonize = new Jsonize("<html><head></head><body><form></form></body></html>");
            var     result  = jsonize.ParseHtmlAsJsonString(jsonizeConfiguration);

            Assert.AreEqual("{\"node\":\"Document\",\"child\":[{\"node\":\"Element\",\"tag\":\"html\",\"child\":[{\"node\":\"Element\",\"tag\":\"head\"},{\"node\":\"Element\",\"tag\":\"body\",\"child\":[{\"node\":\"Element\",\"tag\":\"form\"}]}]}]}",
                            result.Replace("\r\n", "").Replace(" ", ""));
        }
        public static string HtmlToJson(string inputHtml)
        {
            var json = new Jsonize(inputHtml).ParseHtmlToTypedJson().Children.FirstOrDefault();

            JsonSerializer jsonWriter = new JsonSerializer
            {
                NullValueHandling = (NullValueHandling)1
            };

            return(JObject.FromObject(json, jsonWriter).ToString());
        }
Exemplo n.º 3
0
        public async Task <dynamic> Convert(string url, string format,
                                            string emptyTextNodeHandling,
                                            string nullValueHandling,
                                            string textTrimHandling,
                                            string classAttributeHandling,
                                            string renderJavascript)
        {
            try
            {
                Jsonize jsonize;
                if (renderJavascript.ToLower().Equals("true"))
                {
                    using (var client = new HttpClient())
                    {
                        client.DefaultRequestHeaders.Add("url", url);
                        var response = await client.GetAsync((Environment.GetEnvironmentVariable("DepthchargeRenderUrl") ?? Environment.GetEnvironmentVariable("APPSETTING_DepthchargeRenderUrl")) + "/api/render");

                        var html = await response.Content.ReadAsStringAsync();

                        jsonize = Jsonize.FromHtmlString(html);
                    }
                }
                else
                {
                    jsonize = await Jsonize.FromHttpUrl(url);
                }
                JsonizeConfiguration jsonizeConfiguration = new JsonizeConfiguration()
                {
                    EmptyTextNodeHandling  = emptyTextNodeHandling.ToLower().Equals("ignore") ? EmptyTextNodeHandling.Ignore : EmptyTextNodeHandling.Include,
                    NullValueHandling      = nullValueHandling.ToLower().Equals("ignore") ? JackWFinlay.Jsonize.NullValueHandling.Ignore : JackWFinlay.Jsonize.NullValueHandling.Include,
                    TextTrimHandling       = textTrimHandling.ToLower().Equals("trim") ? TextTrimHandling.Trim : TextTrimHandling.Include,
                    ClassAttributeHandling = classAttributeHandling.ToLower().Equals("array") ? ClassAttributeHandling.Array : ClassAttributeHandling.String
                };

                if (format.ToLower().Equals("json"))
                {
                    JObject json = jsonize.ParseHtmlAsJson(jsonizeConfiguration);
                    return(json);
                }
                else
                {
                    string jsonString = jsonize.ParseHtmlAsJsonString(jsonizeConfiguration);
                    return(jsonString);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemplo n.º 4
0
        public void TestEmptyTextNodesHandlingIgnore()
        {
            JsonizeConfiguration jsonizeConfiguration = new JsonizeConfiguration
            {
                EmptyTextNodeHandling = EmptyTextNodeHandling.Ignore
            };

            string  html    = "<html><head></head><body><form></form><p></p></body></html>";
            Jsonize jsonize = new Jsonize(html);
            string  result  = jsonize.ParseHtmlAsJsonString(jsonizeConfiguration);

            Assert.AreEqual("{\"node\":\"Document\",\"child\":[{\"node\":\"Element\",\"tag\":\"html\",\"child\":[{\"node\":\"Element\",\"tag\":\"head\"},{\"node\":\"Element\",\"tag\":\"body\",\"child\":[{\"node\":\"Element\",\"tag\":\"form\"},{\"node\":\"Element\",\"tag\":\"p\"}]}]}]}",
                            result.Replace("\r\n", "").Replace(" ", ""));
        }
Exemplo n.º 5
0
        private static async Task <string> TestJsonizeAsString(JsonizeConfiguration jsonizeConfiguration)
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                string url = @"http://jackfinlay.com";

                HttpResponseMessage response = await client.GetAsync(url);

                string html = await response.Content.ReadAsStringAsync();

                Jsonize jsonize = new Jsonize(html);

                return(jsonize.ParseHtmlAsJsonString(jsonizeConfiguration));
            }
        }
Exemplo n.º 6
0
        private static async Task <string> TestJsonizeAsString()
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                string url = @"http://jackfinlay.com";

                HttpResponseMessage response = await client.GetAsync(url);

                string html = await response.Content.ReadAsStringAsync();

                //html = System.IO.File.ReadAllText(@"C:\Users\Public\file.html");
                Jsonize jsonize = new Jsonize(html);
                jsonize.ShowEmptyTextNodes(false);

                return(jsonize.ParseHtmlAsJsonString());
            }
        }
Exemplo n.º 7
0
        private static async Task <JsonizeNode> TestJsonizeAsJsonizeNode()
        {
            if (_html == null)
            {
                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Clear();
                    const string        url      = @"http://jackfinlay.com";
                    HttpResponseMessage response = await client.GetAsync(url);

                    _html = await response.Content.ReadAsStringAsync();
                }
            }

            Jsonize jsonize = new Jsonize(_html);

            return(jsonize.ParseHtmlAsJsonizeNode());
        }
        public async Task <string> ParseStructure(string markdownText)
        {
            var nodes = new Jsonize(markdownText).ParseHtmlToTypedJson();//.Children.FirstOrDefault();//HtmlToJsonService.HtmlToJson(markdownText).Replace("\r\n", "");

            var flattenedNode = FlatNodes(nodes);

            foreach (var node in flattenedNode)
            {
                // ReSharper disable once StringLiteralTypo
                if (node.Tag == "linktocomponent")
                {
                    var(_, idValue) = node.Attributes.FirstOrDefault(x => x.Key == "id");
                    if (idValue != null)
                    {
                        if (!int.TryParse((string)idValue, out var intId))
                        {
                            throw ExceptionFactory.SoftException(ExceptionEnum.SomethingWentWrong,
                                                                 "Something went wrong");
                        }

                        var linkToComment = await _publicContext.Comment.FirstOrDefaultAsync(x => x.Id == intId);

                        if (linkToComment == null) //first i check is it link to comment because link to comment have bigger chance to appear
                        {
                            var linkToPost = await _publicContext.Post.FirstOrDefaultAsync(x => x.Id == intId);

                            if (linkToPost != null)
                            {
                                node.Attributes.TryAdd("isPost", "true");
                                continue;
                            }

                            node.Attributes.TryAdd("isExist", "false");
                            continue;
                        }
                        node.Attributes.TryAdd("isComment", "true");
                    }
                }
            }

            return(JObject.FromObject(nodes, _jsonWriter).ToString()); //.Replace("\r\n", "");
        }