Repository navigation
Expand file tree
/
Copy pathTextformatterInsertDummyContent.module
More file actions
82 lines (60 loc) · 1.94 KB
/
Copy pathTextformatterInsertDummyContent.module
File metadata and controls
82 lines (60 loc) · 1.94 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
78
79
80
81
82
<?php namespace ProcessWire;
/**
* ProcessWire Textformatter to insert dummy content
*
* ProcessWire 3.x, Copyright 2016 by Ryan Cramer
* https://processwire.com
*
* Usage
* [dc3] will be replaced by 3 paras from 'dummytext.txt' e.g. for body text
* [dc4w] will be replaced by 4 words from paragraph 1 of 'dummytext.txt' e.g. for headings
*
*/
class TextformatterInsertDummyContent extends Textformatter {
public static function getModuleInfo() {
return array(
'title' => 'Insert Dummy Content',
'summary' => "Development Tool - allows special tags to be replaced by paragraphs or optionally words of dummy content.",
'version' => '1.0.0',
);
}
public function format(&$str) {
static $seeded = false;
if (!$seeded) {
mt_srand($this->page->id);
$seeded = true;
}
$textfile = $this->config->paths->siteModules.'TextformatterInsertDummyContent'.DIRECTORY_SEPARATOR.'dummytext.txt';
if(!file_exists($textfile)){
$str = "<span style='color:red'>Error - 'dummytext.txt' not found</span> (TextformatterInsertDummyContent)";
return;
}
$paras = file($textfile,FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
preg_match("/\[dc:?(\d)-?(\d?)(p|w?)\]/", $str, $matches);
if(!$matches) return;
$fullMatch = $matches[0];
$showMin = $matches[1];
$showMax = $matches[2];
$showWhat = $matches[3];
if($showMax){
$showHowMany = mt_rand($showMin, $showMax);
} else {
$showHowMany = $showMin;
}
$out = '';
if($showWhat == 'w'){
$wordStart = $this->page->id % (str_word_count($paras[0]) - $showMin);
$words = str_word_count($paras[0],1);
for($w = $wordStart; $w < ($wordStart + $showHowMany); $w++){
$out .= "{$words[$w]} ";
}
$out = rtrim(ucwords($out));
} else {
$paraStart = mt_rand(0, count($paras) - $showHowMany);
for($p = $paraStart; $p < ($paraStart + $showHowMany); $p++){
$out .= "<p>{$paras[$p]}</p>";
}
}
$str = str_replace($fullMatch, $out, $str);
}
}