1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage GitHub
5 *
6 * @copyright Copyright (C) 2012 - 2016 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * GitHub API Markdown class.
14 *
15 * @documentation https://developer.github.com/v3/markdown
16 *
17 * @since 3.3 (CMS)
18 * @deprecated 4.0 Use the `joomla/github` package via Composer instead
19 */
20 class JGithubPackageMarkdown extends JGithubPackage
21 {
22 /**
23 * Method to render a markdown document.
24 *
25 * @param string $text The text object being parsed.
26 * @param string $mode The parsing mode; valid options are 'markdown' or 'gfm'.
27 * @param string $context An optional repository context, only used in 'gfm' mode.
28 *
29 * @since 3.3 (CMS)
30 * @throws DomainException
31 * @throws InvalidArgumentException
32 *
33 * @return string Formatted HTML
34 */
35 public function render($text, $mode = 'gfm', $context = null)
36 {
37 // The valid modes
38 $validModes = array('gfm', 'markdown');
39
40 // Make sure the scope is valid
41 if (!in_array($mode, $validModes))
42 {
43 throw new InvalidArgumentException(sprintf('The %s mode is not valid. Valid modes are "gfm" or "markdown".', $mode));
44 }
45
46 // Build the request path.
47 $path = '/markdown';
48
49 // Build the request data.
50 $data = str_replace('\\/', '/', json_encode(
51 array(
52 'text' => $text,
53 'mode' => $mode,
54 'context' => $context,
55 )
56 )
57 );
58
59 // Send the request.
60 $response = $this->client->post($this->fetchUrl($path), $data);
61
62 // Validate the response code.
63 if ($response->code != 200)
64 {
65 // Decode the error response and throw an exception.
66 $error = json_decode($response->body);
67 $message = (isset($error->message)) ? $error->message : 'Error: ' . $response->code;
68 throw new DomainException($message, $response->code);
69 }
70
71 return $response->body;
72 }
73 }
74