Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.8 KiB

4 years ago
  1. #!/usr/bin/env node
  2. var program = require('commander')
  3. , read = require('node-readability')
  4. , he = require('he')
  5. , pkg = require('../package.json')
  6. , clc = require('cli-color');
  7. program
  8. .version(pkg.version)
  9. .option('-c, --color', 'use colors in terminal')
  10. .option('-f, --format [format]', 'set the format for the result (text or html)', '')
  11. .parse(process.argv);
  12. var styles = {};
  13. styles.h1 = styles.h2 = styles.muted = function(s) { return s; };
  14. if(program.color) {
  15. styles.h1 = clc.underline.bold;
  16. styles.h2 = clc.bold;
  17. styles.muted = clc.white;
  18. console.log(clc.reset);
  19. }
  20. var error = function(msg) {
  21. if(msg) console.log(clc.red("Error: ")+msg);
  22. console.log();
  23. usage();
  24. process.exit(0);
  25. }
  26. var usage = function() {
  27. console.log("Usage: readability [url]");
  28. console.log();
  29. console.log("E.g.: readability http://techcrunch.com/2014/02/12/marc-andreessen-tech-is-still-recovering-from-a-depression");
  30. console.log();
  31. }
  32. if(process.argv.length < 2) return error();
  33. var url = process.argv[2];
  34. if(typeof url!='string' || !url.match(/^https?:\/\//)) return error(url+" is an invalid url");
  35. console.log(url);
  36. read(url, function(err, article, meta) {
  37. // The title of the page.
  38. console.log();
  39. console.log(styles.h1(article.title));
  40. console.log();
  41. // The main body of the page.
  42. // console.log(article.content);
  43. var content = article.content;
  44. if(program.format == 'html') return console.log(content);
  45. content = content.replace(/<h[2-9]>([^<])+<\/h[2-9]>/gi, function(match, header) {
  46. return styles.h2(match);
  47. });
  48. content = content.replace(/ {2,*}|\t/g,' ');
  49. var content = content.replace(/<\/?[^>]+(>|$)/g, "");
  50. content = he.decode(content);
  51. console.log(content);
  52. // The raw HTML code of the page
  53. //console.log(article.html);
  54. // The document object of the page
  55. //console.log(article.document);
  56. });