This repository has been archived by the owner on Mar 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTextformatterParsedownExtraPlugin.module.php
136 lines (123 loc) · 5.03 KB
/
TextformatterParsedownExtraPlugin.module.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
136
<?php
/**
* TextformatterParsedownExtraPlugin
*
* @processwire 2.8+
* @author: Mike Rockett
* @license ISC
*/
class TextformatterParsedownExtraPlugin extends Textformatter
{
/**
* Module constructor
*/
public function __construct()
{
parent::__construct();
}
/**
* Textformatter formatValue
* @param $text
*/
public function formatValue(Page $page, Field $field, &$value)
{
$value = $this->parsedown($value);
}
/**
* Required function for obtaining module info
* in the "Details" tab of a field.
* @return Array
*/
public function getModuleInfo()
{
return json_decode(file_get_contents(__DIR__ . '/TextformatterParsedownExtraPlugin.info.json'), true);
}
/**
* Configure ParsedownExtraPlugin, parse text, and return.
* @param $input
* @return string
*/
public function parsedown($input)
{
// Require the three libaries
require_once __DIR__ . '/lib/Parsedown.php';
require_once __DIR__ . '/lib/ParsedownExtra.php';
require_once __DIR__ . '/lib/ParsedownExtraPlugin.php';
// Instatiate the parser
$parser = new ParsedownExtraPlugin();
// Set options
// Module Config Name Package Name Module default [| package default]
// - html5ElementSuffix element_suffix true | " />"
// - abbreviations == ''
// - linkAttributes links_attr ''
// - externalLinkAttributes links_external_attr ''
// - imageAttributes images_attr ''
// - externalImageAttributes images_external_attr ''
// - codeClass code_class 'language-%s'
// - codeBlockAttrParent code_block_attr_on_parent false
// - tableClass table_class null
// - tableAlignClass table_align_class null
// - footnoteLinkId footnote_link_id 'fn:%s'
// - footnoteBacklinkId footnote_back_link_id 'fnref%s:%s'
// - footnoteClass footnote_class 'footnotes'
// - footnoteLinkClass footnote_link_class 'footnote-ref'
// - footnoteBacklinkClass footnote_back_link_class 'footnote-backref'
// - footnoteLinkText footnote_link_text null
// - footnoteBacklinkText footnote_back_link_text '↩'
// Element Suffix (HTML5 or XHTML)
$parser->element_suffix = ($this->html5ElementSuffix === true) ? '>' : ' />';
// Parse any provided property lists to an array
// for ParsedownExtraPlugin.
$propertyMappings = [
'abbreviations' => ['abbreviations', "\n"], # ==
'linkAttributes' => ['links_attr', ','],
'externalLinkAttributes' => ['links_external_attr', ','],
'imageAttributes' => ['images_attr', ','],
'externalImageAttributes' => ['images_external_attr', ','],
];
foreach ($propertyMappings as $config => $setting) {
if (!empty($this->$config)) {
$parser->{$setting[0]} = $this->parsePropertyList($this->$config, $setting[1]);
}
}
// Any remaining mappings may just be set as-is
$configMappings = [
'codeClass' => 'code_class',
'codeBlockAttrParent' => 'code_block_attr_on_parent',
'tableClass' => 'table_class',
'tableAlignClass' => 'table_align_class',
'footnoteLinkId' => 'footnote_link_id',
'footnoteBacklinkId' => 'footnote_back_link_id',
'footnoteClass' => 'footnote_class',
'footnoteLinkClass' => 'footnote_link_class',
'footnoteBacklinkClass' => 'footnote_back_link_class',
'footnoteLinkText' => 'footnote_link_text',
'footnoteBacklinkText' => 'footnote_back_link_text',
];
foreach ($configMappings as $config => $setting) {
$parser->$setting = $this->$config;
}
// Parse input and return
return $parser->text($input);
}
/**
* Parse a property list
* Valid separators are "=" and ":"
* Separators may be surrounded by whitespace - this will be trimmed. Ex:
* MSFT=Microsoft
* HP: Hewlett Packard
* SKU = Stock Keeping Unit
* @param $list
* @return Array
*/
protected function parsePropertyList($list, $propertySeparator = "\n")
{
$properties = explode($propertySeparator, $list);
foreach ($properties as $property) {
$separator = strpbrk($property, '=:');
list($key, $value) = explode($separator[0], $property);
$propertiesArray[trim($key)] = trim($value);
}
return $propertiesArray;
}
}