forked from ghandic/nbconvert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbconvert.php
134 lines (106 loc) · 3.48 KB
/
nbconvert.php
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
<?php
/*
Plugin Name: NbConvert
Description: A plugin to add ipynb files to a blog post or page using nbviewer
Version: 1.0
Author: Andrew Challis
Author URI: http://www.andrewchallis.com
License: MIT
*/
function nbconvert_handler($atts) {
//run function that actually does the work of the plugin
$nb_output = nbconvert_function($atts);
//send back text to replace shortcode in post
return $nb_output;
}
function nbconvert_get_most_recent_git_change_for_file_from_api($url) {
$url_list = explode('/', $url);
$owner = $url_list[3];
$repo = $url_list[4];
$branch = $url_list[6];
$path = implode("/", array_slice($url_list, 7));
$request_url = 'https://api.github.com/repos/'.$owner.'/'.$repo.'/commits/'.$branch.'?path='. $path.'&page=1';
$context_params = array(
'http' => array(
'method' => 'GET',
'user_agent' => 'Bogus user agent',
'timeout' => 1
)
);
$res = file_get_contents($request_url, FALSE, stream_context_create($context_params));
$datetime = json_decode($res, true)['commit']['committer']['date'];
$max_datetime = strtotime($datetime);
$max_datetime_f = date('d/m/Y H:i:s', $max_datetime);
return $max_datetime_f;
}
/*
function get_most_recent_git_change_for_file($url) {
$url_list = explode('/', $url);
$url_list[5] = 'blame';
$new_url = implode("/", $url_list);
//Load the HTML page
$html = file_get_contents($new_url);
//Create a new DOM document
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($html);
// Get all time-ago tags
$time_agos = $dom->getElementsByTagName('time-ago');
$mostRecent= 0;
foreach($time_agos as $time_ago){
$datetime = $time_ago->getAttribute('datetime');
$curDate = strtotime($datetime);
if ($curDate > $mostRecent) {
$mostRecent = $curDate;
}
}
$max_date = date('d/m/Y H:i:s', $mostRecent);
return $max_date;
}
*/
function nbconvert_function($atts) {
//process plugin
extract(shortcode_atts(array(
'url' => "",
), $atts));
$clean_url = preg_replace('#^https?://#', '', rtrim($url,'/'));
$html = file_get_contents("https://nbviewer.jupyter.org/url/" . $clean_url);
$nb_output = nbconvert_getHTMLByID('notebook-container', $html);
$last_update_date_time = nbconvert_get_most_recent_git_change_for_file_from_api($url);
$converted_nb = '<div class="notebook">
<div class="nbconvert-labels">
<label class="github-link">
<a href="'.$url.'" target="_blank">Check it out on github</a>
<label class="github-last-update"> Last updated: '.$last_update_date_time.'</label>
</label>
</div>
<div class="nbconvert">'.$nb_output.'
</div>
</div>';
//send back text to calling function
return $converted_nb;
}
function nbconvert_innerHTML(DOMNode $elm) {
$innerHTML = '';
$children = $elm->childNodes;
foreach($children as $child) {
$innerHTML .= $elm->ownerDocument->saveHTML($child);
}
return $innerHTML;
}
function nbconvert_getHTMLByID($id, $html) {
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($html);
$node = $dom->getElementById($id);
if ($node) {
$inner_output = nbconvert_innerHTML($node);
return $inner_output;
}
return FALSE;
}
function nbconvert_enqueue_style() {
wp_enqueue_style( 'NbConvert', plugins_url( '/css/nbconvert.css', __FILE__ ));
}
add_action( 'wp_enqueue_scripts', 'nbconvert_enqueue_style' );
add_shortcode("nbconvert", "nbconvert_handler");