Permalink
Browse files

Parse items through GitHub's Markdown API when received so we don't h…

…ave to do it every time a page is rendered
  • Loading branch information...
mbabker committed Jan 3, 2013
1 parent ba25795 commit 935fef6f37e2c049ed2534b11c0d28e18e5e3f9c
@@ -64,6 +64,14 @@ class TrackerApplicationComments extends JApplicationCli
*/
protected $db;
/**
* JGithub object
*
* @var JGithub
* @since 1.0
*/
protected $github;
/**
* Array containing the issues from the database and their GitHub ID
*
@@ -123,7 +131,7 @@ protected function getComments()
}
// Instantiate JGithub
$github = new JGithub($options);
$this->github = new JGithub($options);
try
{
@@ -132,7 +140,7 @@ protected function getComments()
$id = $issue->gh_id;
$this->out('Retrieving comments for issue #' . $id . ' from GitHub.', true);
$this->comments[$id] = $github->issues->getComments('joomla', 'joomla-cms', $id);
$this->comments[$id] = $this->github->issues->getComments('joomla', 'joomla-cms', $id);
}
}
// Catch any DomainExceptions and close the script
@@ -245,14 +253,17 @@ protected function processComments()
$this->db->quoteName('id'), $this->db->quoteName('issue_id'), $this->db->quoteName('submitter'), $this->db->quoteName('text'), $this->db->quoteName('created')
);
// Parse the body through GitHub's markdown parser
$body = $this->github->markdown->render($comment->body, 'gfm', 'JTracker/jissues');
$query->clear();
$query->insert($this->db->quoteName('#__issue_comments'));
$query->columns($columnsArray);
$query->values(
(int) $comment->id . ', '
. (int) $issue->id . ', '
. $this->db->quote($comment->user->login) . ', '
. $this->db->quote($comment->body) . ', '
. $this->db->quote($body) . ', '
. $this->db->quote(JFactory::getDate($comment->created_at)->toSql())
);
$this->db->setQuery($query);
@@ -46,10 +46,20 @@
*/
class TrackerApplicationRetrieve extends JApplicationCli
{
/**
* JGithub object
*
* @var JGithub
* @since 1.0
*/
protected $github;
/**
* Method to run the application routines.
*
* @return void
*
* @since 1.0
*/
protected function doExecute()
{
@@ -92,7 +102,7 @@ protected function getData()
}
// Instantiate JGithub
$github = new JGithub($options);
$this->github = new JGithub($options);
try
{
@@ -104,7 +114,7 @@ protected function getData()
do
{
$page++;
$issues_more = $github->issues->getListByRepository(
$issues_more = $this->github->issues->getListByRepository(
'joomla', // Owner
'joomla-cms', // Repository
null, // Milestone
@@ -188,7 +198,7 @@ protected function processIssues($issues)
$table = JTable::getInstance('Issue');
$table->gh_id = $issue->number;
$table->title = $issue->title;
$table->description = $issue->body;
$table->description = $this->github->markdown->render($issue->body, 'gfm', 'JTracker/jissues');
$table->status = ($issue->state == 'open') ? 1 : 10;
$table->opened = JFactory::getDate($issue->created_at)->toSql();
$table->modified = JFactory::getDate($issue->updated_at)->toSql();
@@ -31,23 +31,23 @@ class TrackerControllerPreview extends JControllerBase
*/
public function execute()
{
$o = new stdClass;
$o->text = $this->input->getHtml('text', '');
$text = $this->input->getHtml('text', '');
if (!$o->text)
// Check if we actually have something to parse
if (!$text)
{
echo 'Nothing to preview...';
$this->app->close();
}
$params = new JRegistry;
$params->set('luminous.format', 'html-full');
// Instantiate JGithub
$github = new JGithub;
JPluginHelper::importPlugin('content');
// Parse the text
$text = $github->markdown->render($text, 'gfm', 'JTracker/jissues');
JEventDispatcher::getInstance()->trigger('onContentPrepare', array('com_content.article', &$o, $params));
echo $o->text ? : 'Nothing to preview...';
// Echo out the text for
echo $text ? : 'Nothing to preview...';
$this->app->close();
}
@@ -57,7 +57,7 @@ public function render()
$this->editor = JEditor::getInstance('kisskontent');
$this->editorParams = array(
'preview-url' => 'index.php?option=com_tracker&task=preview&format=raw',
'preview-url' => 'index.php?option=com_tracker&task=preview',
'syntaxpage-link' => 'index.php?option=com_tracker&view=markdowntestpage',
);
@@ -97,7 +97,7 @@ public function render()
$this->editor = JEditor::getInstance('kisskontent');
$this->editorParams = array(
'preview-url' => 'index.php?option=com_tracker&task=preview&format=raw',
'preview-url' => 'index.php?option=com_tracker&task=preview',
'syntaxpage-link' => 'index.php?option=com_tracker&view=markdowntestpage',
);
@@ -54,25 +54,6 @@ public function render()
$this->comments = $this->model->getComments($id);
$this->fields = $this->model->getFields($id);
$dispatcher = JEventDispatcher::getInstance();
$o = new stdClass;
$o->text = $this->item->description;
$params = new JRegistry;
JPluginHelper::importPlugin('content');
$dispatcher->trigger('onContentPrepare', array ('com_tracker.markdown', &$o, $params));
$this->item->description_raw = $this->item->description;
$this->item->description = $o->text;
foreach ($this->comments as &$comment)
{
// @todo Maybe we should parse the comments on retrieval and write the result to the database
$dispatcher->trigger('onContentPrepare', array ('com_tracker.markdown', &$comment, $params));
}
// Build the toolbar
$this->buildToolbar();
@@ -153,13 +153,16 @@ protected function insertComment($data)
$db->quoteName('id'), $db->quoteName('issue_id'), $db->quoteName('submitter'), $db->quoteName('text'), $db->quoteName('created')
);
// Get a JGithub instance to parse the body through their parser
$github = new JGithub;
$query->insert($db->quoteName('#__issue_comments'));
$query->columns($columnsArray);
$query->values(
(int) $data->comment->id . ', '
. (int) $issueID . ', '
. $db->quote($data->comment->user->login) . ', '
. $db->quote($data->comment->body) . ', '
. $db->quote($github->markdown->render($data->comment->body, 'gfm', 'JTracker/jissues')) . ', '
. $db->quote(JFactory::getDate($data->comment->created_at)->toSql())
);
$db->setQuery($query);
@@ -191,10 +194,13 @@ protected function insertComment($data)
*/
protected function insertIssue($data)
{
// Get a JGithub instance to parse the body through their parser
$github = new JGithub;
$table = JTable::getInstance('Issue');
$table->gh_id = $data->issue->number;
$table->title = $data->issue->title;
$table->description = $data->issue->body;
$table->description = $github->markdown->render($data->issue->body, 'gfm', 'JTracker/jissues');
$table->status = ($data->issue->state) == 'open' ? 1 : 10;
$table->opened = JFactory::getDate($data->issue->created_at)->toSql();
$table->modified = JFactory::getDate($data->issue->updated_at)->toSql();
@@ -141,9 +141,12 @@ protected function insertData(JTableIssue $table, $data)
}
// Get a JGithub instance to parse the body through their parser
$github = new JGithub;
$table->gh_id = $data->issue->number;
$table->title = $data->issue->title;
$table->description = $data->issue->body;
$table->description = $github->markdown->render($data->issue->body, 'gfm', 'JTracker/jissues');
$table->status = $status;
$table->opened = JFactory::getDate($data->issue->created_at)->toSql();
$table->modified = JFactory::getDate($data->issue->updated_at)->toSql();
@@ -25,13 +25,13 @@ class JGithubMarkdown extends JGithubObject
* @param string $mode The parsing mode; valid options are 'markdown' or 'gfm'.
* @param string $context An optional repository context, only used in 'gfm' mode.
*
* @return object
* @return string Formatted HTML
*
* @since 1.0
* @throws DomainException
* @throws InvalidArgumentException
*/
public function create($text, $mode = 'gfm', $context = null)
public function render($text, $mode = 'gfm', $context = null)
{
// The valid modes
$validModes = array('gfm', 'markdown');
@@ -46,13 +46,13 @@ public function create($text, $mode = 'gfm', $context = null)
$path = '/markdown';
// Build the request data.
$data = json_encode(
$data = str_replace('\\/', '/', json_encode(
array(
'text' => $text,
'mode' => $mode,
'context' => $context
)
);
));
// Send the request.
$response = $this->client->post($this->fetchUrl($path), $data);
@@ -65,6 +65,6 @@ public function create($text, $mode = 'gfm', $context = null)
throw new DomainException($error->message, $response->code);
}
return json_decode($response->body);
return $response->body;
}
}

0 comments on commit 935fef6

Please sign in to comment.