LoginSignup
15
16

More than 5 years have passed since last update.

Markdownで書いたドキュメントをSphinxで閲覧するまで、Gruntを使ってある程度自動化してみた

Last updated at Posted at 2013-12-12

Markdownで書いたドキュメントをSphinxで閲覧するにはpandocでreStructuredTextに変換し、その後make htmlするのが一般的?な流れだと思うが、都度都度ビルドするのも面倒なのでGruntを使ってタスク化してみました。ついでにgrunt-este-watchとgrunt-contrib-connectでMarkdownファイルが編集されたらビルドとブラウザリロードをするようにしてみました。

Gruntfile.jsは以下のような感じです。

Gruntfile.js
var fs   = require("fs")
  , exec = require('child_process').exec;

module.exports = function(grunt) {

  grunt.initConfig({
    connect: {
      server: {
        options: {
            livereload: true,
            port: 8000,
            base: 'build/html',
            hostname: 'localhost'
        }
      }
    },
    esteWatch: {
      options: {
        dirs: ['source/**/'],
        livereload: {
          enabled: true,
          extensions: ['md'],
          port: 35729
        }
      },
      md: function(filepath) {
        var files = [{
          expand: true,
          src: filepath,
          ext: '.md'
        }];
        return ['to_rest', 'build'];
      }
    }
  });

  grunt.registerTask('to_rest', 'convert to reST from markdown', function() {
    var taskInfo = 'convert to reST from markdown... ';
    var cb = this.async();

    fs.readdir('./source', function(err, files) {
      files.forEach(function(file) {

        var rest_file, cmd;

        if (file.match(/\.md$/g)) {
          cmd  = 'pandoc --toc -f markdown -t rst source/' + file;
          cmd += ' -o source/' + file.replace(/md$/g, '') + 'rst';

          var child = exec(cmd, function(err, stdout, stderr) {
            if (err) {
               console.log(err);
               grunt.log.write(taskInfo).error();
            }
          });
        }
      });
      grunt.log.write(taskInfo).ok();
      cb();
    });

  });

  grunt.registerTask('build', 'to make standalone HTML files for Sphinx', function() {
    var taskInfo = 'to make standalone HTML files for Sphinx... ';

    var cb = this.async();

    var child = exec('make html', function(err, stdout, stderr) {
      if (!err) {
        console.log('stdout: ' + stdout);
        grunt.log.write(taskInfo).ok();
      } else {
        console.log(err);
        grunt.log.write(taskInfo).error();
      }
      cb();
    });
  });

  grunt.loadNpmTasks('grunt-contrib-connect');
  grunt.loadNpmTasks('grunt-este-watch');
  grunt.registerTask('default', ['to_rest', 'build', 'connect', 'esteWatch']);
};

需要な無さげなので説明は詳しくは書きませんが、Sphinxコンテンツはmake htmlが可能な状況であるとして、とりあえずnpmで必要なモジュールをインストールします。

npm install grunt grunt-contrib-connect grunt-este-watch

grunt-cliを入れてなければ以下も実行しておきましょう。

npm install -g grunt-cli

あとは上のGruntfile.jsを書いてgrunt実行します。

grunt

するとポート番号8000でWEBサーバが起動しているので、ブラウザを開きます。

open http://localhost:8000

この状態でsource以下のMarkdownファイルを変更するとsphinx用html生成とブラウザのリロードまで行ってくれます。

※反響あれば記事を更新しようと思います。

15
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
16