Skip to content

Commit

Permalink
Adds a programmatic node api (#41)
Browse files Browse the repository at this point in the history
* Minor refactors to make function easier to move
* Moves cli specific code to cli.js
* Moves pdf, epub, html functions to index.js
* Adds cli.js as bin in package.json
* Fixes code of conduct change
  • Loading branch information
phenax authored and danburzo committed Oct 15, 2018
1 parent 3506b37 commit 7cc203f
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 76 deletions.
20 changes: 10 additions & 10 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ In the interest of fostering an open and welcoming environment, we as contributo

Examples of behavior that contributes to creating a positive environment include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
- Using welcoming and inclusive language
- Being respectful of differing viewpoints and experiences
- Gracefully accepting constructive criticism
- Focusing on what is best for the community
- Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a professional setting
- The use of sexualized language or imagery and unwelcome sexual attention or advances
- Trolling, insulting/derogatory comments, and personal or political attacks
- Public or private harassment
- Publishing others' private information, such as a physical or electronic address, without explicit permission
- Other conduct which could reasonably be considered inappropriate in a professional setting

## Our Responsibilities

Expand Down
41 changes: 41 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env node
const program = require('commander');
const pkg = require('./package.json');

const { configure, pdf, epub, html } = require('.');

/*
Some setup
----------
*/
configure();

/*
Command-Line Interface definition
---------------------------------
*/

function with_common_options(cmd) {
return cmd
.option('-o, --output [output]', 'Path for the generated bundle')
.option('--template [template]', 'Path to custom HTML template')
.option('--style [stylesheet]', 'Path to custom CSS')
.option('--css [style]', 'Additional CSS style');
}

program.version(pkg.version);

with_common_options(program.command('pdf [urls...]'))
.option('--no-sandbox', 'Passed to Puppeteer')
.description('Bundle web pages as a PDF file')
.action(pdf);

with_common_options(program.command('epub [urls...]'))
.description('Bundle web pages as an EPUB file')
.action(epub);

with_common_options(program.command('html [urls...]'))
.description('Bundle web pages as a HTML file')
.action(html);

program.parse(process.argv);
95 changes: 30 additions & 65 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node
const program = require('commander');
const pup = require('puppeteer');
const got = require('got');
const { JSDOM } = require('jsdom');
Expand All @@ -8,24 +7,34 @@ const tmp = require('tmp');
const fs = require('fs');
const css = require('css');
const slugify = require('slugify');
const readability = require('./vendor/readability');

const pkg = require('./package.json');
const Readability = require('./vendor/readability');

const { imagesAtFullSize, wikipediaSpecific } = require('./src/enhancements');
const get_style_attribute_value = require('./src/get-style-attribute-value');

function resolve(path) {
return require.resolve(path, {
const resolve = path =>
require.resolve(path, {
paths: [process.cwd(), __dirname]
});

const enhancePage = function(dom) {
imagesAtFullSize(dom.window.document);
wikipediaSpecific(dom.window.document);
};

function createDom({ url, content }) {
const dom = new JSDOM(content);
dom.reconfigure({ url });
return dom;
}

/*
Some setup
----------
*/
nunjucks.configure({ autoescape: false, noCache: true });
function configure() {
nunjucks.configure({ autoescape: false, noCache: true });
}

/*
Fetch a web page and clean the HTML
Expand All @@ -36,35 +45,26 @@ async function cleanup(url) {
const content = (await got(url)).body;

console.log('Enhancing web page');
const dom = new JSDOM(content);
dom.reconfigure({ url: url });
const dom = createDom({ url, content });

/*
Run enhancements
----------------
*/
imagesAtFullSize(dom.window.document);
wikipediaSpecific(dom.window.document);
enhancePage(dom);

// Run through readability and return
let parsed = new readability(dom.window.document).parse();

return {
...parsed,
const parsed = new Readability(dom.window.document).parse();

// Add in some stuff
url: url
};
return { ...parsed, url };
}

/*
Bundle the HTML files into a PDF
--------------------------------
*/
async function bundle(items, options) {
var temp_file = tmp.tmpNameSync({
postfix: '.html'
});
const temp_file = tmp.tmpNameSync({ postfix: '.html' });

console.log(`Generating temporary HTML file:\nfile://${temp_file}`);

Expand All @@ -77,21 +77,19 @@ async function bundle(items, options) {
'utf8'
),
{
items: items,
style: style,

// deprecated
stylesheet: stylesheet
items,
style,
stylesheet // deprecated
}
);

const doc = new JSDOM(html).window.document;
let headerTemplate = doc.querySelector('.header-template');
let footerTemplate = doc.querySelector('.footer-template');
let header = new JSDOM(
const headerTemplate = doc.querySelector('.header-template');
const footerTemplate = doc.querySelector('.footer-template');
const header = new JSDOM(
headerTemplate ? headerTemplate.innerHTML : '<span></span>'
).window.document;
let footer = new JSDOM(
const footer = new JSDOM(
footerTemplate ? footerTemplate.innerHTML : '<span></span>'
).window.document;

Expand Down Expand Up @@ -165,41 +163,6 @@ async function bundle(items, options) {
await browser.close();
}

/*
Command-Line Interface definition
---------------------------------
*/

function with_common_options(cmd) {
return cmd
.option('-o, --output [output]', 'Path for the generated bundle')
.option('--template [template]', 'Path to custom HTML template')
.option('--style [stylesheet]', 'Path to custom CSS')
.option('--css [style]', 'Additional CSS style');
}

program.version(pkg.version);

with_common_options(program.command('pdf [urls...]'))
.option('--no-sandbox', 'Passed to Puppeteer')
.description('Bundle web pages as a PDF file')
.action(pdf);

with_common_options(program.command('epub [urls...]'))
.description('Bundle web pages as an EPUB file')
.action(epub);

with_common_options(program.command('html [urls...]'))
.description('Bundle web pages as a HTML file')
.action(html);

program.parse(process.argv);

/*
CLI commands
------------
*/

/*
Generate PDF
*/
Expand All @@ -224,3 +187,5 @@ async function epub(urls, options) {
async function html(urls, options) {
console.log('TODO', urls, options);
}

module.exports = { configure, pdf, epub, html };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"tmp": "^0.0.33"
},
"bin": {
"percollate": "./index.js"
"percollate": "./cli.js"
},
"engines": {
"node": ">= 8.0.0"
Expand Down

0 comments on commit 7cc203f

Please sign in to comment.