silverstripe-docsviewer /code/DocumentationParser.php

Language PHP Lines 431
MD5 Hash 334ac85ad2be287bfefc2e731f71c374
Repository https://github.com/pixeltricks/silverstripe-docsviewer.git View Raw File
  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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<?php

/**
 * Parser wrapping the Markdown Extra parser.
 * 
 * @see http://michelf.com/projects/php-markdown/extra/
 *
 * @package docsviewer
 */
class DocumentationParser {

	const CODE_BLOCK_BACKTICK = 1;
	const CODE_BLOCK_COLON = 2;

	/**
	 * @var string Rewriting of api links in the format "[api:MyClass]" or "[api:MyClass::$my_property]".
	 */
	public static $api_link_base = 'http://api.silverstripe.org/search/lookup/?q=%s&version=%s&module=%s';
	
	/**
	 * @var array
	 */
	public static $heading_counts = array();
	
	/**
	 * Parse a given path to the documentation for a file. Performs a case 
	 * insensitive lookup on the file system. Automatically appends the file 
	 * extension to one of the markdown extensions as well so /install/ in a
	 * web browser will match /install.md or /INSTALL.md.
	 * 
	 * Filepath: /var/www/myproject/src/cms/en/folder/subfolder/page.md
	 * URL: http://myhost/mywebroot/dev/docs/2.4/cms/en/folder/subfolder/page
	 * Webroot: http://myhost/mywebroot/
	 * Baselink: dev/docs/2.4/cms/en/
	 * Pathparts: folder/subfolder/page
	 * 
	 * @param DocumentationPage $page
	 * @param String $baselink Link relative to webroot, up until the "root" 
	 *							of the module. Necessary to rewrite relative 
	 *							links
	 *
	 * @return String
	 */
	public static function parse(DocumentationPage $page, $baselink = null) {
		if(!$page || (!$page instanceof DocumentationPage)) return false;

		$md = $page->getMarkdown();
		
		// Pre-processing
		$md = self::rewrite_image_links($md, $page);
		$md = self::rewrite_relative_links($md, $page, $baselink);

		$md = self::rewrite_api_links($md, $page);
		$md = self::rewrite_heading_anchors($md, $page);
		$md = self::rewrite_code_blocks($md);
	
		require_once(DOCSVIEWER_PATH .'/thirdparty/markdown/markdown.php');
		
		$parser = new MarkdownExtra_Parser();
		$parser->no_markup = true;
		
		return $parser->transform($md);
	}
	
	public static function rewrite_code_blocks($md) {
		$started = false;
		$inner = false;
		$mode = false;
		$end = false;

		$lines = explode("\n", $md);
		$output = array();

		foreach($lines as $i => $line) {
			if(!$started && preg_match('/^\t*:::\s*(.*)/', $line, $matches)) {
				// first line with custom formatting
				$started = true;
				$mode = self::CODE_BLOCK_COLON;
				$output[$i] = sprintf('<pre class="brush: %s">', (isset($matches[1])) ? $matches[1] : "");
			} 
			elseif(!$started && preg_match('/^\t*```\s*(.*)/', $line, $matches)) {
				$started = true;
				$mode = self::CODE_BLOCK_BACKTICK;
				$output[$i] = sprintf('<pre class="brush: %s">', (isset($matches[1])) ? $matches[1] : "");
			} 
			elseif($started && $mode == self::CODE_BLOCK_BACKTICK) {
				// inside a backtick fenced box
				if(preg_match('/^\t*```\s*/', $line, $matches)) {
					// end of the backtick fenced box. Unset the line that contains the backticks
					$end = true;
				}
				else {
					// still inside the line.
					$output[$i] = ($started) ? '' : '<pre>' . "\n";
					$output[$i] .= htmlentities($line, ENT_COMPAT, 'UTF-8');
					$inner = true;
				}
			} 
			elseif(preg_match('/^\t(.*)/', $line, $matches)) {
				// inner line of block, or first line of standard markdown code block
				// regex removes first tab (any following tabs are part of the code).
				$output[$i] = ($started) ? '' : '<pre>' . "\n";
				$output[$i] .= htmlentities($matches[1], ENT_COMPAT, 'UTF-8');
				$inner = true;
				$started = true;
			}
			elseif($started && $inner && $mode == self::CODE_BLOCK_COLON && trim($line) === "") {
				// still inside a colon based block, if the line is only whitespace 
				// then continue with  with it. We can continue with it for now as 
				// it'll be tidied up later in the $end section.
				$inner = true;
				$output[$i] = $line;
			}
			elseif($started && $inner) {
				// line contains something other than whitespace, or tabbed. E.g
				// 	> code
				//	> \n
				//	> some message
				//
				// So actually want to reset $i to the line before this new line
				// and include this line. The edge case where this will fail is
				// new the following segment contains a code block as well as it
				// will not open.
				$end = true;
				$output[$i] = $line;
				$i = $i -1;
			}
			else {
				$output[$i] = $line;
			}

			if($end) {
				$output = self::finalize_code_output($i, $output);

				// reset state
				$started = $inner = $mode = $end = false;
			}
		}

		if($started) {
			$output = self::finalize_code_output($i, $output);
		}

		return join("\n", $output);

	}

