-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmarkdown-rubytext.php
81 lines (76 loc) · 2.43 KB
/
markdown-rubytext.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace Grav\Plugin;
use Composer\Autoload\ClassLoader;
use Grav\Common\Plugin;
/**
* Class MarkdownRubytextPlugin
* @package Grav\Plugin
*/
class MarkdownRubytextPlugin extends Plugin
{
/**
* @return array
*
* The getSubscribedEvents() gives the core a list of events
* that the plugin wants to listen to. The key of each
* array section is the event that the plugin listens to
* and the value (in the form of an array) contains the
* callable (or function) as well as the priority. The
* higher the number the higher the priority.
*/
public static function getSubscribedEvents(): array
{
return [
'onMarkdownInitialized' => [
['onMarkdownInitialized', 0]
]
];
}
/**
* Composer autoload.
*is
* @return ClassLoader
*/
public function autoload(): ClassLoader
{
return require __DIR__ . '/vendor/autoload.php';
}
/**
* Initialize the plugin
*/
public function onMarkdownInitialized(Event $event)
{
$markdown = $event['markdown'];
// Initialize Text example
$markdown->addInlineType('{', 'RubyText');
// Add function to handle this
$markdown->inlineRubyText = function($excerpt) {
if (preg_match('/(*UTF8)\{r}([^\s{]+){\/r}|{r=([a-z]{2,3})\/([a-z]{2,3})}([^\s{]+){\/r}/', $excerpt['text'], $match))
{
$matchright = array_slice($match,1);
$rubyatt = 'ruby';
$rbatt = 'rt';
if ($matchright[0] == "") {
$matchright[0] = $matchright[3];
$rubyatt = 'ruby lang="'.$matchright[1].'"';
$rbatt = 'rt lang="'.$matchright[2].'"';
};
$matchleftright = rtrim($matchright[0],')');
$extract = explode (')',$matchleftright);
$out = "";
foreach ($extract as $value) {
$value = explode('(',$value);
$out .= $value[0].'<rp>(</rp><'.$rbatt.'>'.$value[1].'</rb><rp>)</rp>';
};
$totout = array(
'extent' => strlen($match[0]),
'element' => array(
'name' => $rubyatt,
'text' => $out
));
return
$totout;
}
};
}
}