How to Add Syntax Highlighting to Any Project

Sep 28, 2013, by admin

Hi all today we are going to see How to Add Syntax Highlighting to Any Project hope you all like the page let us see the steps given below

Step 1 — Download the Source Code

You can download the syntax highlighter source files here.


Step 2 — Drag the src Directory into your Project

I generally rename this folder to highlighter. Don’t delete anything within here, unless you don’t anticipate using some of the language specific JavaScript files.


Step 3 — Import the Necessary Files

Within your HTML file (or whichever page is responsible for displaying your view), import both the prettify.css and prettify.js files.

  1. <!DOCTYPE html>
  2. <html lang=”en”>
  3. <head>
  4.    <meta charset=”utf&mdash;8″>
  5.    <title>untitled</title>
  6.    <link rel=”stylesheet” href=”highlighter/prettify.css” />
  7. </head>
  8. <body>

11.<script src=”highlighter/prettify.js”></script>

13.</body>

14.</html>

Notice how we’ve placed our script at the bottom of the page, just before the closing body tag. This is always a smart move, as it improves performance.

Next, we need something to work with! The syntax highlighter will search for either a pre or code element that has a class of prettyprint. Let’s add that now.

  1. <pre class=”prettyprint”>
  2. (function() {
  3.    var jsSyntaxHighlighting = ‘rocks’;
  4. })();
  5. </pre>

Step 4 — Calling the prettyPrint() Function

The last step is to execute the prettyPrint() function. We can place this bit of code at the bottom of the page as well.

  1. <!DOCTYPE html>
  2. <html lang=”en”>
  3. <head>
  4.    <meta charset=”utf-8″>
  5.    <title>untitled</title>
  6.    <link rel=”stylesheet” href=”highlighter/prettify.css” />
  7. </head>
  8. <body>

11.<pre>

12.(function() {

  1.  var jsSyntaxHighlighting = ‘rocks’;

14.})();

15.</pre>

16.<script src=”highlighter/prettify.js”></script>

17.<script>prettyPrint();</script>

18.</body>

19.</html>

If we now view the page in the browser…

To get more updates like the page Bugtreat Technologies and all a happy weekend