Fix scope order when importing partials with metadata.

Render error when importing wiki pages that do not exist.
This commit is contained in:
Chase Sterling 2016-08-09 21:45:07 -04:00
parent 55e2ceccaa
commit 9a730a947e
4 changed files with 18 additions and 34 deletions

View file

@ -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")

View file

@ -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}});
});
}
}

View file

@ -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);
})
}

View file

@ -8,22 +8,6 @@
</script>
<script src="{{ url_for('static', filename='js/editor.js') }}"></script>
{% if partials %}
<script>
$(function() {
{% for name, value in partials.items() %}
{% if name and value %}
try {
Handlebars.registerPartial({{ name|tojson|safe }}, {{ value|tojson|safe }});
} catch (e) {
// no data?
}
{% endif %}
{% endfor %}
});
</script>
{% endif %}
{% if config.get('COLLABORATION') %}
<script src="{{ url_for('static', filename='js/collaboration/main.js') }}"></script>
{% endif %}