<commit_before><?php
/**
 * @file src/Content/Text/HTML.php
 */

namespace Friendica\Content\Text;

use DOMDocument;
use DOMXPath;
use Friendica\Content\Widget\ContactBlock;
use Friendica\Core\Hook;
use Friendica\Core\L10n;
use Friendica\Core\Config;
use Friendica\Core\Renderer;
use Friendica\Model\Contact;
use Friendica\Util\Network;
use Friendica\Util\Proxy as ProxyUtils;
use Friendica\Util\Strings;
use Friendica\Util\XML;
use League\HTMLToMarkdown\HtmlConverter;

class HTML
{
	public static function sanitizeCSS($input)
	{
		$cleaned = "";

		$input = strtolower($input);

		for ($i = 0; $i < strlen($input); $i++) {
			$char = substr($input, $i, 1);

			if (($char >= "a") && ($char <= "z")) {
				$cleaned .= $char;
			}

			if (!(strpos(" #;:0123456789-_.%", $char) === false)) {
				$cleaned .= $char;
			}
		}

		return $cleaned;
	}

	/**
	 * Search all instances of a specific HTML tag node in the provided DOM document and replaces them with BBCode text nodes.
	 *
	 * @see HTML::tagToBBCodeSub()
	 */
	private static function tagToBBCode(DOMDocument $doc, string $tag, array $attributes, string $startbb, string $endbb, bool $ignoreChildren = false)
	{
		do {
			$done = self::tagToBBCodeSub($doc, $tag, $attributes, $startbb, $endbb, $ignoreChildren);
		} while ($done);
	}

	/**
	 * Search the first specific HTML tag node in the provided DOM document and replaces it with BBCode text nodes.
	 *
	 * @param DOMDocument $doc
	 * @param string      $tag            HTML tag name
	 * @param array       $attributes     Array of attributes to match and optionally use the value from
	 * @param string      $startbb        BBCode tag opening
	 * @param string      $endbb          BBCode tag closing
	 * @param bool        $ignoreChildren If set to false, the HTML tag children will be appended as text inside the BBCode tag
	 *                                    Otherwise, they will be entirely ignored. Useful for simple BBCode that draw their
	 *                                    inner value from an attribute value and disregard the tag children.
	 * @return bool Whether a replacement was done
	 */
	private static function tagToBBCodeSub(DOMDocument $doc, string $tag, array $attributes, string $startbb, string $endbb, bool $ignoreChildren = false)
	{
		$savestart = str_replace('$', '\x01', $startbb);
		$replace = false;

		$xpath = new DOMXPath($doc);

		/** @var \DOMNode[] $list */
		$list = $xpath->query("//" . $tag);
		foreach ($list as $node) {
			$attr = [];
			if ($node->attributes->length) {
				foreach ($node->attributes as $attribute) {
					$attr[$attribute->name] = $attribute->value;
				}
			}

			$replace = true;

			$startbb = $savestart;

			$i = 0;

			foreach ($attributes as $attribute => $value) {
				$startbb = str_replace('\x01' . ++$i, '$1', $startbb);
				if (strpos('*' . $startbb, '$1') > 0) {
					if ($replace && (@$attr[$attribute] != '')) {
						$startbb = preg_replace($value, $startbb, $attr[$attribute], -1, $count);

						// If nothing could be changed
						if ($count == 0) {
							$replace = false;
						}
					} else {
						$replace = false;
					}
				} else {
					if (@$attr[$attribute] != $value) {
						$replace = false;
					}
				}
			}

			if ($replace) {
				$StartCode = $doc->createTextNode($startbb);
				$EndCode = $doc->createTextNode($endbb);

				$node->parentNode->insertBefore($StartCode, $node);

				if (!$ignoreChildren && $node->hasChildNodes()) {
					/** @var \DOMNode $child */
					foreach ($node->childNodes as $key => $child) {
						/* Remove empty text nodes at the start or at the end of the children list */
						if ($key > 0 && $key < $node->childNodes->length - 1 || $child->nodeName != '#text' || trim($child->nodeValue)) {
							$newNode = $child->cloneNode(true);
							$node->parentNode->insertBefore($newNode, $node);
						}
					}
				}

				$node->parentNode->insertBefore($EndCode, $node);
				$node->parentNode->removeChild($node);
			}
		}

		return $replace;
	}

