Merge pull request #170 from gazpachoking/partials-update
Partials improvements
This commit is contained in:
		
						commit
						bd39255dd0
					
				
					 6 changed files with 66 additions and 42 deletions
				
			
		| 
						 | 
					@ -191,16 +191,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) or {}
 | 
				
			||||||
            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):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -82,10 +82,26 @@ var deletePage = function() {
 | 
				
			||||||
    bootbox.alert('Error deleting page!');
 | 
					    bootbox.alert('Error deleting page!');
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					var last_imports = '';
 | 
				
			||||||
 | 
					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 (doc.meta && 'import' in doc.meta) {
 | 
				
			||||||
 | 
					      // If the imports have changed, refresh them from the server
 | 
				
			||||||
 | 
					      if (doc.meta['import'].toString() != last_imports) {
 | 
				
			||||||
 | 
					        last_imports = doc.meta['import'].toString();
 | 
				
			||||||
 | 
					        $.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}});
 | 
				
			||||||
 | 
					          });
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return MDR.convert(md, partials)
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
  info: Commit.info,
 | 
					  info: Commit.info,
 | 
				
			||||||
  submit: function(content) {
 | 
					  submit: function(content) {
 | 
				
			||||||
    var data = {
 | 
					    var data = {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -8,22 +8,6 @@
 | 
				
			||||||
  </script>
 | 
					  </script>
 | 
				
			||||||
  <script src="{{ url_for('wiki.static', filename='js/editor.js') }}"></script>
 | 
					  <script src="{{ url_for('wiki.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.data|tojson|safe }});
 | 
					 | 
				
			||||||
            } catch (e) {
 | 
					 | 
				
			||||||
              // no data?
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
          {% endif %}
 | 
					 | 
				
			||||||
        {% endfor %}
 | 
					 | 
				
			||||||
      });
 | 
					 | 
				
			||||||
    </script>
 | 
					 | 
				
			||||||
  {% endif %}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  {% if config.get('COLLABORATION') %}
 | 
					  {% if config.get('COLLABORATION') %}
 | 
				
			||||||
    <script src="{{ url_for('wiki.static', filename='js/collaboration/main.js') }}"></script>
 | 
					    <script src="{{ url_for('wiki.static', filename='js/collaboration/main.js') }}"></script>
 | 
				
			||||||
  {% endif %}
 | 
					  {% endif %}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -23,12 +23,7 @@
 | 
				
			||||||
{% block js %}
 | 
					{% block js %}
 | 
				
			||||||
  <script>
 | 
					  <script>
 | 
				
			||||||
    $(function(){
 | 
					    $(function(){
 | 
				
			||||||
      {% if partials %}
 | 
					      $("#page-content").html(MDR.convert({{ page.data|tojson|safe }}, {{ partials|d([])|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 %}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,6 @@
 | 
				
			||||||
from __future__ import absolute_import
 | 
					from __future__ import absolute_import
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import collections
 | 
				
			||||||
import itertools
 | 
					import itertools
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
from datetime import datetime
 | 
					from datetime import datetime
 | 
				
			||||||
| 
						 | 
					@ -27,7 +28,9 @@ def commit(name, sha):
 | 
				
			||||||
    if not data:
 | 
					    if not data:
 | 
				
			||||||
        abort(404)
 | 
					        abort(404)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return render_template('wiki/page.html', name=name, page=data, commit=sha)
 | 
					    partials = _partials(data.imports, sha=sha)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return render_template('wiki/page.html', name=name, page=data, commit=sha, partials=partials)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@blueprint.route(r"/_compare/<path:name>/<regex('\w+'):fsha><regex('\.{2,3}'):dots><regex('\w+'):lsha>")
 | 
					@blueprint.route(r"/_compare/<path:name>/<regex('\w+'):fsha><regex('\.{2,3}'):dots><regex('\w+'):lsha>")
 | 
				
			||||||
| 
						 | 
					@ -103,7 +106,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):
 | 
				
			||||||
| 
						 | 
					@ -120,8 +122,32 @@ 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=page.partials)
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def _partials(imports, sha='HEAD'):
 | 
				
			||||||
 | 
					    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, sha=sha)
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
 | 
					            partials[page_name] = page.data
 | 
				
			||||||
 | 
					        except KeyError:
 | 
				
			||||||
 | 
					            partials[page_name] = "`Error importing wiki page '{0}'`".format(page_name)
 | 
				
			||||||
 | 
					            continue
 | 
				
			||||||
 | 
					        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")
 | 
				
			||||||
 | 
					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})
 | 
				
			||||||
| 
						 | 
					@ -245,6 +271,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))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -70,11 +70,12 @@ var MDR = {
 | 
				
			||||||
  parse: function(md){
 | 
					  parse: function(md){
 | 
				
			||||||
    return markdownit.render(md);
 | 
					    return markdownit.render(md);
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  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);
 | 
				
			||||||
| 
						 | 
					@ -105,7 +106,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(index, item) {
 | 
				
			||||||
 | 
					        var doc = metaMarked(item[1]);
 | 
				
			||||||
 | 
					        Handlebars.registerPartial(item[0], 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);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue