summaryrefslogtreecommitdiff
path: root/bridges/GitHubGistBridge.php
blob: 98e40800b386d00280e0022972820047ef7d796d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?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;

	const PARAMETERS = array(array(
		'id' => 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, $this->getURI());

		$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'] = $uri->href;
			$item['title'] = str_replace('commented', 'commented on', $title->plaintext);
			$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();

			$this->items[] = $item;

		}

	}

	/** 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;

			}

		}

		// 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;

		}

		// 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>'
		);

		return $content;

	}

}