	/**
	 * Made by: <EMAIL>
	 * Originally made for the syncom project: http://wiki.piratenpartei.de/Syncom
	 *                    https://github.com/annando/Syncom
	 *
	 * @brief Converter for HTML to BBCode
	 * @param string $message
	 * @param string $basepath
	 * @return string
	 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
	 */
	public static function toBBCode($message, $basepath = '')
	{
		$message = str_replace("\r", "", $message);

		// Removing code blocks before the whitespace removal processing below
		$codeblocks = [];
		$message = preg_replace_callback(
			'#<pre><code(?: class="language-([^"]*)")?>(.*)</code></pre>#iUs',
			function ($matches) use (&$codeblocks) {
				$return = '[codeblock-' . count($codeblocks) . ']';

				$prefix = '[code]';
				if ($matches[1] != '') {
					$prefix = '[code=' . $matches[1] . ']';
				}

				$codeblocks[] = $prefix . PHP_EOL . trim($matches[2]) . PHP_EOL . '[/code]';
				return $return;
			},
			$message
		);

		$message = str_replace(
			[
				"<li><p>",
				"</p></li>",
			],
			[
				"<li>",
				"</li>",
			],
			$message
		);

		// remove namespaces
		$message = preg_replace('=<(\w+):(.+?)>=', '<removeme>', $message);
		$message = preg_replace('=</(\w+):(.+?)>=', '</removeme>', $message);

		$doc = new DOMDocument();
		$doc->preserveWhiteSpace = false;

		$message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");

		@$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);

		XML::deleteNode($doc, 'style');
		XML::deleteNode($doc, 'head');
		XML::deleteNode($doc, 'title');
		XML::deleteNode($doc, 'meta');
		XML::deleteNode($doc, 'xml');
		XML::deleteNode($doc, 'removeme');

		$xpath = new DomXPath($doc);
		$list = $xpath->query("//pre");
		foreach ($list as $node) {
			// Ensure to escape unescaped & - they will otherwise raise a warning
			$safe_value = preg_replace('/&(?!\w+;)/', '&amp;', $node->nodeValue);
			$node->nodeValue = str_replace("\n", "\r", $safe_value);
		}

		$message = $doc->saveHTML();
		$message = str_replace(["\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"], ["<", ">", "<br />", " ", ""], $message);
		$message = preg_replace('= [\s]*=i', " ", $message);

