gulpfile.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. var gulp = require("gulp"),
  3. gulpLoadPlugins = require("gulp-load-plugins"),
  4. del = require("del"),
  5. fs = require("fs"),
  6. path = require("path"),
  7. through = require("through2"),
  8. roaster = require("roaster"),
  9. pdf = require("html-pdf");
  10. var $ = gulpLoadPlugins(),
  11. opts = {
  12. isDebug: Boolean($.util.env.debug),
  13. md: {},
  14. pdf: {
  15. type: "pdf",
  16. width: "8.5in",
  17. height: "11in",
  18. border: "0in", // handled via CSS
  19. },
  20. },
  21. errLogger = function(err) {
  22. $.util.log($.util.colors.red("Error: "), String(opts.isDebug ? err.stack : err));
  23. throw err;
  24. },
  25. md2html = function(opts) {
  26. if (!opts) opts = {};
  27. var tmplPath = opts.template ? fs.realpathSync(opts.template) : path.join(__dirname, "/src/template/index.html"),
  28. tmpl = fs.readFileSync(tmplPath, "utf8");
  29. return through.obj(function(file, encoding, done) {
  30. var md = file.contents.toString();
  31. file.path = file.path.replace(/\.md$/, ".html");
  32. roaster(md, opts, function(err, contents) {
  33. if (err) return done(err);
  34. var html = tmpl.replace("{{content}}", contents);
  35. file.contents = new Buffer(html);
  36. done(null, file);
  37. });
  38. });
  39. },
  40. html2pdf = function(opts) {
  41. if (!opts) opts = {};
  42. return through.obj(function(file, encoding, done) {
  43. var baseUri = "file://" + path.dirname(file.path) + "/",
  44. html = file.contents.toString()
  45. .replace("<head>", "<head>\n<base href=" + JSON.stringify(baseUri) + ">\n");
  46. file.path = file.path.replace(/\.html$/, ".pdf");
  47. pdf.create(html, opts)
  48. .toBuffer(function(err, buf) {
  49. if (err) return done(err);
  50. file.contents = buf;
  51. done(null, file);
  52. });
  53. });
  54. };
  55. gulp
  56. .task("default", ["build"], function() {})
  57. .task("deploy", ["build"], function() {
  58. return gulp
  59. .src("dist/**/*")
  60. .pipe($.plumber(errLogger))
  61. .pipe($.if(opts.isDebug, $.debug()))
  62. .pipe($.ghPages({
  63. push: !$.util.env.nopush,
  64. }));
  65. })
  66. .task("clean", function() {
  67. del.sync(["dist/**/*"]);
  68. })
  69. .task("watch", ["build"], function() {
  70. gulp.watch([
  71. "src/template/**/*",
  72. "src/resume.md",
  73. ], ["build"]);
  74. })
  75. .task("build", ["clean", "build:pdf"], function() {})
  76. .task("build:pdf", ["build:html"], function() {
  77. return gulp
  78. .src("dist/index.html")
  79. .pipe($.plumber(errLogger))
  80. .pipe(html2pdf(opts.pdf))
  81. .pipe($.rename("resume.pdf"))
  82. .pipe(gulp.dest("dist"))
  83. .pipe($.if(opts.isDebug, $.debug()));
  84. })
  85. .task("build:html", function() {
  86. return gulp
  87. .src([
  88. "src/template/**/*",
  89. "src/resume.md",
  90. ])
  91. .pipe($.plumber(errLogger))
  92. .pipe($.if("**/resume.md", md2html(opts.md)))
  93. .pipe($.if("**/resume.html", $.rename("index.html")))
  94. .pipe(gulp.dest("dist"))
  95. .pipe($.if(opts.isDebug, $.debug()));
  96. })
  97. ;