博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Drupal 7 Syntax Highlighting with WYSIWYG and TinyMCE
阅读量:5335 次
发布时间:2019-06-15

本文共 6741 字,大约阅读时间需要 22 分钟。

原文:

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 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 context)
{
    
databaseOpenHelper =
new
TranslationOpenHelper(context);
}

 

Prerequisites

You should 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 .

 

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: 

Project Home and Installation Instructions: 

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).

 WYSIWYG - Module Install

 

2. Syntax Highlighter Module

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

Download Link: 

Project Home: 

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 languages like the image below, check the items which you will be using.
  • Save the modified configuration.

 SyntaxHighlighter configuration settings

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: 

Project Home: 

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 included a copy of the current working build from Git. You should check the project site for an update before using my copy.

Download Link: 

Project Home: 

  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: 

Project Home: 

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
    WYSIWYG Content Authoring setup
  • Click Save, you will then see an Edit link appear in the operations section, click on this link to bring up the Edit Window.WYSIWYG Content Authoring - Edit
  • There are many 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).
    SyntaxHighlighter configuration button settings
  • In the Cleanup and Output section make sure only the following options are checked:
    • Verify HTML
    • Convertn <font> tags to styles
    • Remove linebreaks
      SyntaxHighlighter configuration cleanup settings
  • Save the configuration changes.
  • Browse to Administration >> Configuration >> Text Formats.
  • Click on the Configure link next to the Full HTML item.
  • In the Enabled Filters section 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 HTMLText Format Setup
  • 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 should see the Body input change and now buttons should be displayed (see pictured).
    Using editor
  • 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.
      Using the editor - part 2
  • Click Insert SyntaxHighlighter Tag to insert the highlighting block.
    Using the editor - part 3
  • Paste your code into the block. Note: It will not be highlighted at this point and may look a little ugly.
    Using the editor - part 4
  • Save and view the page to see the results!

 

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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 context)
    
{
        
databaseOpenHelper =
new
TranslationOpenHelper(context);
    
}

 

转载于:https://www.cnblogs.com/wangkangluo1/archive/2012/11/30/2796458.html

你可能感兴趣的文章
创新课程管理系统数据库设计心得
查看>>
管道,数据共享,进程池
查看>>
SDUTOJ3754_黑白棋(纯模拟)
查看>>
把word文档中的所有图片导出
查看>>
ubuntu 18.04取消自动锁屏以及设置键盘快捷锁屏
查看>>
arcgis api 4.x for js 结合 Echarts4 实现散点图效果(附源码下载)
查看>>
YTU 2625: B 构造函数和析构函数
查看>>
加固linux
查看>>
Hyper-V虚拟机上安装一个图形界面的Linux系统
查看>>
【Crash Course Psychology】2. Research & Experimentation笔记
查看>>
关于 linux 的 limit 的设置
查看>>
MTK笔记
查看>>
【题解】 bzoj1597: [Usaco2008 Mar]土地购买 (动态规划+斜率优化)
查看>>
fat32转ntfs ,Win7系统提示对于目标文件系统文件过大解决教程
查看>>
shell cat 合并文件,合并数据库sql文件
查看>>
构建自己的项目管理方案
查看>>
安装Endnote X6,但Word插件显示的总是Endnote Web"解决办法
查看>>
python全栈 计算机硬件管理 —— 硬件
查看>>
Delphi7编译的程序自动中Win32.Induc.a病毒的解决办法
查看>>
各种正则验证
查看>>