		@$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);

		self::tagToBBCode($doc, 'html', [], "", "");
		self::tagToBBCode($doc, 'body', [], "", "");

		// Outlook-Quote - Variant 1
		self::tagToBBCode($doc, 'p', ['class' => 'MsoNormal', 'style' => 'margin-left:35.4pt'], '[quote]', '[/quote]');

		// Outlook-Quote - Variant 2
		self::tagToBBCode(
			$doc,
			'div',
			['style' => 'border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt'],
			'[quote]',
			'[/quote]'
		);

		// MyBB-Stuff
		self::tagToBBCode($doc, 'span', ['style' => 'text-decoration: underline;'], '[u]', '[/u]');
		self::tagToBBCode($doc, 'span', ['style' => 'font-style: italic;'], '[i]', '[/i]');
		self::tagToBBCode($doc, 'span', ['style' => 'font-weight: bold;'], '[b]', '[/b]');

		/* self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[font=$1][size=$2][color=$3]', '[/color][/size][/font]');
		  self::node2BBCode($doc, 'font', array('size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[size=$1][color=$2]', '[/color][/size]');
		  self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(.+)/'), '[font=$1][size=$2]', '[/size][/font]');
		  self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'color'=>'/(.+)/'), '[font=$1][color=$3]', '[/color][/font]');
		  self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/'), '[font=$1]', '[/font]');
		  self::node2BBCode($doc, 'font', array('size'=>'/(\d+)/'), '[size=$1]', '[/size]');
		  self::node2BBCode($doc, 'font', array('color'=>'/(.+)/'), '[color=$1]', '[/color]');
		 */
		// Untested
		//self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*font-family:\s*(.+?)[,;].*color:\s*(.+?)[,;].*/'), '[size=$1][font=$2][color=$3]', '[/color][/font][/size]');
		//self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(\d+)[,;].*/'), '[size=$1]', '[/size]');
		//self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*/'), '[size=$1]', '[/size]');

		self::tagToBBCode($doc, 'span', ['style' => '/.*color:\s*(.+?)[,;].*/'], '[color="$1"]', '[/color]');

		//self::node2BBCode($doc, 'span', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
		//self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)pt.*/'), '[font=$1][size=$2]', '[/size][/font]');
		//self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)px.*/'), '[font=$1][size=$2]', '[/size][/font]');
		//self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
		// Importing the classes - interesting for importing of posts from third party networks that were exported from friendica
		// Test
		//self::node2BBCode($doc, 'span', array('class'=>'/([\w ]+)/'), '[class=$1]', '[/class]');
		self::tagToBBCode($doc, 'span', ['class' => 'type-link'], '[class=type-link]', '[/class]');
		self::tagToBBCode($doc, 'span', ['class' => 'type-video'], '[class=type-video]', '[/class]');

		self::tagToBBCode($doc, 'strong', [], '[b]', '[/b]');
		self::tagToBBCode($doc, 'em', [], '[i]', '[/i]');
		self::tagToBBCode($doc, 'b', [], '[b]', '[/b]');
		self::tagToBBCode($doc, 'i', [], '[i]', '[/i]');
		self::tagToBBCode($doc, 'u', [], '[u]', '[/u]');
		self::tagToBBCode($doc, 's', [], '[s]', '[/s]');
		self::tagToBBCode($doc, 'del', [], '[s]', '[/s]');
		self::tagToBBCode($doc, 'strike', [], '[s]', '[/s]');

		self::tagToBBCode($doc, 'big', [], "[size=large]", "[/size]");
		self::tagToBBCode($doc, 'small', [], "[size=small]", "[/size]");

		self::tagToBBCode($doc, 'blockquote', [], '[quote]', '[/quote]');

		self::tagToBBCode($doc, 'br', [], "\n", '');

		self::tagToBBCode($doc, 'p', ['class' => 'MsoNormal'], "\n", "");
		self::tagToBBCode($doc, 'div', ['class' => 'MsoNormal'], "\r", "");

		self::tagToBBCode($doc, 'span', [], "", "");

		self::tagToBBCode($doc, 'span', [], "", "");
		self::tagToBBCode($doc, 'pre', [], "", "");

		self::tagToBBCode($doc, 'div', [], "\r", "\r");
		self::tagToBBCode($doc, 'p', [], "\n", "\n");

		self::tagToBBCode($doc, 'ul', [], "[list]", "[/list]");
		self::tagToBBCode($doc, 'ol', [], "[list=1]", "[/list]");
		self::tagToBBCode($doc, 'li', [], "[*]", "");

		self::tagToBBCode($doc, 'hr', [], "[hr]", "");

		self::tagToBBCode($doc, 'table', [], "", "");
		self::tagToBBCode($doc, 'tr', [], "\n", "");
		self::tagToBBCode($doc, 'td', [], "\t", "");
		//self::node2BBCode($doc, 'table', array(), "[table]", "[/table]");
		//self::node2BBCode($doc, 'th', array(), "[th]", "[/th]");
		//self::node2BBCode($doc, 'tr', array(), "[tr]", "[/tr]");
		//self::node2BBCode($doc, 'td', array(), "[td]", "[/td]");
		//self::node2BBCode($doc, 'h1', array(), "\n\n[size=xx-large][b]", "[/b][/size]\n");
		//self::node2BBCode($doc, 'h2', array(), "\n\n[size=x-large][b]", "[/b][/size]\n");
		//self::node2BBCode($doc, 'h3', array(), "\n\n[size=large][b]", "[/b][/size]\n");
		//self::node2BBCode($doc, 'h4', array(), "\n\n[size=medium][b]", "[/b][/size]\n");
		//self::node2BBCode($doc, 'h5', array(), "\n\n[size=small][b]", "[/b][/size]\n");
		//self::node2BBCode($doc, 'h6', array(), "\n\n[size=x-small][b]", "[/b][/size]\n");
