Commits

Anonymous committed 189a193 Draft

.

Comments (0)

Files changed (16)

js/editor/toolbar/styleoptions.js

 
         for (var name in style.options) {
             var option = style.options[name];
-            $div.append(option.generate_$e(this.widget_builder));
+            $div.append(option.generate_$e(this.widget_builder, this));
         }
 
         this.$e.append($div);

js/editor/toolbar/toolbar.js

         var opts = {};
         $e.find('div[data-name]').each(function () {
             var $this = $(this);
-            opts[$this.attr('data-name')] = $this.find('div.selected').attr('data-value');
+            var $sel = $this.find('div.selected');
+            if ($sel.length) {
+                opts[$this.attr('data-name')] = $sel.attr('data-value');
+            } else {
+                opts[$this.attr('data-name')] = $this.find('[data-value]').attr('data-value');
+            }
         });
         return opts;
     };

js/format/sembook.js

             return _.reduce(results, _sum_data, { data: null });
         };
 
+        results.each = function (func) {
+            var last = null;
+            for (var i=0; i<results.length; i++) {
+                last = func(results[i], i);
+                if (last === false) {
+                    return false;
+                }
+            }
+            return last;
+        };
+
         return results;
     };
 

js/style/asset.js

     /*
      * The following functions operate on the asset element itself
      */
