django_mce_pygments
Description
Django app and tinymce extension that allows you to insert syntax highlighted sourcecode. It is configured to work by default with a Mezzanine installation. Syntax highlighting is provided by pygments. You can adjust line numbering, highlighted ranges, styles and every language your version of pygments supports is selectable. You can optionally select to have a link to download the unformatted source code inserted, in which case a text file containing the source is saved to your server. The default paths for tinymce and jquery are designed for a mezzanine installation, but can be customized in settings.py.
This project is based on https://github.com/fruitschen/django_mce_pygments
Screenshots
The pygments icon in tinymce:
The dialog:
The preview tab:
The inserted source with download link:
Requirements
it is assumed you are using django.contrib.staticfiles to manage static media
File path defaults assume a Mezzanine installation but this can be easily overridden in your settings.py
INSTALLATION
Place the app on your python path
Add django_mce_pygments to your INSTALLED APPS
Check that the paths below for jquery and tinymce are correct, and override
in your settings file if neccessary
Make sure the SOURCE_CODE_FILE_DIR
exists and is writable
Find your tinymce_setup.js file and add pygments to the plugins line, here is an example:
plugins: "inlinepopups,contextmenu,tabfocus,searchreplace,fullscreen,advimage,advlink,paste,media,table,pygments,spellchecker"
Also add it to your theme, here is an example:
theme_advanced_buttons1: "bold,italic,|,link,unlink,|,image,|,media,charmap,|,code,|,table,|,bullist,numlist,blockquote,|,undo,redo,|,formatselect,|,search,replace,|,pygments,|,fullscreen,",
If you are using mezzanine and static files, the plugin should be available by default. If it does not load, copy the django_mce_pygments/static/grappelli/tinymce/jscripts/tiny_mce/plugins/pygments directory into your tinymce plugins directory.
Finally add the pygments urls to your urls.py. In a mezzanine installation, make sure this is before the mezzanine.urls include, as this includes a catchall. Example:
("^pygments/", include("django_mce_pygments.urls")),
("^", include("mezzanine.urls")),
If you are running Mezzanine, it automatically strips out inline styles which breaks pygments source highlighting, you can turn this off in your settings.py file as follows:
RICHTEXT_FILTER_LEVEL = 3
Configuration
The following can be set in your settings.py. All are optional, but the first two probably need adjustment if you are not using Mezzanine.
MCE_POPUP_LOCATION
- The location of your tiny_mce_popup.js file, defaults to 'grappelli/tinymce/jscripts/tiny_mce/tiny_mce_popup.js'. This assumes you are using static files, so this url will be passed through the static template tag, and probably resolve to /staticgrappelli/tinymce/jscripts/tiny_mce/tiny_mce_popup.js.
JQUERY_LOCATION
- The location of your jquery library. Defaults to 'mezzanine/js/jquery-1.7.1.min.js'. This assumes you are using static files, so this url will be passed through the static template tag, and probably resolve to /static/mezzanine/js/jquery-1.7.1.min.js
MCE_DEFAULT_LEXERS
- Languages in this list will be the first in the dropdown in the pygments dialog to save you time hunting. Defaults to an empty list, an example would be:
MCE_DEFAULT_LEXERS = ['Python', 'Javascript']
MCE_DEFAULT_STYLE
- The default style to have selected in the pygments dialog. This defaults to None. An example of its use:
MCE_DEFAULT_STYLE = 'pastie'
SOURCE_CODE_FILE_DIR
- The location to store raw source code files used for lines. The default is static/source, make sure it exists and is writable.
The template pygments/source_link.html can be overridden or customized to change how the download link for source code is presented.