annotate console/lib/highlight/README.md @ 109:cd1f57661439 default tip

Fix files permissions.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 01 Aug 2012 23:18:03 +0200
parents 5cb4733c5189
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
1 # Highlight.js
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
2
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
3 Highlight.js highlights syntax in code examples on blogs, forums and,
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
4 in fact, on any web page. It's very easy to use because it works
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
5 automatically: finds blocks of code, detects a language, highlights it.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
6
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
7 Autodetection can be fine tuned when it fails by itself (see "Heuristics").
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
8
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
9
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
10 ## Basic usage
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
11
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
12 Link the library and a stylesheet from your page and hook highlighting to
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
13 the page load event:
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
14
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
15 ```html
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
16 <link rel="stylesheet" href="styles/default.css">
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
17 <script src="highlight.pack.js"></script>
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
18 <script>hljs.initHighlightingOnLoad();</script>
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
19 ```
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
20
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
21 This will highlight all code on the page marked up as `<pre><code> .. </code></pre>`.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
22 If you use different markup or need to apply highlighting dynamically, read
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
23 "Custom initialization" below.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
24
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
25 - You can download your own customized version of "highlight.pack.js" or
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
26 use the hosted one as described on the download page:
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
27 <http://softwaremaniacs.org/soft/highlight/en/download/>
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
28
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
29 - Style themes are available in the download package or as hosted files.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
30 To create a custom style for your site see the class reference in the file
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
31 [classref.txt][cr] from the downloaded package.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
32
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
33 [cr]: http://github.com/isagalaev/highlight.js/blob/master/classref.txt
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
34
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
35
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
36 ## Tab replacement
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
37
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
38 You can replace TAB ('\x09') characters used for indentation in your code
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
39 with some fixed number of spaces or with a `<span>` to give them special
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
40 styling:
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
41
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
42 ```html
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
43 <script type="text/javascript">
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
44 hljs.tabReplace = ' '; // 4 spaces
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
45 // ... or
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
46 hljs.tabReplace = '<span class="indent">\t</span>';
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
47
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
48 hljs.initHighlightingOnLoad();
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
49 </script>
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
50 ```
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
51
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
52 ## Custom initialization
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
53
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
54 If you use different markup for code blocks you can initialize them manually
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
55 with `highlightBlock(code, tabReplace, useBR)` function. It takes a DOM element
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
56 containing the code to highlight and optionally a string with which to replace
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
57 TAB characters.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
58
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
59 Initialization using, for example, jQuery might look like this:
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
60
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
61 ```javascript
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
62 $(document).ready(function() {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
63 $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
64 });
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
65 ```
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
66
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
67 You can use `highlightBlock` to highlight blocks dynamically inserted into
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
68 the page. Just make sure you don't do it twice for already highlighted
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
69 blocks.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
70
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
71 If your code container relies on `<br>` tags instead of line breaks (i.e. if
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
72 it's not `<pre>`) pass `true` into the third parameter of `highlightBlock`
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
73 to make highlight.js use `<br>` in the output:
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
74
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
75 ```javascript
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
76 $('div.code').each(function(i, e) {hljs.highlightBlock(e, null, true)});
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
77 ```
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
78
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
79
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
80 ## Heuristics
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
81
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
82 Autodetection of a code's language is done using a simple heuristic:
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
83 the program tries to highlight a fragment with all available languages and
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
84 counts all syntactic structures that it finds along the way. The language
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
85 with greatest count wins.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
86
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
87 This means that in short fragments the probability of an error is high
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
88 (and it really happens sometimes). In this cases you can set the fragment's
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
89 language explicitly by assigning a class to the `<code>` element:
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
90
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
91 ```html
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
92 <pre><code class="html">...</code></pre>
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
93 ```
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
94
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
95 You can use class names recommended in HTML5: "language-html",
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
96 "language-php". Classes also can be assigned to the `<pre>` element.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
97
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
98 To disable highlighting of a fragment altogether use "no-highlight" class:
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
99
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
100 ```html
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
101 <pre><code class="no-highlight">...</code></pre>
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
102 ```
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
103
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
104
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
105 ## Export
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
106
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
107 File export.html contains a little program that allows you to paste in a code
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
108 snippet and then copy and paste the resulting HTML code generated by the
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
109 highlighter. This is useful in situations when you can't use the script itself
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
110 on a site.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
111
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
112
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
113 ## Meta
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
114
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
115 - Version: 7.0
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
116 - URL: http://softwaremaniacs.org/soft/highlight/en/
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
117 - Author: Ivan Sagalaev (<maniac@softwaremaniacs.org>)
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
118
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
119 For the license terms see LICENSE files.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
120 For the list of contributors see AUTHORS.en.txt file.