diff --git a/api.css b/api.css
index 2b2d7fa..38f8776 100644
--- a/api.css
+++ b/api.css
@@ -34,6 +34,12 @@ html.js div.api-expandable div.content {
   color: #060;
 }
 
+ol.code-lines,
+ol.code-lines li {
+   padding: 0;
+   margin: 0 0 0 20px;
+}
+
 /* Function signature */
 #api-function-signature td,
 #api-function-signature td a,
diff --git a/parser.inc b/parser.inc
index 8a7254b..8c64c23 100644
--- a/parser.inc
+++ b/parser.inc
@@ -60,7 +60,7 @@ function api_parse_file($callback, $file_path, $branch, $file_name) {
  */
 Function api_parse_text_file($docblock) {
   $docblock['documentation'] = api_format_documentation($docblock['source']);
-  $docblock['code'] = api_format_php($docblock['source']);
+  $docblock['code'] = api_format_php($docblock['source'], TRUE);
 
   api_save_documentation(array($docblock));
 }
@@ -115,7 +115,7 @@ function api_parse_php_file($docblock) {
   $statements = $reader->getStatements();
   if (!$statements) {
     // This is a text file or template file with no functions, constants, etc.
-    $docblock['code'] = api_format_php($docblock['source']);
+    $docblock['code'] = api_format_php($docblock['source'], TRUE);
     api_save_documentation(array($docblock));
     // Free up memory.
     $reader->reset();
@@ -123,7 +123,7 @@ function api_parse_php_file($docblock) {
   }
 
   // Reserve the first array slot for the file documentation block.
-  $docblock['code'] = api_format_php($docblock['source']);
+  $docblock['code'] = api_format_php($docblock['source'], TRUE);
   $docblocks = array($docblock);
 
   // Set default documenation block array for items other than the file.
@@ -672,11 +672,19 @@ function api_documentation_summary($documentation) {
  *
  * @param $code
  *   PHP code to format.
+ * @param $number
+ *   FALSE to not number the lines (default). TRUE to number the lines, or an
+ *   integer to start numbering at that line number.
+ * @param $id_base
+ *   If numbering lines, each line is put into an HTML "li" element with an "id"
+ *   attribute equal to $id_base concatenated with the line number. IDs must be
+ *   unique on a page, so if numbering multiple code elements on a page, use
+ *   a different $id_base for each.
  *
  * @return
  *   HTML-formatted code, with spans enclosing various PHP elements.
  */
-function api_format_php($code) {
+function api_format_php($code, $number = FALSE, $id_base = 'code-line-') {
   $output = '';
 
   if (!defined('T_ML_COMMENT')) {
@@ -766,8 +774,25 @@ function api_format_php($code) {
     }
   }
 
-  // Manage whitespace:
-  return '<pre class="php"><code>'. trim($output) .'</code></pre>';
+  $output = trim($output);
+
+  // Add line numbering, if requested.
+  if ($number !== FALSE) {
+    if (!is_int($number)) {
+      $number = 1;
+    }
+    $lines = explode("\n", $output);
+    $output = '<ol class="code-lines" start="' . $number . '">';
+    foreach ($lines as $line) {
+      $output .= '<li id="' . $id_base . $number . '">' . $line . "\n</li>";
+      $number++;
+    }
+    $output .= '</ol>';
+  }
+
+  $output = '<pre class="php"><code>'. $output .'</code></pre>';
+
+  return $output;
 }
 
 /**
