1<?php
2// $Header: /cvsroot/html2ps/xhtml.autoclose.inc.php,v 1.5 2005/07/28 17:04:33 Konstantin Exp $
3
4function autoclose_tag(&$sample_html, $offset, $tags, $nested, $close) {
5  $tags = mk_open_tag_regexp($tags);
6
7  while (preg_match("#^(.*?)({$tags})#is", substr($sample_html, $offset),$matches)) {
8    // convert tag name found to lower case
9    $tag = strtolower($matches[3]);
10    // calculate position of the tag found
11    $tag_start = $offset + strlen($matches[1]);
12    $tag_end   = $tag_start + strlen($matches[2]);
13
14    if ($tag == $close) { return $tag_end; };
15
16    // REQ: PHP 4.0.5
17    if (isset($nested[$tag])) {
18      $offset = $nested[$tag]($sample_html, $tag_end);
19    } else {
20      $to_be_inserted = "<".$close.">";
21
22      $sample_html = substr_replace($sample_html, $to_be_inserted, $tag_start ,0);
23      return $tag_start + strlen($to_be_inserted);
24    };
25  };
26
27  return $offset;
28}
29
30// removes from current html string a piece from the current $offset to
31// the beginning of next $tag; $tag should contain a '|'-separated list
32// of opening or closing tags. This function is useful for cleaning up
33// messy code containing trash between TD, TR and TABLE tags.
34function skip_to(&$html, $offset, $tag) {
35  $prefix = substr($html,0,$offset);
36  $suffix = substr($html,$offset);
37
38  if (preg_match("#^(.*?)<\s*({$tag})#is", $suffix, $matches)) {
39    $suffix = substr($suffix, strlen($matches[1]));
40  };
41
42  $html = $prefix . $suffix;
43}
44
45function autoclose_tag_cleanup(&$sample_html, $offset, $tags_raw, $nested, $close) {
46  $tags = mk_open_tag_regexp($tags_raw);
47  skip_to($sample_html, $offset, $tags_raw);
48
49  while (preg_match("#^(.*?)({$tags})#is", substr($sample_html, $offset),$matches)) {
50    // convert tag name found to lower case
51    $tag = strtolower($matches[3]);
52    // calculate position of the tag found
53    $tag_start = $offset + strlen($matches[1]);
54    $tag_end   = $tag_start + strlen($matches[2]);
55
56    if ($tag == $close) { return $tag_end; };
57
58    // REQ: PHP 4.0.5
59    if (isset($nested[$tag])) {
60      $offset = $nested[$tag]($sample_html, $tag_end);
61    } else {
62      $to_be_inserted = "<".$close.">";
63
64      $sample_html = substr_replace($sample_html, $to_be_inserted, $tag_start ,0);
65      return $tag_start + strlen($to_be_inserted);
66    };
67
68    skip_to($sample_html, $offset, $tags_raw);
69  };
70
71  return $offset;
72}
73
74?>