Skip to content

Commit

Permalink
feat: add span elements parser
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Jul 19, 2018
1 parent e3f75b2 commit 2d5268e
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/SpanElementParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Ahc;

class SpanElementParser
{
use HtmlHelper;

const RE_URL = '~<(https?:[\/]{2}[^\s]+?)>~';
const RE_EMAIL = '~<(\S+?@\S+?)>~';
const RE_MD_IMG = '~!\[(.+?)\]\s*\((.+?)\s*(".+?")?\)~';
const RE_MD_URL = '~\[(.+?)\]\s*\((.+?)\s*(".+?")?\)~';
const RE_MD_FONT = '!(\*{1,2}|_{1,2}|`|~~)(.+?)\\1!';

public function parse($markup)
{
return $this->spans(
$this->anchors(
$this->links($markup)
)
);
}

protected function links($markup)
{
$markup = $this->emails($markup);

return \preg_replace(
static::RE_URL,
'<a href="$1">$1</a>',
$markup
);
}

protected function emails($markup)
{
return \preg_replace(
static::RE_EMAIL,
'<a href="mailto:$1">$1</a>',
$markup
);
}

protected function anchors($markup)
{
$markup = $this->images($markup);

return \preg_replace_callback(static::RE_MD_URL, function ($a) {
$title = isset($a[3]) ? " title={$a[3]} " : '';

return "<a href=\"{$a[2]}\"{$title}>{$a[1]}</a>";
}, $markup);
}

protected function images($markup)
{
return \preg_replace_callback(static::RE_MD_IMG, function ($img) {
$title = isset($img[3]) ? " title={$img[3]} " : '';
$alt = $img[1] ? " alt=\"{$img[1]}\" " : '';

return "<img src=\"{$img[2]}\"{$title}{$alt}/>";
}, $markup);
}

protected function spans($markup)
{
// em/code/strong/del
return \preg_replace_callback(static::RE_MD_FONT, function ($em) {
switch (\substr($em[1], 0, 2)) {
case '**':
case '__':
$tag = 'strong';
break;

case '~~':
$tag = 'del';
break;

case $em[1] === '*':
case $em[1] === '_':
$tag = 'em';
break;

default:
$tag = 'code';
$em[2] = $this->escape($em[2]);
}

return "<$tag>{$em[2]}</$tag>";
}, $markup);
}
}

0 comments on commit 2d5268e

Please sign in to comment.