<commit_before>﻿namespace AngleSharp
{
    using AngleSharp.Dom;
    using AngleSharp.Dom.Css;
    using AngleSharp.Extensions;
    using AngleSharp.Parser.Css;
    using AngleSharp.Parser.Html;
    using System;
    using System.IO;
    using System.Threading;
    using System.Threading.Tasks;

    /// <summary>
    /// A handy helper to construct various kinds of documents from a given
    /// source code, URL or stream.
    /// </summary>
    public static class DocumentBuilder
    {
        #region HTML Construction

        /// <summary>
        /// Builds a new empty HTML Document with the provided configuration.
        /// </summary>
        /// <param name="configuration">
        /// Optional custom options to use for the document generation.
        /// </param>
        /// <param name="url">The optional base URL of the document.</param>
        /// <returns>The constructed HTML document.</returns>
        public static IDocument Html(IConfiguration configuration, String url = null)
        {
            if (configuration == null)
                configuration = AngleSharp.Configuration.Default;

            var browsingContext = new SimpleBrowsingContext(configuration, Sandboxes.None);
            return browsingContext.OpenNew(url);
        }

        /// <summary>
        /// Builds a new HTML Document with the given source code string.
        /// </summary>
        /// <param name="sourceCode">The string to use as source code.</param>
        /// <param name="configuration">
        /// Optional custom options to use for the document generation.
        /// </param>
        /// <param name="url">The optional base URL of the document.</param>
        /// <returns>The constructed HTML document.</returns>
        public static IDocument Html(String sourceCode, IConfiguration configuration = null, String url = null)
        {
            if (sourceCode == null)
                throw new ArgumentNullException("sourceCode");

            if (configuration == null)
                configuration = AngleSharp.Configuration.Default;

            var stream = new TextSource(sourceCode);
            var browsingContext = new SimpleBrowsingContext(configuration, Sandboxes.None);
            var doc = new Document(browsingContext, stream) { DocumentUri = url };
            browsingContext.NavigateTo(doc);
            return ParserFor(doc).Parse();
        }

        /// <summary>
        /// Builds a new HTML Document with the given URL.
        /// </summary>
        /// <param name="url">
        /// The URL which points to the address containing the source code.
        /// </param>
        /// <param name="configuration">
        /// Optional custom options to use for the document generation.
        /// </param>
        /// <returns>The constructed HTML document.</returns>
        public static IDocument Html(Uri url, IConfiguration configuration = null)
        {
            return HtmlAsync(url, configuration).Result;
        }

        /// <summary>
        /// Builds a new HTML Document by asynchronously requesting the given
        /// URL.
        /// </summary>
        /// <param name="url">
        /// The URL which points to the address containing the source code.
        /// </param>
        /// <param name="configuration">
        /// Optional custom options to use for the document generation.
        /// </param>
        /// <returns>The task that constructs the HTML document.</returns>
        public static Task<IDocument> HtmlAsync(Uri url, IConfiguration configuration = null)
        {
            return HtmlAsync(url, CancellationToken.None, configuration);
        }

        /// <summary>
        /// Builds a new HTML Document by asynchronously requesting the given
        /// URL.
        /// </summary>
        /// <param name="url">
        /// The URL which points to the address containing the source code.
        /// </param>
        /// <param name="cancel">
        /// The cancellation token for cancelling the asynchronous request.
        /// </param>
        /// <param name="configuration">
        /// Optional custom options to use for the document generation.
        /// </param>
        /// <returns>The task that constructs the HTML document.</returns>
        public static Task<IDocument> HtmlAsync(Uri url, CancellationToken cancel, IConfiguration configuration = null)
        {
            if (url == null)
                throw new ArgumentNullException("url");

            if (configuration == null)
                configuration = AngleSharp.Configuration.Default;

            var browsingContext = new SimpleBrowsingContext(configuration, Sandboxes.None);
            return browsingContext.OpenAsync(new Url(url), cancel);
        }

        /// <summary>
        /// Builds a new HTML Document with the given (network) stream.
        /// </summary>
        /// <param name="content">
        /// The stream of chars to use as source code.
        /// </param>
        /// <param name="configuration">
        /// Optional custom options to use for the document generation.<commit_msg>Changed to convert<commit_after>﻿namespace AngleSharp
{
    using AngleSharp.Dom;
    using AngleSharp.Dom.Css;
    using AngleSharp.Extensions;
    using AngleSharp.Parser.Css;
    using AngleSharp.Parser.Html;
    using System;
    using System.IO;
    using System.Threading;
    using System.Threading.Tasks;

    /// <summary>
    /// A handy helper to construct various kinds of documents from a given
    /// source code, URL or stream.
    /// </summary>
    public static class DocumentBuilder
    {
        #region HTML Construction

        /// <summary>
        /// Builds a new empty HTML Document with the provided configuration.
        /// </summary>
        /// <param name="configuration">
        /// Optional custom options to use for the document generation.
        /// </param>
        /// <param name="url">The optional base URL of the document.</param>
        /// <returns>The constructed HTML document.</returns>
        public static IDocument Html(IConfiguration configuration, String url = null)
        {
            if (configuration == null)
                configuration = AngleSharp.Configuration.Default;

            var browsingContext = new SimpleBrowsingContext(configuration, Sandboxes.None);
            return browsingContext.OpenNew(url);
        }

        /// <summary>
        /// Builds a new HTML Document with the given source code string.
        /// </summary>
        /// <param name="sourceCode">The string to use as source code.</param>
        /// <param name="configuration">
        /// Optional custom options to use for the document generation.
        /// </param>
        /// <param name="url">The optional base URL of the document.</param>
        /// <returns>The constructed HTML document.</returns>
        public static IDocument Html(String sourceCode, IConfiguration configuration = null, String url = null)
        {
            if (sourceCode == null)
                throw new ArgumentNullException("sourceCode");

            if (configuration == null)
                configuration = AngleSharp.Configuration.Default;

            var stream = new TextSource(sourceCode);
            var browsingContext = new SimpleBrowsingContext(configuration, Sandboxes.None);
            var doc = new Document(browsingContext, stream) { DocumentUri = url };
            browsingContext.NavigateTo(doc);
            return ParserFor(doc).Parse();
        }

        /// <summary>
        /// Builds a new HTML Document with the given URL.
        /// </summary>
        /// <param name="url">
        /// The URL which points to the address containing the source code.
        /// </param>
        /// <param name="configuration">
        /// Optional custom options to use for the document generation.
        /// </param>
        /// <returns>The constructed HTML document.</returns>
        public static IDocument Html(Uri url, IConfiguration configuration = null)
        {
            return HtmlAsync(url, configuration).Result;
        }

        /// <summary>
        /// Builds a new HTML Document by asynchronously requesting the given
        /// URL.
        /// </summary>
        /// <param name="url">
        /// The URL which points to the address containing the source code.
        /// </param>
        /// <param name="configuration">
        /// Optional custom options to use for the document generation.
        /// </param>
        /// <returns>The task that constructs the HTML document.</returns>
        public static Task<IDocument> HtmlAsync(Uri url, IConfiguration configuration = null)
        {
            return HtmlAsync(url, CancellationToken.None, configuration);
        }

        /// <summary>
        /// Builds a new HTML Document by asynchronously requesting the given
        /// URL.
        /// </summary>
        /// <param name="url">
        /// The URL which points to the address containing the source code.
        /// </param>
        /// <param name="cancel">
        /// The cancellation token for cancelling the asynchronous request.
        /// </param>
        /// <param name="configuration">
        /// Optional custom options to use for the document generation.
        /// </param>
        /// <returns>The task that constructs the HTML document.</returns>
        public static Task<IDocument> HtmlAsync(Uri url, CancellationToken cancel, IConfiguration configuration = null)
        {
            if (url == null)
                throw new ArgumentNullException("url");

            if (configuration == null)
                configuration = AngleSharp.Configuration.Default;

            var browsingContext = new SimpleBrowsingContext(configuration, Sandboxes.None);
            return browsingContext.OpenAsync(Url.Convert(url), cancel);
        }

        /// <summary>
        /// Builds a new HTML Document with the given (network) stream.
        /// </summary>
        /// <param name="content">
        /// The stream of chars to use as source code.
        /// </param>
        /// <param name="configuration">
        /// Optional custom options to use for the document generation.
{'fraction_non_alphanumeric': 0.1093734727788095, 'fraction_numerical': 0.0, 'mean_word_length': 1.9393852341281241, 'pattern_counts': {'":': 0, '<': 109, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5422606', 'n_tokens_mistral': 2422, 'n_tokens_neox': 2351, 'n_words': 840}