<commit_msg>Support for tables in HTML2BBCode parser<commit_after><?php
/**
 * @file src/Content/Text/HTML.php
 */

namespace Friendica\Content\Text;

use DOMDocument;
use DOMXPath;
use Friendica\Content\Widget\ContactBlock;
use Friendica\Core\Hook;
use Friendica\Core\L10n;
use Friendica\Core\Config;
use Friendica\Core\Renderer;
use Friendica\Model\Contact;
use Friendica\Util\Network;
use Friendica\Util\Proxy as ProxyUtils;
use Friendica\Util\Strings;
use Friendica\Util\XML;
use League\HTMLToMarkdown\HtmlConverter;

class HTML
{
	public static function sanitizeCSS($input)
	{
		$cleaned = "";

		$input = strtolower($input);

		for ($i = 0; $i < strlen($input); $i++) {
			$char = substr($input, $i, 1);

			if (($char >= "a") && ($char <= "z")) {
				$cleaned .= $char;
			}

			if (!(strpos(" #;:0123456789-_.%", $char) === false)) {
				$cleaned .= $char;
			}
		}

		return $cleaned;
	}

	/**
	 * Search all instances of a specific HTML tag node in the provided DOM document and replaces them with BBCode text nodes.
	 *
	 * @see HTML::tagToBBCodeSub()
	 */
	private static function tagToBBCode(DOMDocument $doc, string $tag, array $attributes, string $startbb, string $endbb, bool $ignoreChildren = false)
	{
		do {
			$done = self::tagToBBCodeSub($doc, $tag, $attributes, $startbb, $endbb, $ignoreChildren);
		} while ($done);
	}

	/**
	 * Search the first specific HTML tag node in the provided DOM document and replaces it with BBCode text nodes.
	 *
	 * @param DOMDocument $doc
	 * @param string      $tag            HTML tag name
	 * @param array       $attributes     Array of attributes to match and optionally use the value from
	 * @param string      $startbb        BBCode tag opening
	 * @param string      $endbb          BBCode tag closing
	 * @param bool        $ignoreChildren If set to false, the HTML tag children will be appended as text inside the BBCode tag
	 *                                    Otherwise, they will be entirely ignored. Useful for simple BBCode that draw their
	 *                                    inner value from an attribute value and disregard the tag children.
	 * @return bool Whether a replacement was done
	 */
	private static function tagToBBCodeSub(DOMDocument $doc, string $tag, array $attributes, string $startbb, string $endbb, bool $ignoreChildren = false)
	{
		$savestart = str_replace('$', '\x01', $startbb);
		$replace = false;

		$xpath = new DOMXPath($doc);

		/** @var \DOMNode[] $list */
		$list = $xpath->query("//" . $tag);
		foreach ($list as $node) {
			$attr = [];
			if ($node->attributes->length) {
				foreach ($node->attributes as $attribute) {
					$attr[$attribute->name] = $attribute->value;
				}
			}

			$replace = true;

			$startbb = $savestart;

			$i = 0;

			foreach ($attributes as $attribute => $value) {
				$startbb = str_replace('\x01' . ++$i, '$1', $startbb);
				if (strpos('*' . $startbb, '$1') > 0) {
					if ($replace && (@$attr[$attribute] != '')) {
						$startbb = preg_replace($value, $startbb, $attr[$attribute], -1, $count);

						// If nothing could be changed
						if ($count == 0) {
							$replace = false;
						}
					} else {
						$replace = false;
					}
				} else {
					if (@$attr[$attribute] != $value) {
						$replace = false;
					}
				}
			}

			if ($replace) {
				$StartCode = $doc->createTextNode($startbb);
				$EndCode = $doc->createTextNode($endbb);

				$node->parentNode->insertBefore($StartCode, $node);

				if (!$ignoreChildren && $node->hasChildNodes()) {
					/** @var \DOMNode $child */
					foreach ($node->childNodes as $key => $child) {
						/* Remove empty text nodes at the start or at the end of the children list */
						if ($key > 0 && $key < $node->childNodes->length - 1 || $child->nodeName != '#text' || trim($child->nodeValue)) {
							$newNode = $child->cloneNode(true);
							$node->parentNode->insertBefore($newNode, $node);
						}
					}
				}

				$node->parentNode->insertBefore($EndCode, $node);
				$node->parentNode->removeChild($node);
			}
		}

		return $replace;
	}

