Drupal 7 syntax highlighting with WYSIWYG and tinymce

Source: Internet
Author: User
Tags drupal

Original article: http://stuartmcgoldrick.com/drupalwysiwyg

Introduction

This article is designed to help you get syntax highlighting working correctly with your Drupal 7 installation and using a WYSIWYG editor. the outcome of this tutorial is to be able to insert blocks of code into Drupal articles/pages like the block below through a WYSIWYG editor (tinymce in this case ).

?
Example
123456789101112131415 // Database variables Private Sqlitedatabase database; Private Translationopenhelper databaseopenhelper; // Define a static array containing the columns names Private String [] columnarray = {translationopenhelper. table_column_id, translationopenhelper. table_column_word, translationopenhelper. table_column_translation }; /**   * Initialise the object (note this does not open the database)   * @ Param Context   */ Public Translationdatasource (context) { Databaseopenhelper = New Translationopenhelper (context ); }

 

Prerequisites

You shoshould have a firm grasp of Drupal and how to install modules and libraries. If you are unfamiliar with these aspects I recommend reading the Drupal documentation at drupal.org.

 

Installation instructions

You will need a handful of Drupal modules and libraries to get syntax highlighting working correctly, unfortunately its not a straightforward process, but this article will guide you through.

These are (as of 19/08/2012 ):

 

1. Drupal WYSIWYG (what you see is what you get) module v7.x

This is the module that will allow us to use a WYSIWYG editor to create articles. Note: This is only an API module,You have to download the WYSIWYG editor separately (which is tinymce in this tutorial ).

Download link:Http://ftp.drupal.org/files/projects/wysiwyg-7.x-2.1.zip

Project home and installation instructions:Http://drupal.org/project/wysiwyg

Installation instructions:

    • Extract all files from the archive into the module folder on your site.
    • Enable the module through the module administation section of your Drupal instance by selecting the enabled heckbox (pictured below ).

 

 

2. Syntax highlighter Module

This is the module that will do the syntax highlighting on our pages.

Download link:Http://ftp.drupal.org/files/projects/syntaxhighlighter-7.x-2.0.zip

Project home:Http://drupal.org/project/syntaxhighlighter

Installation instructions:

    • Extract into your Module Directory on your site, then enable from the administation console.
    • After you have saved your changes and the module is enabled, click on the configure link next to the item.
    • You will see a list of programming versions like the image below, check the items which you will be using.
    • Save the modified configuration.

 

3. syntaxhighlighter insert:

This module adds a handy insert syntaxhighlighter tag button into the WYSIWYG editor to streamline the process of embedding code into an article or page.

Download link:Http://ftp.drupal.org/files/projects/syntaxhighlighter_insert-7.x-1.0.zip

Project home:Http://drupal.org/project/syntaxhighlighter_insert

Installation instructions:

    • Extract into your Module Directory on your site, then enable from the administation console. Note that there will be 3 items to enable.
    • We will integrate this module with tinymce in a later step.

 

4. Wysiwyg pre element fix

The WYSIWYG editor currently has some issues with <PRE> elements which causes the syntax highlighter not to work. so to get around this we need to use this module. as of writing, the Drupal 7 compatible version is still being developed, so I 've got ded a copy of the current Working build from git. you shoshould check the project site for an update before using my copy.

Download link:Http://stuartmcgoldrick.com/downloads/wysiwyg_preelementfix.zip

Project home:Http://drupal.org/project/wysiwyg_preelementfix

Installation instructions:

    • Extract into your Module Directory on your site, then enable from the administation console.

5. tinymce v3.5.6

Tinymce will be the WYSIWYG editor used to create articles and pages with rich content (and it is free ).

Download link: Http://github.com/downloads/tinymce/tinymce/tinymce_3.5.6.zip

Project home:Http://www.tinymce.com/

Installation instructions:

  • Unpack tinymce folder into your sites/All/libraries directory.
  • Go to your administration> Configuration> WYSIWYG profiles section
  • For the full HTML item, change the editor to tiny MCE

  • Click Save, you will then see an edit link appear in the Operations Section, click on this link to bring up the edit window.
  • There are available options here you can play around with but the first important item to update is the list of buttons and Plugins. this will define what buttons will be available for use in your editor. I recommend selecting almost all of the items in plain black text as a base. if you have installed the syntaxhighlighter insert module correctly you will also have an item called insert syntaxhighlighter tag at the bottom of the list, make sure this is checked (see picture below ).
  • In the cleanup and output section make sure only the following options are checked:
      • Verify html
      • Convertn <font> tags to styles
      • Remove linebreaks
  • Save the configuration changes.
  • BrowseAdministration> Configuration> text formats.
  • Click onConfigureLink next to the full HTML item.
  • InEnabled FiltersSection make sure only the following options are checked (as pictured below ):
      • Convert URLs into links
      • Convert line breaks into HTML
      • Syntax highlighter -- if this is not showing then something is wrong with your installation.
      • Correct fault and chopped off html
  • Check that the filter processing order is as pictured above, this is important otherwise the syntax highlighting will be overridden by normal formatting
      • Convert URLs into links
      • Syntax highlighter
      • Convert line breaks into HTML
      • Correct fault and chopped off html
  • Save your configuration

 

Using the WYSIWYG editor and syntax highlighting
  • Now that you are setup and ready to go, all you need to do is open up a new or existing article/Page an existing page.
  • Under the body element is a drop down list called text format, change it to full HTML (if not already set ). you shoshould see the body input change and now buttons shoshould be displayed (see pictured ).
  • The icon on the far right of the tool bar is the "insert syntaxhighlighter" button, click it to view the insert page (pictured below ).
  • This page allows you to set various options but the main ones are:
      • Title:A title for your code block
      • Brush:Defines the type of syntax highlighting, ie Java, HTML etc.
  • Click Insert syntaxhighlighter tag to insert the highlighting block.
  • Paste your code into the block.Note: It will not be highlighted at this point and may look a little uugly.
  • Save and view the page to see the results!

 

?
Example
123456789101112131415 <Br Class = "Apple-interchange-newline" > // Database variables Private Sqlitedatabase database; Private Translationopenhelper databaseopenhelper; // Define a static array containing the columns names Private String [] columnarray = {translationopenhelper. table_column_id, translationopenhelper. table_column_word, translationopenhelper. table_column_translation }; /** * Initialise the object (note this does not open the database) * @ Param Context */ Public Translationdatasource (context) { Databaseopenhelper = New Translationopenhelper (context ); }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.