forked from Renobird/TextformatterAccordion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextformatterAccordion.module
More file actions
77 lines (58 loc) · 2.32 KB
/
TextformatterAccordion.module
File metadata and controls
77 lines (58 loc) · 2.32 KB
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
<?php
/**
* ProcessWire Accordion Textformatter
* by "Renobird" Tom Reno
*
* Convert portions of textarea content into a <dl> and collapse into an accordion.
*
* ProcessWire 2.x
* Copyright (C) 2010 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class TextformatterAccordion extends Textformatter {
public static function getModuleInfo() {
return array(
'title' => 'Accordion',
'version' => 1,
'summary' => "Convert portions of textarea content into a <dl> and collapse into an accordion. Put 5+ hyphens '-----Your Title Here' on a single line (within paragraph tags) to create specify title the paragraphs below will be collapsed. This module is largely build using code from the TextformatterPagination plugin with the idea that they could be interchangeable.",
);
}
protected $options = array(
'outerMarkup' => "<dl class='accordion'>{out}</dl>",
'innerMarkup' => "<dt><a href='#{anchor}'>{title}</a></dt><dd id='{anchor}'>{body}</dd>",
);
public function format(&$str){
$out = '';
if(strpos($str, '<p>-----') === false) return; // quick exit
$blocks = explode("<p>/////</p>", $str);
if(empty($blocks[0])) array_shift($blocks);
foreach ($blocks as $key => $val){
$accordionItems = '';
$start = strpos($val, '<p>-----');
if ($start === false){
$out .= $val; // no accordion items in this block, just add $val to $out
continue;
}
$intro = trim(substr($val, 0, $start));
$str = trim(substr($val, $start));
$separator = '~~~Panel~~~';
$string = preg_replace('{<p>\s*-----+\s*(.*?)</p>}i', $separator . '$1~~~' . "\t", $val);
$accordion = explode($separator, $string);
if(empty($$accordion[0])) array_shift($accordion);
foreach($accordion as $k => $v) {
$pos = strpos($v, "~~~\t");
$title = trim(substr($v, 0, $pos));
$title == '' ? $title = "Untitled Item" : ''; // no title specified
$anchor = strtolower(wire('sanitizer')->pageName($title));
$accordion[$k] = substr($v, $pos+4);
$accordionItems .= str_replace(array('{anchor}', '{title}', '{body}'), array($anchor, trim($title), substr($v, $pos+4)), $this->options['innerMarkup']);
}
$out .= $intro . str_replace('{out}', $accordionItems, $this->options['outerMarkup']);
}
$str = $out;
}
}