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 return len(cached_revs), True
@property @property
def partials(self): def imports(self):
data = self.data """Names"""
if not data: meta = self._get_meta(self.data)
return {} return meta.get('import', [])
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
@staticmethod @staticmethod
def _get_meta(content): def _get_meta(content):

View file

@ -1,3 +1,4 @@
import collections
import itertools import itertools
import sys import sys
from datetime import datetime from datetime import datetime
@ -97,7 +98,6 @@ def history_data(name):
} }
@blueprint.route("/_edit/<path:name>") @blueprint.route("/_edit/<path:name>")
@login_required @login_required
def edit(name): def edit(name):
@ -115,7 +115,32 @@ def edit(name):
# 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=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}) @blueprint.route("/_create/", defaults={'name': None})
@ -239,6 +264,6 @@ def page(name):
data = g.current_wiki.get_page(cname) data = g.current_wiki.get_page(cname)
if data: 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: else:
return redirect(url_for('wiki.create', name=cname)) return redirect(url_for('wiki.create', name=cname))

View file

@ -82,10 +82,26 @@ var deletePage = function() {
bootbox.alert('Error deleting page!'); bootbox.alert('Error deleting page!');
}); });
}; };
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) { 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, info: Commit.info,
submit: function(content) { submit: function(content) {
var data = { var data = {

View file

@ -58,11 +58,12 @@ var MDR = {
parse: function(md){ parse: function(md){
return marked(md, { renderer: this.renderer }); return marked(md, { renderer: this.renderer });
}, },
convert: function(md, sanitize) { convert: function(md, partials, sanitize) {
if (this.sanitize !== null) { if (this.sanitize !== null) {
sanitize = this.sanitize; sanitize = this.sanitize;
} }
this.md = md; this.md = md;
this.partials = partials;
this.processMeta(); this.processMeta();
try { try {
var html = this.parse(this.md); var html = this.parse(this.md);
@ -93,7 +94,15 @@ var MDR = {
processMeta: function() { processMeta: function() {
var doc = metaMarked(this.md); var doc = metaMarked(this.md);
this.md = doc.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) { if (this.meta) {
try { try {
var template = Handlebars.compile(this.md); var template = Handlebars.compile(this.md);

View file

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

View file

@ -23,12 +23,7 @@
{% block js %} {% block js %}
<script> <script>
$(function(){ $(function(){
{% if partials %} $("#page-content").html(MDR.convert({{ page.data|tojson|safe }}, {{ partials|tojson|safe }})).show();
{% 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();
}); });
</script> </script>
{% endblock %} {% endblock %}