public
Description: Web Application component for Open Australia (phplib module)
Homepage: http://openaustralia.org/
Clone URL: git://github.com/mlandauer/phplib.git
phplib / htmlstring.php
100644 121 lines (107 sloc) 4.59 kb
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/*
* htmlstring.php:
* Experimental - not really ready yet.
*
* Copyright (c) 2006 UK Citizens Online Democracy. All rights reserved.
* Email: francis@mysociety.org; WWW: http://www.mysociety.org/
*
* $Id: htmlstring.php,v 1.3 2006/06/19 16:40:31 francis Exp $
*
*/
 
/* Example usage:
 
print_html(p(format("hello is %0.2f foi"), 8.12345));
print_html(p(html(format("foo < bar %d foo %s ha"), 7, html("hello > foo"))));
print_html(ul(li("First"), li("Second"), li("The first % of life")));
 
TODO:
 
- How to handle character types? Links cause similar problem.
print_html(p('This is a really long sentence with <strong>emphasis on good bits</strong>, and less on others.'));
- format is a bit too generic a name. Chris suggests F(), but ugly.
- PledgeBank already uses obvious names like p() and so on.
Maybe put all the functions in an (effectively singleton) object like $q in CGI.pm?
- Let <li> say take an array and wrap elements separately in <li> and join together
*/
 
// Prints an HTMLString, can construct one wth format parameters
function print_html() {
    $args = func_get_args();
    $hs = new HTMLString($args);
    print $hs->html;
}
 
// Constructs HTMLString, with format parameters passed through
function html($text) {
    $args = func_get_args();
    return new HTMLString($args);
}
 
// Constructs HTMLString wrapped in tag opening and closing
function htmlstring_tag($tag, $args) {
    $hs = new HTMLString($args);
    $hs->html = "<$tag>" . $hs->html . "</$tag>";
    return $hs;
}
 
// Standard HTML tags
function p() { $args = func_get_args(); return htmlstring_tag("p", $args); }
function h1() { $args = func_get_args(); return htmlstring_tag("h1", $args); }
function h2() { $args = func_get_args(); return htmlstring_tag("h2", $args); }
function h3() { $args = func_get_args(); return htmlstring_tag("h3", $args); }
function h4() { $args = func_get_args(); return htmlstring_tag("h4", $args); }
function strong() { $args = func_get_args(); return htmlstring_tag("strong", $args); }
function em() { $args = func_get_args(); return htmlstring_tag("em", $args); }
function dt() { $args = func_get_args(); return htmlstring_tag("dt", $args); }
function dd() { $args = func_get_args(); return htmlstring_tag("dd", $args); }
function ul() { $args = func_get_args(); return htmlstring_tag("ul", $args); }
function ol() { $args = func_get_args(); return htmlstring_tag("ol", $args); }
function li() { $args = func_get_args(); return htmlstring_tag("li", $args); }
 
// Indicates a format specifier
function format($text) {
    return new HTMLString_FormatSpecifier($text);
}
 
// Type for a string containing HTML
class HTMLString {
    var $html;
    function HTMLString($args = array()) {
        if (is_a($args[0], "HTMLString_FormatSpecifier")) {
            $format = array_shift($args);
            // sprintf constructor
            $new_args = array();
            foreach ($args as $arg) {
                if (gettype($arg) == "string")
                    $arg = htmlspecialchars($arg);
                elseif (gettype($arg) == "integer")
                    { } // do nothing
                elseif (gettype($arg) == "double")
                    { } // do nothing
                elseif (is_a($arg, "HTMLString"))
                    $arg = $arg->html;
                else
                    trigger_error("HTMLString formatting does not know type " . gettype($arg), E_USER_ERROR);
                $new_args[] = $arg;
            }
            $this->html = vsprintf(htmlspecialchars($format->text), $new_args);
        } else {
            // appending constructor
            // (appends all parametrs, can be strings or HTMLStrings)
            foreach ($args as $arg) {
                if (is_a($arg, "HTMLString"))
                    $this->html .= $arg->html;
                elseif (gettype($arg) == "string")
                    $this->html .= htmlspecialchars($arg);
                else
                    trigger_error("Appending constructor takes HTMLString and string only, not " . gettype($arg), E_USER_ERROR);
            }
        }
    }
 
    function append($a) {
        if (gettype($a) == "string")
            $a = new HTMLString($a);
        if (!is_a($a, "HTMLString"))
            trigger_error("HTMLString.append expects an HTMLString, not " . get_class($a), E_USER_ERROR);
        $this->html .= $a->html;
    }
}
 
// Type to indicate a printf style format specifier
class HTMLString_FormatSpecifier {
    var $text;
    function HTMLString_FormatSpecifier($a) {
        $this->text = $a;
    }
}