WIP partial improvements

This commit is contained in:
Chase Sterling 2016-08-09 00:52:08 -04:00
parent 4a38e896eb
commit 55e1ed3a2b
6 changed files with 64 additions and 25 deletions

View file

@ -187,16 +187,10 @@ class WikiPage(HookMixin):
return len(cached_revs), True
@property
def partials(self):
data = self.data
if not data:
return {}
partials = {}
meta = self._get_meta(data)
if meta and 'import' in meta:
for partial_name in meta['import']:
partials[partial_name] = self.wiki.get_page(partial_name, sha=self.sha)
return partials
def imports(self):
"""Names"""
meta = self._get_meta(self.data)
return meta.get('import', [])
@staticmethod
def _get_meta(content):

View file

@ -1,3 +1,4 @@
import collections
import itertools
import sys
from datetime import datetime
@ -97,7 +98,6 @@ def history_data(name):
}
@blueprint.route("/_edit/<path:name>")
@login_required
def edit(name):
@ -115,7 +115,32 @@ def edit(name):
# TODO: Remove this? See #148
info=next(page.history),
sha=page.sha,
partials=page.partials)
partials=_partials(page.imports))
def _partials(imports):
page_queue = collections.deque(imports)
partials = collections.OrderedDict()
while page_queue:
page_name = page_queue.popleft()
if page_name in partials:
continue
page = g.current_wiki.get_page(page_name)
data = page.data
partials[page_name] = data
if not data:
continue
meta = page._get_meta(data)
if meta and meta.get('import'):
page_queue.extend(meta['import'])
return partials
@blueprint.route("/_partials")
def partials():
if current_app.config.get('PRIVATE_WIKI') and current_user.is_anonymous():
return current_app.login_manager.unauthorized()
return {'partials': _partials(request.args.getlist('imports[]'))}
@blueprint.route("/_create/", defaults={'name': None})
@ -239,6 +264,6 @@ def page(name):
data = g.current_wiki.get_page(cname)
if data:
return render_template('wiki/page.html', name=cname, page=data, partials=data.partials)
return render_template('wiki/page.html', name=cname, page=data, partials=_partials(data.imports))
else:
return redirect(url_for('wiki.create', name=cname))

View file

@ -82,10 +82,26 @@ var deletePage = function() {
bootbox.alert('Error deleting page!');
});
};
var partials = {};
var aced = new Aced({
editor: $('#entry-markdown-content').find('.editor').attr('id'),
renderer: function(md) { return MDR.convert(md) },
renderer: function(md) {
var doc = metaMarked(md);
if ('import' in doc.meta) {
if (!doc.meta['import'].every(function(val) {return val in partials;})) {
$.ajax({
url: '/_partials',
data: {'imports': doc.meta['import']},
async: true,
dataType: 'json',
success: function (response) {
$.extend(partials, response['partials']);
//TODO: Force editor rerender
}});
}
}
return MDR.convert(md, partials)
},
info: Commit.info,
submit: function(content) {
var data = {

View file

@ -58,11 +58,12 @@ var MDR = {
parse: function(md){
return marked(md, { renderer: this.renderer });
},
convert: function(md, sanitize) {
convert: function(md, partials, sanitize) {
if (this.sanitize !== null) {
sanitize = this.sanitize;
}
this.md = md;
this.partials = partials;
this.processMeta();
try {
var html = this.parse(this.md);
@ -93,7 +94,15 @@ var MDR = {
processMeta: function() {
var doc = metaMarked(this.md);
this.md = doc.md;
this.meta = doc.meta;
var meta = this.meta = {};
if (this.partials) {
$.each(this.partials, function(key, value) {
var doc = metaMarked(value);
Handlebars.registerPartial(key, doc.md);
$.extend(meta, doc.meta);
})
}
$.extend(this.meta, doc.meta);
if (this.meta) {
try {
var template = Handlebars.compile(this.md);

View file

@ -14,7 +14,7 @@
{% for name, value in partials.items() %}
{% if name and value %}
try {
Handlebars.registerPartial({{ name|tojson|safe }}, {{ value.data|tojson|safe }});
Handlebars.registerPartial({{ name|tojson|safe }}, {{ value|tojson|safe }});
} catch (e) {
// no data?
}

View file

@ -23,12 +23,7 @@
{% block js %}
<script>
$(function(){
{% if partials %}
{% for name, value in partials.items() %}
Handlebars.registerPartial({{ name|tojson|safe }}, {{ value.data|tojson|safe }});
{% endfor %}
{% endif %}
$("#page-content").html(MDR.convert({{ page.data|tojson|safe }})).show();
$("#page-content").html(MDR.convert({{ page.data|tojson|safe }}, {{ partials|tojson|safe }})).show();
});
</script>
{% endblock %}