	/**
	 * Made by: <EMAIL>
	 * Originally made for the syncom project: http://wiki.piratenpartei.de/Syncom
	 *                    https://github.com/annando/Syncom
	 *
	 * @brief Converter for HTML to BBCode
	 * @param string $message
	 * @param string $basepath
	 * @return string
	 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
	 */
	public static function toBBCode($message, $basepath = '')
	{
		$message = str_replace("\r", "", $message);

		// Removing code blocks before the whitespace removal processing below
		$codeblocks = [];
		$message = preg_replace_callback(
			'#<pre><code(?: class="language-([^"]*)")?>(.*)</code></pre>#iUs',
			function ($matches) use (&$codeblocks) {
				$return = '[codeblock-' . count($codeblocks) . ']';

				$prefix = '[code]';
				if ($matches[1] != '') {
					$prefix = '[code=' . $matches[1] . ']';
				}

				$codeblocks[] = $prefix . PHP_EOL . trim($matches[2]) . PHP_EOL . '[/code]';
				return $return;
			},
			$message
		);

		$message = str_replace(
			[
				"<li><p>",
				"</p></li>",
			],
			[
				"<li>",
				"</li>",
			],
			$message
		);

		// remove namespaces
		$message = preg_replace('=<(\w+):(.+?)>=', '<removeme>', $message);
		$message = preg_replace('=</(\w+):(.+?)>=', '</removeme>', $message);

		$doc = new DOMDocument();
		$doc->preserveWhiteSpace = false;

		$message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");

		@$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);

		XML::deleteNode($doc, 'style');
		XML::deleteNode($doc, 'head');
		XML::deleteNode($doc, 'title');
		XML::deleteNode($doc, 'meta');
		XML::deleteNode($doc, 'xml');
		XML::deleteNode($doc, 'removeme');

		$xpath = new DomXPath($doc);
		$list = $xpath->query("//pre");
		foreach ($list as $node) {
			// Ensure to escape unescaped & - they will otherwise raise a warning
			$safe_value = preg_replace('/&(?!\w+;)/', '&amp;', $node->nodeValue);
			$node->nodeValue = str_replace("\n", "\r", $safe_value);
		}

		$message = $doc->saveHTML();
		$message = str_replace(["\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"], ["<", ">", "<br />", " ", ""], $message);
		$message = preg_replace('= [\s]*=i', " ", $message);

		@$doc->loadHTML($message, LIBXML_HTML_NODEFDTD);

		self::tagToBBCode($doc, 'html', [], "", "");
		self::tagToBBCode($doc, 'body', [], "", "");

		// Outlook-Quote - Variant 1
		self::tagToBBCode($doc, 'p', ['class' => 'MsoNormal', 'style' => 'margin-left:35.4pt'], '[quote]', '[/quote]');

		// Outlook-Quote - Variant 2
		self::tagToBBCode(
			$doc,
			'div',
			['style' => 'border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt'],
			'[quote]',
			'[/quote]'
		);

		// MyBB-Stuff
		self::tagToBBCode($doc, 'span', ['style' => 'text-decoration: underline;'], '[u]', '[/u]');
		self::tagToBBCode($doc, 'span', ['style' => 'font-style: italic;'], '[i]', '[/i]');
		self::tagToBBCode($doc, 'span', ['style' => 'font-weight: bold;'], '[b]', '[/b]');