	/**
	 * @param int
	 * @param array
	 *
	 * @return array
	 */
	private static function finalize_code_output($i, $output) {
		$j = $i;

		while(isset($output[$j]) && trim($output[$j]) === "") {
			unset($output[$j]);
		
			$j--;
		}
				
		if(isset($output[$j])) {
			$output[$j] .= "</pre>\n";
		}

		else {
			$output[$j] = "</pre>\n\n";
		}

		return $output;				
	}
	
	static function rewrite_image_links($md, $page) {
		// Links with titles
		$re = '/
			!
			\[
				(.*?) # image title (non greedy)
			\] 
			\(
				(.*?) # image url (non greedy)
			\)
		/x';
		preg_match_all($re, $md, $images);
		if($images) foreach($images[0] as $i => $match) {
			$title = $images[1][$i];
			$url = $images[2][$i];
			
			// Don't process absolute links (based on protocol detection)
			$urlParts = parse_url($url);

			if($urlParts && isset($urlParts['scheme'])) continue;
			
			// Rewrite URL (relative or absolute)
			$baselink = Director::makeRelative(dirname($page->getPath()));
			$relativeUrl = rtrim($baselink, '/') . '/' . ltrim($url, '/');
			
			// Resolve relative paths
			while(strpos($relativeUrl, '/..') !== FALSE) {
				$relativeUrl = preg_replace('/\w+\/\.\.\//', '', $relativeUrl);
			}
			
			// Replace any double slashes (apart from protocol)
			$relativeUrl = preg_replace('/([^:])\/{2,}/', '$1/', $relativeUrl);
			
			// Make it absolute again
			$absoluteUrl = Director::absoluteBaseURL() . $relativeUrl;
			
			// Replace in original content
			$md = str_replace(
				$match, 
				sprintf('![%s](%s)', $title, $absoluteUrl),
				$md
			);
		}
		
		return $md;
	}
		
	/**
	 * Rewrite links with special "api:" prefix, from two possible formats:
	 * 1. [api:DataObject]
	 * 2. (My Title)(api:DataObject)
	 * 
	 * Hack: Replaces any backticks with "<code>" blocks,
	 * as the currently used markdown parser doesn't resolve links in backticks,
	 * but does resolve in "<code>" blocks.
	 * 
	 * @param String $md
	 * @param DocumentationPage $page
	 * @return String
	 */
	static function rewrite_api_links($md, $page) {
		// Links with titles
		$re = '/
			`?
			\[
				(.*?) # link title (non greedy)
			\] 
			\(
				api:(.*?) # link url (non greedy)
			\)
			`?
		/x';
		preg_match_all($re, $md, $linksWithTitles);
		if($linksWithTitles) {
			foreach($linksWithTitles[0] as $i => $match) {
				$title = $linksWithTitles[1][$i];
				$subject = $linksWithTitles[2][$i];
				$url = sprintf(self::$api_link_base, $subject, $page->getVersion(), $page->getEntity()->getFolder());
				$md = str_replace(
					$match, 
					sprintf('[%s](%s)', $title, $url),
					$md
				);
			}
		}
		
		// Bare links
		$re = '/
			`?
			\[
				api:(.*?)
			\]
			`?
		/x';
		preg_match_all($re, $md, $links);
		if($links) {
			foreach($links[0] as $i => $match) {
				$subject = $links[1][$i];
				$url = sprintf(self::$api_link_base, $subject, $page->getVersion(), $page->getEntity()->getFolder());
				$md = str_replace(
					$match, 
					sprintf('[%s](%s)', $subject, $url),
					$md
				);
			}
		}
		
		return $md;
	}
	
