Tutorial Details
- Topic - Syntax Highlighting
- Difficulty - Easy
- Format - Screencast
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 theprettify.css
and prettify.js
files. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <!DOCTYPE html> < html lang = "en" > < head > < meta charset = "utf—8" > < title >untitled</ title > < link rel = "stylesheet" href = "highlighter/prettify.css" /> </ head > < body > < script src = "highlighter/prettify.js" ></ script > </ body > </ html > |
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 2 3 4 5 | < pre class = "prettyprint" > (function() { var jsSyntaxHighlighting = 'rocks'; })(); </ 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <!DOCTYPE html> < html lang = "en" > < head > < meta charset = "utf-8" > < title >untitled</ title > < link rel = "stylesheet" href = "highlighter/prettify.css" /> </ head > < body > < pre class = "prettyprint" > (function() { var jsSyntaxHighlighting = 'rocks'; })(); </ pre > < script src = "highlighter/prettify.js" ></ script > < script >prettyPrint();</ script > </ body > </ html > |
head
section of your document. 1 2 3 4 5 | < head > < meta charset = "utf—8" > < title >untitled</ title > < link rel = "stylesheet" href = "highlighter/dessert.css" /> </ head > |
0 comments:
Post a Comment