Skip to content

Instantly share code, notes, and snippets.

@Munter
Last active December 14, 2015 20:59
Show Gist options
  • Save Munter/5147899 to your computer and use it in GitHub Desktop.
Save Munter/5147899 to your computer and use it in GitHub Desktop.
all: 2011.pdf 2012.pdf
%.pdf:
node report.js $* > $*.tex
pdflatex -interaction=nonstopmode -halt-on-error $*.tex
#!/usr/bin/env node
var year = Number(process.argv[2]);
var data = {
revenue: require('./data/revenue.json'),
expenses: require('./data/expenses.json'),
debt: require('./data/debt.json'),
assets: require('./data/assets.json'),
debt: require('./data/debt.json')
};
console.log('\\documentclass{article}');
console.log('\\title{' + year + ' balance sheet}\n');
console.log('\\begin{document}');
console.log('\\maketitle');
console.log('\\section*{revenue}\n');
renderData(data.revenue);
console.log('\\section*{expenses}\n');
renderData(data.expenses);
console.log('\\section*{assets}\n');
renderData(data.assets);
console.log('\\section*{liabilities}\n');
renderData(data.debt);
console.log('\\end{document}');
function renderTable (rows) {
console.log('\\begin{tabular}{ l r }');
Object.keys(rows).forEach(function (key) {
if (key === 'total') return;
console.log(key + ' & ' + rows[key] + ' \\\\');
});
console.log('{\\bf total}' + ' & {\\bf ' + rows.total + '}');
console.log('\\end{tabular}');
console.log('\\\\');
console.log('\\\\');
}
function renderData (rows) {
var rows_ = rows
.filter(function (r) { return r.year === year })
.reduce(function (totals, r) {
totals[r.source] = round((totals[r.source] || 0) + r.amount);
totals.total = round(totals.total + r.amount);
return totals;
}, { total: 0 })
;
renderTable(rows_);
}
function round (n) {
return Math.round(100 * n) / 100;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment