This repository has been archived by the owner on Sep 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathsrc.js
234 lines (218 loc) · 7.21 KB
/
src.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
const config = {
divider: '\n---',
};
const reducers = [
[
function avatar (u) {
return u.avatar ? ' ![avatar][]' : '';
},
function fullName (u) {
return u.fullName ? ` ${u.fullName}` : '';
},
function heading (u, [avatar, fullName]) {
return (avatar || fullName) ? `\n#${avatar}${fullName}\n${config.divider}` : '';
}
],
[
function title (u) {
return u.title ? `\n> #### ${u.title}` : '';
},
[
function location (u) {
return u.location ? ` ${u.location}` : '';
},
function homepage (u) {
return u.homepage ? `   [${utils.prettyUrl(u.homepage)}][homepage]` : '';
},
function twitter (u) {
return u.twitter ? `   [@${u.twitter.replace(/^@/, '')}][twitter] ![twit][]` : '';
},
function shortDetails (u, [location, homepage, twitter]) {
return (location || homepage || twitter) ? `\n> ##### ${location}${homepage}${twitter}` : '';
}
],
function tech (u) {
var tech = utils.techList(u.tech);
return tech ? `\n> ${tech}` : '';
},
function introBox (u, [title, shortDetails, tech]) {
return (title || shortDetails || tech) ? `\n${title}${shortDetails}${tech}\n${config.divider}` : '';
}
],
function about (u) {
if (!u.about) return '';
var about = utils.blockQuote(u.about, '**"**');
return `\n${about}\n\n${config.divider}`;
},
function experience (u) {
var experienceList = utils.mapExpOrEdToMD(u.experience);
return experienceList ? `\n## Experience${experienceList}` : '';
},
function education (u) {
var educationList = utils.mapExpOrEdToMD(u.education);
return educationList ? `\n## Education${educationList}` : '';
},
function oss (u) {
var ossList = u.projects && u.projects.map(function (p) {
var url = utils.formatUrl(p.url);
var title = p.title ? (url ? ` [${p.title}](${url})` : ` ${p.title}`) : '';
var info = {};
if (!p.desc || !popularity) {
info = utils.getProjectInfo(p);
}
var popularity = p.popularity || info.popularity;
popularity = popularity ? `   *<small>${popularity}</small>*` : '';
var heading = (title || popularity) ? `####${title}${popularity}` : '';
var desc = p.desc || info.desc;
desc = desc ? `\n${desc}\n${config.divider}` : '';
return (heading || desc) ? `\n${heading}${desc}` : '';
}).join('');
return ossList ? `\n## Open Source${ossList}` : '';
},
[
function writingHabits (u) {
return u.writingHabits ? `\n> ${u.writingHabits}` : '';
},
function writingsList (u) {
return (u.writing && Array.isArray(u.writing)) ? u.writing.map(function (w) {
var url = utils.formatUrl(w.url);
var title = w.title ? (w.url ? ` [${w.title}](${url}) *<small>@${utils.prettyUrl(url)}</small>*` : ` ${w.title}`) : '';
return title ? `\n####${title}` : '';
}).join('') : '';
},
function writings (u, [writingHabits, writingsList]) {
return (writingHabits || writingsList) ? `\n## Writing\n${writingHabits}${writingsList}\n${config.divider}` : '';
}
],
function favorites (u) {
var favHeading = '## Favorites';
var editor = u.editor ? `\n#### Editor\n${u.editor}` : '';
var os = u.os ? `\n#### Operating System\n${u.os}` : '';
var term = u.terminal ? `\n#### Terminal\n${u.terminal}` : '';
return (editor || os || term) ? `\n${favHeading}${editor}${os}${term}\n${config.divider}` : '';
},
function args (u) {
var avatarArg = u.avatar ? `[avatar]: ${utils.formatUrl(u.avatar)}` : '';
var twitIco = '\n[twit]: http://cdn-careers.sstatic.net/careers/Img/icon-twitter.png?v=b1bd58ad2034';
var homepageArg = u.homepage ? `\n[homepage]: ${utils.formatUrl(u.homepage)}` : '';
var twitterArg = u.twitter ? `\n[twitter]: https://twitter.com/${u.twitter.replace(/^@/, '')}${twitIco}` : '';
return `\n${avatarArg}${homepageArg}${twitterArg}`;
},
function finish (u, everythingTillNow) {
return everythingTillNow.join('');
}
];
function renderer (profile, reducers) {
const stack = reducers.reduce(function (total, reducer) {
let val;
if (Array.isArray(reducer)) {
val = renderer(profile, reducer);
} else if (typeof reducer !== 'function') {
throw new Error('invalid reducer type');
} else {
val = reducer(profile, total);
}
total.push(val);
return total;
}, []);
return stack[stack.length - 1];
}
var utils = {
mapExpOrEdToMD (list) {
if (!list || !list.map) return;
return list.map(e => { // .n
if (!e) return '';
var title = e.title ? ` ${e.title}` : ''; // .n.1.1
var timeframe = (e.since || e.till) ? `   <small>*${utils.timeframe(e.since, e.till)}*</small>` : ''; // .n.1.2
var heading = (title || timeframe) ? `####${title}${timeframe}` : ''; // .n.1
var techList = utils.techList(e.tech);
techList = techList ? `\n${techList}` : ''; // .n.2
var about = utils.blockQuote(e.about);
about = about ? `\n${about}` : ''; // .n.3
return (heading || techList || about) ? `\n${heading}${techList}${about}\n${config.divider}` : ''; // .n
}).join('');
},
formatUrl(url) {
// TODO: make this a bit more intelligent?
if (!url) return '';
if (!url.startsWith('http://') && !url.startsWith('https://') && !url.startsWith('//')) {
return `http://${url}`;
}
return url;
},
blockQuote(p, firstQuote) {
return p ? p.split('\n').map(function (el, i) {
if (i === 0 && firstQuote) {
return `> ${firstQuote} ${el}`;
}
return `> ${el}`;
}).join('\n') : '';
},
prettyUrl(url) {
// TODO: remove all trailing part and keep only domain
return url ? url.replace(/^https?:\/\//, '') : '';
},
techList(p) {
return p ? p.split(/,\s+/g).map(el => `\`${el}\``).join(', ') : '';
},
getProjectInfo(wow) {
return {
desc: '',
popularity: ''
};
},
timeframe(since, till) {
if (since || till) {
if (since && !till) {
return `${since} - current`;
}
if (!since) {
return 'current';
}
return `${since} - ${till}`;
}
return '';
},
downloadFromData(data, type, filename) {
var blob = new Blob([data], {type});
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.download = filename;
a.href = url;
a.classList.add('hidden');
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
setTimeout(() => URL.revokeObjectURL(url), 0);
}
};
way.watch('inputData', function (val) {
var md = renderer(val, reducers);
document.getElementById('previewer').textContent = md;
document.getElementById('live-preview').innerHTML = marked(md);
});
document.getElementById('btnDownload').addEventListener('click', downloadRaw);
document.getElementById('btnExport').addEventListener('click', downloadJSON);
function downloadJSON() {
utils.downloadFromData(JSON.stringify(way.get('inputData'), null, ' '), 'application/json', 'cv.json');
}
function downloadRaw () {
utils.downloadFromData(renderer(way.get('inputData'), reducers), 'text/plain', 'cv.md');
}
document.getElementById('btnMd').addEventListener('click', function () {
switchTab('md');
});
document.getElementById('btnLive').addEventListener('click', function () {
switchTab('live');
});
function switchTab (to) {
var prev = document.getElementById('previewer');
var live = document.getElementById('live-preview');
if (to === 'md') {
prev.classList.remove('hidden');
live.classList.add('hidden');
} else {
live.classList.remove('hidden');
prev.classList.add('hidden');
}
}