diff --git a/bridges/GitHubGistBridge.php b/bridges/GitHubGistBridge.php new file mode 100644 index 00000000..6abd2ecd --- /dev/null +++ b/bridges/GitHubGistBridge.php @@ -0,0 +1,164 @@ + array( + 'name' => 'Gist', + 'type' => 'text', + 'required' => true, + 'title' => 'Insert Gist ID or URI', + 'exampleValue' => '2646763, https://gist.github.com/2646763' + ) + )); + + private $filename; + + public function getURI() { + + $id = $this->getInput('id') ?: ''; + + $urlpath = parse_url($id, PHP_URL_PATH); + + if($urlpath) { + + $components = explode('/', $urlpath); + $id = end($components); + + } + + return static::URI . '/' . $id; + + } + + public function getName() { + return $this->filename ? $this->filename . ' - ' . static::NAME : static::NAME; + } + + public function collectData() { + + $html = getSimpleHTMLDOM($this->getURI(), + null, + null, + true, + true, + DEFAULT_TARGET_CHARSET, + false, // Do NOT remove line breaks + DEFAULT_BR_TEXT, + DEFAULT_SPAN_TEXT) + or returnServerError('Could not request ' . $this->getURI()); + + $html = defaultLinkTo($html, static::URI); + + $fileinfo = $html->find('[class="file-info"]', 0) + or returnServerError('Could not find file info!'); + + $this->filename = $fileinfo->plaintext; + + $comments = $html->find('div[class="timeline-comment-wrapper"]'); + + if(is_null($comments)) { // no comments yet + return; + } + + foreach($comments as $comment) { + + $uri = $comment->find('a[href^=#gistcomment]', 0) + or returnServerError('Could not find comment anchor!'); + + $title = $comment->find('div[class="unminimized-comment"] h3[class="timeline-comment-header-text"]', 0) + or returnServerError('Could not find comment header text!'); + + $datetime = $comment->find('[datetime]', 0) + or returnServerError('Could not find comment datetime!'); + + $author = $comment->find('a.author', 0) + or returnServerError('Could not find author name!'); + + $message = $comment->find('[class="comment-body"]', 0) + or returnServerError('Could not find comment body!'); + + $item = array(); + + $item['uri'] = $this->getURI() . $uri->href; + $item['title'] = str_replace('commented', 'commented on', $title->plaintext); + $item['timestamp'] = strtotime($datetime->datetime); + $item['author'] = '' . $author->plaintext . ''; + $item['content'] = $this->fixContent($message); + // $item['enclosures'] = array(); + // $item['categories'] = array(); + + $this->items[] = $item; + + } + + } + + /** Removes all unnecessary tags and adds formatting */ + private function fixContent($content){ + + // Restore code (inside
) highlighting
+		foreach($content->find('pre') as $pre) {
+
+			$pre->style = <<find('code', 0);
+
+			if($code) {
+
+				$code->style = << not inside 
 (`inline-code`)
+		foreach($content->find('code') as $code) {
+
+			if($code->parent()->tag === 'pre') {
+				continue;
+			}
+
+			$code->style = <<find('p') as $p) {
+			$p->style = 'margin-bottom: 16px;';
+		}
+
+		// Remove unnecessary tags
+		$content = strip_tags(
+			$content->innertext,
+			'



    • ' + ); + + return $content; + + } + +}