Custom Format Programming Language Code

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. You may define your own extended classes by following the procedure for the extended PHP or HTML format classes. Replace delimiters and keywords with those specific to a particular language. The PHP extended code formatter class may be used for C, C++ and JavaScript.

* 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 to a Web page or further manipulated.

Use of the code formatter class in your PHP Web page will require this fragment within the page:

<?

include ("codeview.php");

$fmtcode = new format_php_code ("snippets/a01.txt", WRAP_YES, 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):

<?

$fmtcode->format_init ("snippets/a02.txt");
echo $fmtcode->format_getcode ();

?>

Code View - dynamicreport.com

The codeview.php source code contains adjustable properties before the definition of the class object. The following may be modified to adjust the system administrator's email address and message incase of a non-existent file error. The number of spaces to be used for indentation while replacing existing tabs or spaces may be adjusted by providing the HTML equivalent space code ( ) 1 or more times within the string.

define ("FILE_NOTFOUND", 
"<p align = \"center\">Code snippet is currently unavailable.".
"Contact the system administrator: ".
"<a href = \"mailto:support@mydomain.com\">".
"support@mydomain.com</a></p>");

define ("INDENT_CODE", "&nbsp;");
$INDENT_WS = "";

Code View - dynamicreport.com

The following PHP format extended class may be modified, or a copy created with a different class name and modified parameters in order to define another extended language class (non-markup). Modify or create a copy of the HTML extended class to define a markup language class such as HTML, XML, etc.

