Skip to content

Commit

Permalink
Merge pull request #7 from paralect/add-partials
Browse files Browse the repository at this point in the history
[+] Add partials
  • Loading branch information
KuhArt authored Jan 18, 2018
2 parents a601eb1 + 94fae3c commit 6cf65ab
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 104 deletions.
30 changes: 10 additions & 20 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ module.exports = class PdfService {
const {
pdfOptions = {},
headers = {},
templateParams = {},
templateHelpers = {},
templateSystem = {},
} = params;

try {
Expand All @@ -37,8 +36,7 @@ module.exports = class PdfService {
pdfOptions,
headers,
type: 'pdf',
templateParams,
templateHelpers,
templateSystem,
serverUrl: this.serverUrl,
mode: this.mode,
});
Expand All @@ -55,17 +53,15 @@ module.exports = class PdfService {
const {
pdfOptions = {},
headers = {},
templateParams = {},
templateHelpers = {},
templateSystem = {},
} = params;

try {
return getStaticFileByContent({
pdfOptions,
headers,
type: 'pdf',
templateParams,
templateHelpers,
templateSystem,
content,
serverUrl: this.serverUrl,
});
Expand All @@ -81,8 +77,7 @@ module.exports = class PdfService {
const {
imgOptions = {},
headers = {},
templateParams = {},
templateHelpers = {},
templateSystem = {},
} = params;

try {
Expand All @@ -97,8 +92,7 @@ module.exports = class PdfService {
outPaths: { htmlPath, staticFilePath: `${staticFilePath}.${imgOptions.type || 'png'}` },
imgOptions,
headers,
templateParams,
templateHelpers,
templateSystem,
type: imgOptions.type || 'png',
serverUrl: this.serverUrl,
mode: this.mode,
Expand All @@ -116,16 +110,14 @@ module.exports = class PdfService {
const {
imgOptions = {},
headers = {},
templateParams = {},
templateHelpers = {},
templateSystem = {},
} = params;

try {
return getStaticFileByContent({
imgOptions,
headers,
templateParams,
templateHelpers,
templateSystem,
content,
type: imgOptions.type || 'png',
serverUrl: this.serverUrl,
Expand All @@ -140,8 +132,7 @@ module.exports = class PdfService {

async watch(pagePath, params) {
const {
templateParams = {},
templateHelpers = {},
templateSystem = {},
pdfOptions = {},
} = params;

Expand All @@ -150,8 +141,7 @@ module.exports = class PdfService {
const watchParams = {
paths,
pdfOptions,
templateParams,
templateHelpers,
templateSystem,
serverUrl: this.serverUrl,
mode: this.mode,
buildPdf: getStaticFileFromHtml,
Expand Down
33 changes: 20 additions & 13 deletions client/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,30 @@ const PassThrough = require('stream').PassThrough;
const logger = require('./logger');
const Handlebars = require('handlebars');

const compileHtml = (html, templateParams, templateHelpers) => {
Object.keys(templateHelpers).forEach((helperName) => {
Handlebars.registerHelper(helperName, templateHelpers[helperName](Handlebars));
const compileHtml = (html, templateSystem) => {
const {
params = {},
helpers = {},
partials = {},
} = templateSystem;

Object.keys(helpers).forEach((helperName) => {
Handlebars.registerHelper(helperName, helpers[helperName](Handlebars));
});

Object.keys(partials).forEach((partialName) => {
Handlebars.registerPartial(partialName, partials[partialName]);
});

const compiledHtml = Handlebars.compile(html);
return compiledHtml(templateParams);
return compiledHtml(params);
};

const readFile = async (filePath, templateParams, templateHelpers) => {
const readFile = async (filePath, templateSystem) => {
try {
const html = (await fs.readFile(filePath)).toString('binary');

return compileHtml(html, templateParams, templateHelpers);
return compileHtml(html, templateSystem);
} catch (err) {
logger.error('Something irreparable happened !!!\n', 'When read file');
throw err;
Expand Down Expand Up @@ -66,15 +76,14 @@ const getStaticFileFromHtml = async ({
outPaths,
pdfOptions,
headers,
templateHelpers,
templateParams,
templateSystem,
serverUrl,
type,
mode = 'development',
watch,
}) => {
const { htmlPath, staticFilePath } = outPaths;
const html = await readFile(htmlPath, templateParams, templateHelpers);
const html = await readFile(htmlPath, templateSystem);

if (watch) {
await fs.writeFile(htmlPath, html);
Expand All @@ -99,13 +108,11 @@ const getStaticFileByContent = async ({
content,
pdfOptions,
headers,
templateHelpers,
templateParams,
templateSystem,
serverUrl,
type,
}) => {
const html = compileHtml(content, templateParams, templateHelpers);
console.log(html);
const html = compileHtml(content, templateSystem);
const pdfStream = type === 'pdf'
? getPdf(html, pdfOptions, headers, serverUrl)
: getImg(html, pdfOptions, headers, serverUrl);
Expand Down
5 changes: 2 additions & 3 deletions client/lib/webpackTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const build = ({ paths }) => {
});
};

const watch = ({ paths, templateParams, templateHelpers, serverUrl, buildPdf, mode }) => {
const watch = ({ paths, templateSystem, serverUrl, buildPdf, mode }) => {
const config = getConfig({ paths });
const compiler = webpack(config);
return new Promise((resolve, reject) => {
Expand All @@ -88,9 +88,8 @@ const watch = ({ paths, templateParams, templateHelpers, serverUrl, buildPdf, mo

await buildPdf({
outPaths: { htmlPath, pdfPath },
templateParams,
templateSystem,
type: 'pdf',
templateHelpers,
serverUrl,
mode,
watch: true,
Expand Down
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@paralect/pdf-service-client",
"version": "0.2.5",
"version": "0.3.0",
"description": "Pdf service client for https://hub.docker.com/r/paralect/pdf-service/",
"main": "index.js",
"bin": {
Expand Down
6 changes: 4 additions & 2 deletions client/samples/bob/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ pdfService.generatePdf(`${__dirname}/src/index.html`, {
pdfOptions: {
format: 'Letter',
},
templateParams: {
tagline: 'Future is near!!!',
templateSystem: {
params: {
tagline: 'Future is near!!!',
},
},
});
52 changes: 38 additions & 14 deletions client/samples/harvestReport/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,46 @@ pdfService.generatePdf(`${__dirname}/src/index.html`, {
pdfOptions: {
format: 'Letter',
},
templateParams: {
tasks: [{ billableHours: 80, noneBillableHours: 0, totalAmount: 0, taskName: 'Programming', uninvoicedAmount: 0, items: [{ billableHours: 80, noneBillableHours: 0, totalAmount: 0, fullName: 'Andrew Orsich', avatar: 'https://cache.harvestapp.com/assets/profile_images/abraj_albait_towers.png?1498516481', totalHours: 80, percentFromMax: '100', billablePercent: '100' }], totalHours: 80, percentFromMax: '100', billablePercent: '100' }], projects: [{ billableHours: 80, noneBillableHours: 0, totalAmount: 0, projectName: 'Paralect Care', totalHours: 80, percentFromMax: '100', billablePercent: '100' }], detailsEntries: [{ totalHours: 80, fullName: 'Andrew Orsich', notes: [{ taskId: 8372101, spentOn: 'Monday, August 7th', projectId: 14838809, notes: null, hoursWorked: 8, projectName: 'Paralect Care', taskName: 'Programming' }, { taskId: 8372101, spentOn: 'Tuesday, August 8th', projectId: 14838809, notes: null, hoursWorked: 8, projectName: 'Paralect Care', taskName: 'Programming' }, { taskId: 8372101, spentOn: 'Wednesday, August 9th', projectId: 14838809, notes: null, hoursWorked: 8, projectName: 'Paralect Care', taskName: 'Programming' }, { taskId: 8372101, spentOn: 'Thursday, August 10th', projectId: 14838809, notes: null, hoursWorked: 8, projectName: 'Paralect Care', taskName: 'Programming' }, { taskId: 8372101, spentOn: 'Friday, August 11th', projectId: 14838809, notes: null, hoursWorked: 8, projectName: 'Paralect Care', taskName: 'Programming' }, { taskId: 8372101, spentOn: 'Monday, August 14th', projectId: 14838809, notes: null, hoursWorked: 8, projectName: 'Paralect Care', taskName: 'Programming' }, { taskId: 8372101, spentOn: 'Tuesday, August 15th', projectId: 14838809, notes: null, hoursWorked: 8, projectName: 'Paralect Care', taskName: 'Programming' }, { taskId: 8372101, spentOn: 'Wednesday, August 16th', projectId: 14838809, notes: null, hoursWorked: 8, projectName: 'Paralect Care', taskName: 'Programming' }, { taskId: 8372101, spentOn: 'Thursday, August 17th', projectId: 14838809, notes: 'Programming biiiiiig stuff', hoursWorked: 8, projectName: 'Paralect Care', taskName: 'Programming' }, { taskId: 8372101, spentOn: 'Friday, August 18th', projectId: 14838809, notes: null, hoursWorked: 8, projectName: 'Paralect Care', taskName: 'Programming' }] }], totalBillableHours: 80, totalUnbillableHours: 0, totalHours: 80, totalAmount: 0, totalUninvoicedAmount: 0, client: { id: 5928851, name: 'Paralect', active: true, currency: 'United States Dollar - USD', updated_at: '2017-08-25T15:48:41Z', created_at: '2017-08-25T15:43:35Z', statement_key: '0aef226a68331872cf3368f4f5e4aa5e', default_invoice_kind: 'project', default_invoice_timeframe: '20170801,20170831', address: null, delete_at: null, currency_symbol: '$', details: null, last_invoice_kind: 'project' }, moreThanOneProject: false, from: 'Aug 01', to: 'Aug 31, 2017', reportName: 'Paralect Team Monthly Time Report', rangeParagraph: 'August', rangeEmailString: 'I have prepared a one-month report for', emailTitle: 'Monthly Report', fileName: 'Paralect Team Monthly Time Report ((Aug 01 - Aug 31, 2017)).pdf', templateName: 'monthly_report',
},
templateHelpers: {
currency: Handlebars => (amount) => {
const htmlData = parseFloat(Handlebars.escapeExpression(amount))
.toFixed(2)
.replace(/./g, (c, i, a) => (i && c !== '.' && ((a.length - i) % 3 === 0) ? `,${c}` : c))
.concat(' USD');

return new Handlebars.SafeString(htmlData);
templateSystem: {
params: {
tasks: [{
billableHours: 80,
noneBillableHours: 0,
totalAmount: 0,
taskName: 'Programming',
uninvoicedAmount: 0,
items: [{
billableHours: 80,
noneBillableHours: 0,
totalAmount: 0,
fullName: 'Andrew Orsich',
avatar: 'https://cache.harvestapp.com/assets/profile_images/abraj_albait_towers.png?1498516481',
totalHours: 80,
percentFromMax: '100',
billablePercent: '100',
}],
totalHours: 80,
percentFromMax: '100',
billablePercent: '100',
}],
},
hours: Handlebars => (hours) => {
const htmlData = parseFloat(Handlebars.escapeExpression(hours)).toFixed(2);
helpers: {
currency: Handlebars => (amount) => {
const htmlData = parseFloat(Handlebars.escapeExpression(amount))
.toFixed(2)
.replace(/./g, (c, i, a) => (i && c !== '.' && ((a.length - i) % 3 === 0) ? `,${c}` : c))
.concat(' USD');

return new Handlebars.SafeString(htmlData);
return new Handlebars.SafeString(htmlData);
},
hours: Handlebars => (hours) => {
const htmlData = parseFloat(Handlebars.escapeExpression(hours)).toFixed(2);

return new Handlebars.SafeString(htmlData);
},
},
partials: {
hello: '<h1> Hello! </h1>',
},
},
});
53 changes: 28 additions & 25 deletions client/samples/invoiceReceipt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const PdfService = require('./../../index');

const pdfService = new PdfService({
serverUrl: 'http://localhost:4444',
mode: 'production',
mode: 'development',
});

pdfService.generatePdf(`${__dirname}/view/index.html`, {
Expand All @@ -15,31 +15,34 @@ pdfService.generatePdf(`${__dirname}/view/index.html`, {
bottom: '0mm',
},
},
templateParams: {
invoice: {
paidOn: '2017-08-22',
totalAmount: 100000,
creditCard: {
type: 'VISA',
last4: '4357',
},
activeUsers: 'Artem',
transaction: {
amount: 12000,
_id: 123,
},
appName: 'Pdf Service',
from: '2017-07-22',
to: '2017-08-22',
items: [
{
description: 'Spinners',
amount: 13000,
templateSystem: {
params: {
invoice: {
paidOn: '2017-08-22',
totalAmount: 100000,
creditCard: {
type: 'VISA',
last4: '4357',
},
{
description: 'Vape',
activeUsers: 'Artem',
transaction: {
amount: 12000,
_id: 123,
},
],
} },
appName: 'Pdf Service',
from: '2017-07-22',
to: '2017-08-22',
items: [
{
description: 'Spinners',
amount: 13000,
},
{
description: 'Vape',
amount: 12000,
},
],
},
},
},
});
51 changes: 27 additions & 24 deletions client/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,36 @@ const pdfParams = {
bottom: '0mm',
},
},
templateParams: {
invoice: {
paidOn: '2017-08-22',
totalAmount: 100000,
creditCard: {
type: 'VISA',
last4: '4357',
},
activeUsers: 'Artem',
transaction: {
amount: 12000,
_id: 123,
},
appName: 'Pdf Service',
from: '2017-07-22',
to: '2017-08-22',
items: [
{
description: 'Spinners',
amount: 13000,
templateSystem: {
params: {
invoice: {
paidOn: '2017-08-22',
totalAmount: 100000,
creditCard: {
type: 'VISA',
last4: '4357',
},
{
description: 'Vape',
activeUsers: 'Artem',
transaction: {
amount: 12000,
_id: 123,
},
],
} },
appName: 'Pdf Service',
from: '2017-07-22',
to: '2017-08-22',
items: [
{
description: 'Spinners',
amount: 13000,
},
{
description: 'Vape',
amount: 12000,
},
],
},
},
},
};

const testDirPath = `${__dirname}/test-waste`;
Expand Down
Loading

0 comments on commit 6cf65ab

Please sign in to comment.