Fixing style errors.

This commit is contained in:
stickler-ci 2020-03-03 09:28:00 +00:00
parent 8ce1f5f360
commit f026b959a2

View File

@ -3,14 +3,16 @@
// Learn more about this regex pattern: https://regexr.com/4vi60 // Learn more about this regex pattern: https://regexr.com/4vi60
define('HTML_REGEX_PATTERN', '/<([A-Za-z]+)(?:\s([A-Za-z]+(?:\-[A-Za-z]+)?(?:=(?:".*?")|(?:[0-9]+))))*(?:(?:\s\/>)|(?:>(.*)<\/\1>))/'); define('HTML_REGEX_PATTERN', '/<([A-Za-z]+)(?:\s([A-Za-z]+(?:\-[A-Za-z]+)?(?:=(?:".*?")|(?:[0-9]+))))*(?:(?:\s\/>)|(?:>(.*)<\/\1>))/');
class HtmlToOdtConverter { class HtmlToOdtConverter
{
/** /**
* Converts a string with html inside into an odt compatible string * Converts a string with html inside into an odt compatible string
* @param string The text to convert * @param string The text to convert
* @return array * @return array
*/ */
public static function htmlToOdt($htmlText) { public static function htmlToOdt($htmlText)
{
/* /*
Default styles: Default styles:
@ -40,7 +42,7 @@ class HtmlToOdtConverter {
'<style:style style:name="subText" style:family="text"><style:text-properties style:text-position="sub 58%" /></style:style>', '<style:style style:name="subText" style:family="text"><style:text-properties style:text-position="sub 58%" /></style:style>',
'<style:style style:name="supText" style:family="text"><style:text-properties style:text-position="super 58%" /></style:style>' '<style:style style:name="supText" style:family="text"><style:text-properties style:text-position="super 58%" /></style:style>'
); );
$odtText = self::replaceHtmlWithOdtTag(self::getDataFromHtml($htmlText), $customStyles, $fontDeclarations); $odtText = self::replaceHtmlWithOdtTag(self::getDataFromHtml($htmlText), $customStyles, $fontDeclarations);
foreach ($customStyles as $key => $value) { foreach ($customStyles as $key => $value) {
@ -59,7 +61,8 @@ class HtmlToOdtConverter {
* @param array $tags An array with html tags generated by the getDataFromHtml() function * @param array $tags An array with html tags generated by the getDataFromHtml() function
* @param array $customStyles An array of style defenitions that should be included inside the odt file * @param array $customStyles An array of style defenitions that should be included inside the odt file
*/ */
private static function replaceHtmlWithOdtTag($tags, &$customStyles, &$fontDeclarations) { private static function replaceHtmlWithOdtTag($tags, &$customStyles, &$fontDeclarations)
{
if ($customStyles == null) $customStyles = array(); if ($customStyles == null) $customStyles = array();
if ($fontDeclarations == null) $fontDeclarations = array(); if ($fontDeclarations == null) $fontDeclarations = array();
@ -69,7 +72,7 @@ class HtmlToOdtConverter {
// Check if the current item is a tag or just plain text // Check if the current item is a tag or just plain text
if (isset($tag['text'])) { if (isset($tag['text'])) {
$odtResult .= $tag['text']; $odtResult .= $tag['text'];
} else if (isset($tag['name'])) { } elseif (isset($tag['name'])) {
switch ($tag['name']) { switch ($tag['name']) {
case 'br': case 'br':
$odtResult .= '<text:line-break/>'; $odtResult .= '<text:line-break/>';
@ -145,7 +148,8 @@ class HtmlToOdtConverter {
* Checks if the given text is a html string * Checks if the given text is a html string
* @param string $text The text to check * @param string $text The text to check
*/ */
public static function isHtmlTag($text) { public static function isHtmlTag($text)
{
return preg_match(HTML_REGEX_PATTERN, $text); return preg_match(HTML_REGEX_PATTERN, $text);
} }
@ -153,7 +157,8 @@ class HtmlToOdtConverter {
* Checks if the given text includes a html string * Checks if the given text includes a html string
* @param string $text The text to check * @param string $text The text to check
*/ */
public static function hasHtmlTag($text) { public static function hasHtmlTag($text)
{
$result = preg_match_all(HTML_REGEX_PATTERN, $text); $result = preg_match_all(HTML_REGEX_PATTERN, $text);
return is_numeric($result) && $result > 0; return is_numeric($result) && $result > 0;
} }
@ -162,7 +167,8 @@ class HtmlToOdtConverter {
* Returns an array of html elements * Returns an array of html elements
* @param string $html A string with html tags * @param string $html A string with html tags
*/ */
private static function getDataFromHtml($html) { private static function getDataFromHtml($html)
{
$tags = array(); $tags = array();
$tempHtml = $html; $tempHtml = $html;
@ -231,4 +237,4 @@ class HtmlToOdtConverter {
} }
return $tags; return $tags;
} }
} }