define([
    "hr/hr",
    "hr/utils",
    "hr/promise",
    "hr/dom",
    "core/server",
    "text!resources/templates/preview.html"
], function(hr, _, Q, $, server, templateFile) {
    var path = node.require("path");
    var parse = node.require("gitbook").parse;

    var Preview = hr.View.extend({
        className: "book-section preview",
        template: templateFile,
        events: {
            "click .content a": "onClickLink",
            "click .server-start": "startServer",
            "click .server-stop": "stopServer",
            "click .toggle-autoscroll": "toggleScroll"
        },

        initialize: function() {
            Preview.__super__.initialize.apply(this, arguments);

            this.book = this.parent;
            this.sections = [];
            this.autoScroll = true;

            this.listenTo(this.book, "article:open", this.onArticleChange);
            this.listenTo(this.book, "article:write", _.debounce(this.onArticleChange, 150));

            this.listenTo(server, "state", this.onServerUpdate);
        },

        templateContext: function() {
            return {
                autoScroll: this.autoScroll,
                sections: this.sections
            };
        },

        parseArticle: function(article, content) {
            var that = this;
            var _input = article.get("path");

            return Q()
            .then(function() {
                // Lex page
                return parse.lex(content);
            })
            .then(function(lexed) {
                // Get HTML generated sections
                return parse.page(lexed, {
                    repo: "",
                    dir: path.dirname(_input) || '/',
                    outdir: path.dirname(_input) || '/',
                });
            })
            .then(function(sections) {
                that.sections = sections;
                that.update();
            });
        },

        // When clicking on a link in the content
        onClickLink: function(e) {
            e.preventDefault();

            var href = $(e.currentTarget).attr("href");
            if (/^https?:\/\//i.test(href)){
                node.gui.Shell.openExternal(href);
            }
        },

        // When article is update (write or open)
        onArticleChange: function(article) {
            var that = this;

            this.book.readArticle(article)
            .then(_.partial(this.parseArticle, article).bind(this));
        },

        // When server state change
        onServerUpdate: function(state) {
            this.$(".server-start").toggleClass("btn-success", state);
            this.$(".server-stop").toggle(state);
        },

        startServer: function(e) {
            if (e) e.preventDefault();

            if (server.isRunning()) {
                server.open();
            } else {
                this.book.refreshPreviewServer();
            }
        },
        stopServer: function(e) {
            if (e) e.preventDefault();

            server.stop();
        },

        // Scroll in percent
        scrollTop: function(x) {
            var th = this.$(".content").height();

            this.$(".content").scrollTop((x*th)/100);
        },

        toggleScroll: function() {
            this.autoScroll = !this.autoScroll;
            this.$(".toggle-autoscroll").toggleClass("btn-success", this.autoScroll);
        }
    });

    return Preview;
});
@@ -208,24 +208,59 @@
 re(%22path%22);%0A
+    var url = node.require(%22url%22);%0A
     var pars
@@ -1251,32 +1251,546 @@
  %7D;%0A        %7D,%0A%0A
+        finish: function() %7B%0A            var currentFile = %22index.html%22;%0A            if (this.book.currentArticle) currentFile = this.book.currentArticle.get(%22path%22);%0A            var current = %22file://%22+path.resolve(this.book.model.root(), currentFile);%0A%0A            // Fix image url%0A            this.$(%22.content img%22).each(function() %7B%0A                $(this).attr(%22src%22, url.resolve(current, $(this).attr(%22src%22)));%0A            %7D);%0A%0A            return Preview.__super__.finish.apply(this, arguments);%0A        %7D,%0A%0A
         parseArt

Fix local image display in preview