HTML utilities
"uweb/html.php"
Uray Web Library (UWeb)
This file contains some functions related to static HTML generation and HTML processing. See also utils.php (which is included by html.php) for the most basic HTML functions. Here are the more complex and/or less frequently used ones.
3.3. print_num_list(), gen_num_list()
function code (...$text);
Enclose $text in <code> HTML tags, and return the result.
Multiple arguments are concatenated to a single string.
function html_to_text ($code);
Remove the HTML tags in $code, reduce the spaces, and return the result. All whitespace characters (newline, tab, space) are converted to spaces, and they are removed from the beginning and end of the string.
Example: html_to_text("\t<p> some <b>text</b> </p>\n") == "some text"
function split_html ($text);
Split the HTML code into an array of text segments and tags.
Example: split_html("<p>some <b>text</b></p>") == ["<p>", "some ", "<b>", "text", "</b>", "</p>"]
Note: always implode(split_html($string)) == $string.
function print_list ($items, $attrs = []); function print_list ($lng, $items, $attrs = []);
Print an HTML bullet list (<ul>).
$items: array of list items. Each element can be:
get_lng_text(), in which case $lng is mandatory; null, which is skipped (i.e. no <li> is produced). $attrs: optional additional attributes of the <ul> element (see: attrs()).
$lng: optional language object for multi-language texts. It can be null or even omitted.
function gen_list ($items, $attrs = []); function gen_list ($lng, $items, $attrs = []);
Same as print_list(), but return the list HTML as a string instead of printing it.
function print_num_list ($items, $attrs = []); function print_num_list ($lng, $items, $attrs = []); function gen_num_list ($items, $attrs = []); function gen_num_list ($lng, $items, $attrs = []);
Same as print_list() and gen_list(), respectively, but they print/return a numbered list (<ol>) instead of a bullet list.
function print_list_recursively ($list); function print_list_recursively ($lng, $list);
Print a multi-level HTML bullet list recursively from an array.
$list: an array containing the list elements as strings or arrays, recursively. It can be in one of the following two forms:
get_lng_text() (if $lng is given). $lng: optional language object for multi-language texts. It can be null or even omitted.
Example: both of the following lines produce the list below:
print_list_recursively(["First item", "Second", ["2A", "2B"], "Third"]); print_list_recursively([["First ","item "], ["Second ", [["2A "], ["2B "]]], ["Third "]]);
function print_toc_content ($obj, $toc); function print_toc_content ($toc);
Generate table of contents (TOC) and the content itself.
For a simple example, see Examples: HTML helpers. Also, this documentation page was generated using print_toc_content(), so its source code (docs/html.php) is another example.
$obj: the object whose methods and constants are referenced by $toc for the section titles and contents by default.
$obj->lng_obj() is valid, then the methods and constants are first searched in the object $obj->lng_obj() (to allow it to be language-dependent), and if that fails, then in $obj itself. $obj is omitted, then each item of $toc must either define an "obj" key itself, or define the content/title in another way. $toc: array of sections (including the table of contents). Each section is given as an array of properties.
For example, [3, "xyz"] defines an <h3>-level section whose content is printed by $obj->xyz(), whose title is defined by the constant $obj->xyz_title, and whose URL is page.html#xyz.
In general, the given array can have the following keys (some of which can be numerical or string, e.g. [3, "xyz"] is equivalent to ["h"=>3, "cont"=>"xyz"]):
"h" or 0 => the level of the HTML heading element for this section between 1 and 6, e.g. 3 means <h3>. The section numbers are deduced from these heading levels, e.g. the headings <h3>, <h6>, <h6>, <h3> will produce the section numbers 1, 1.1, 1.2, 2. "cont" or 1 => the name of the method of $obj that prints the content of the section. "title" and "id" (see below). "toc", then this section is actually the table of contents, and no method is called. This "toc" should be present in the list, usually at the beginning, otherwise the TOC is not printed. "*"), it means that this section has no section number (equivalently to "n" => null). It is usually applied to the TOC: "*toc". "title", but not "id"). "id" or 2 => the HTML id of the section, i.e. the URL of the section will be page.html#id. By default, it is the same as "cont". "title" => the title of the section, in the format of get_lng_text() (e.g. "#title_constant" means $obj->title_constant). By default, it is "#{$cont}_title", where $cont is the value of "cont". "obj" => object used by "cont" and "title", overriding the parameter $obj for this section. "n" => section number (last component), to override sequential numbering (useful if the TOC is split into multiple parts). The value null removes the number for this section. By default, the first numbered section is 1, and each subsequent numbered section gets the previous number plus 1 (in the same level, under the same parent). "href" => external URL for the section, e.g. "otherpage.html#section" (useful if the content spans across multiple HTML pages). If given, this section will appear only in the TOC, and no content will be generated for it. Then "cont" and "id" are ignored (other than using"cont" for "title" by default). function print_toc ($obj, $toc); function print_toc ($toc);
Generate table of contents (TOC).
This function works similarly to print_toc_content(), with the following differences:
<h*>) of the TOC is not printed. "toc" section is not necessary (it determines only whether the TOC is listed inside the TOC). "cont" is not used by itself (only to define defaults for "title" and "id").