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

feat: Allow callable to clickable replacement #314

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

RyujiAMANO
Copy link
Contributor

After applying this change, you can use callable for clickable conversion table replacements.

Example of Usage

This is an example of preloading that truncates the length of the text portion when making a URL clickable.

class Ryus_TextFilterClickable extends XCube_ActionFilter
{
    const SHORTEN_CLICKABLE_LENGTH = 80;
    public function preBlockFilter()
    {
        $this->mRoot->mDelegateManager->add(
            'Legacy_TextFilter.MakeClickableConvertTable',
            [&$this, 'hook'],
            XCUBE_DELEGATE_PRIORITY_3
        );
    }

    public function hook(&$patterns, &$replacements)
    {
        $http_pat = "(^|[^]_a-z0-9-=\"'\/])([a-z]+?):\/\/([-_.!~*'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)(?=[\s\x80-\xff]|$)";
        $patterns =
            [
                "/{$http_pat}/i",
                "/(^|[^]_a-z0-9-=\"'\/])www\.([a-z0-9\-]+)\.([^, \r\n\"\(\)'<>]+)(?=[\s\x80-\xff]|$)/i",
                "/(^|[^]_a-z0-9-=\"'\/])ftp\.([a-z0-9\-]+)\.([^, \r\n\"\(\)'<>]+)(?=[\s\x80-\xff]|$)/i",
                "/(^|[^]_a-z0-9-=\"'\/:\.])([a-z0-9\-_\.]+?)@([a-z0-9!#\$%&'\*\+\-\/=\?^_\`{\|}~\.]+)(?=[\s\x80-\xff]|$)/i",
            ];
        $replacements =
            [
               // callable
                function (array $matches) {
                    $urlText = xoops_substr($matches[2] . '://' . $matches[3], 0, self::SHORTEN_CLICKABLE_LENGTH);
                    return sprintf('%s<a href="%s://%s" target="_blank">%s</a>', $matches[1], $matches[2], $matches[3],
                        $urlText);

                },
                function (array $matches) {
                    $urlText = 'www.' . xoops_substr($matches[2] . '.' . $matches[3], 0, self::SHORTEN_CLICKABLE_LENGTH);
                    return sprintf('%s<a href="http://www.%s.%s" target="_blank">%s</a>', $matches[1], $matches[2],
                        $matches[3], $urlText);

                },

                "\\1<a href=\"ftp://ftp.\\2.\\3\" target=\"_blank\">ftp.\\2.\\3</a>",
                "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>"
            ];
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant