-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-plus-json2html
executable file
·45 lines (35 loc) · 1.21 KB
/
google-plus-json2html
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
#!/usr/bin/env node
'use strict';
let fs = require('fs')
let program = require('commander')
let Mustache = require('mustache')
let concat = require('concat-stream')
let cli = require('./lib/cli')
let template = function(name) {
return fs.readFileSync(__dirname + '/lib/templates/' + name).toString()
}
String.prototype.spaces = function() {
return this.replace(/\s+/g, ' ')
}
let render = function(input, opt) {
let json = JSON.parse(input)
let body = Mustache.render(template('body.html'), json.object)
if (!opt.standalone) return body
let date = new Date(json.published)
let view = {
title: `${json.actor.displayName} :: ${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()} :: ${json.title.spaces()}`,
author: json.actor.displayName,
body
}
return Mustache.render(template('standalone.html'), view)
}
program
.usage('[options] < file.json')
.version(cli.meta.version)
.option('-s, --standalone', 'output w/ an appropriate header & footer ')
.parse(process.argv)
process.stdin.pipe(concat( input => {
input = input.toString()
if (!input.trim().length) cli.errx('invalid input')
process.stdout.write(render(input, {standalone: program.standalone}))
}))