<?php error_reporting(E_ALL | E_STRICT); require_once dirname(__FILE__) . '/../anewt.lib.php'; anewt_include('xml/dom'); anewt_include('gpc'); $doc = new AnewtXMLDomDocument(); if (AnewtGPC::get_bool('debug')) { $doc->set_content_type('text/plain'); } $root = $doc->create_element('Toplevel', array('foo' => 'bar', 'bar' => '&baz')); $root->set_attribute('foo', null); // remove attribute $doc->append_child($root); $element1 = $root->append_child($doc->create_element('SomeElement')); $element1->append_child($doc->create_text_node('Some & text.')); $element1->append_child($doc->create_raw_node(' Some <b>previously marked up</b> text.')); $element1->append_child_raw(' Some more text with <b>markup</b>.'); $element1->append_child($doc->create_comment('This is a comment -- containing << strange characters >>.')); $element1->append_child($doc->create_text_node(' Even more text.')); $element2 = $root->append_child($doc->create_element('SomeElement')); $frag = $doc->create_document_fragment(); $frag->append_child($doc->create_text_node('AAA.')); $frag->append_child($doc->create_text_node('BBB.')); $frag->append_child($doc->create_text_node('CCC.')); $element2->append_child($frag); $sub1 = $element2->append_child($doc->create_element('Sub1')); $sub1->append_child($doc->create_element('Sub2')); $sub2 = $sub1->append_child($doc->create_element('Sub2')); $sub3 = $sub2->append_child($doc->create_element('Sub3')); $sub3->append_child($doc->create_text_node('First content in sub 3'));
/** * Render this page into XHTML. * * This methods renders the whole page into a complete XHTML page. Usually * you want to use flush() to output the page to the browser. * * \return * The rendered page as a string. * * \see AnewtPage::flush */ public function render() { /* Create basic element nodes */ $head = new AnewtXMLDomElement('head'); $body = new AnewtXMLDomElement('body'); $head->always_render_closing_tag = true; $body->always_render_closing_tag = true; /* Content-type in meta tag. This must be the first element inside the * <head>...</head> element. */ $head->append_child(ax_meta(array('http-equiv' => 'Content-type', 'content' => $this->build_content_type_charset()))); /* Base URI */ $base_uri = $this->_get('base-uri'); if (!is_null($base_uri)) { assert('is_string($base_uri); // base-uri must be a simple string'); /* Employ a simple heuristic to make sure we use an absolute URI, as * is required by the HTML specification. */ if (!str_contains($base_uri, '://')) { $base_uri = AnewtRequest::canonical_base_url() . $base_uri; } $base = new AnewtXHTMLBase(); $base->set_attribute('href', $base_uri); $head->append_child($base); } /* Page title (always include, even if empty, since this is required by * the HTML specification) */ $title = $this->_get('title'); $head->append_child(ax_title($title)); /* Dublin Core metadata. See http://dublincore.org/documents/dcq-html/ * */ if ($this->_get('show-dublin-core')) { $head->append_child(ax_link(array('rel' => 'schema.DC', 'href' => 'http://purl.org/dc/elements/1.1/'))); $head->append_child(ax_meta_name_content('DC.language', $this->_get('language'))); if (!is_null($title)) { $head->append_child(ax_meta_name_content('DC.title', $title)); } if ($this->_isset('creator')) { $head->append_child(ax_meta_name_content('DC.creator', $this->_get('creator'))); } if ($this->_isset('description')) { $head->append_child(ax_meta_name_content('DC.description', $this->_get('description'))); } if ($this->_isset('date')) { $date = $this->get('date'); } else { $date = AnewtDateTime::now(); } $head->append_child(ax_meta_name_content('DC.date', AnewtDateTime::date($date))); } /* Powered by Anewt! */ $generator = $this->_get('generator'); if (!is_null($generator)) { assert('is_string($generator);'); $head->append_child(ax_meta_name_content('generator', $generator)); } /* Robots */ $robots = $this->_get('robots'); if (!is_null($robots)) { assert('is_string($robots);'); $head->append_child(ax_meta(array('name' => 'robots', 'content' => $robots))); } /* Links (including stylesheets) and JavaScripts */ $head->append_children($this->_links); $head->append_children($this->_javascripts); /* Favicon */ $favicon = $this->_get('favicon'); if (!is_null($favicon)) { assert('is_string($favicon);'); $head->append_child(ax_link_favicon($favicon)); } /* Body content */ if ($this->_get('blocks')) { /* This is a page using div blocks */ if ($this->_content && $this->_content->has_child_nodes()) { throw new AnewtException('Pages using blocks should not have content outside blocks.'); } /* The buffer holding all content is either a wrapper div or an * invisible fragment (both XML nodes, so the API is the same). */ if ($this->_get('use-wrapper-div')) { $buffer = ax_div_id(null, $this->_get('wrapper-div-id')); } else { $buffer = ax_fragment(); } /* Add the content */ foreach ($this->_get('blocks') as $block_name) { $block = $this->get_block($block_name); $buffer->append_child(ax_div_id($block, $block_name)); unset($block); unset($div); } $body->append_child($buffer); } else { /* This page has no blocks, so we use the nodes in _content instead * (if any) */ if ($this->_blocks) { throw new AnewtException('Pages not using blocks should not have content in blocks'); } if ($this->_content) { $body->append_child($this->_content); } } /* Assemble the top level elements */ $document = new AnewtXMLDomDocument(); $document->set_document_type($this->_get('document-type')); $document->set_content_type($this->_get('content-type')); $document->set_encoding($this->_get('encoding')); $html = new AnewtXMLDomElement('html', array('xmlns' => 'http://www.w3.org/1999/xhtml', 'xml:lang' => $this->_get('language'), 'lang' => $this->_get('language'))); $html->append_child($head); $html->append_child($body); $document->append_child($html); return $document; }
/** * Build an XML document for this channel */ private function build_document() { /* Build the channel and description properties*/ $channel = new AnewtXMLDomElement('channel'); foreach ($this->properties as $property => $property_spec) { list($tagname, $status, $type) = $property_spec; $element = AnewtRssChannel::_build_rss_element($this, $property, $tagname, $status, $type); if (is_null($element)) { continue; } $channel->append_child($element); unset($element); } /* Always add 'docs' element */ $d = new AnewtXMLDomElement('docs'); $d->append_child(new AnewtXMLDomText('http://www.rssboard.org/rss-specification')); $channel->append_child($d); /* Add an atom:link element. Only call the canonical_url() method if no * explicit url was provided. */ $url = $this->_get('url'); if (is_null($url)) { $url = AnewtRequest::canonical_url(); } $channel->append_child(new AnewtXMLDomElement('atom:link', array('href' => $url, 'rel' => 'self', 'type' => $this->_get('content-type')))); /* Loop over items in the channel and append those */ foreach ($this->items as $item) { $channel->append_child($item->_build_element()); } /* Final output */ $document = new AnewtXMLDomDocument(); $document->set_content_type($this->_get('content-type')); $document->set_encoding($this->_get('encoding')); $rss = new AnewtXMLDomElement('rss', array('version' => '2.0', 'xmlns:atom' => 'http://www.w3.org/2005/Atom')); $rss->append_child($channel); $document->append_child($rss); return $document; }