Add XMLStringer for building XML strings

We had a bunch of
This commit is contained in:
Evan Prodromou 2009-02-12 08:38:43 -05:00
parent 99773e3b5e
commit eaae456222
2 changed files with 102 additions and 7 deletions

View File

@ -475,13 +475,14 @@ function common_linkify($url) {
$display = $url;
$url = (!preg_match('#^([a-z]+://|(mailto|aim|tel):)#i', $url)) ? 'http://'.$url:$url;
$attrs = array('href' => $url, 'rel' => 'external');
if ($longurl = common_longurl($url)) {
$longurl = htmlentities($longurl, ENT_QUOTES, 'UTF-8');
$title = "title=\"$longurl\"";
$attrs['title'] = $longurl;
}
else $title = '';
return "<a href=\"$url\" $title rel=\"external\">$display</a>";
return XMLStringer::estring('a', $attrs, $display);
}
function common_longurl($short_url)
@ -582,7 +583,13 @@ function common_tag_link($tag)
{
$canonical = common_canonical_tag($tag);
$url = common_local_url('tag', array('tag' => $canonical));
return '<span class="tag"><a href="' . htmlspecialchars($url) . '" rel="tag">' . htmlspecialchars($tag) . '</a></span>';
$xs = new XMLStringer();
$xs->elementStart('span', 'tag');
$xs->element('a', array('href' => $url,
'rel' => 'tag'),
$tag);
$xs->elementEnd();
return $xs->getString();
}
function common_canonical_tag($tag)
@ -600,7 +607,14 @@ function common_at_link($sender_id, $nickname)
$sender = Profile::staticGet($sender_id);
$recipient = common_relative_profile($sender, common_canonical_nickname($nickname));
if ($recipient) {
return '<span class="vcard"><a href="'.htmlspecialchars($recipient->profileurl).'" class="url"><span class="fn nickname">'.$nickname.'</span></a></span>';
$xs = new XMLStringer(false);
$xs->elementStart('span', 'vcard');
$xs->elementStart('a', array('href' => $recipient->profileurl,
'class' => 'url'));
$xs->element('span', 'fn nickname', $nickname);
$xs->elementEnd('a');
$xs->elementEnd('span');
return $xs->getString();
} else {
return $nickname;
}
@ -611,7 +625,14 @@ function common_group_link($sender_id, $nickname)
$sender = Profile::staticGet($sender_id);
$group = User_group::staticGet('nickname', common_canonical_nickname($nickname));
if ($group && $sender->isMember($group)) {
return '<span class="vcard"><a href="'.htmlspecialchars($group->permalink()).'" class="url"><span class="fn nickname">'.$nickname.'</span></a></span>';
$xs = new XMLStringer();
$xs->elementStart('span', 'vcard');
$xs->elementStart('a', array('href' => $group->permalink(),
'class' => 'url'));
$xs->element('span', 'fn nickname', $nickname);
$xs->elementEnd('a');
$xs->elementEnd('span');
return $xs->getString();
} else {
return $nickname;
}
@ -628,7 +649,13 @@ function common_at_hash_link($sender_id, $tag)
$url = common_local_url('subscriptions',
array('nickname' => $user->nickname,
'tag' => $tag));
return '<span class="tag"><a href="'.htmlspecialchars($url).'" rel="tag">'.$tag.'</a></span>';
$xs = new XMLStringer();
$xs->elementStart('span', 'tag');
$xs->element('a', array('href' => $url,
'rel' => $tag),
$tag);
$xs->elementEnd('span');
return $xs->getString();
} else {
return $tag;
}

68
lib/xmlstringer.php Normal file
View File

@ -0,0 +1,68 @@
<?php
/**
* Laconica, the distributed open-source microblogging tool
*
* Generator for in-memory XML
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Output
* @package Laconica
* @author Evan Prodromou <evan@controlyourself.ca>
* @copyright 2009 Control Yourself, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://laconi.ca/
*/
if (!defined('LACONICA')) {
exit(1);
}
/**
* Create in-memory XML
*
* @category Output
* @package Laconica
* @author Evan Prodromou <evan@controlyourself.ca>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://laconi.ca/
* @see Action
* @see HTMLOutputter
*/
class XMLStringer extends XMLOutputter
{
function __construct($indent=false)
{
$this->xw = new XMLWriter();
$this->xw->openMemory();
$this->xw->setIndent($indent);
}
function getString()
{
return $this->xw->outputMemory();
}
// utility for quickly creating XML-strings
static function estring($tag, $attrs=null, $content=null)
{
$xs = new XMLStringer();
$xs->element($tag, $attrs, $content);
return $xs->getString();
}
}