[moriarty] r316 committed - Modified to_html() on simplegraph to render bnodes as anchor links in ...

0 views
Skip to first unread message

mori...@googlecode.com

unread,
Aug 2, 2011, 11:54:02 AM8/2/11
to moriarty...@googlegroups.com
Revision: 316
Author: jhi...@gmail.com
Date: Tue Aug 2 08:53:06 2011
Log: Modified to_html() on simplegraph to render bnodes as anchor
links in the page.
http://code.google.com/p/moriarty/source/detail?r=316

Modified:
/trunk/simplegraph.class.php
/trunk/tests/simplegraph.test.php

=======================================
--- /trunk/simplegraph.class.php Tue Aug 2 01:29:25 2011
+++ /trunk/simplegraph.class.php Tue Aug 2 08:53:06 2011
@@ -243,7 +243,22 @@
return json_encode($this->_index);
}

-
+ function get_valid_id($s) {
+ return htmlspecialchars(str_replace('_:','',$s));
+ }
+
+ function get_link_href($s) {
+ $prefix = (strpos($s,'_:')===0) ? '#' : '';
+ return $prefix . $this->get_valid_id($s);
+ }
+
+ function get_link_id($s) {
+ if (strpos($s,'_:')===0) {
+ return $this->get_valid_id($s);
+ }
+ return null;
+ }
+
/**
* Serialise the graph to HTML
* @return string a HTML version of the graph
@@ -275,7 +290,11 @@
if (count($subjects) > 0) {
foreach ($subjects as $subject) {
if (count($subjects) > 1) {
- $h .= '<h1><a href="' . htmlspecialchars($subject) . '">' .
htmlspecialchars($this->get_label($subject)) . '</a></h1>' . "\n";
+ $h .= '<h1><a';
+ if($link_id = $this->get_link_id($subject)) {
+ $h .= ' id="' . $link_id . '"';
+ }
+ $h .= ' href="' . $this->get_link_href($subject) . '">' .
htmlspecialchars($this->get_label($subject)) . '</a></h1>' . "\n";
}
$h .= '<table>' . "\n";

@@ -284,7 +303,7 @@
$properties = array_merge($priority_properties,
array_diff($properties, $priority_properties));

foreach ($properties as $p) {
- $h .= '<tr><th valign="top"><a href="' .
htmlspecialchars($p). '">' . htmlspecialchars($this->get_label($p,
true)). '</a></th>';
+ $h .= '<tr><th valign="top"><a href="' .
$this->get_link_href($p). '">' . htmlspecialchars($this->get_label($p,
true)). '</a></th>';
$h .= '<td valign="top">';
for ($i = 0; $i < count($this->_index[$subject][$p]); $i++) {
if ($i > 0) $h .= '<br />';
@@ -292,7 +311,7 @@
$h .=
htmlspecialchars($this->_index[$subject][$p][$i]['value'] );
}
else {
- $h .= '<a href="' .
htmlspecialchars($this->_index[$subject][$p][$i]['value']). '">';
+ $h .= '<a href="' .
$this->get_link_href($this->_index[$subject][$p][$i]['value']). '">';
if ($guess_labels) {
$h .=
htmlspecialchars($this->get_label($this->_index[$subject][$p][$i]['value'])
);
}
@@ -322,12 +341,12 @@
}

foreach ($backlinks as $backlink_p => $backlink_values) {
- $h .= '<tr><th valign="top"><a href="' .
htmlspecialchars($backlink_p). '">' .
htmlspecialchars($this->get_inverse_label($backlink_p, true)). '</a></th>';
+ $h .= '<tr><th valign="top"><a href="' .
$this->get_link_href($backlink_p). '">' .
htmlspecialchars($this->get_inverse_label($backlink_p, true)). '</a></th>';
$h .= '<td valign="top">';
for ($i = 0; $i < count($backlink_values); $i++) {
if ($i > 0) $h .= '<br />';

- $h .= '<a href="' .
htmlspecialchars($backlink_values[$i]). '">';
+ $h .= '<a href="' .
$this->get_link_href($backlink_values[$i]). '">';
if ($guess_labels) {
$h .=
htmlspecialchars($this->get_label($backlink_values[$i]) );
}
=======================================
--- /trunk/tests/simplegraph.test.php Tue Aug 2 01:29:25 2011
+++ /trunk/tests/simplegraph.test.php Tue Aug 2 08:53:06 2011
@@ -1233,5 +1233,20 @@
$expected = array('_:a', '_:b');
$this->assertEquals($expected, $actual, "get_bnodes() should return
bnodes in subject and object positions");
}
+
+ function test_to_html_renders_bnodes_as_anchors() {
+
+ $g = new SimpleGraph();
+ $g->from_rdfxml($this->_single_triple);
+
$g->add_resource_triple('http://example.org/subj', 'http://example.org/pred', '_:bn123');
+
$g->add_resource_triple('_:bn123', 'http://example.org/pred', 'http://example.org/obj');
+
+ $html = $g->to_html();
+
+ $this->assertContains('<a href="http://example.org/subj">subj</a>',
$html, "html should contain links to bnode anchors");
+ $this->assertContains('<a href="#bn123">_:bn123</a>', $html, "html
should contain links to bnode anchors");
+ $this->assertContains('<a id="bn123" href="#bn123">_:bn123</a>',
$html, "html should contain anchors for bnodes");
+ }
+
}
?>

Reply all
Reply to author
Forward
0 new messages