<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Alex Tomkins - javascript</title>
    <link>https://www.alextomkins.com/tag/javascript/</link>
    <atom:link href="https://www.alextomkins.com/tag/javascript/feed.xml" rel="self" type="application/rss+xml" />
    <description>Posts tagged with javascript</description>
    <language>en</language>
    
    
    
    
    
    
    
    
    
    
    
    
    <item>
      <title>Improving a Pelican blog with gulp</title>
      <link>https://www.alextomkins.com/2016/08/improving-pelican-blog-with-gulp/</link>
      <guid>https://www.alextomkins.com/2016/08/improving-pelican-blog-with-gulp/</guid>
      <pubDate>Sat, 13 Aug 2016 22:49:00 +0000</pubDate>
      <description><![CDATA[<p><a href="http://blog.getpelican.com/">Pelican</a> is a Python powered static site generator, which is useful
for blogs or other small static sites. Although a respectable number of
<a href="https://github.com/getpelican/pelican-plugins">Pelican plugins</a> are available - the
Node.js/JavaScript ecosystem for build systems is currently much stronger and updated far more
frequently.</p>
<p>In this example I'm going to use <a href="http://gulpjs.com/">gulp.js</a> to take the output from Pelican,
process it and output something slightly better.</p>
<h1>Installing gulp</h1>
<p>We'll quickly create a <code>package.json</code> file to make things easier later:</p>
<pre><code>$ npm init</code></pre>
<p>Then we can install the packages we need. In this small example we'll use
<a href="https://github.com/jonschlinkert/gulp-htmlmin">gulp-htmlmin</a> to minify the HTML output from
Pelican:</p>
<pre><code>$ npm install --save gulp gulp-htmlmin</code></pre>
<p>To make running gulp a bit easier, update the <code>package.json</code> file and add an entry into <code>scripts</code>:</p>
<pre><code>{
  "scripts": {
    "gulp": "gulp"
  }
}</code></pre>
<p>Now we can run gulp with <code>npm run gulp</code> - however we'll need to create a gulpfile first.</p>
<h1>gulpfile.js</h1>
<p>Create a file named <code>gulpfile.js</code>:</p>
<pre><code>var gulp = require("gulp");
var htmlmin = require("gulp-htmlmin");

gulp.task("copy", function () {
  return gulp.src(["./output/**", "!./output/**/**.html"]).pipe(gulp.dest("dist"));
});

gulp.task("html", function () {
  return gulp
    .src("./output/**/*.html")
    .pipe(
      htmlmin({
        collapseWhitespace: true,
      })
    )
    .pipe(gulp.dest("dist"));
});

gulp.task("default", ["copy", "html"]);</code></pre>
<p>This gulpfile has a <code>copy</code> task, which will just copy all files which don't have an HTML extension
from <code>output</code> to <code>dist</code>, so any images, CSS, or RSS feed files will be copied across without any
modifications.</p>
<p>We've also got an <code>html</code> task, which takes any files with an <code>.html</code> extension and runs it a
minifier before being output to the <code>dist</code> directory.</p>
<h1>Updating the Makefile</h1>
<p>A few tweaks are needed for publishing to</p>
<p>Add a <code>DISTDIR</code> variable at the top of your Makefile, this is where we're going to output files
from gulp:</p>
<pre><code>DISTDIR=$(BASEDIR)/dist</code></pre>
<p>Update the <code>publish</code> target to run gulp every time we regenerate the site:</p>
<pre><code>publish:
    $(PELICAN) $(INPUTDIR) -o $(OUTPUTDIR) -s $(PUBLISHCONF) $(PELICANOPTS)
    npm run gulp</code></pre>
<p>Update the <code>service_upload</code> target for the provider you upload your Pelican site with, replace
<code>OUTPUTDIR</code> with <code>DISTDIR</code>. In this example I've updated the <code>rsync_upload</code> target:</p>
<pre><code>rsync_upload: publish
    rsync -e "ssh -p $(SSH_PORT)" -P -rvzc --delete $(DISTDIR)/ $(SSH_USER)@$(SSH_HOST):$(SSH_TARGET_DIR) --cvs-exclude</code></pre>
<h1>Publish!</h1>
<p>Now we've got gulp installed, created our gulpfile, and updated the Makefile, updating the
published content should be as simple as:</p>
<pre><code>$ make publish</code></pre>
<p>Pelican will run first, then gulp will process all the files into the <code>dist</code> directory. Any other
targets will perform both of these tasks first before uploading the content to your preferred
provider.</p>
<h1>What's next?</h1>
<p>Improve your gulpfile and add more tasks to it! You might find some inspiration from
<a href="https://github.com/vigetlabs/gulp-starter">Gulp Starter</a>, or from hundreds of other gulp tutorials
online.</p>
<p>Alternatively you could use <a href="http://gruntjs.com/">Grunt</a>, or an entirely different build system
which isn't JavaScript powered. Thanks to the Makefile you can easily change the build process for
your site.</p>
]]></description>
    </item>
    
    
    
    
    
    
    
    
    
  </channel>
</rss>
