Snippets
Created by
Runsun Pan
last modified
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>bitbucket_aids demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="http://runsun.info/prog/js/proj/jxlog/jxlog.js"></script>
<script src="http://runsun.info/prog/js/proj/jxx/jxx.js"></script>
<script src="bitbucket_aids.js"></script>
<!-- https://highlightjs.org/usage/ -->
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/styles/androidstudio.min.css">
<script src="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<style>
div { margin: 10px; border:1px gray solid; padding: 10px}
</style>
<script>
var log = jxlog
var url_load_Bitbucket_files = 'https://bitbucket.org/!api/2.0/snippets/runsun/bMKzz/9c6667de03f50ecb6adf589c073aa54e80dbb5ae/files/load_Bitbucket_files.js'
/*
get_Bitbucket_repo_last_commit_id(
acc_name,
repo_name,
cmtid_handler
*/
$.get(url_load_Bitbucket_files
,(data)=>{
log.b('To run get_Bitbucket_repo_last_commit_id(...):')
eval(data); // activate load_Bitbucket_files.js
get_Bitbucket_repo_last_commit_id( //<== defined in load_Bitbucket_files.js
acc_name= 'birkenfeld',
repo_name= 'pygments-main',
cmtid_handler= (cmtid)=>{
document.getElementById('div_pygments_cmtid').innerHTML+=
"<code>"+cmtid+"</code>"
}
)
log.e()
}
)
/*
function get_Bitbucket_file_urls(
acc_name,
repo_name,
folder_name,
urls_handler
)
*/
$.get( url_load_Bitbucket_files
,(data)=>{
log.b('to run load_Bitbucket_files(...):')
eval(data); // activate load_Bitbucket_files.js
get_Bitbucket_file_urls( //<== defined in load_Bitbucket_files.js
acc_name= 'birkenfeld',
repo_name= 'pygments-main',
folder_name= 'scripts',
urls_handler= (urls)=>{
document.getElementById('div_pygments/scripts').innerHTML+=
urls.join('<br/>')
}
)
log.e()
}
)
function display_file(fileInfo)
{
log.b()
$('#div_pygments_loadfiles')[0].innerHTML +=
` <details class="fileContent">
<summary><b>${fileInfo.path}</b></summary>
<pre><code class="python hljs">${fileInfo.data}</code></pre>
</details>
`
log.e()
}
/*
function load_Bitbucket_files(
acc_name,
repo_name,
folder_name,
fileInfo_handler=null,
onload= null
)
*/
$.get( url_load_Bitbucket_files
,(data)=>{
log.b('To run get_Bitbucket_file_urls(...):')
eval(data); // activate load_Bitbucket_files.js
load_Bitbucket_files( //<== defined in load_Bitbucket_files.js
acc_name= 'birkenfeld',
repo_name= 'pygments-main',
folder_name= 'scripts',
fileInfo_handler= display_file,
onload= rawfiles=>{
document.body.innerHTML +=
`
==> Loaded total of ${rawfiles.length} files !!
`
}
)
log.e()
}
)
</script>
<body>
<h1>Demo of <a href="https://bitbucket.org/!api/2.0/snippets/runsun/bMKzz/9c6667de03f50ecb6adf589c073aa54e80dbb5ae/files/load_Bitbucket_files.js">load_Bitbucket_files.js</a></h1>
In this demo we load birkenfeld's <a href="https://bitbucket.org/birkenfeld/pygments-main">pygments-main</a> files.
<div id='div_pygments_cmtid'>
Bitbucket repo <code><b><a href="https://bitbucket.org/birkenfeld/pygments-main">pygments-main</a></b></code>'s last (the newest) commit id</code>:
<pre><code class="javascript codebox">
get_Bitbucket_repo_last_commit_id( //<== defined in load_Bitbucket_files.js
acc_name= 'birkenfeld',
repo_name= 'pygments-main',
cmtid_handler= (cmtid)=>{
document.getElementById('div_pygments_cmtid').innerHTML+=
"<code>${cmtid}</code>"
}
)
</code></pre>
Result:
<br/><br/>
</div>
<div id='div_pygments/scripts'>
Bitbucket repo <code><b>pygments-main</b></code>'s file urls in <code>/src/scripts</code>:
<pre><code class="javascript codebox">
get_Bitbucket_file_urls( //<== defined in load_Bitbucket_files.js
acc_name= 'birkenfeld',
repo_name= 'pygments-main',
folder_name= 'scripts',
urls_handler= (urls)=>{
document.getElementById('div_pygments/scripts').innerHTML+=
urls.join('<br/>')
}
)
</code></pre>
Results:
<br/><br/>
</div>
<div id='div_pygments_loadfiles'>
Load Bitbucket repo <code><b>pygments-main</b></code>'s files in <code>/src/scripts</code>:
<pre><code class="javascript codebox">
function display_file(fileInfo)
{
$('#div_pygments_loadfiles')[0].innerHTML +=
` <details class="fileContent">
<summary><b>${fileInfo.path}</b></summary>
<div style="background:#eee;border:1px black inset;margin:5px"><code><pre>${fileInfo.data}</pre></code></div>
</details>
`
}
load_Bitbucket_files( //<== defined in load_Bitbucket_files.js
acc_name= 'birkenfeld',
repo_name= 'pygments-main',
folder_name= 'scripts',
fileInfo_handler= display_file,
onload= rawfiles=>
document.body.innerHTML +=
` ==> Loaded total of ${rawfiles.length} files !!
`
)
</code></pre>
Results:
<br/><br/>
</div>
</body>
</html>
|
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | /*
This snippet provides three Bitbucket-related functions:
get_Bitbucket_repo_last_commit_id( acc_name, repo_name, cmtid_handler )
-- Get the last commit id from a poblic Bitbucket repo
-- Need a cmtid_handler( cmtid ) to handle it
get_Bitbucket_file_urls( acc_name, repo_name, folder_name, urls_handler )
-- Get urls of files from a poblic Bitbucket repo
-- Need a urls_handler( urls ) to handle it
load_Bitbucket_files( acc_name, repo_name, folder_name, onload, fileInfo_handler=null )
-- Load files from a folder of public Bitbucket repo
-- For the repo root folder, set folder_name = ''
-- onload( rawfiles ):
called after ALL files are loaded.
rawfiles is an array of loaded raw files
-- fileInfo_handler( fileInfo ) :
called after EACH file is loaded.
fileInfo is an object:
{
"node": "59f0df03c063",
"path": "scores/Amazing_Grace.json",
"data": " (file content in string) ",
"size": 416
}
The function log.b(), log(), log.e() are from https://bitbucket.org/runsun/jxlog
with log = jxlog
demo: https://jsfiddle.net/runsun/kv5vo6g2/2/
*/
function get_Bitbucket_repo_last_commit_id(
acc_name,
repo_name,
cmtid_handler
){
//
// Get the last commit id from a poblic Bitbucket repo
// Need a cmtid_handler( cmtid ) to handle it
//
let url = 'https://api.bitbucket.org/2.0/repositories/'
+ acc_name + '/' + repo_name +'/commits'
$.get(
url,
(commits)=>
{
let cmtid = commits.values[0].hash
//log('last commit uuid= ', cmtid)
if(cmtid_handler) cmtid_handler(cmtid)
}
)
}
function get_Bitbucket_file_urls(
acc_name,
repo_name,
folder_name,
urls_handler
){
//
// Get urls of files from a poblic Bitbucket repo
// Need a urls_handler( urls ) to handle it
//
get_Bitbucket_repo_last_commit_id(
acc_name,
repo_name,
cmtid_handler
)
//--------------------------------------
function cmtid_handler(cmtid){
//log.b()
let srcUrl='https://api.bitbucket.org/1.0/repositories/'
+ acc_name + '/'
+ repo_name + '/src/'
+ cmtid +'/'
let folderUrl = srcUrl+ folder_name+ (folder_name?'/':'')
//log('srcUrl = ', srcUrl)
//log('folderUrl = ', folderUrl)
$.get( folderUrl, folderInfo_handler )
function folderInfo_handler( folderInfo ){
/*
A folderInfo is a string containing:
{ ...
"files": [{
"size": 416,
"path": "scores/Amazing_Grace.json",
"timestamp": "2016-09-03T23:13:07Z",
"utctimestamp": "2016-09-03 23:13:07+00:00",
"revision": "fdf715cc14f8"
}, {
"size": 449,
"path": "scores/Come_Home_Sooner.json",
"timestamp": "2016-09-03T23:13:07Z",
"utctimestamp": "2016-09-03 23:13:07+00:00",
"revision": "fdf715cc14f8"
}, { ...]
eval( folderInfo ).files ==> array of fileInfo
*/
//log.b('get_Bitbucket_file_urls/cmtid_handler/folderInfo_handler(folderInfo)')
//log('folderInfo = ', folderInfo)
folderInfo = eval(folderInfo)
//log('folderInfo = ', folderInfo)
let fileUrls=[], fileInfos = folderInfo.files
//fileUrls = fileInfos.map( (x)=> x.path )
for(var i=0;i<fileInfos.length;i++){
fileUrls.push( srcUrl+ fileInfos[i].path )
}
//fileUrls = Array(folderInfo.files).map( (o)=> folderUrl+ o.path )
//log('fileUrls = ', JSON.stringify(fileUrls) )
if(urls_handler) urls_handler(fileUrls)
//log.e()
}//_folderInfo_handler
//log.e()
}//_cmtid_handler
}
function load_Bitbucket_files(
acc_name,
repo_name,
folder_name,
fileInfo_handler=null,
onload= null
){
/* --------------------------------------------------
Load files from a folder of public Bitbucket repo
For the repo root folder, set folder_name = ''
Two types of file handlers:
- onload( rawfiles ):
called after ALL files are loaded.
rawfiles is an array of loaded raw files
- fileInfo_handler( fileInfo ) :
called after EACH file is loaded.
fileInfo is an object:
{
"node": "59f0df03c063",
"path": "scores/Amazing_Grace.json",
"data": " (file content in string) ",
"size": 416
}
---------------------------------------------------- */
//log.b()
let rawfiles=[]
//log('fileInfo_handler = ', fileInfo_handler)
get_Bitbucket_file_urls(
acc_name,
repo_name,
folder_name,
url_handler
)
//--------------------------------------
function url_handler( urls ){
//log.b()
//log('urls: ', JSON.stringify( urls ) )
//log('len(scoreUrls) = ', scoreUrls.length)
//log('load_OcaNotation_scores handler= ', handler)
$.get(
urls.shift(), //[0],
(fileInfo)=>{
/* a string:
{
"node": "59f0df03c063",
"path": "scores/Amazing_Grace.json",
"data": "score= {
\"title\": \"Amazing Grace\"\n
, \"label\": \"\"\n
, \"author\": \"\"\n
, \"score\": \n\n`Amazing Grace\n\n(a\nv5 | 1 - 3....}",
"size": 416
}
*/
//log.b('url_handler get fileInfo_handler')
rawfiles.push( fileInfo.data )
//log('rawfiles length= ', rawfiles.length)
//log('rawfiles stringed: ', JSON.stringify(rawfiles))
//log('fileInfo_handler= ', ''+fileInfo_handler)
if(fileInfo_handler) fileInfo_handler( eval(fileInfo) );
//log('Checking urls.length = ', urls.length)
if(urls.length) {
url_handler( urls )
}
else if(onload) {
//log.b('\n\nAll files loaded, calling load_Bitbucket_files onload')
onload(rawfiles)
//log.e()
}
//log.e()
}
)
//log.e()
}//_url_handler
//log.e()
}//_load_Bitbucket_files
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.