-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbkground.js
264 lines (227 loc) · 8.48 KB
/
bkground.js
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
hRecipeHelper, a Chrome extention for building microformat compatable.
Copyright (c) 2012 Lee Clarke
LICENSE:
This file is part of hRecipeHelper (http://github.com/leeclarke/hRecipeHelper).
hRecipeHelper is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
later version.
hRecipeHelper is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with TheGardenDroid. If not, see
<http://www.gnu.org/licenses/>.
*/
var BLOGGER = "www.blogger.com";
var BLOGGER_POST = "/post-edit.g";
var BLOGGER_CREATE = "/post-create.g";
/**
* //Cant use Chrome with the Blogger api with out getting an ugly popup... so odd, come on google really?
* Moving output to the popup to display for copy/paste
*/
function processData(data, options) {
var currentURL = "";
//debug
var toString = "rec-name:"+ data.$recName + " ingredients:"+ data.$ingredients + " yield:" + data.$yield;
console.log("got data: "+toString)
var hRecipeCode = convertTohRecipe(data, options);
console.log("hrecipe=="+hRecipeCode);
return hRecipeCode;
//chrome.tabs.executeScript(null, {code:"updateText('"+hRecipeCode+"')"})
console.log("done");
}
/* a few notes to be used for new features
chrome.tabs.getSelected(null, function(tab) {
var url = tab.url;
jQuery.url.setUrl(url)
console.log("HOST="+jQuery.url.attr("host"));
//var currField == document.activeElement;
//chrome.tabs.executeScript(null, {code:"updateText('TESTER')"})
//chrome.tabs.executeScript(null, {code:"console.log('active=='+document.activeElement)"})
});
*/
/**
* Converts data to the hRecipe format with default formatting tags.
*/
function convertTohRecipe(data, options) {
var microformat = "<div class=\"hrecipe\">\n";
if(data){
microformat += ("<h1 class=\"fn\">" + data.$recName + "</h1>\n");
//if(data.$summary) microformat += ("<><p class=\"summary\">" + data.$summary + "</p>\n");
if(data.$author) microformat += ("<p><label>By </label><span class=\"author\">" + data.$author + "</span></p>\n");
if(data.$published) microformat += ("<p>Published <span class=\"published\">" + data.$published + "</span></p>\n");
if(data.$photos.length>0 && data.$photos[0].length > 0) {
microformat += ("<img src=\"" + data.$photos[0] + "\" class=\"photo\" style=\"height:140px;width:140px;border-width:0px;\" alt=\"" + data.$recName + "\">\n");
}
if(data.$summary) microformat += ("<p class=\"summary\">" + data.$summary + "</p>\n");
microformat +="<h2>Ingredients</h2>\n";
if(options.blt_ingrediants == true) {
microformat +="<ul>\n";
for(var i in data.$ingredients) {
if(data.$ingredients[i].length >0) {
microformat += ("<li class=\"ingredient\">" + data.$ingredients[i] + "</li>\n");
}
}
microformat +="</ul>\n";
}else {
microformat +="<div>\n";
for(var i in data.$ingredients) {
if(data.$ingredients[i].length >0) {
microformat += ("<span class=\"ingredient\">" + data.$ingredients[i] + "</span><br>\n");
}
}
microformat +="</div>\n";
}
if(data.$instructions) {
microformat +="<h2>Instructions</h2>\n";
if(options.nbr_instr == true) {
var listType = (data.$instructions.length==1)?"ul":"ol";
microformat +="<" + listType + ">\n";
for(i in data.$instructions) {
if(data.$instructions[i].length >0) {
microformat += ("<li class=\"instructions\">" + data.$instructions[i] + "</li>\n");
}
}
microformat +="</" + listType + ">\n";
} else {
microformat +="<div>\n";
for(i in data.$instructions) {
if(data.$instructions[i].length >0) {
microformat += ("<div class=\"instructions\">" + data.$instructions[i] + "</div><br>\n");
}
}
microformat +="</div>\n";
}
}
if(data.$yield) microformat += ("<p><span style='font-weight:bold;'>Yield:</span><span class=\"yield\"> "+data.$yield+"</span></p>\n");
if(data.$preptime || data.$cooktime) {
//microformat += "<span class=\"duration\">\n";
var prep = "0M";
var cook = "0M";
if(data.$preptime) {
prep = parseTime(data.$preptime);
microformat += ("<span style='font-weight:bold;'>Prep Time:</span><span class=\"preptime\"><span class=\"value-title\" title=\""+prep+"\"></span> "+ formatReadableTime(prep)+"</span></p>\n");
}
if(data.$cooktime) {
cook = parseTime(data.$cooktime);
microformat += ("<p><span style='font-weight:bold;'>Cook time:</span><span class=\"cooktime\"><span class=\"value-title\" title=\""+cook+"\"></span> "+ formatReadableTime(cook)+"</span></p>\n");
}
data.$duration = formatArrayToISO8601(addFormattedTimes(prep, cook));
if(data.$duration) {
microformat += ("<p><span style='font-weight:bold;'>Total time:</span><span class=\"duration\"><span class=\"value-title\" title=\""+data.$duration+"\"></span> "+ formatReadableTime(data.$duration)+"</span></p>\n");
}
//microformat += "</span>\n";
}
if(options.blt_nutrition == true) {
if(data.$nutritions.length>0 && data.$nutritions[0].length > 0) {
microformat +="<h2>Nutrition</h2>\n";
microformat +="<p>\n";
microformat +="<ul class=\"nutrition\">";
for(var n in data.$nutritions) {
if(data.$nutritionTypes[n].length >0) {
microformat += "<li>"+toProperCase(data.$nutritionTypes[n])+": <span class=\""+ (data.$nutritionTypes[n].replace(" ","")).toLowerCase() + "\">" +(data.$nutritions[n] + "</span></li>\n");
}
}
microformat +="</ul>\n</p>\n";
}
} else {
if(data.$nutritions.length>0 && data.$nutritions[0].length > 0) {
microformat +="<h2>Nutrition</h2>\n";
microformat +="\n";
microformat +="<div class=\"nutrition\">";
for(var n in data.$nutritions) {
if(data.$nutritionTypes[n].length >0) {
microformat += "<div>"+toProperCase(data.$nutritionTypes[n])+": <span class=\""+ (data.$nutritionTypes[n].replace(" ","")).toLowerCase() + "\">" +(data.$nutritions[n] + "</span></div>\n");
}
}
microformat +="</div><br>\n\n";
}
}
if(data.$tags && data.$tags.length >0 && data.$tags[0].length > 0) {
var tagCode = [];
microformat +="<span style='font-weight:bold;'>Tags: </span>";
for(var t in data.$tags) {
tagCode.push("<span class=\"tag\">"+data.$tags[t]+"</span>");
}
microformat += tagCode.join(',');
microformat +="</span>";
}
}
microformat += "</div>";
return microformat;
}
/**
* Makes first letter of each word uppercase.
*/
function toProperCase(strIn) {
var results = []
if(strIn) {
var words = strIn.toLowerCase().split(" ");
for(w in words) {
var first = words[w].charAt(0).toUpperCase();
results.push(first + words[w].substr(1));
}
}
return results.join(" ");
}
/**
* Parse method expects to recieve time formatted in the hh:mm format and simply yanks out the : and adds markers
*/
function parseTime(timeValue) {
var result = "PT";
var timeSplit = timeValue.split(":");
if(timeSplit.length == 2) {
result += (timeSplit[0]+"H"+timeSplit[1]+"M");
} else {
result += (timeValue+"M");
}
return result;
}
/**
* Adds two time values formatted in the PT00H00M format
*/
function addFormattedTimes(time1, time2){
var hrs = 0;
var mins = 0;
var splitTime1 = time1.replace(/PT/g,'').split("H");
var splitTime2 = time2.replace(/PT/g,'').split("H");
if(time1){
var timeVal = parseFormattedTime(splitTime1);
hrs+= timeVal[0];
mins+= timeVal[1];
}
if(time2){
var timeVal = parseFormattedTime(splitTime2);
hrs+= timeVal[0];
mins+= timeVal[1];
}
return [hrs,mins];
}
/**
* Return an array of [hrs,mins]
*/
function parseFormattedTime(timeValue){
var hrs = 0;
var mins = 0;
if(timeValue.length >1){
hrs = parseInt(timeValue[0]);
mins = parseInt(timeValue[1].replace(/M/g,''));
} else {
mins= parseInt(timeValue[0].replace(/M/g,''));
}
return [hrs,mins];
}
function formatArrayToISO8601(hrMinArray){
var result = "PT";
if(hrMinArray[0] != 0){
result += hrMinArray[0] + "H";
}
result += hrMinArray[1] + "M";
return result;
}
/**
* Format iso8601 time to readable.
*/
function formatReadableTime(isoTime){
return isoTime.replace(/PT/g,'').replace(/H/g,' hrs. ').replace(/M/g,' mins. ')
}