GitHubGistBridge.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. class GitHubGistBridge extends BridgeAbstract {
  3. const NAME = 'GitHubGist comment bridge';
  4. const URI = 'https://gist.github.com';
  5. const DESCRIPTION = 'Generates feeds for Gist comments';
  6. const MAINTAINER = 'logmanoriginal';
  7. const CACHE_TIMEOUT = 3600;
  8. const PARAMETERS = array(array(
  9. 'id' => array(
  10. 'name' => 'Gist',
  11. 'type' => 'text',
  12. 'required' => true,
  13. 'title' => 'Insert Gist ID or URI',
  14. 'exampleValue' => '2646763, https://gist.github.com/2646763'
  15. )
  16. ));
  17. private $filename;
  18. public function getURI() {
  19. $id = $this->getInput('id') ?: '';
  20. $urlpath = parse_url($id, PHP_URL_PATH);
  21. if($urlpath) {
  22. $components = explode('/', $urlpath);
  23. $id = end($components);
  24. }
  25. return static::URI . '/' . $id;
  26. }
  27. public function getName() {
  28. return $this->filename ? $this->filename . ' - ' . static::NAME : static::NAME;
  29. }
  30. public function collectData() {
  31. $html = getSimpleHTMLDOM($this->getURI(),
  32. null,
  33. null,
  34. true,
  35. true,
  36. DEFAULT_TARGET_CHARSET,
  37. false, // Do NOT remove line breaks
  38. DEFAULT_BR_TEXT,
  39. DEFAULT_SPAN_TEXT)
  40. or returnServerError('Could not request ' . $this->getURI());
  41. $html = defaultLinkTo($html, static::URI);
  42. $fileinfo = $html->find('[class="file-info"]', 0)
  43. or returnServerError('Could not find file info!');
  44. $this->filename = $fileinfo->plaintext;
  45. $comments = $html->find('div[class="timeline-comment-wrapper"]');
  46. if(is_null($comments)) { // no comments yet
  47. return;
  48. }
  49. foreach($comments as $comment) {
  50. $uri = $comment->find('a[href^=#gistcomment]', 0)
  51. or returnServerError('Could not find comment anchor!');
  52. $title = $comment->find('div[class="unminimized-comment"] h3[class="timeline-comment-header-text"]', 0)
  53. or returnServerError('Could not find comment header text!');
  54. $datetime = $comment->find('[datetime]', 0)
  55. or returnServerError('Could not find comment datetime!');
  56. $author = $comment->find('a.author', 0)
  57. or returnServerError('Could not find author name!');
  58. $message = $comment->find('[class="comment-body"]', 0)
  59. or returnServerError('Could not find comment body!');
  60. $item = array();
  61. $item['uri'] = $this->getURI() . $uri->href;
  62. $item['title'] = str_replace('commented', 'commented on', $title->plaintext);
  63. $item['timestamp'] = strtotime($datetime->datetime);
  64. $item['author'] = '<a href="' . $author->href . '">' . $author->plaintext . '</a>';
  65. $item['content'] = $this->fixContent($message);
  66. // $item['enclosures'] = array();
  67. // $item['categories'] = array();
  68. $this->items[] = $item;
  69. }
  70. }
  71. /** Removes all unnecessary tags and adds formatting */
  72. private function fixContent($content){
  73. // Restore code (inside <pre />) highlighting
  74. foreach($content->find('pre') as $pre) {
  75. $pre->style = <<<EOD
  76. padding: 16px;
  77. overflow: auto;
  78. font-size: 85%;
  79. line-height: 1.45;
  80. background-color: #f6f8fa;
  81. border-radius: 3px;
  82. word-wrap: normal;
  83. box-sizing: border-box;
  84. margin-bottom: 16px;
  85. EOD;
  86. $code = $pre->find('code', 0);
  87. if($code) {
  88. $code->style = <<<EOD
  89. white-space: pre;
  90. word-break: normal;
  91. EOD;
  92. }
  93. }
  94. // find <code /> not inside <pre /> (`inline-code`)
  95. foreach($content->find('code') as $code) {
  96. if($code->parent()->tag === 'pre') {
  97. continue;
  98. }
  99. $code->style = <<<EOD
  100. background-color: rgba(27,31,35,0.05);
  101. padding: 0.2em 0.4em;
  102. border-radius: 3px;
  103. EOD;
  104. }
  105. // restore text spacing
  106. foreach($content->find('p') as $p) {
  107. $p->style = 'margin-bottom: 16px;';
  108. }
  109. // Remove unnecessary tags
  110. $content = strip_tags(
  111. $content->innertext,
  112. '<p><a><img><ol><ul><li><table><tr><th><td><string><pre><code><br><hr><h>'
  113. );
  114. return $content;
  115. }
  116. }