Google Prettify Code example
Code-highlighting with google prettify.
For introduction visit another blog:Code Highlighting using Google Prettify
Here is an example web page with codehilighter
The below code shows how the HTML script looks, and how to add it to code highlighter.
For various scripts, there are different syntax available. For embedding it in highlighter.
Download the file and open it in the browser and check each script like java, python,
JavaScript etc. are added.
You can find the code for how to add line numbers.
The theme used in this code is desert.
Code download
index.php
Example:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>e-Trainings Demo on google prettify</title> <script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js?lang=css&skin=desert"></script> <style> .prettyprint ol.linenums > li { list-style-type: decimal; } </style> </head> <body> <h1>Using Google Prettify </h1> <h2>To show the code first you convert your code into html encoded form and place in pre tag as follows </h2> <h2>Adding HTML Code</h2> <pre class="prettyprint lang-markup"> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <title>Example HTML Page</title> </head> <body> <pre><code class="language-css">body { font: 100% Helvetica, sans-serif; color: #333; } </pre> <div> <img src="example.png"/> </div> </body> </html> </pre> <h2>You can also write as follows: using code tags </h2> <p> adding img tag </p> <pre class="prettyprint"> <code> <div> <img src="example.png"/> </div> </code> </pre> </pre> <h2>Adding line numbers </h2> <p> For line numbers add the style given in head section </p> <pre class="prettyprint linenums"> foo(); bar(); baz(); boo(); far(); faz(); </pre> <pre class="prettyprint linenums:3"> // This is line 3. foo bar bis sed awk cat </pre> <h2>For any other script, use the following tags example PHP</h2> <pre class="prettyprint">…</pre> <pre class="prettyprint"> <?php $password = 'abcdefg'; $salt = 'anythingyouwant_'; $pw_hash = md5($salt.$password); ///example2 $password = "myPassword"; $salt = sha1(md5($password)).'k32duem01vZsQ2lB8g0s'; $password = md5($password.$salt); ?> </pre> <h2>Java Example</h2> <pre class="prettyprint lang-java"> // Your First Program class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } </pre> <h2>Python Example</h2> <pre class="prettyprint lang-py"> // Your First Program # This program prints Hello, world! print('Hello, world!') </pre> <h2>JavaScript Example</h2> <pre class="prettyprint lang-js"> <script> // the hello world program document.write('Hello, World!'); </script> </pre> <a href="https://github.com/googlearchive/code-prettify">Source</a> </body> </html>
Sample Output:
Comments
Post a Comment