	/**
	 *
	 */
	public static function rewrite_heading_anchors($md, $page) {
		$re = '/^\#+(.*)/m';	
		$md = preg_replace_callback($re, array('DocumentationParser', '_rewrite_heading_anchors_callback'), $md);
		
		return $md; 
	}
	
	public static function _rewrite_heading_anchors_callback($matches) {
		$heading = $matches[0];
		$headingText = $matches[1];

		if(preg_match('/\{\#.*\}/', $headingText)) return $heading;

		if(!isset(self::$heading_counts[$headingText])) {
			self::$heading_counts[$headingText] = 1; 
		}
		else {
			self::$heading_counts[$headingText]++; 
			$headingText .= "-" . self::$heading_counts[$headingText]; 
		}

		return sprintf("%s {#%s}", preg_replace('/\n/', '', $heading), self::generate_html_id($headingText));
	}
	
	/**
	 * Generate an html element id from a string
	 * 
	 * @return String
	 */ 
	static function generate_html_id($title) {
		$t = $title;
		$t = str_replace('&amp;','-and-',$t);
		$t = str_replace('&','-and-',$t);
		$t = preg_replace('/[^A-Za-z0-9]+/','-',$t);
		$t = preg_replace('/-+/','-',$t);
		$t = trim($t, '-');
		$t = strtolower($t);
				
		return $t;
	}
	
	/**
	 * Resolves all relative links within markdown.
	 * 
	 * @param String $md Markdown content
	 * @param DocumentationPage $page
	 * @param String $baselink
	 * @return String Markdown
	 */
	static function rewrite_relative_links($md, $page, $baselink = null) {
		if(!$baselink) $baselink = $page->getEntity()->getRelativeLink();

		$re = '/
			([^\!]?) # exclude image format
			\[
				(.*?) # link title (non greedy)
			\] 
			\(
				(.*?) # link url (non greedy)
			\)
		/x';
		preg_match_all($re, $md, $matches);
		
		// relative path (relative to module base folder), without the filename.
		// For "sapphire/en/current/topics/templates", this would be "templates"
		$relativePath = dirname($page->getRelativePath());
		if($relativePath == '.') $relativePath = '';
		
		// file base link
		$fileBaseLink = Director::makeRelative(dirname($page->getPath()));
		
		if($matches) {
			foreach($matches[0] as $i => $match) {
				$title = $matches[2][$i];
				$url = $matches[3][$i];
			
				// Don't process API links
				if(preg_match('/^api:/', $url)) continue;
			
				// Don't process absolute links (based on protocol detection)
				$urlParts = parse_url($url);
				if($urlParts && isset($urlParts['scheme'])) continue;

				// for images we need to use the file base path
				if(preg_match('/_images/', $url)) {
					$relativeUrl = Controller::join_links(
						Director::absoluteBaseURL(),
						$fileBaseLink,
						$url
					);
				}
				else {
					// Rewrite public URL
					if(preg_match('/^\//', $url)) {
						// Absolute: Only path to module base
						$relativeUrl = Controller::join_links($baselink, $url);
					} else {
						// Relative: Include path to module base and any folders
						$relativeUrl = Controller::join_links($baselink, $relativePath, $url);
					}
				}
				
				// Resolve relative paths
				while(strpos($relativeUrl, '..') !== FALSE) {
					$relativeUrl = preg_replace('/\w+\/\.\.\//', '', $relativeUrl);
				}
			
				// Replace any double slashes (apart from protocol)
				$relativeUrl = preg_replace('/([^:])\/{2,}/', '$1/', $relativeUrl);
			
				// Replace in original content
				$md = str_replace(
					$match, 
					sprintf('%s[%s](%s)', $matches[1][$i], $title, $relativeUrl),
					$md
				);
			}
		}
		
		return $md;
	}
	
	/**
	 * Strips out the metadata for a page
	 *
	 * @param DocumentationPage
	 */
	public static function retrieve_meta_data(DocumentationPage &$page) {
		if($md = $page->getMarkdown()) {
			$matches = preg_match_all('/
				(?<key>[A-Za-z0-9_-]+): 
				\s*
				(?<value>.*)
			/x', $md, $meta);
		
			if($matches) {
				foreach($meta['key'] as $index => $key) {
					if(isset($meta['value'][$index])) {
						$page->setMetaData($key, $meta['value'][$index]);
					}
				}
			}
		}
	}
}
Back to Top