Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextHandling: implement a tool to solve string-based text handling problems #8096

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions components/ILIAS/Data/src/TextHandling/Markup/HTML.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace Data\src\TextHandling\Markup;

class HTML implements Markup
{
}
25 changes: 25 additions & 0 deletions components/ILIAS/Data/src/TextHandling/Markup/Markup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace Data\src\TextHandling\Markup;

interface Markup
{
}
75 changes: 75 additions & 0 deletions components/ILIAS/Data/src/TextHandling/Shape/Markdown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace Data\src\TextHandling\Shape;

use Data\src\TextHandling\Text\HTML;
use Data\src\TextHandling\Text\PlainText;
use Data\src\TextHandling\Markup\Markup;
use Data\src\TextHandling\Text\Text;
use ILIAS\Refinery\Factory;

class Markdown extends Shape
{
public function __construct(
protected Factory $refinery,
protected Markup $markup
) {
$this->refinery = $refinery;
$this->markup = $this->markup;
}

/**
* @throws \InvalidArgumentException
*/
public function toHTML($text): HTML
{
if (!is_string($text)) {
throw new \InvalidArgumentException("Text does not match format.");
}
return new HTML($this->refinery->string()->markdown()->toHTML()->transform($text));
}

/**
* @throws \InvalidArgumentException
*/
public function toPlainText($text): PlainText
{
if (!is_string($text)) {
throw new \InvalidArgumentException("Text does not match format.");
}
return new PlainText($text);
}

public function getMarkup(): Markup
{
return $this->markup;
}

public function fromString(string $text): Text
{
return new \Data\src\TextHandling\Text\Markdown($this, $text);
}

public function isRawStringCompliant(string $text): bool
{
return true;
}
}
42 changes: 42 additions & 0 deletions components/ILIAS/Data/src/TextHandling/Shape/Shape.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace Data\src\TextHandling\Shape;

use Data\src\TextHandling\Text\HTML;
use Data\src\TextHandling\Text\PlainText;
use Data\src\TextHandling\Text\Text;
use Data\src\TextHandling\Markup\Markup;

abstract class Shape
{
/**
* @throws \InvalidArgumentException if $text does not match format.
*/
abstract public function toHTML($text): HTML;

/**
* @throws \InvalidArgumentException if $text does not match format.
*/
abstract public function toPlainText($text): PlainText;
abstract public function getMarkup(): Markup;
abstract public function fromString(string $text): Text;
abstract public function isRawStringCompliant(string $text): bool;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace Data\src\TextHandling\Shape;

use Data\src\TextHandling\Structure;
use Data\src\TextHandling\Text\Text;

class SimpleDocumentMarkdown extends Markdown
{
/**
* @return mixed[] consts from Structure
*/
public function getSupportedStructure(): array
{
return [
Structure::BOLD,
Structure::ITALIC,
Structure::HEADING_1,
Structure::HEADING_2,
Structure::HEADING_3,
Structure::HEADING_4,
Structure::HEADING_5,
Structure::HEADING_6,
Structure::UNORDERED_LIST,
Structure::ORDERED_LIST,
Structure::PARAGRAPH,
Structure::LINK,
Structure::BLOCKQUOTE,
Structure::CODE
];
}

public function fromString(string $text): Text
{
return new \Data\src\TextHandling\Text\SimpleDocumentMarkdown($this, $text);
}

public function isRawStringCompliant(string $text): bool
{
$structure_patterns = [
'\!\[(.)*\]\((.)+\)', // images ![](url)
'\!\[(.)+\]', // images ![][url]
'\[(.)*\]\:(.)+' // images []:url
];

foreach ($structure_patterns as $pattern) {
if (mb_ereg_match($pattern, $text)) {
return false;
}
}

return true;
}
}
72 changes: 72 additions & 0 deletions components/ILIAS/Data/src/TextHandling/Shape/WordOnlyMarkdown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace Data\src\TextHandling\Shape;

use Data\src\TextHandling\Structure;
use Data\src\TextHandling\Text\Text;

class WordOnlyMarkdown extends SimpleDocumentMarkdown
{
/**
* @return mixed[] consts from Structure
*/
public function getSupportedStructure(): array
{
return [
Structure::BOLD,
Structure::ITALIC
];
}

public function fromString(string $text): Text
{
return new \Data\src\TextHandling\Text\WordOnlyMarkdown($this, $text);
}

public function isRawStringCompliant(string $text): bool
{
$structure_patterns = [
'^(\#){1,6}(\ )', // headings 1 - 6
'^(\- )', // unordered list
'^(\* )', // unordered list
'^(\+ )', // unordered list
'^([0-9]+)(\.\ )', // ordered list
'(\ ){2}', // paragraph via space
'(\\\)', // paragraph
'\[(.)*\]\((.)+\)', // link [title](url)
'\[(.)*\]\([.]+\)', // link [id][url]
'\[(.)*\]\:(.)+', // link [id]:url
'\!\[(.)*\]\((.)+\)', // images ![](url)
'\!\[(.)+\]', // images ![][url]
'\[(.)*\]\:(.)+', // images []:url
'^(\>)+', // blockquote
'(\`)' // code only
];

foreach ($structure_patterns as $pattern) {
if (mb_ereg_match($pattern, $text)) {
return false;
}
}

return true;
}
}
40 changes: 40 additions & 0 deletions components/ILIAS/Data/src/TextHandling/Structure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace Data\src\TextHandling;

enum Structure
{
// heading 1-6 are cases for <h1> to <h6>
case HEADING_1;
case HEADING_2;
case HEADING_3;
case HEADING_4;
case HEADING_5;
case HEADING_6;
case BOLD;
case ITALIC;
case UNORDERED_LIST;
case ORDERED_LIST;
case LINK;
case PARAGRAPH;
case BLOCKQUOTE;
case CODE;
}
30 changes: 30 additions & 0 deletions components/ILIAS/Data/src/TextHandling/Text/HTML.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace Data\src\TextHandling\Text;

class HTML
{
public function __construct(
protected string $html_text = ""
) {
$this->html_text = $html_text;
}
}
Loading
Loading