		/* self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[font=$1][size=$2][color=$3]', '[/color][/size][/font]');
		  self::node2BBCode($doc, 'font', array('size'=>'/(\d+)/', 'color'=>'/(.+)/'), '[size=$1][color=$2]', '[/color][/size]');
		  self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'size'=>'/(.+)/'), '[font=$1][size=$2]', '[/size][/font]');
		  self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/', 'color'=>'/(.+)/'), '[font=$1][color=$3]', '[/color][/font]');
		  self::node2BBCode($doc, 'font', array('face'=>'/([\w ]+)/'), '[font=$1]', '[/font]');
		  self::node2BBCode($doc, 'font', array('size'=>'/(\d+)/'), '[size=$1]', '[/size]');
		  self::node2BBCode($doc, 'font', array('color'=>'/(.+)/'), '[color=$1]', '[/color]');
		 */
		// Untested
		//self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*font-family:\s*(.+?)[,;].*color:\s*(.+?)[,;].*/'), '[size=$1][font=$2][color=$3]', '[/color][/font][/size]');
		//self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(\d+)[,;].*/'), '[size=$1]', '[/size]');
		//self::node2BBCode($doc, 'span', array('style'=>'/.*font-size:\s*(.+?)[,;].*/'), '[size=$1]', '[/size]');

		self::tagToBBCode($doc, 'span', ['style' => '/.*color:\s*(.+?)[,;].*/'], '[color="$1"]', '[/color]');

		//self::node2BBCode($doc, 'span', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
		//self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)pt.*/'), '[font=$1][size=$2]', '[/size][/font]');
		//self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*font-size:\s*(\d+?)px.*/'), '[font=$1][size=$2]', '[/size][/font]');
		//self::node2BBCode($doc, 'div', array('style'=>'/.*font-family:\s*(.+?)[,;].*/'), '[font=$1]', '[/font]');
		// Importing the classes - interesting for importing of posts from third party networks that were exported from friendica
		// Test
		//self::node2BBCode($doc, 'span', array('class'=>'/([\w ]+)/'), '[class=$1]', '[/class]');
		self::tagToBBCode($doc, 'span', ['class' => 'type-link'], '[class=type-link]', '[/class]');
		self::tagToBBCode($doc, 'span', ['class' => 'type-video'], '[class=type-video]', '[/class]');

		self::tagToBBCode($doc, 'strong', [], '[b]', '[/b]');
		self::tagToBBCode($doc, 'em', [], '[i]', '[/i]');
		self::tagToBBCode($doc, 'b', [], '[b]', '[/b]');
		self::tagToBBCode($doc, 'i', [], '[i]', '[/i]');
		self::tagToBBCode($doc, 'u', [], '[u]', '[/u]');
		self::tagToBBCode($doc, 's', [], '[s]', '[/s]');
		self::tagToBBCode($doc, 'del', [], '[s]', '[/s]');
		self::tagToBBCode($doc, 'strike', [], '[s]', '[/s]');

		self::tagToBBCode($doc, 'big', [], "[size=large]", "[/size]");
		self::tagToBBCode($doc, 'small', [], "[size=small]", "[/size]");

		self::tagToBBCode($doc, 'blockquote', [], '[quote]', '[/quote]');

		self::tagToBBCode($doc, 'br', [], "\n", '');

		self::tagToBBCode($doc, 'p', ['class' => 'MsoNormal'], "\n", "");
		self::tagToBBCode($doc, 'div', ['class' => 'MsoNormal'], "\r", "");

		self::tagToBBCode($doc, 'span', [], "", "");

		self::tagToBBCode($doc, 'span', [], "", "");
		self::tagToBBCode($doc, 'pre', [], "", "");

		self::tagToBBCode($doc, 'div', [], "\r", "\r");
		self::tagToBBCode($doc, 'p', [], "\n", "\n");

		self::tagToBBCode($doc, 'ul', [], "[list]", "[/list]");
		self::tagToBBCode($doc, 'ol', [], "[list=1]", "[/list]");
		self::tagToBBCode($doc, 'li', [], "[*]", "");

		self::tagToBBCode($doc, 'hr', [], "[hr]", "");

		self::tagToBBCode($doc, 'table', array(), "[table]", "[/table]");
		self::tagToBBCode($doc, 'th', array(), "[th]", "[/th]");
		self::tagToBBCode($doc, 'tr', array(), "[tr]", "[/tr]");
		self::tagToBBCode($doc, 'td', array(), "[td]", "[/td]");

{'fraction_non_alphanumeric': 0.27716386270353166, 'fraction_numerical': 0.008705065175228902, 'mean_word_length': 3.8137212331741206, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 171, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2328414', 'n_tokens_mistral': 8987, 'n_tokens_neox': 8012, 'n_words': 1633}