private void button1_Click(object sender, EventArgs e)
        {
            HtmlDocument htmldoc;

            htmldoc = HtmlDocumentFactory.CreateHtmlDocument();

            //заполним содержимое документа
            htmldoc.Title = "Hello";

            HtmlElement el = htmldoc.CreateElement("h1");

            el.InnerText = "Hello, world!";
            htmldoc.Body.AppendChild(el);

            el           = htmldoc.CreateElement("div");
            el.InnerText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
            el.Style     = "color: red";
            htmldoc.Body.AppendChild(el);

            //получаем все содержимое документа в виде html
            textBox1.Text = HtmlDocumentFactory.HtmlDocumentToString(htmldoc);

            //освобождаем неуправляемые ресурсы, связанные с HtmlDocument
            HtmlDocumentFactory.ReleaseHtmlDocument(htmldoc);
        }
        public void HtmlDocumentFactory_FromPath()
        {
            var result = HtmlDocumentFactory.FromPath(Constants.SampleFutureSyncFilePath);

            Assert.IsNotNull(
                result,
                "Expected to be able to retrieve document from path");
        }
        public void HtmlDocumentFactory_FromUrl()
        {
            var result = HtmlDocumentFactory.FromUrl(Constants.FutureSyncUrl);

            Assert.IsNotNull(
                result,
                "Expected to be able to retrieve document from url (requires web connection)");
        }
            public async Task ShouldReturnFileWhenFound()
            {
                var assemblyPath = An.AssemblyPath();
                var path         = Path.Combine(assemblyPath, "altbau-wohnungen.html");

                var result = await HtmlDocumentFactory.FromFileAsync(path);

                Assert.Multiple(() => {
                    Assert.That(result, Is.Not.Null, "was null");
                    Assert.That(result.DocumentNode.InnerHtml.GetHashCode(), Is.EqualTo(1207631242), "hash code");
                });
            }
 public static IEnumerable <Speaker> AllSpeakers()
 {
     try
     {
         return(AllSpeakers(HtmlDocumentFactory.FromUrl(Constants.FutureSyncUrl)));
     }
     catch (Exception ex)
     {
         throw new SpeakerListProviderException(
                   "AllSpeakers failed",
                   ex);
     }
 }
        public void SpeakerListProvider_Success()
        {
            var htmlDocument = HtmlDocumentFactory.FromPath(Constants.SampleFutureSyncFilePath);

            var speakers = SpeakerListProvider.AllSpeakers(htmlDocument);

            Assert.IsTrue(
                speakers.Count(o => o.Track == "keynote") == 1,
                "Expected a single keynote speaker");

            Assert.IsTrue(
                speakers.Any(o => o.Track != "keynote"),
                "Expected speakers who are not keynotes");
        }
Exemple #7
0
        static void CreateDocument(string[] command)
        {
            switch (command[0])
            {
            case "Markdown":
                _factory = MarkdownDocumentFactory.GetInstance();
                break;

            case "Html":
                _factory = HtmlDocumentFactory.GetInstance();
                break;
            }

            _document = _factory.CreateDocument(command[1]);
        }
Exemple #8
0
        public static Talk FromTalkUrl(string url)
        {
            try
            {
                var talkDocument = HtmlDocumentFactory.FromUrl(url);

                var container = talkDocument.DocumentNode.SelectNodes("//div[@class='container']").FirstOrDefault();

                var talk = new Talk
                {
                    Title   = container.Descendants("h1").First().InnerText ?? "No Title",
                    TalkUrl = url
                };

                StringBuilder description = new StringBuilder();

                foreach (var paragraph in container.Descendants("p"))
                {
                    string text = paragraph.InnerText;
                    if (!string.IsNullOrWhiteSpace(text))
                    {
                        description.AppendLine(text);
                    }
                }

                talk.Description = description.ToString().Trim();

                if (string.IsNullOrEmpty(talk.Description))
                {
                    throw new TalkProviderException($"Failed to retrieve description for talk: '{url}'");
                }

                return(talk);
            }
            catch (Exception ex)
            {
                throw new TalkProviderException(
                          $"FromTalkUrlFailed: '{url ?? "Null Url"}'",
                          ex);
            }
        }
 public void Init()
 {
     htmlDocument = HtmlDocumentFactory.FromPath(Constants.SampleFutureSyncFilePath);
     scraper      = new Scraper(htmlDocument);
 }
 public void ShouldThrowFileNotFoundExceptionWhenPathIsInvalid()
 {
     Assert.That(async() => await HtmlDocumentFactory.FromFileAsync("foobar.dat"), Throws.InstanceOf <FileNotFoundException>());
 }
 public void ShouldThrowArgumentNullExceptionWhenPathIsNull()
 {
     Assert.That(() => HtmlDocumentFactory.FromFileAsync(null), Throws.ArgumentNullException);
 }
 public void HtmlDocumentFactory_FromPath_Invalid()
 {
     _ = HtmlDocumentFactory.FromPath(Guid.NewGuid().ToString());
 }
        static void Main(string[] args)
        {
            string[] commands;
            var      list = File.ReadAllText("CreateDocumentScript.txt");

            commands = list.Split('#');

            //create documentfactory, markdown document
            var markdownFactory          = MarkdownDocumentFactory.GetInstance();
            var htmlFactory              = HtmlDocumentFactory.GetInstance();
            MarkdownDocument markdownDoc = null;
            HtmlDocument     htmlDoc     = null;

            bool docType = false;

            //true == markdown, false == html

            foreach (var command in commands)
            {
                var strippedCommand = Regex.Replace(command, @"\t|\n|\r", "");
                var commandList     = strippedCommand.Split(':');
                switch (commandList[0])
                {
                case "Document":
                    var properties = commandList[1].Split(';');
                    //create document
                    //if properties[0] is "Markdown" make a markdown doc else make html
                    if (properties[0] == "Markdown")
                    {
                        docType     = true;
                        markdownDoc = (MarkdownDocument)markdownFactory.CreateDocument(properties[1]);
                    }
                    else
                    {
                        docType = false;
                        htmlDoc = (HtmlDocument)htmlFactory.CreateDocument(properties[1]);
                    }
                    break;

                case "Element":
                    if (docType)    // markdown
                    {
                        markdownDoc.AddElement(markdownFactory.CreateElement(commandList[1], commandList[2]));
                    }
                    else     //html
                    {
                        htmlDoc.AddElement(htmlFactory.CreateElement(commandList[1], commandList[2]));
                    }
                    break;

                case "Run":
                    // Your document running code goes here
                    if (docType)
                    {
                        markdownDoc.RunDocument();
                    }
                    else
                    {
                        htmlDoc.RunDocument();
                    }
                    break;

                default:
                    break;
                }
            }
        }