Permalink
Browse files

Added an XML Review UI extension.

This provides a basic review UI and thumbnail renderer for XML files.
It's intended partially as an example for writing an extension for
writing review UIs.

Reviewed at http://reviews.reviewboard.org/r/3564/
  • Loading branch information...
chipx86 committed Dec 4, 2012
1 parent f6e0ab4 commit 31b329271cad9ab203d58c1ed5a38397a13f4fde
@@ -0,0 +1,8 @@
+from django.conf.urls.defaults import patterns, url
+
+from rbxmlreview.extension import XMLReviewUIExtension
+
+
+urlpatterns = patterns('rbxmlreview.views',
+ url(r'^$', 'configure'),
+)
@@ -0,0 +1,15 @@
+# rbxmlreview Extension for Review Board.
+from mimetypes import XMLMimetype
+from reviewboard.extensions.base import Extension
+from reviewboard.extensions.hooks import ReviewUIHook, \
+ FileAttachmentThumbnailHook, \
+ URLHook
+
+from rbxmlreview import XMLReviewUI
+
+
+class XMLReviewUIExtension(Extension):
+ def __init__(self, *args, **kwargs):
+ super(XMLReviewUIExtension, self).__init__(*args, **kwargs)
+ self.reviewui_hook = ReviewUIHook(self, [XMLReviewUI])
+ self.thumbnail_hook = FileAttachmentThumbnailHook(self, [XMLMimetype])
@@ -0,0 +1,15 @@
+/*
+ * Displays a review UI for XML files.
+ */
+RB.XMLReviewableView = RB.FileAttachmentReviewableView.extend({
+ className: 'xml-review-ui',
+
+ /*
+ * Renders the view.
+ */
+ renderContent: function() {
+ this.$el.html(this.model.get('rendered'));
+
+ return this;
+ }
+});
@@ -0,0 +1,16 @@
+from django.utils.encoding import force_unicode
+import pygments
+
+from reviewboard.attachments.mimetypes import TextMimetype
+
+
+class XMLMimetype(TextMimetype):
+ """This handles XML (.xml) mimetypes."""
+ supported_mimetypes = ['application/xml', 'text/xml']
+
+ def _generate_preview_html(self, data_string):
+ """Returns syntax-highlighted XML."""
+ return pygments.highlight(
+ force_unicode(data_string),
+ pygments.lexers.XmlLexer(),
+ pygments.formatters.HtmlFormatter())
@@ -0,0 +1,30 @@
+import logging
+
+from django.utils.encoding import force_unicode
+from reviewboard.reviews.ui.base import FileAttachmentReviewUI
+import pygments
+
+
+class XMLReviewUI(FileAttachmentReviewUI):
+ """This is a ReviewUI for XML mimetypes."""
+ supported_mimetypes = ['application/xml', 'text/xml']
+ template_name = 'rbxmlreview/xml.html'
+ object_key = 'xml'
+
+ def render(self):
+ """Returns syntax-highlighted XML as HTML."""
+ data_string = ""
+ f = self.obj.file
+
+ try:
+ f.open()
+ data_string = f.read()
+ except (ValueError, IOError), e:
+ logging.error('Failed to read from file %s: %s' % (self.obj.pk, e))
+
+ f.close()
+
+ return pygments.highlight(
+ force_unicode(data_string),
+ pygments.lexers.XmlLexer(),
+ pygments.formatters.HtmlFormatter())
@@ -0,0 +1,30 @@
+{% extends base_template %}
+{% load i18n %}
+{% load reviewtags %}
+{% load staticfiles %}
+
+{% block title %}{{xml.filename}}{% if caption %}: {{caption}}{% endif %}{% endblock %}
+
+{% block scripts-post %}
+{{block.super}}
+<script language="javascript" src="{{MEDIA_URL}}ext/xml-review-ui-extension/js/xmlReviewableView.js"></script>
+
+<script language="javascript">
+ $(document).ready(function() {
+ var view = new RB.XMLReviewableView({
+ model: new RB.FileAttachmentReviewable({
+ attachmentID: '{{xml.id}}',
+ caption: '{{caption|escapejs}}',
+ rendered: '{{review_ui.render|escapejs}}'
+ })
+ });
+
+ view.render();
+ $('#xml-review-ui-container').append(view.$el);
+ });
+</script>
+{% endblock %}
+
+{% block review_ui_content %}
+<div id="xml-review-ui-container"></div>
+{% endblock %}
View
@@ -0,0 +1,27 @@
+from setuptools import setup
+
+
+PACKAGE = "rbxmlreview"
+VERSION = "0.1"
+
+setup(
+ name=PACKAGE,
+ version=VERSION,
+ description="Review Board extension for XML review UI and "
+ "thumbnail support",
+ author="Sampson Chen",
+ packages=["rbxmlreview"],
+ entry_points={
+ 'reviewboard.extensions':
+ '%s = rbxmlreview.extension:XMLReviewUIExtension'
+ % PACKAGE,
+ },
+ package_data={
+ 'rbxmlreview': [
+ 'htdocs/css/*.css',
+ 'htdocs/js/*.js',
+ 'templates/rbxmlreview/*.txt',
+ 'templates/rbxmlreview/*.html',
+ ],
+ }
+)

0 comments on commit 31b3292

Please sign in to comment.