2018-07-29 17:26:20 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class GitHubGistBridge extends BridgeAbstract
|
|
|
|
{
|
|
|
|
const NAME = 'GitHubGist comment bridge';
|
|
|
|
const URI = 'https://gist.github.com';
|
|
|
|
const DESCRIPTION = 'Generates feeds for Gist comments';
|
|
|
|
const MAINTAINER = 'logmanoriginal';
|
|
|
|
const CACHE_TIMEOUT = 3600;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-29 17:26:20 +03:00
|
|
|
const PARAMETERS = [[
|
|
|
|
'id' => [
|
|
|
|
'name' => 'Gist',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => true,
|
|
|
|
'title' => 'Insert Gist ID or URI',
|
2022-03-24 13:59:34 +03:00
|
|
|
'exampleValue' => '2646763'
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
2018-07-29 17:26:20 +03:00
|
|
|
]];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-29 17:26:20 +03:00
|
|
|
private $filename;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-29 17:26:20 +03:00
|
|
|
public function getURI()
|
|
|
|
{
|
|
|
|
$id = $this->getInput('id') ?: '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-29 17:26:20 +03:00
|
|
|
$urlpath = parse_url($id, PHP_URL_PATH);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-29 17:26:20 +03:00
|
|
|
if ($urlpath) {
|
|
|
|
$components = explode('/', $urlpath);
|
|
|
|
$id = end($components);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-29 17:26:20 +03:00
|
|
|
return static::URI . '/' . $id;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-29 17:26:20 +03:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->filename ? $this->filename . ' - ' . static::NAME : static::NAME;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-29 17:26:20 +03:00
|
|
|
public function collectData()
|
|
|
|
{
|
|
|
|
$html = getSimpleHTMLDOM(
|
|
|
|
$this->getURI(),
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
true,
|
2022-07-01 16:10:30 +03:00
|
|
|
true,
|
2018-07-29 17:26:20 +03:00
|
|
|
DEFAULT_TARGET_CHARSET,
|
|
|
|
false, // Do NOT remove line breaks
|
|
|
|
DEFAULT_BR_TEXT,
|
2022-01-02 12:36:09 +03:00
|
|
|
DEFAULT_SPAN_TEXT
|
|
|
|
);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-03 13:52:31 +03:00
|
|
|
$html = defaultLinkTo($html, $this->getURI());
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-10-29 23:32:31 +03:00
|
|
|
$fileinfo = $html->find('[class~="file-info"]', 0)
|
2018-07-29 17:26:20 +03:00
|
|
|
or returnServerError('Could not find file info!');
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-29 17:26:20 +03:00
|
|
|
$this->filename = $fileinfo->plaintext;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-10-29 23:32:31 +03:00
|
|
|
$comments = $html->find('div[class~="TimelineItem"]');
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-29 17:26:20 +03:00
|
|
|
if (is_null($comments)) { // no comments yet
|
|
|
|
return;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-29 17:26:20 +03:00
|
|
|
foreach ($comments as $comment) {
|
2018-11-03 13:52:31 +03:00
|
|
|
$uri = $comment->find('a[href*=#gistcomment]', 0)
|
2018-07-29 17:26:20 +03:00
|
|
|
or returnServerError('Could not find comment anchor!');
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-24 03:50:07 +03:00
|
|
|
$title = $comment->find('h3', 0);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-29 17:26:20 +03:00
|
|
|
$datetime = $comment->find('[datetime]', 0)
|
|
|
|
or returnServerError('Could not find comment datetime!');
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-29 17:26:20 +03:00
|
|
|
$author = $comment->find('a.author', 0)
|
|
|
|
or returnServerError('Could not find author name!');
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-10-29 23:32:31 +03:00
|
|
|
$message = $comment->find('[class~="comment-body"]', 0)
|
2018-07-29 17:26:20 +03:00
|
|
|
or returnServerError('Could not find comment body!');
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-29 17:26:20 +03:00
|
|
|
$item = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-03 13:52:31 +03:00
|
|
|
$item['uri'] = $uri->href;
|
2022-03-24 03:50:07 +03:00
|
|
|
$item['title'] = str_replace('commented', 'commented on', $title->plaintext ?? '');
|
2018-07-29 17:26:20 +03:00
|
|
|
$item['timestamp'] = strtotime($datetime->datetime);
|
|
|
|
$item['author'] = '<a href="' . $author->href . '">' . $author->plaintext . '</a>';
|
|
|
|
$item['content'] = $this->fixContent($message);
|
|
|
|
// $item['enclosures'] = array();
|
|
|
|
// $item['categories'] = array();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-07-29 17:26:20 +03:00
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-07-29 17:26:20 +03:00
|
|
|
/** Removes all unnecessary tags and adds formatting */
|
|
|
|
private function fixContent($content)
|
|
|
|
{
|
|
|
|
// Restore code (inside <pre />) highlighting
|
|
|
|
foreach ($content->find('pre') as $pre) {
|
|
|
|
$pre->style = <<<EOD
|
|
|
|
padding: 16px;
|
|
|
|
overflow: auto;
|
|
|
|
font-size: 85%;
|
|
|
|
line-height: 1.45;
|
|
|
|
background-color: #f6f8fa;
|
|
|
|
border-radius: 3px;
|
|
|
|
word-wrap: normal;
|
|
|
|
box-sizing: border-box;
|
|
|
|
margin-bottom: 16px;
|
|
|
|
EOD;
|
|
|
|
|
|
|
|
$code = $pre->find('code', 0);
|
|
|
|
|
|
|
|
if ($code) {
|
|
|
|
$code->style = <<<EOD
|
|
|
|
white-space: pre;
|
|
|
|
word-break: normal;
|
|
|
|
EOD;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
}
|
2018-07-29 17:26:20 +03:00
|
|
|
|
|
|
|
// find <code /> not inside <pre /> (`inline-code`)
|
|
|
|
foreach ($content->find('code') as $code) {
|
|
|
|
if ($code->parent()->tag === 'pre') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$code->style = <<<EOD
|
|
|
|
background-color: rgba(27,31,35,0.05);
|
|
|
|
padding: 0.2em 0.4em;
|
|
|
|
border-radius: 3px;
|
|
|
|
EOD;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2018-07-29 17:26:20 +03:00
|
|
|
|
|
|
|
// restore text spacing
|
|
|
|
foreach ($content->find('p') as $p) {
|
|
|
|
$p->style = 'margin-bottom: 16px;';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove unnecessary tags
|
|
|
|
$content = strip_tags(
|
|
|
|
$content->innertext,
|
|
|
|
'<p><a><img><ol><ul><li><table><tr><th><td><string><pre><code><br><hr><h>'
|
|
|
|
);
|
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
return $content;
|
2018-07-29 17:26:20 +03:00
|
|
|
}
|
|
|
|
}
|