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
부모 55e2ceccaa
커밋 9a730a947e
4개의 변경된 파일18개의 추가작업 그리고 34개의 파일을 삭제

파일 보기

@ -114,8 +114,7 @@ def edit(name):
content=page.data, content=page.data,
# TODO: Remove this? See #148 # TODO: Remove this? See #148
info=next(page.history), info=next(page.history),
sha=page.sha, sha=page.sha)
partials=_partials(page.imports))
def _partials(imports): def _partials(imports):
@ -126,14 +125,14 @@ def _partials(imports):
if page_name in partials: if page_name in partials:
continue continue
page = g.current_wiki.get_page(page_name) page = g.current_wiki.get_page(page_name)
data = page.data try:
partials[page_name] = data partials[page_name] = page.data
if not data: except KeyError:
partials[page_name] = "`Error importing wiki page '{0}'`".format(page_name)
continue continue
meta = page._get_meta(data) page_queue.extend(page.imports)
if meta and meta.get('import'): # We want to retain the order (and reverse it) so that combining metadata from the imports works
page_queue.extend(meta['import']) return list(reversed(partials.items()))
return partials
@blueprint.route("/_partials") @blueprint.route("/_partials")

파일 보기

@ -82,19 +82,20 @@ var deletePage = function() {
bootbox.alert('Error deleting page!'); bootbox.alert('Error deleting page!');
}); });
}; };
var partials = {}; var partials = [];
var aced = new Aced({ var aced = new Aced({
editor: $('#entry-markdown-content').find('.editor').attr('id'), editor: $('#entry-markdown-content').find('.editor').attr('id'),
renderer: function(md) { renderer: function(md) {
var doc = metaMarked(md); var doc = metaMarked(md);
if (doc.meta && 'import' in doc.meta) { if (doc.meta && 'import' in doc.meta) {
// If we don't have all the imports loaded as partials, get them from server // If the imports have changed, refresh them from the server
if (!doc.meta['import'].every(function(val) {return val in partials;})) { if (partials.length < doc.meta['import'].length ||
$.getJSON('/partials', {'imports': doc.meta['import']}, function (response) { !doc.meta['import'].every(function(impname, index) {return partials[partials.length-index-1][0] == impname})) {
$.extend(partials, response['partials']); $.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? // TODO: Better way to force update of the preview here than this fake signal?
aced.editor.session.doc._signal('change', aced.editor.session.doc._signal('change',
{'action': 'insert', 'lines': [''], 'start': {'row': 0}, 'end': {'row': 0}}); {'action': 'insert', 'lines': [], 'start': {'row': 0}, 'end': {'row': 0}});
}); });
} }
} }

파일 보기

@ -96,9 +96,9 @@ var MDR = {
this.md = doc.md; this.md = doc.md;
var meta = this.meta = {}; var meta = this.meta = {};
if (this.partials) { if (this.partials) {
$.each(this.partials, function(key, value) { $.each(this.partials, function(index, item) {
var doc = metaMarked(value); var doc = metaMarked(item[1]);
Handlebars.registerPartial(key, doc.md); Handlebars.registerPartial(item[0], doc.md);
$.extend(meta, doc.meta); $.extend(meta, doc.meta);
}) })
} }

파일 보기

@ -8,22 +8,6 @@
</script> </script>
<script src="{{ url_for('static', filename='js/editor.js') }}"></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') %} {% if config.get('COLLABORATION') %}
<script src="{{ url_for('static', filename='js/collaboration/main.js') }}"></script> <script src="{{ url_for('static', filename='js/collaboration/main.js') }}"></script>
{% endif %} {% endif %}