From 9a730a947e34c3d96eda5ee77dc04d265e66c9ff Mon Sep 17 00:00:00 2001 From: Chase Sterling Date: Tue, 9 Aug 2016 21:45:07 -0400 Subject: [PATCH] Fix scope order when importing partials with metadata. Render error when importing wiki pages that do not exist. --- realms/modules/wiki/views.py | 17 ++++++++--------- realms/static/js/editor.js | 13 +++++++------ realms/static/js/mdr.js | 6 +++--- realms/templates/wiki/edit.html | 16 ---------------- 4 files changed, 18 insertions(+), 34 deletions(-) diff --git a/realms/modules/wiki/views.py b/realms/modules/wiki/views.py index b3de441..ab7d99f 100644 --- a/realms/modules/wiki/views.py +++ b/realms/modules/wiki/views.py @@ -114,8 +114,7 @@ def edit(name): content=page.data, # TODO: Remove this? See #148 info=next(page.history), - sha=page.sha, - partials=_partials(page.imports)) + sha=page.sha) def _partials(imports): @@ -126,14 +125,14 @@ def _partials(imports): if page_name in partials: continue page = g.current_wiki.get_page(page_name) - data = page.data - partials[page_name] = data - if not data: + try: + partials[page_name] = page.data + except KeyError: + partials[page_name] = "`Error importing wiki page '{0}'`".format(page_name) continue - meta = page._get_meta(data) - if meta and meta.get('import'): - page_queue.extend(meta['import']) - return partials + page_queue.extend(page.imports) + # We want to retain the order (and reverse it) so that combining metadata from the imports works + return list(reversed(partials.items())) @blueprint.route("/_partials") diff --git a/realms/static/js/editor.js b/realms/static/js/editor.js index 71a17c9..fed5e73 100644 --- a/realms/static/js/editor.js +++ b/realms/static/js/editor.js @@ -82,19 +82,20 @@ var deletePage = function() { bootbox.alert('Error deleting page!'); }); }; -var partials = {}; +var partials = []; var aced = new Aced({ editor: $('#entry-markdown-content').find('.editor').attr('id'), renderer: function(md) { var doc = metaMarked(md); if (doc.meta && 'import' in doc.meta) { - // If we don't have all the imports loaded as partials, get them from server - if (!doc.meta['import'].every(function(val) {return val in partials;})) { - $.getJSON('/partials', {'imports': doc.meta['import']}, function (response) { - $.extend(partials, response['partials']); + // If the imports have changed, refresh them from the server + if (partials.length < doc.meta['import'].length || + !doc.meta['import'].every(function(impname, index) {return partials[partials.length-index-1][0] == impname})) { + $.getJSON('/_partials', {'imports': doc.meta['import']}, function (response) { + partials = response['partials']; // TODO: Better way to force update of the preview here than this fake signal? aced.editor.session.doc._signal('change', - {'action': 'insert', 'lines': [''], 'start': {'row': 0}, 'end': {'row': 0}}); + {'action': 'insert', 'lines': [], 'start': {'row': 0}, 'end': {'row': 0}}); }); } } diff --git a/realms/static/js/mdr.js b/realms/static/js/mdr.js index b3e9ddb..0f077b3 100644 --- a/realms/static/js/mdr.js +++ b/realms/static/js/mdr.js @@ -96,9 +96,9 @@ var MDR = { this.md = doc.md; var meta = this.meta = {}; if (this.partials) { - $.each(this.partials, function(key, value) { - var doc = metaMarked(value); - Handlebars.registerPartial(key, doc.md); + $.each(this.partials, function(index, item) { + var doc = metaMarked(item[1]); + Handlebars.registerPartial(item[0], doc.md); $.extend(meta, doc.meta); }) } diff --git a/realms/templates/wiki/edit.html b/realms/templates/wiki/edit.html index 479a44b..dd1dc86 100644 --- a/realms/templates/wiki/edit.html +++ b/realms/templates/wiki/edit.html @@ -8,22 +8,6 @@ - {% if partials %} - - {% endif %} - {% if config.get('COLLABORATION') %} {% endif %}