Add postimage support for reST
authorIvan Fedorov <oxyum@oxyum.ru>
Sun Oct 19 01:02:59 2008 +0400 (6 days ago)
changeset 105749cf2bfe5b56
parent 1056d84a54042a22
child 1058e1e99c66a1de
Add postimage support for reST
apps/blog/admin.py
apps/postimage/views.py
static/js/postimage.js
templates/admin/postimage/base.html
       1 --- a/apps/blog/admin.py	Sat Oct 18 21:57:33 2008 +0400
       2 +++ b/apps/blog/admin.py	Sun Oct 19 01:02:59 2008 +0400
       3 @@ -26,7 +26,7 @@
       4                  settings.STATIC_URL + 'js/wymeditor/jquery.wymeditor.pack.js',
       5                  '/' + settings.BLOG_URLCONF_ROOT + 'wysiwyg_js/',
       6                  )
       7 -        if settings.RENDER_METHOD=='markdown':
       8 +        if settings.RENDER_METHOD in ('html', 'markdown', 'rst'):
       9              js = (settings.STATIC_URL + 'js/postimage.js', )
      10  
      11      def queryset(self, request):
     1.1 --- a/apps/postimage/views.py	Sat Oct 18 21:57:33 2008 +0400
     1.2 +++ b/apps/postimage/views.py	Sun Oct 19 01:02:59 2008 +0400
     1.3 @@ -1,4 +1,4 @@
     1.4 -from django.template import Context, loader
     1.5 +from django.template import Context, RequestContext, loader
     1.6  from django.http import HttpResponseRedirect, HttpResponse
     1.7  from django.views.decorators.cache import never_cache
     1.8  from django.core.urlresolvers import reverse
     1.9 @@ -24,7 +24,7 @@
    1.10           'postimage_url': settings.POSTIMAGE_URL,
    1.11           'postimage_root': settings.POSTIMAGE_ROOT,
    1.12           'for': request.GET['for'], }
    1.13 -    c = Context(c)
    1.14 +    c = RequestContext(request, c)
    1.15      return HttpResponse(t.render(c))
    1.16  
    1.17  
     2.1 --- a/static/js/postimage.js	Sat Oct 18 21:57:33 2008 +0400
     2.2 +++ b/static/js/postimage.js	Sun Oct 19 01:02:59 2008 +0400
     2.3 @@ -1,13 +1,23 @@
     2.4  Postimage = {
     2.5 -	showLinkOn : Array('id_text', 'id_content'),
     2.6 +	// add here the #id-s that you would like to use postimage for
     2.7 +	showLinkOn: Array('id_text', 'id_content'),
     2.8  
     2.9 -	init: function() {
    2.10 +	// what markup do you use? see the createLink function for supported markups,
    2.11 +	// defaults to markdown
    2.12 +	markup: 'markdown',
    2.13 +
    2.14 +	init: function(markup) {
    2.15 +		this.markup = (markup == null) ? this.markup : markup;
    2.16  		if( document.getElementById('postimage') ) {
    2.17  			return;
    2.18  		}
    2.19  		else {
    2.20  			this.showLink();
    2.21  		}
    2.22 +	},
    2.23 +
    2.24 +	setMarkup: function(markup) {
    2.25 +		this.markup = (markup == null) ? 'markdown' : markup;
    2.26  	},
    2.27  
    2.28  	showLink: function() {
    2.29 @@ -29,25 +39,35 @@
    2.30  	},
    2.31  
    2.32  	showLinkFor: function(elem) {
    2.33 -        elem.previousSibling.appendChild(document.createElement('br'));
    2.34 +		elem.previousSibling.appendChild(document.createElement('br'));
    2.35  		elem.previousSibling.appendChild(this.getLink(elem.id));
    2.36  	},
    2.37  
    2.38  	pathToImage: function(image) {
    2.39  		var url = document.getElementById('baseurl').value;
    2.40 -        var root = document.getElementById('image_root').value;
    2.41 -        var len = root.length;
    2.42 -        if (root[len-1] != '/') {
    2.43 -            len += 1;
    2.44 -        }
    2.45 +		var root = document.getElementById('image_root').value;
    2.46 +		var len = root.length;
    2.47 +		if (root[len-1] != '/') { len += 1; };
    2.48  		return url.concat(image.slice(len));
    2.49  	},
    2.50  
    2.51  	submit: function() {
    2.52  		var elem = document.getElementById('id_file');
    2.53  		var target = window.opener.document.getElementById(document.getElementById('for').value);
    2.54 -		target.value = target.value + '\n![' + document.getElementById('id_alt').value + '](' + this.pathToImage(elem.value) + ')\n';
    2.55 +		target.value = target.value + this.createLink(document.getElementById('id_alt').value, this.pathToImage(elem.value));
    2.56  		window.close();
    2.57 +	},
    2.58 +
    2.59 +	createLink: function(alt, src) {
    2.60 +		switch (this.markup) {
    2.61 +		case 'html':
    2.62 +			return '\n<img alt="' + alt + '" src="' + src + '" />\n';
    2.63 +		case 'rst':
    2.64 +			return '\n.. image:: ' + src + '\n  :alt: ' + alt + '\n';
    2.65 +		default:
    2.66 +			return '\n![' + alt + '](' + src + ')\n';
    2.67 +			break;
    2.68 +		}
    2.69  	}
    2.70  };
    2.71  
     3.1 --- a/templates/admin/postimage/base.html	Sat Oct 18 21:57:33 2008 +0400
     3.2 +++ b/templates/admin/postimage/base.html	Sun Oct 19 01:02:59 2008 +0400
     3.3 @@ -3,7 +3,9 @@
     3.4  <head>
     3.5  <title>{% block title %}{% endblock %}</title>
     3.6  <link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% load adminmedia %}{% admin_media_prefix %}css/forms.css{% endblock %}" />
     3.7 +<script type="text/javascript" src="{% admin_media_prefix %}js/core.js"/></script>
     3.8  <script type="text/javascript" src="{{ media_url }}js/postimage.js"></script>
     3.9 +<script type="text/javascript">Postimage.setMarkup('{{ settings.RENDER_METHOD }}');</script>
    3.10  {% if LANGUAGE_BIDI %}<link rel="stylesheet" type="text/css" href="{% block stylesheet_rtl %}{% admin_media_prefix %}css/rtl.css{% endblock %}" />{% endif %}
    3.11  {% block blockbots %}<meta name="robots" content="NONE,NOARCHIVE" />{% endblock %}
    3.12  </head>