// language-specific (PHP-related) code delimiters
// call format_init (file) to format another external
// snippet of code. obtain result with format_getcode ()
class format_php_code extends format_code
{
  function format_php_code ($sParam = NULL, $paramWrap, $indentSize, $bFile = true, $bInit = true)
  {
    // case sensitivity: true = insensitive, false = sensitive
    $case = false;
    // auto-correct case: true = defined keyword/delimiter syntax case, false = original code syntax
    $autoCase = false;
    // call child class constructor to initialize generic code formatter class
    $this->format_code ($paramWrap, $indentSize, $case, $autoCase);
    // mark-up language start/end delimiter indices into common delimiters (-1 disables mark-up)
    $this->iMarkupS = $this->iMarkupE = -1;
    // common ending delimiters for single non-nested keywords 
    // - most common 1st for efficiency
    // semi-colon interferes with html escaped characters, 
    // hence the custom encoded semi-colon
    $this->sDelimitC = array ( "|sc|", ")", "-", "[", "&amp;",
    ".", "=", "(",  "]", ",", "+", "*", "/", "%", "|", "&gt;", "&lt;",
    "^", "?", "{", "}", "!", ":" );
    // common style property start tag
    $this->sPropertyCS = "<span style=\"color:#0000CD\">";
    // common style property end tag
    $this->sPropertyCE = "</span>";

    // starting and ending delimiters for whole nested fragments and keywords 
    // or symbols -- order of definition is important as a '<' before a '<?' will result in 
    // only the '<' being formatted in a '<?' fragment
    // PARAMETER ORDER: format_add_def ($start, $arrEnd, $escIgnore, $sPropS, $sPropE, $incProp)
    // $start = opening delimiter, $arrEnd = ending delimiter can be array (optional 0+ elements), 
    // $escIgnore = escaped delimiter to ignore style application, $sPropS = HTML style property start tag, 
    // $sPropE = HTML style property end tag, $incProp = flag for ending delimiter style inclusion
    // $dType = OPTIONAL: data type character that must follow delimiter (TYPE_VAR variable name or TYPE_ANY default)
    $this->format_add_def ("&#039;", array ("&#039;"), "\\", "<span style=\"color:#BC0000\">", "</span>", 1);
    $this->format_add_def ("&quot;", array ("&quot;"), "\\", "<span style=\"color:#BC0000\">", "</span>", 1);
    $this->format_add_def ("//", array ("\n"), "", "<span style=\"color:#FF7000\">", "</span>", 1);
    $this->format_add_def ("#", array ("\n"), "", "<span style=\"color:#FF7000\">", "</span>", 1);
    $this->format_add_def ("/*", array ("*/"), "", "<span style=\"color:#FF7000\">", "</span>", 1);
    $this->format_add_def ("$", "", "", "<span style=\"color:#0060CD\">", "</span>", 1);
    $this->format_add_def ("array", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("&lt;?", "", "", "<span style=\"color:#FF3300\"><b>", "</b></span>", 0);
    $this->format_add_def ("&lt;", "", "", "<span style=\"color:#CCCC00\"><b>", "</b></span>", 0);
    $this->format_add_def ("?&gt;", "", "", "<span style=\"color:#FF3300\"><b>", "</b></span>", 0);
    $this->format_add_def ("function", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("class", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("extends", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("define", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("return", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("new", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("include_once", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("include", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("require", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("global", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("true", "", "", "<span style=\"color:#880088\">", "</span>", 0);
    $this->format_add_def ("false", "", "", "<span style=\"color:#880088\">", "</span>", 0);
    $this->format_add_def ("NULL", "", "", "<span style=\"color:#880088\">", "</span>", 0);
    $this->format_add_def ("while", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("do", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("foreach", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("for", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("as", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("continue", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("switch", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("case", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("break", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("goto", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("echo", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("if", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    $this->format_add_def ("else", "", "", "<span style=\"color:#0000CD\">", "</span>", 0);
    // type casts
    $this->format_add_def ("int", "", "", "<span style=\"color:#00AACD\">", "</span>", 0);
    $this->format_add_def ("integer", "", "", "<span style=\"color:#00AACD\">", "</span>", 0);
    $this->format_add_def ("bool", "", "", "<span style=\"color:#00AACD\">", "</span>", 0);
    $this->format_add_def ("boolean", "", "", "<span style=\"color:#00AACD\">", "</span>", 0);
    $this->format_add_def ("float", "", "", "<span style=\"color:#00AACD\">", "</span>", 0);
    $this->format_add_def ("double", "", "", "<span style=\"color:#00AACD\">", "</span>", 0);
    $this->format_add_def ("real", "", "", "<span style=\"color:#00AACD\">", "</span>", 0);
    $this->format_add_def ("string", "", "", "<span style=\"color:#00AACD\">", "</span>", 0);
    $this->format_add_def ("binary", "", "", "<span style=\"color:#00AACD\">", "</span>", 0);
    $this->format_add_def ("object", "", "", "<span style=\"color:#00AACD\">", "</span>", 0);

    // numeric style property start tag
    $this->sPropertyNS = "<span style=\"color:#FF3300\">";
    // numeric style property end tag
    $this->sPropertyNE = "</span>";
    // 1. custom encoding (non-conflicting with HTML encoding scheme and 
    // unlikely string to occur in original text)
    // 2. actual character to encode
    // 3. valid HTML encoding of the character (or just the character 
    // itself usually)
    // non-regular expressions
    $this->encode_last = array ( array ("|sc|", ";", ";") );
    // regular expressions - encode the double backslash to avoid code 
    // formatter confusion when a backslash and quotation mark is 
    // encountered for double backslashes
    $this->encode_html = array ( array ("/[\\\]{2}/", "&#092;&#092;") );

    if ($bInit) // initialize code parameter upon instantiation
      $this->format_init ($sParam, $bFile);
  }
}

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. It is based on a mark-up language definition from which the styles and colors are completely modifiable. Markup languages can be highlighted as well as non-markup regular code.

<!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 http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <link rel = "STYLESHEET" href = "styles.css" type = "text/css">
    <script src = "funcs.js" type="text/javascript"></script>
  </head>

  <body onload = 'javascript: init ();' bgcolor = '#C0C0C0'>
  <!-- layout marker: begin -->
  <table border = 0 cellspacing = 5 cellpadding = 10 width = '90%'>
    <tr>
      <td background = 'images/top.jpg' align = 'right' valign = 'top'>
        <img src = 'images/right.jpg' width = '82'></td>
    </tr>
    <tr>
      <td>
        <p>Menu options</p>
        Option 1 <br>
        <form id = 'dt' name = 'dt' method = 'post' action = 'cgi/f.cgi'>
          <input id = 'val1' name = 'val1' type = 'text' value = '0.00'>
          <select id = 'sel1' name = 'sel1'>
            <option value = '1'>One &amp; &lt;1&gt;</option>
          </select> <!-- link follows -->
          <a href = '#' onclick = 'javascript: v(); return false;'>
            <img src = 'images/icon1.gif' border = '0'> 1
          </a>
        </form>
      </td>
    </tr>
  </table>

  </body>
</html>

Code View - dynamicreport.com

The statements necessary to pass a string code parameter as opposed to a file path containing the code for import follows. * Note that the default parameter is a file import, therefore the last parameter to the class instantiation or to format_init should be set to false if the first parameter is a code string buffer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?

$strCode = "".
"Private Sub VB_Example()\n".
" ' comment here\n".
" Dim dataPath = \"C:\myfile.doc\" 'path to a document\n".
" Dim boolVal = False\n".
" Dim dblVal = -999.34\n".
" MsgBox \"Hello World\"\n".
" Set f = objFSO.GetFolder(dataPath)\n".
" Set fc = f.Files\n".
"  For Each f1 In fc\n".
"   If (InStr(f1, \"relevant\") > 0) Then\n".
"    Exit For\n".
"   End If\n".
"  Next\n".
" Set f = Nothing\n".
" Set fc = Nothing\n".
"End Sub\n";

// forth parameter is false (string as opposed to file)
// last parameter is false so that the code is not auto-initialized
$fmtcode = new format_vb_code (NULL, WRAP_YES, 1, false, false);
// highlight specifc code of interest
$arrE = array ("\n"); // ending delimiter (array 1+ delimiters are possible)
$fmtcode->format_add_highlight ("Set f =", $arrE, "", 
 "<span style=\"background-color:#EEEF00; color:#000000;\">", "</span>");
$fmtcode->format_init ($strCode, false); // initialize string code format
echo $fmtcode->format_getcode ();

?>

Code View - dynamicreport.com

The following code snippet is from the string of VB code passed directly to the VB code view extended class as a string parameter, which is one of the two methods to pass code to the code view object (file or string).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Private Sub VB_Example()
 ' comment here
 Dim dataPath = "C:\myfile.doc" 'path to a document
 Dim boolVal = False
 Dim dblVal = -999.34
 MsgBox "Hello World"
 Set f = objFSO.GetFolder(dataPath)
 Set fc = f.Files
  For Each f1 In fc
   If (InStr(f1, "relevant") > 0) Then
    Exit For
   End If
  Next
 Set f = Nothing
 Set fc = Nothing
End Sub

Code View - dynamicreport.com

This script can be found at dynamicreport.com