Skip to content

Commit e092e38

Browse files
committed
improved github embedding. Now embed also geojson and 3d files
1 parent b79e6e6 commit e092e38

File tree

3 files changed

+69
-9
lines changed

3 files changed

+69
-9
lines changed

src/Adapters/Github.php

+26-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public static function check(Request $request)
1616
{
1717
return $request->match([
1818
'https://gist.github.com/*/*',
19+
'https://github.com/*',
1920
]);
2021
}
2122

@@ -24,24 +25,44 @@ public static function check(Request $request)
2425
*/
2526
public function run()
2627
{
27-
$this->addProvider('gist', new Api\Gist());
28+
if ($this->request->getHost() === 'gist.github.com') {
29+
$this->addProvider('gist', new Api\Gist());
30+
}
2831

2932
parent::run();
3033
}
3134

3235
/**
3336
* {@inheritdoc}
3437
*/
35-
public function getProviderName()
38+
public function getUrl()
3639
{
37-
return 'Gist';
40+
//Open graph returns as canonical url the main repo url, instead the file
41+
return $this->request->getUrl();
3842
}
3943

4044
/**
4145
* {@inheritdoc}
4246
*/
43-
public function getProviderUrl()
47+
public function getCode()
4448
{
45-
return 'https://gist.github.com';
49+
if ($this->request->match('https://github.com/*/*/blob/*')) {
50+
$username = $this->request->getDirectoryPosition(0);
51+
$repo = $this->request->getDirectoryPosition(1);
52+
$ref = $this->request->getDirectoryPosition(3);
53+
$path_to_file = implode('/', $this->request->getSlicePath(4));
54+
55+
switch ($this->request->getExtension()) {
56+
case 'geojson':
57+
//https://help.github.com/articles/mapping-geojson-files-on-github/#embedding-your-map-elsewhere
58+
return "<script src=\"https://embed.githubusercontent.com/view/geojson/{$username}/{$repo}/{$ref}/{$path_to_file}\"></script>";
59+
60+
case 'stl':
61+
//https://help.github.com/articles/3d-file-viewer/#embedding-your-model-elsewhere
62+
return "<script src=\"https://embed.githubusercontent.com/view/3d/{$username}/{$repo}/{$ref}/{$path_to_file}\"></script>";
63+
}
64+
}
65+
66+
return parent::getCode();
4667
}
4768
}

src/Url.php

+19
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,25 @@ public function getDirectories()
215215
return !empty($this->info['path']) ? '/'.implode('/', $this->info['path']).'/' : '/';
216216
}
217217

218+
/**
219+
* Slice path
220+
*
221+
* @param int $offset
222+
* @param int|null $length
223+
*
224+
* @return array
225+
*/
226+
public function getSlicePath($offset, $length = null)
227+
{
228+
$array = explode('/', $this->getPath());
229+
230+
if (empty($array[0])) {
231+
array_shift($array);
232+
}
233+
234+
return array_slice($array, $offset, $length);
235+
}
236+
218237
/**
219238
* Return the url path
220239
*

tests/GithubTest.php

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
<?php
22
class GithubTest extends PHPUnit_Framework_TestCase
33
{
4-
public function testOne()
4+
public function testGist()
55
{
66
$info = Embed\Embed::create('https://gist.github.com/oscarotero/7749998');
77

8-
$this->assertEquals($info->title, 'oscarotero/Git-cheatsheet.sh');
9-
$this->assertEquals($info->imageWidth, 140);
10-
$this->assertEquals($info->imageHeight, 140);
8+
$this->assertEquals($info->title, 'Git-cheatsheet.sh');
9+
$this->assertEquals($info->imageWidth, 400);
10+
$this->assertEquals($info->imageHeight, 400);
1111
$this->assertEquals($info->type, 'rich');
1212
$this->assertEquals($info->providerName, 'Gist');
1313
}
14+
15+
public function testGeojson()
16+
{
17+
$info = Embed\Embed::create('https://github.com/benbalter/dc-wifi-social/blob/master/bars.geojson');
18+
19+
$this->assertEquals($info->title, 'benbalter/dc-wifi-social');
20+
$this->assertEquals($info->code, '<script src="https://embed.githubusercontent.com/view/geojson/benbalter/dc-wifi-social/master/bars.geojson"></script>');
21+
$this->assertEquals($info->type, 'rich');
22+
$this->assertEquals($info->providerName, 'GitHub');
23+
}
24+
25+
public function test3d()
26+
{
27+
$info = Embed\Embed::create('https://github.com/skalnik/secret-bear-clip/blob/master/stl/clip.stl');
28+
29+
$this->assertEquals($info->title, 'skalnik/secret-bear-clip');
30+
$this->assertEquals($info->code, '<script src="https://embed.githubusercontent.com/view/3d/skalnik/secret-bear-clip/master/stl/clip.stl"></script>');
31+
$this->assertEquals($info->type, 'rich');
32+
$this->assertEquals($info->providerName, 'GitHub');
33+
}
1434
}

0 commit comments

Comments
 (0)