diff --git a/index.js b/index.js index 2fbe8e3..028cffb 100755 --- a/index.js +++ b/index.js @@ -79,11 +79,14 @@ async function cleanup(item, blueprint) { spinner.start('Enhancing web page'); const dom = createDom({ url: item.url, content }); + if (!item.amp) { + item._url = item.url; + } const amp = dom.window.document.querySelector('link[rel=amphtml]'); if (amp && blueprint.options.amp) { spinner.succeed('Found AMP version'); return cleanup( - Object.assign({}, item, { url: amp.href }), + Object.assign({}, item, { url: amp.href, amp: amp.href }), blueprint ); } @@ -144,6 +147,24 @@ async function bundle(blueprint) { style += fs.readFileSync(resolve(blueprint.toc.css), 'utf8'); } + if (blueprint.document.groups && blueprint.document.groups.length > 0) { + blueprint.document.useGroups = true; + blueprint.toc.template = './templates/default_toc_w_groups.html'; + let itemIndex = {}; + blueprint.document.items.forEach(function(item) { + itemIndex[item._url] = item; + }); + blueprint.document.groups = blueprint.document.groups.map(function( + group + ) { + group.items = group.items.map(function(item) { + return itemIndex[item.url]; + }); + return group; + }); + } + console.log(blueprint); + const html = nunjucks.renderString( fs.readFileSync(resolve(blueprint.document.template), 'utf8'), { diff --git a/samples/blueprint-full.json b/samples/blueprint-full.json new file mode 100644 index 0000000..5528489 --- /dev/null +++ b/samples/blueprint-full.json @@ -0,0 +1,52 @@ +{ + "cover": { + "generate": true, + "template": "./templates/default_cover.html", + "css": "./templates/default_cover.css", + "title": null, + "picture": null, + "header": "Percollate", + "footer": "Generated on 2018-10-30" + }, + "toc": { + "generate": true, + "template": "./templates/default_toc.html", + "css": "./templates/default_toc.css" + }, + "document": { + "template": "./templates/default.html", + "css": "./templates/default.css", + "items": [ + { + "url": "https://www.engadget.com/2018/10/28/fallout-76-not-coming-to-switch/" + }, + { + "url": "https://techcrunch.com/2018/10/28/silicon-valleys-sovereign-wealth-problem/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+Techcrunch+%28TechCrunch%29" + } + ], + "groups": [ + { + "label": "Group1", + "items": [ + { + "url": "https://www.engadget.com/2018/10/28/fallout-76-not-coming-to-switch/" + } + ] + }, + { + "label": "Group2", + "items": [ + { + "url": "https://techcrunch.com/2018/10/28/silicon-valleys-sovereign-wealth-problem/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+Techcrunch+%28TechCrunch%29" + } + ] + } + ] + }, + "options": { + "output": "percollate.pdf", + "individual": false, + "amp": true, + "sandbox": true + } +} diff --git a/samples/blueprint.json b/samples/blueprint.json new file mode 100644 index 0000000..385a933 --- /dev/null +++ b/samples/blueprint.json @@ -0,0 +1,38 @@ +{ + "cover": { "generate": true }, + "toc": { "generate": true }, + "document": { + "items": [ + { + "url": "https://www.engadget.com/2018/10/28/fallout-76-not-coming-to-switch/" + }, + { + "url": "https://techcrunch.com/2018/10/28/silicon-valleys-sovereign-wealth-problem/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+Techcrunch+%28TechCrunch%29" + } + ], + "groups": [ + { + "label": "Group1", + "items": [ + { + "url": "https://www.engadget.com/2018/10/28/fallout-76-not-coming-to-switch/" + } + ] + }, + { + "label": "Group2", + "items": [ + { + "url": "https://techcrunch.com/2018/10/28/silicon-valleys-sovereign-wealth-problem/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+Techcrunch+%28TechCrunch%29" + } + ] + } + ] + }, + "options": { + "output": "percollate.pdf", + "individual": false, + "amp": true, + "sandbox": true + } +} diff --git a/src/blueprints.js b/src/blueprints.js index 439d225..4b647b4 100644 --- a/src/blueprints.js +++ b/src/blueprints.js @@ -12,7 +12,9 @@ function defaultBlueprint() { function fromCommandLineOptions(urls, options) { let blueprint = null; if (options.blueprint) { - blueprint = JSON.parse(fs.readFileSync(options.blueprint, 'utf8')); + blueprint = _buildBluePrint( + JSON.parse(fs.readFileSync(options.blueprint, 'utf8')) + ); } else { blueprint = _parseCommandLineOptions(options, defaultBlueprint()); } @@ -24,6 +26,23 @@ function fromCommandLineOptions(urls, options) { return blueprint; } +function _buildBluePrint(userBlueprint) { + let blueprint = Object.assign({}, defaultBlueprint()); + blueprint.cover = Object.assign({}, blueprint.cover, userBlueprint.cover); + blueprint.toc = Object.assign({}, blueprint.toc, userBlueprint.toc); + blueprint.document = Object.assign( + {}, + blueprint.document, + userBlueprint.document + ); + blueprint.options = Object.assign( + {}, + blueprint.options, + userBlueprint.options + ); + return blueprint; +} + function _parseCommandLineOptions(options, blueprint) { if (options.cover) { blueprint.cover['generate'] = options.cover; @@ -79,7 +98,8 @@ function _defaultDocument() { template: './templates/default.html', css: './templates/default.css', assets: {}, - items: [] + items: [], + groups: [] }; } diff --git a/src/enhancements.js b/src/enhancements.js index 21da1ca..2d02195 100644 --- a/src/enhancements.js +++ b/src/enhancements.js @@ -15,8 +15,8 @@ function ampToHtml(doc) { function extractImage(doc) { const selectors = [ - "meta[name='og:image']", "meta[name='twitter:image']", + "meta[name='og:image']", 'amp-img', 'img' ]; diff --git a/templates/default.css b/templates/default.css index d7bff78..943074c 100644 --- a/templates/default.css +++ b/templates/default.css @@ -93,9 +93,6 @@ pre code { article { font-size: 1em; -} - -article:not(:last-of-type) { page-break-after: always; } diff --git a/templates/default.html b/templates/default.html index e202b16..45036e3 100644 --- a/templates/default.html +++ b/templates/default.html @@ -11,35 +11,22 @@ {% if blueprint.cover.generate %} - {% include blueprint.cover.template %} + {% include blueprint.cover.template %} {% endif %} {% if blueprint.toc.generate %} - {% include blueprint.toc.template %} + {% include blueprint.toc.template %} {% endif %} - {% for item in items %} -
-
-

- {{ item.title }} -

- {% if item.byline %} - - {% endif %} -

- Source: - {{item.url}} -

-
- -
- {{ item.content }} -
-
- {% endfor %} + {% if blueprint.document.useGroups %} + {% for group in blueprint.document.groups %} + {% include "./templates/default_group.html" %} + {% endfor %} + {% else %} + {% for item in items %} + {% include "./templates/default_item.html" %} + {% endfor %} + {% endif %}