-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarkdownMacros.php
135 lines (120 loc) · 4.67 KB
/
MarkdownMacros.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* Markdown Macros.
*
* Designed to give the power user more flexibility in her designs.
* Currently, as Markdown stands, it works well for simple pages.
* Unfortunately, as it stands, it doesn't allow you to add classes or
* id's to all objects.
*
* An example would be tables. You can easily create a table. However,
* it is nigh impossible to create a left justified and right justified
* one on the same page.
*
* Markdown Macros solves this problem by implement macro substitution.
* Just before the page is rendered to HTML, Markdown Macros substitutes
* it's user defined text.
*
* @author Lloyd Sargent
* @link
* @link https://github.com/lloydsargent/MarkdownMacros
* @license http://opensource.org/licenses/MIT The MIT License
*
* Hours having written php code, maybe 4 hours. Heh.
*/
class MarkdownMacros extends AbstractPicoPlugin
{
const API_VERSION = 2;
private $currentPagePath;
private $base_url = "";
/**
* Triggered after Pico has prepared the raw file contents for parsing
*
* @see DummyPlugin::onContentParsing()
* @see Pico::parseFileContent()
* @see DummyPlugin::onContentParsed()
*
* @param string &$markdown Markdown contents of the requested page
*
* @return void
*/
public function onContentPrepared(&$markdown)
{
$myconfig = $this->getPico()->getConfig(); // get the configuration list
$macros_name = $myconfig['custom_macros']; // get my custom macros name out of the list
$macros = $myconfig[$macros_name]; // get my list of custom macros
//----- build an array of key:value pairs representing the macros
$replacement_list = $this->createReplacementList($markdown, $macros);
//----- replace the macro with the substitute text
$this->performSubstitution($markdown, $macros, $replacement_list);
return;
}
/**
* @fn createReplacementList
*
* @brief createReplacementList searches through the list for our macros.
* It then creates an array of macro name: character position.
* Finally it sorts in descending order (last is first).
*
* The idea is that we don't traverse the entire text search using
* grep.
*
* @param $original_text - orignal markdown text
* @param $macro_list - a list of key:value macros
*
* @returns array of key:value pairs
*/
private function createReplacementList($original_text, $macro_list) {
$result = [];
$offset = 0;
foreach ($macro_list as $macro => $replacement) {
// $start_of_macro = rtrim($macro, ']'); // too simple, matched short strings
$start_of_macro = $macro;
while ($offset < strlen($original_text)) {
$temp = NULL;
$offset = strpos($original_text, $start_of_macro, $offset);
if ($offset == false) {
break;
}
$temp[$macro] = $offset;
$offset = $offset + 1;
array_push($result, $temp); // store the dictionary item
}
}
//----- sort by descending order
usort($result, 'MarkdownMacros::descendingArray');
return $result;
}
/**
* @fn descendingArray
*
* @param $first - dictionary item
* @param $second - dictionary item
*/
private static function descendingArray($first, $second) {
//----- get the value for the key:value pairs
$val1 = current($first); // get the value of $first
$val2 = current($second); // get the value of $second
if ($val1 == $val2) {
return 0;
}
if ($val1 > $val2) {
return -1;
}
return 1;
}
/**
* @fn performSubstitution
*
* @details
*
* @returns
*/
private function performSubstitution(&$markdown, $macros, $replacement_list) {
//----- now go through the replacement_list and substitute our macro's text
foreach ($replacement_list as $replacement_item) {
$replacement_text = $macros[key($replacement_item)];
$markdown = substr_replace($markdown, $replacement_text, current($replacement_item), strlen(key($replacement_item)));
}
}
}