|
This article explains the general structure of a script for posting code on your Web page(s) in HTML format.
By instantiating a code formatting sub-class object for a particular programming language,
you will be well on your way to providing your Web site visitors with a very legible means of interpreting code. This article
focuses on the PHP language for code formatting, since it was developed initially for PHP script code
snippets used in the majority of the articles on this Web site. You may define your own extended classes
by following the procedure for the extended PHP format class. Replace delimiters and keywords with those
specific to a particular language. This Web site uses extended code formatter classes for PHP, HTML and JavaScript
languages.
Originally, this article was intended to illustrate a very basic method of formatting code, but
the overall efficiency and versatility of this generic script resulted in expansive code. The indexing method
was necessary for the handling of essential conditions in order to avoid processing them during the actual
parsing, which would increase the complexity and reduce performance. Conditional handling during indexing has eliminated
several levels of nested loops. This script surpasses decent time for generated code
spanning at least several pages. Code snippets are usually broken down into multiple separate pages
for the purposes of performance, and so as not to overwhelm the audience with code.
* Remarks: Your PHP script will generally require two statements when formatting an external code snippet
text file on a Web page. One is to instantiate the language specific class with the parameters being the full
path and filename, a flag for enabling word-wrap when no fixed width is required, and the number of spaces to be
used for indentation of each new line of code, which replaces ASCII text spaces with HTML encoded spaces.
The second statement retrieves the formatted code as a string, which can be output or further manipulated.
Use of the code formatter class in your PHP Web page would require this fragment within the page:
|
1
2
3
4
5
6
7
8
|
<?
include ("codeview.php");
$fmtcode = new format_php_code ("a1.txt", true, 2);
echo $fmtcode->format_getcode ();
?>
Code View - dynamicreport.com
|
|
Use of the code formatter class for all subsequent code snippets in your PHP Web page would require this
fragment within the page (after the fragment above containing the class object instantiation):
The following snippet is an example demonstrating the use of Code View, and results from the use of
the PHP format extended class. Note that selections of code may be highlighted to distinguish distinct aspects
of source code.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?
define ("INDENT_CODE", " ");
$INDENT_WS = "";
class format_code
{ // generic code parser and format styles injection
function format_code ($enableWrap, $indentSize)
{ // indent the beginning of each line only
$this->enableWrap = $enableWrap;
global $INDENT_WS;
$INDENT_WS = "";
for ($i = 0; $i < $indentSize; $i++)
$INDENT_WS .= INDENT_CODE;
}
}
?>
Code View - dynamicreport.com
|
|
The following snippet is an example demonstrating the use of Code View, and results from the use of
the HTML format extended class. Note that the styles and colors are completely modifiable.
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>code view example</title>
<meta name="keywords" content="code,view">
<!--
meta info
-->
<script src = "funcs.js" type="text/javascript">
</script>
</head>
<body onload = 'javascript: init ();'>
<!-- layout marker: begin -->
<table border = 0 cellspacing = 5 width = '90%'>
<tr>
<td align = "left" background = "left.jpg">
<img src = 'images/left.jpg'></td>
</tr>
<tr>
<td>
<p>Menu options</p>
Option 1 <br>
<form method = 'post' action = 'cgi/f.cgi'>
<input id = 'val1' name = 'val1' type = 'text'>
<select id = 'sel1' name = 'sel1'>
<option value = '1'>One & ></option>
</select> <!-- link -->
<a href = '#' onclick = 'javascript: v();'>
<img src = 'images/icon1.gif'> 1
</a>
</form>
</td>
</tr>
</table>
</body>
</html>
Code View - dynamicreport.com
|
Features include extended class support, the ability to highlight code on-the-fly, and
parameters to pass code to the formatter via text strings (buffers) in addition to file imports. Extensive documentation
is included in the Code View archive.
Click here for an example page included in the archive.
|