This repository is private.
All pages are served over SSL and all pushing and pulling is done over SSH.
No one may fork, clone, or view it unless they are added as a member.
Every repository with this icon (
) is private.
Every repository with this icon (
This repository is public.
Anyone may fork, clone, or view it.
Every repository with this icon (
) is public.
Every repository with this icon (
phplib / htmlstring.php
| 8e69377e » | francis | 2005-11-16 | 1 | <?php | |
| 2 | /* | ||||
| 3 | * htmlstring.php: | ||||
| 4 | * Experimental - not really ready yet. | ||||
| 5 | * | ||||
| 371339dc » | francis | 2006-06-19 | 6 | * Copyright (c) 2006 UK Citizens Online Democracy. All rights reserved. | |
| 8e69377e » | francis | 2005-11-16 | 7 | * Email: francis@mysociety.org; WWW: http://www.mysociety.org/ | |
| 8 | * | ||||
| 371339dc » | francis | 2006-06-19 | 9 | * $Id: htmlstring.php,v 1.3 2006/06/19 16:40:31 francis Exp $ | |
| 8e69377e » | francis | 2005-11-16 | 10 | * | |
| 11 | */ | ||||
| 12 | |||||
| 13 | /* Example usage: | ||||
| 14 | |||||
| 15 | print_html(p(format("hello is %0.2f foi"), 8.12345)); | ||||
| 16 | print_html(p(html(format("foo < bar %d foo %s ha"), 7, html("hello > foo")))); | ||||
| 17 | print_html(ul(li("First"), li("Second"), li("The first % of life"))); | ||||
| 18 | |||||
| 19 | TODO: | ||||
| 20 | |||||
| 21 | - How to handle character types? Links cause similar problem. | ||||
| 22 | print_html(p('This is a really long sentence with <strong>emphasis on good bits</strong>, and less on others.')); | ||||
| 23 | - format is a bit too generic a name. Chris suggests F(), but ugly. | ||||
| 24 | - PledgeBank already uses obvious names like p() and so on. | ||||
| 25 | Maybe put all the functions in an (effectively singleton) object like $q in CGI.pm? | ||||
| 8120bc58 » | francis | 2005-11-23 | 26 | - Let <li> say take an array and wrap elements separately in <li> and join together | |
| 8e69377e » | francis | 2005-11-16 | 27 | */ | |
| 28 | |||||
| 29 | // Prints an HTMLString, can construct one wth format parameters | ||||
| 30 | function print_html() { | ||||
| 31 | $args = func_get_args(); | ||||
| 32 | $hs = new HTMLString($args); | ||||
| 33 | print $hs->html; | ||||
| 34 | } | ||||
| 35 | |||||
| 36 | // Constructs HTMLString, with format parameters passed through | ||||
| 37 | function html($text) { | ||||
| 38 | $args = func_get_args(); | ||||
| 39 | return new HTMLString($args); | ||||
| 40 | } | ||||
| 41 | |||||
| 42 | // Constructs HTMLString wrapped in tag opening and closing | ||||
| 43 | function htmlstring_tag($tag, $args) { | ||||
| 44 | $hs = new HTMLString($args); | ||||
| 45 | $hs->html = "<$tag>" . $hs->html . "</$tag>"; | ||||
| 46 | return $hs; | ||||
| 47 | } | ||||
| 48 | |||||
| 49 | // Standard HTML tags | ||||
| 50 | function p() { $args = func_get_args(); return htmlstring_tag("p", $args); } | ||||
| 51 | function h1() { $args = func_get_args(); return htmlstring_tag("h1", $args); } | ||||
| 52 | function h2() { $args = func_get_args(); return htmlstring_tag("h2", $args); } | ||||
| 53 | function h3() { $args = func_get_args(); return htmlstring_tag("h3", $args); } | ||||
| 54 | function h4() { $args = func_get_args(); return htmlstring_tag("h4", $args); } | ||||
| 55 | function strong() { $args = func_get_args(); return htmlstring_tag("strong", $args); } | ||||
| 56 | function em() { $args = func_get_args(); return htmlstring_tag("em", $args); } | ||||
| 57 | function dt() { $args = func_get_args(); return htmlstring_tag("dt", $args); } | ||||
| 58 | function dd() { $args = func_get_args(); return htmlstring_tag("dd", $args); } | ||||
| 59 | function ul() { $args = func_get_args(); return htmlstring_tag("ul", $args); } | ||||
| 60 | function ol() { $args = func_get_args(); return htmlstring_tag("ol", $args); } | ||||
| 61 | function li() { $args = func_get_args(); return htmlstring_tag("li", $args); } | ||||
| 62 | |||||
| 63 | // Indicates a format specifier | ||||
| 64 | function format($text) { | ||||
| 65 | return new HTMLString_FormatSpecifier($text); | ||||
| 66 | } | ||||
| 67 | |||||
| 68 | // Type for a string containing HTML | ||||
| 69 | class HTMLString { | ||||
| 70 | var $html; | ||||
| 71 | function HTMLString($args = array()) { | ||||
| 72 | if (is_a($args[0], "HTMLString_FormatSpecifier")) { | ||||
| 73 | $format = array_shift($args); | ||||
| 74 | // sprintf constructor | ||||
| 75 | $new_args = array(); | ||||
| 76 | foreach ($args as $arg) { | ||||
| 77 | if (gettype($arg) == "string") | ||||
| 78 | $arg = htmlspecialchars($arg); | ||||
| 79 | elseif (gettype($arg) == "integer") | ||||
| 80 | { } // do nothing | ||||
| 81 | elseif (gettype($arg) == "double") | ||||
| 82 | { } // do nothing | ||||
| 83 | elseif (is_a($arg, "HTMLString")) | ||||
| 84 | $arg = $arg->html; | ||||
| 85 | else | ||||
| 86 | trigger_error("HTMLString formatting does not know type " . gettype($arg), E_USER_ERROR); | ||||
| 87 | $new_args[] = $arg; | ||||
| 88 | } | ||||
| 89 | $this->html = vsprintf(htmlspecialchars($format->text), $new_args); | ||||
| 90 | } else { | ||||
| 91 | // appending constructor | ||||
| 92 | // (appends all parametrs, can be strings or HTMLStrings) | ||||
| 93 | foreach ($args as $arg) { | ||||
| 94 | if (is_a($arg, "HTMLString")) | ||||
| 95 | $this->html .= $arg->html; | ||||
| 96 | elseif (gettype($arg) == "string") | ||||
| 97 | $this->html .= htmlspecialchars($arg); | ||||
| 98 | else | ||||
| 99 | trigger_error("Appending constructor takes HTMLString and string only, not " . gettype($arg), E_USER_ERROR); | ||||
| 100 | } | ||||
| 101 | } | ||||
| 102 | } | ||||
| 103 | |||||
| 104 | function append($a) { | ||||
| 105 | if (gettype($a) == "string") | ||||
| 106 | $a = new HTMLString($a); | ||||
| 107 | if (!is_a($a, "HTMLString")) | ||||
| 108 | trigger_error("HTMLString.append expects an HTMLString, not " . get_class($a), E_USER_ERROR); | ||||
| 109 | $this->html .= $a->html; | ||||
| 110 | } | ||||
| 111 | } | ||||
| 112 | |||||
| 113 | // Type to indicate a printf style format specifier | ||||
| 114 | class HTMLString_FormatSpecifier { | ||||
| 115 | var $text; | ||||
| 116 | function HTMLString_FormatSpecifier($a) { | ||||
| 117 | $this->text = $a; | ||||
| 118 | } | ||||
| 119 | } | ||||
| 120 | |||||