+    var ASSET_PATH_PREFIX = './assets/'
     exports.generate_rendered_preview_html = function (asset) {
+
         var asset_path = asset.$('path').data();
+
+        // Add in relative path to asset
+        asset_path = ASSET_PATH_PREFIX + asset_path;
+
         var asset_info = {
             path: asset_path,
             asset_processing: asset.$('asset_processing').data(),

js/style/option.js

  */
 
 define([], function () {
-
     var Option = function (settings) {
         if (!settings) { return; } // extension
 
         return $("<div>").append(label).append($e);
     };
 
+    var AssetOption = function (settings) {
+        Option.call(this, settings);
+        this.label = settings.label;
+        this.name = settings.name;
+
+        // TODO add in json parse --v
+        this.constraints = settings.constraints || {
+                min_height: 700,
+                min_width: 433
+            };
+    };
+
+    AssetOption.prototype = new Option();
+
+    AssetOption.prototype.generate_$e = function (widget_builder, toolbar) {
+        /*
+         * Generates two buttons, one to choose the other to crop, the given asset ID
+         */
+        var $make_button = _.bind(function (verb, icon, func) {
+                return $("<button>").text(verb + " image for " + this.label)
+                    .button({ icons: { primary: 'ui-icon-'+icon }})
+                    .click(function () {
+                        // Run given function with the Editor god object as the
+                        // first thing passed
+                        _.bind(func, this)(toolbar.editor);
+                    });
+            }, this);
+
+        this.$choose_button = $make_button("Choose", this.action_choose);
+        this.$choose_button.attr('data-name', this.name);
+        this.$choose_button.attr('data-value', '');
+
+        return $("<div>")
+            .append($make_button("Choose", this.action_choose))
+            .append($make_button("Crop", this.action_crop));
+    };
+
+    AssetOption.prototype.action_crop = function (editor) {
+        editor.locked_in_insert = true; // XXX Hack
+        var c = this.constraints;
+        editor.dialogs.asset_edit.show(this.assetinfo, c);
+    };
+
+    AssetOption.prototype.action_choose = function (editor) {
+        editor.locked_in_insert = true; // XXX Hack
+        editor.dialogs.assetmanager.show(
+                            _.bind(this.set_asset_id, this));
+    };
+
+    AssetOption.prototype.set_asset_id = function (asset_id) {
+        //editor.locked_in_insert = false; // XXX Hack
+        //console.log("THIS IS ASSETID", asset_id);
+        // OMG this is so hacky here :( oh well will go away in next-ish
+        // version
+        this.$choose_button.attr('data-value', asset_id);
+    };
 
     var create = function (opts) {
         // Parsed clearconf opts is in opts
         var Class = {
-                'select': SelectOption // presently only type
+                'select': SelectOption, // presently only normal type
+                'asset': AssetOption
             }[opts.type || 'select'];
 
         if (opts.html) {
         Option: Option,
         create: create
     };
+
+
 });

js/style/style.js

     };
 
 
-		/*
-		 * Fetches asset object for given asset_id
-		 */
+    /*
+     * Fetches asset object for given asset_id
+     */
     Style.prototype.get_asset = function (asset_id) {
-				var asset_id = this.style_options.book_cover_asset_id || null;
-				var assets = this.current.sembook.$("asset");
-				for (var i in assets) {
-						var asset = assets[i];
-						if (asset.$('assetid').data() == asset_id) {
-								return asset;
-						}
-				}
-				return null;
-		};
+        var assets = this.current.sembook.$("asset");
 
-		/*
-		 * Fetches asset object for book cover
-		 */
+        var result = null;
+        assets.each(function (asset) {
+                if (asset.$('assetid').data() == asset_id) {
+                    result = asset;
+                    return false;
+                }
+            });
+
+        return result;
+    };
+
+    /*
+     * Fetches asset object for book cover
+     */
     Style.prototype.get_book_cover_asset = function () {
-				var asset_id = this.style_options.book_cover_asset_id || null;
-				return this.get_asset(asset_id);
-		};
+        var asset_id = this.current.style_options.book_cover_asset_id;
+
+        if (!asset_id && asset_id !== 0) {
+            console.error("Book Cover asset ID not specified", this.current.style_options);
+            return null;
+        }
+
+        return this.get_asset(asset_id);
+    };
 
     Style.prototype.preview_render = function (sembook, render_options, style_options) {
         // Later add in partial preview function

js/style/templatetags.js

  */
 
 define(['swig',
-				'style/asset',
-				'style/swig_helpers',
-				'format/spec'], function (
-				swig,
-				asset_style,
-				swig_helpers,
-				spec) { return function(opts, swig_opts) {
+        'style/asset',
+        'style/swig_helpers',
+        'format/spec'], function (
+        swig,
+        asset_style,
+        swig_helpers,
+        spec) { return function(opts, swig_opts) {
     var _render = function (elems, as_tagname) {
         // Render as list...
         if (!_.isArray(elems)) {
         },
 
         rendered_asset_preview: function (elem) {
-						return asset_style.generate_rendered_preview_html(elem);
-						// (Plain rendered, in the case of an image in the final book, is
-						// just handled in the styling, I guess?)
+            if (!elem) {
+                return "ASSET NOT FOUND";
+            }
+            return asset_style.generate_rendered_preview_html(elem);
+            // (Plain rendered, in the case of an image in the final book, is
+            // just handled in the styling, I guess?)
         },
 
         rendered: function (elem) {
 
 exports.options_from_sembook = function (sembook_report, cover_path) {
     // Uses are port like the one generated in render.js
-	var opts = {
-		// For now, until the asset system works, simply use cover_path
-		cover:                cover_path,
-		publisher:            'Leebre.org',
-		title:                sembook_report["title"],
-		title_sort:           sortable_title(sembook_report["title"]),
-		authors:              sembook_report["author_name"],
-		author_sort:          sortable_name(sembook_report["author_name"]),
-		comments:             clean_html(sembook_report["summary"]),
-		tags:                 sembook_report["tags"],
-		chapter:              '//*[@class="chapter"]',
-		page_breaks_before:   '//*[@class="pagebreak"]',
-		chapter_mark:         'none',
-		book_producer:        'Leebre.org',
-		language:             'en',
-		// 'input-encoding':  'utf-8',
-	}
+    var opts = {
+        // For now, until the asset system works, simply use cover_path
+        cover:                cover_path,
+        publisher:            'Leebre.org',
+        title:                sembook_report["title"],
+        title_sort:           sortable_title(sembook_report["title"]),
+        authors:              sembook_report["author_name"],
+        author_sort:          sortable_name(sembook_report["author_name"]),
+        comments:             clean_html(sembook_report["summary"]),
+        tags:                 sembook_report["tags"],
+        chapter:              '//*[@class="chapter"]',
+        page_breaks_before:   '//*[@class="pagebreak"]',
+        chapter_mark:         'none',
+        book_producer:        'Leebre.org',
+        language:             'en',
+        // 'input-encoding':  'utf-8',
+    }
     return opts;
 };
 
     _v("Using style ", style_name);
     var style = find_style(style_list, style_name, false);
 
+    _v("Using style options ",  style_options);
+
     if (!style) {
         console.error("Could not find style '" + argv.style_name + "'.");
         process.exit()
     var sembook_report = exports.sembook_report(sb);
     var calibre_options = calibre.options_from_sembook(sembook_report, output_paths.cover);
 
+    var start = function (the_style_list) {
+        style_list = the_style_list;
+
+        // TODO to get assets working: "ln -s /abs/path/assets_dir  /tmp/.../assets"
+        // (so relative paths from ebook will work properly)
+        // Set up asset directory to be relative to /tmp/ folders
+        _v("THIS IS SEMBOK_PATH", sembook_path);
+        var assets_dir = path.join(path.dirname(sembook_path), ASSETS);
+        tmp.link(assets_dir);
+
+        // do render_browser_html at the same time as render_assets, since the
+        // only dependency requirement is the following sequence:
+        // render_assets -> render_cover -> any ebook
+        // (browser target, individual assets, and each ebook target can be
+        // done simultaneously)
+        var browser_target = render_browser_html();
+
+        render_assets(browser_target, function () {
+            render_cover();
+        });
+    };
+
     // Function to render HTML as a ePub, AZW etc
     var render_html_as_ebook = function (html, target, callback) {
         // Synchronously write it
         var p = tmp.get('ebook.html');
         _write(p, html);
 
-        // TODO to get assets working: "ln -s /abs/path/assets_dir  /tmp/.../assets"
-        // (so relative paths from ebook will work properly)
-
         // Get profile options based on target extension (epub or azw)
         var profile_options = calibre.options_output_profile({
                 // add on input and output
 
     var style_list = null;
 
-    var do_it = function (the_style_list) {
-        style_list = the_style_list;
-
-        // do render_browser_html at the same time as render_assets, since the
-        // only dependency requirement is the following sequence:
-        // render_assets -> render_cover -> any ebook
-        // (browser target, individual assets, and each ebook target can be
-        // done simultaneously)
-        var browser_target = render_browser_html();
-
-        render_assets(browser_target, function () {
-            render_cover();
-        });
-    };
-
     var render_assets = function (browser_target, callback) {
         // Loop through all assets and render them
         var done = function () {
         _v('Thanks and have a nice day.');
     };
 
-    get_style_list(do_it);
+    get_style_list(start);
 };
 
 exports.read_sembook = function (input_path) {
 
     print(j)
 
-if __name__=='__main__':
+if __name__ == '__main__':
     test_main()
 
 
     return p;
 };
 
+exports.link = function (in_path) {
+    /*
+     * Links given path into the current tmp directory
+     */
+    var base = path.basename(in_path);
+    var new_path = path.join(_tmp_dir(), base);
+    fs.symlinkSync(in_path, new_path);
+};
 

testdata/test_sembook/assets/le_cat.jpg

Added
New image

testdata/test_sembook/assets/tony_trestings.jpg

Added
New image

testdata/test_sembook/le_cat.jpg

Removed
Old image

testdata/test_sembook/test_sembook.sembook

 			blog
 		book_style
 			source_api:classic
-			style_options:{}
+			style_options:{"book_cover_asset_id": 0}
 		cover_style
 			source_api:bold
-			style_options:{}
+			style_options:{"book_cover_asset_id": 0}
 		cover
 			author_name
 			author_url
 	assets
 		asset
 			assetid:0
-			path:le_cat.jpg
+			path:assets/le_cat.jpg
 			asset_type:image
 			asset_processing:{"crop": {"height": 701, "width": 433, "top": 0, "left": 0}, "scale": {"height": 701, "width": 433}, "filters": {"blur": "5px", "sepia": 0.53}}
 		asset
 			assetid:1
-			path:tony_trestings.jpg
+			path:assets/tony_trestings.jpg
 			asset_type:image
 			asset_processing:{"crop": {"width": 400, "height": 400, "top": 0, "left": 0}, "scale": {"width": 400, "height": 400}}
 	document

testdata/test_sembook/tony_trestings.jpg

Removed
Old image