Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2bb840b

Browse files
committed
Merge github.com:formula1/NodeOS-Blog
2 parents 6ec5a34 + 4f2f4d6 commit 2bb840b

File tree

13 files changed

+699
-240
lines changed

13 files changed

+699
-240
lines changed

‎dist/api.min.js

Lines changed: 11 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/article.html

Lines changed: 224 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css" />
66
<link rel="stylesheet" href="http://node-os.com/css/default.css" />
77
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js"></script>
8-
<script type="text/javascript" src="api.min.js" ></script>
8+
<script type="text/javascript" src="/api.min.js" ></script>
99
<style>
1010
#articles img{max-width:100%}
11+
#articles article .title img{width:100px}
1112
</style>
1213
</head>
1314
<body>
@@ -35,18 +36,17 @@
3536
<section ng-app="NodeOsBlog" id="articles" class="section">
3637
<div class="container" ng-controller="ErrorListCtrl" >
3738
<article ng-repeat="(index, error) in errors" class="row">
38-
<div class="col-sm-8 col-sm-offset-2 {{error.class}}" ng-mousedown="removeError(error)">
39+
<div class="col-sm-8 col-sm-offset-2 {{error.class}}" >
3940
<header class="alert alert-danger" role="alert">
4041
<h3 class="title">{{error.name}}</h3>
42+
<p ng-mousedown="removeError(error)" > To remove this message, click here</p>
4143
</header>
42-
<div class="content">{{error.message}}</div>
44+
<div class="content"ng-bind-html="error.message | to_trusted"></div>
4345
<footer>
4446
Don't worry, it probably was not your fault... probably....
4547
<br/><br/>
4648
Even if it was,
4749
<a target="_blank" href="https://github.com/formula1/NodeOS-Blog/issues" >just submit an issue :)</a>
48-
<br/><br/>
49-
To remove this message, click it.
5050
</footer>
5151
</div>
5252
</article>
@@ -134,20 +134,22 @@ <h3 class="footer-title">Share</h3>
134134
};
135135
}]);
136136
var errors = [];
137-
varis_authed=false;
137+
138138
function add403(){
139139
var topush = {
140140
class:"four-zero-three",
141141
name:"You've hit max data"
142142
};
143-
if(is_authed){
144-
topush.message = "Apparently, people have been using our app too much...";
143+
if(is_authed === 1){
144+
topush.message = "Apparently, people have been using our app too much..."+
145+
"<button onclick='login()'>Log in</button>";
145146
}else{
146147
topush.message = "If you'd like to continue, please "+
147-
"<button onclick='hello(\"github\").login()'>Log in</button>";
148+
"<button onclick='login()'>Log in</button>";
148149
}
149150
errors.push(topush);
150151
}
152+
151153
NodeOsBlog.controller('ErrorListCtrl', function($scope){
152154
$scope.removeError = function(error){
153155
var l = errors.length;
@@ -163,89 +165,239 @@ <h3 class="footer-title">Share</h3>
163165
}
164166
};
165167
});
166-
var hello = require("hello");
167-
hello.on("auth.login", function(auth){
168-
hello(auth.network).api("/me").then(function(r){
169-
document.querySelector(".four-zero-three .content").innerHTML("You've authenticated!");
168+
/* */
169+
function parseMarkdown(item,next){
170+
uriAsAuthority("https://api.github.com/markdown/raw",function(uri){
171+
jQuery.ajax({
172+
url:uri,
173+
type:'POST',
174+
headers: {
175+
'Content-Type': 'text/plain'
176+
},
177+
data:item.body
178+
}).done(function(data){
179+
item.bodyHTML = data;
180+
}).fail(function(response, type, title, config) {
181+
if(response.status === 403){
182+
add403();
183+
}else{
184+
errors.push({name:"Bad markdown call: "+response.status, message: data.message});
185+
}
186+
item.bodyHTML = "<pre>"+item.body+"</pre>";
187+
}).always(function(){
188+
next(item);
189+
});
170190
});
171-
});
191+
}
192+
193+
/*
194+
var markdown = require("markdown").markdown;
195+
function parseMarkdown(item,next){
196+
console.log("parsing");
197+
198+
try{
199+
var t = jQuery("<div>"+markdown.toHTML(item.body)+"</div>");
200+
t.find("code").wrap("<pre></pre>");
201+
202+
item.bodyHTML = t.html();
203+
console.log(item.bodyHTML);
204+
}catch(e){
205+
console.log(e);
206+
item.bodyHTML = "<pre>"+item.body+"</pre>";
207+
}
208+
setTimeout(next.bind(next,item),1);
209+
}
210+
/* */
211+
function CookieStore(id){
212+
this.id = id;
213+
var cook = document.cookie.split(";");
214+
var current;
215+
var i;
216+
for(i=0;i<cook.length;i++){
217+
if(cook[i].indexOf("=") != -1){
218+
current = cook[i].split("=");
219+
if(current[0].replace(/s/, "") == id){
220+
this.cookie = JSON.parse(decodeURIComponent(current[1]));
221+
return;
222+
}
223+
}
224+
}
225+
this.cookie = {};
226+
this.save();
227+
}
228+
229+
CookieStore.prototype.get = function(key){
230+
return this.cookie[key];
231+
};
232+
CookieStore.prototype.set = function(key, value){
233+
this.cookie[key] = value;
234+
this.save();
235+
return this;
236+
};
237+
CookieStore.prototype.delete = function(key){
238+
delete this.cookie[key];
239+
this.save();
240+
return this;
241+
};
242+
243+
CookieStore.prototype.save = function(){
244+
document.cookie = this.id+"="+encodeURIComponent(JSON.stringify(this.cookie))+";";
245+
};
246+
247+
//==========Auth Start===================
248+
249+
250+
var auth = new CookieStore("auth");
251+
var is_authed = -1;
252+
var auth_queue = [];
253+
var url = require("url");
254+
var querystring = require("querystring");
255+
var docuri = url.parse(window.location.href);
256+
docuri.query = querystring.parse(docuri.query);
257+
var config = {
258+
cid: "dafb27cb88db35267e75",
259+
yqluri: "store://gdDAnJkTXAuVgzAQ8wboA2"
260+
};
261+
if(docuri.query && docuri.query.code){
262+
if(docuri.query.state != auth.get("state")){
263+
errors.push(new Error("Improper State"));
264+
}else{
265+
getAccess(docuri.query.code);
266+
}
267+
}else if(auth.get("access_token")){
268+
console.log(auth.get("access_token"));
269+
is_authed = 1;
270+
}
271+
272+
function login(){
273+
var state = Date.now()+"_"+Math.random();
274+
auth.set("state", state);
275+
window.location.href = "https://github.com/login/oauth/authorize" +
276+
"?client_id="+config.cid +
277+
"&state="+state;
278+
}
279+
function getAccess(code){
280+
is_authed = 0;
281+
auth.delete("state");
282+
jQuery.get(compileYQL([
283+
"env \""+config.yqluri+"\"",
284+
"select * from github where CODE=\""+code+"\""
285+
])).done(function(results){
286+
console.log("auth success");
287+
console.log(arguments);
288+
auth.set("access_token",results.query.results.OAuth.access_token);
289+
is_authed = 1;
290+
jQuery(".four-zero-three .content").text("You've authenticated!");
291+
}).fail(function(e){
292+
console.error(arguments);
293+
is_authed = -1;
294+
errors.push({
295+
class:"four-zero-three",
296+
name:"You've failed authorization",
297+
message: "You can always try again"
298+
});
299+
}).always(function(){
300+
console.log("always");
301+
while(auth_queue.length){
302+
uriAsAuthority.apply(void(0),auth_queue.pop());
303+
}
304+
});
305+
}
306+
307+
function compileYQL(query){
308+
if(Array.isArray(query)){
309+
query = query.join(";");
310+
}
311+
if(typeof query != "string"){
312+
throw new Error("A yql query must be a string");
313+
}
314+
return "https://query.yahooapis.com/v1/public/yql?q=" +
315+
encodeURIComponent(query) +
316+
"&diagnostics=true&format=json";
317+
}
318+
319+
function uriAsAuthority(uri,next){
320+
if(is_authed === -1) return next(uri);
321+
if(is_authed === 0) return auth_queue.push([uri,next]);
322+
uri = url.parse(uri);
323+
uri.query = querystring.parse(uri.query);
324+
uri.query.access_token = auth.get("access_token");
325+
uri.search = "?"+querystring.stringify(uri.query);
326+
next(url.format(uri));
327+
}
328+
329+
function logout(){
330+
auth.delete("access_token");
331+
is_authed = -1;
332+
}
172333
var hash = document.location.toString().split("#")[1];
173334
NodeOsBlog.controller('BlogSingleCtrl', function ($scope, $http) {
174335
$scope.uriPath = "/NodeOS/NodeOS/issues/"+hash;
175336
$scope.blog = [];
337+
$scope.uriAsAuthority = uriAsAuthority;
338+
$scope.parseMarkdown = parseMarkdown;
176339

177340
var markdown = require("markdown").markdown;
178-
$scope.parseMarkdown = function(item,next){
179-
try{
180-
item.bodyHTML = markdown.toHTML(item.body);
181-
}catch(e){
182-
item.bodyHTML = "<pre>"+item.body+"</pre>";
183-
}
184-
next(item);
185-
};
186-
$http.get('https://api.github.com/repos'+$scope.uriPath)
187-
.success(function(data,status,headers) {
188-
var l = data.labels.length;
189-
while(l--){
190-
if(data.labels[l].name === "blog"){
191-
break;
341+
$scope.uriAsAuthority('https://api.github.com/repos'+$scope.uriPath,function(uri){
342+
$http.get(uri).success(function(data,status,headers) {
343+
var l = data.labels.length;
344+
while(l--){
345+
if(data.labels[l].name === "blog"){
346+
break;
347+
}
192348
}
193-
}
194-
if(l < 0){
195-
return errors.push({
196-
name:"Trying to load a non-blog?",
197-
message: "I technically can't stop you since this is clientside."+
198-
" Hopefully my code feels clean enough to hack"
349+
if(l < 0){
350+
return errors.push({
351+
name:"Trying to load a non-blog?",
352+
message: "I technically can't stop you since this is clientside."+
353+
" Hopefully my code feels clean enough to hack"
354+
});
355+
}
356+
$scope.parseMarkdown(data,function(item){
357+
$scope.blog.push(item);
358+
$scope.$apply();
199359
});
200-
}
201-
$scope.parseMarkdown(data,function(item){
202-
$scope.blog.push(item);
360+
}).error(function(data, status, headers, config) {
361+
if(status === 403){
362+
add403();
363+
}else{
364+
errors.push({name:"Bad issues list request: "+status, message: data.message});
365+
}
203366
});
204-
}).error(function(data, status, headers, config) {
205-
if(status === 403){
206-
add403();
207-
}else{
208-
errors.push({name:"Bad issues list request: "+status, message: data.message});
209-
}
210367
});
211368
});
212369
NodeOsBlog.controller('CommentListCtrl', function ($scope, $http) {
213370
$scope.uriPath = "/NodeOS/NodeOS/issues/"+hash+"/comments";
214371
$scope.blog = [];
215-
$scope.parseMarkdown = function(item,next){
216-
$http.post("https://api.github.com/markdown",{text:item.body})
217-
.success(function(data){
218-
item.bodyHTML = data;
219-
next(item);
220-
}).error(function(data, status, headers, config) {
221-
errors.push({name:"Bad markdown call: "+status, message: data.message});
222-
item.bodyHTML = "<pre>"+item.body+"</pre>";
223-
next(item);
224-
});
225-
};
226372
$scope.last = void(0);
373+
$scope.uriAsAuthority = uriAsAuthority;
374+
$scope.parseMarkdown = parseMarkdown;
227375
$scope.loadMore = function(page){
228376
if($scope.last && $scope.last < page) return;
229377
var i=0;
230378
var l=-1;
231-
$http.get('https://api.github.com/repos'+$scope.uriPath+'?labels=blog&sort=created&page='+page)
232-
.success(function(data,status,headers) {
233-
if(!$scope.last) $scope.last = headers.link?headers.link.split("=").pop():1;
234-
l = data.length;
235-
if(i === l) return; //No more
236-
var iterator = function(item){
237-
$scope.blog.push(item);
238-
i++;
239-
if(i === l) return;
240-
$scope.parseMarkdown(data[i],iterator);
241-
};
242-
$scope.parseMarkdown(data[0],iterator);
243-
}).error(function(data, status, headers, config) {
244-
if(status === 403){
245-
add403();
246-
}else{
247-
errors.push({name:"Bad issues list request: "+status, message: data.message});
248-
}
379+
$scope.uriAsAuthority(
380+
'https://api.github.com/repos'+$scope.uriPath+'?labels=blog&sort=created&page='+page,
381+
function(uri){
382+
$http.get(uri).success(function(data,status,headers) {
383+
if(!$scope.last) $scope.last = headers.link?headers.link.split("=").pop():1;
384+
l = data.length;
385+
if(i === l) return; //No more
386+
var iterator = function(item){
387+
$scope.blog.push(item);
388+
$scope.$apply();
389+
i++;
390+
if(i === l) return;
391+
$scope.parseMarkdown(data[i],iterator);
392+
};
393+
$scope.parseMarkdown(data[0],iterator);
394+
}).error(function(data, status, headers, config) {
395+
if(status === 403){
396+
add403();
397+
}else{
398+
errors.push({name:"Bad issues list request: "+status, message: data.message});
399+
}
400+
});
249401
});
250402
};
251403
$scope.loadMore(1);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /