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&amp;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">
	
	&lt;!doctype html&gt;
	&lt;html lang=&quot;en&quot;&gt;
	
	&lt;head&gt;
	&lt;meta charset=&quot;utf-8&quot;&gt;
	&lt;meta http-equiv=&quot;x-ua-compatible&quot; content=&quot;ie=edge&quot;&gt;
	&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
	
	&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js&quot;&gt;&lt;/script&gt;
	&lt;title&gt;Example HTML Page&lt;/title&gt;
	
	&lt;/head&gt;
	
	&lt;body&gt;
	
	&lt;pre&gt;&lt;code class=&quot;language-css&quot;&gt;body {
	font: 100% Helvetica, sans-serif;
	color: #333;
	}
	
	&lt;/pre&gt;
	
	&lt;div&gt;
	&lt;img src="example.png"/&gt;
	&lt;/div&gt;
	
	&lt;/body&gt;
	
	&lt;/html&gt;
	
</pre>

<h2>You can also write as follows: using code tags </h2>
<p> adding img tag </p>
<pre class="prettyprint">
<code>
&lt;div&gt;
&lt;img src="example.png"/&gt;
&lt;/div&gt;
</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">

&lt;?php
$password = 'abcdefg';
$salt = 'anythingyouwant_';
$pw_hash = md5($salt.$password);

///example2
$password = &quot;myPassword&quot;;
$salt = sha1(md5($password)).'k32duem01vZsQ2lB8g0s'; 
$password = md5($password.$salt);


?&gt;

</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">

&lt;script&gt;
// the hello world program
document.write('Hello, World!');
&lt;/script&gt;
</pre>

<a href="https://github.com/googlearchive/code-prettify">Source</a>


</body>

</html>
 
 
 
 Sample Output:

 
 
sample-output-google-prettify

 
 
 
 
 
 
 
 
 
sample-output-google-prettify2

 
 
 
 
 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP