xoxo-sample-code: Difference between revisions

From Microformats Wiki
Jump to navigation Jump to search
m (sub-divide)
(add github link)
 
(4 intermediate revisions by 3 users not shown)
Line 2: Line 2:


A whole bunch of open source ([http://creativecommons.org/licenses/by/2.0/ CC-by-2.0], [http://www.apache.org/licenses/LICENSE-2.0 Apache 2.0]) sample code to read and write [[xoxo]] files in Python and Java (with Perl, PHP, ... to follow).
A whole bunch of open source ([http://creativecommons.org/licenses/by/2.0/ CC-by-2.0], [http://www.apache.org/licenses/LICENSE-2.0 Apache 2.0]) sample code to read and write [[xoxo]] files in Python and Java (with Perl, PHP, ... to follow).
Also on [https://github.com/microformats/xoxo github]
== Python ==
== Python ==
See [[xoxo-sample-code-python]]
See [[xoxo-sample-code-python]]
Line 8: Line 10:
See [[xoxo-sample-code-java]]
See [[xoxo-sample-code-java]]


= PHP =
== PHP ==
== xoxolib.php ==
See [[xoxo-sample-code-php]]
<code><pre><nowiki>
<?
function getKind($struct)
{
    if (!is_array($struct)) return 'string';
    if (!isset($struct[0]))
        $result = 'dictionary';
    else if (array_keys($struct)==range(0,count($struct)-1))
        $result = 'list';
    else
        $result = 'dictionary';
    return $result;
}
function makeXOXO($struct,$className='')
{
    $s='';
    $kind = getKind($struct);
    #echo "$kind:\n";
    #var_dump($struct);
    if ($kind=='list')
        {
        if ($className)
            $s .= "<ol class=\"$className\">";
        else
            $s .= "<ol>";
        foreach ($struct as $key => $value)
            $s .= "<li>" . makeXOXO($value) ."</li>";
        $s .="</ol>";
        }
    else if ($kind=='dictionary')
        {
        if (isset($struct['url']))
            {
            $s .='<a href="' .$struct['url']. '" ';
            if (isset($struct['text']))
                $text= $struct['text'];
            else if (isset($struct['title']))
                $text= $struct['title'];
            else
                $text= $struct['url'];
            foreach (array('title','rel','type') as $attr)
                if (isset($struct[$attr]))
                    {
                    $s .= "$attr=\"" . $struct[$attr] .'" ';
                    unset($struct[$attr]);
                    }
            $s .= ">" . makeXOXO($text) ."</a>";
            unset($struct['url'],$struct['text']);
            }
        if (count($struct))
            {
            $s .="<dl>";
            foreach ($struct as $key => $value)
                $s .= "<dt>$key</dt><dd>". makeXOXO($value) . "</dd>";
            $s .= "</dl>";
            }
        }
    else
        $s .= "$struct";
    #echo "returned $s\n";
    return $s;
}
function toXOXO($struct,$addHTMLWrapper=FALSE,$cssUrl='')
{
    if (getKind($struct) != 'list')
        $struct = array($struct);
    $xoxo = makeXOXO($struct,'xoxo');
    if ($addHTMLWrapper)
        {
        $s= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head profile=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
        if ($cssUrl) $s .="<style type=\"text/css\" >@import \"$cssUrl\";</style>";
        $s .="</head><body>$xoxo</body></html>";
        return $s;
        }
    return $xoxo;
}
 
 
function pushStruct($struct,&$structstack,&$xostack,$structType)
{
    if (is_array($struct) && $structType=='dict' && count($structstack) && is_array(end($structstack)) && isset($structstack[count($structstack)-1]['url']) && end($structstack) != end($xostack))
        $xostack[] = &$structstack[count($structstack)-1]; # put back the <a>-made one for extra def's
    else
        {
        $structstack[]=$struct;
        $xostack[]=&$structstack[count($structstack)-1];
        }
}
 
function fromXOXO($html)
{
    $structs=array();
    $xostack=array();
    $textstack=array('');
    $dumpStacks=0;
    $p = xml_parser_create();
    xml_parse_into_struct($p, $html, $xoxoVals, $xoxoIndex);
    xml_parser_free($p);
 
  if($dumpStacks)
        {
        echo "<pre>";
        var_dump($xoxoVals);
        var_dump($xoxoIndex);
        echo "</pre>";
        }
    $howmany = sizeof($xoxoVals);
   
    #echo "<pre>";
    $x = $xoxoIndex['OL'];
    for ($x=0;$x<$howmany;++$x)
        {
        if ($xoxoVals[$x]['tag'] == 'OL' || $xoxoVals[$x]['tag'] == 'DL'|| $xoxoVals[$x]['tag'] == 'UL')
            {
            if ($xoxoVals[$x]['tag'] == 'DL')
                $structType = 'dict';
            else
                $structType = 'list';
            if ($xoxoVals[$x]['type'] == 'open')
                pushStruct(array(),$structs,$xostack,$structType);
            if ($xoxoVals[$x]['type'] == 'close')
                array_pop($xostack);
            if($dumpStacks)
                {
                echo $xoxoVals[$x]['type'] .' ' . $xoxoVals[$x]['tag'] .":\n";
                var_dump($structs);
                var_dump($xostack);
                }
            }
        if ($xoxoVals[$x]['tag'] == 'LI')
            {
            if ($xoxoVals[$x]['type'] == 'complete')
              array_push($xostack[count($xostack)-1],$xoxoVals[$x]['value']);
            if ($xoxoVals[$x]['type'] == 'close')
                {
                array_push($xostack[count($xostack)-1],array_pop($structs));
                }
            if($dumpStacks)
                {
                echo $xoxoVals[$x]['type'] .' ' . $xoxoVals[$x]['tag'] .":\n";
                var_dump($structs);
                var_dump($xostack);
                }
            }
 
        if ($xoxoVals[$x]['tag'] == 'DT')
            {
            if ($xoxoVals[$x]['type'] == 'complete')
                array_push($textstack,$xoxoVals[$x]['value']);
            }
        if ($xoxoVals[$x]['tag'] == 'DD')
            {
            if ($xoxoVals[$x]['type'] == 'complete')
                {
                $key = array_pop($textstack);
                $xostack[count($xostack)-1][$key] = $xoxoVals[$x]['value'];
                }
            if ($xoxoVals[$x]['type'] == 'close')
                {
                $key = array_pop($textstack);
                $xostack[count($xostack)-1][$key] =array_pop($structs);
                }
          if($dumpStacks)
                {
                echo $xoxoVals[$x]['type'] .' ' . $xoxoVals[$x]['tag'] .":\n";
                var_dump($structs);
                var_dump($xostack);
                }
            }
        if ($xoxoVals[$x]['tag'] == 'A')
            {
            if ($xoxoVals[$x]['type'] == 'complete')
                {
                $attrs = $xoxoVals[$x]['attributes'];
                $dict=array();
                foreach ($attrs as $key=> $value)
                    {
                    if ($key=='HREF')
                        $dict['url'] = $value;
                    else
                        $dict[strtolower($key)] = $value;
                    }
                $val = $xoxoVals[$x]['value'];
                if (isset($val) && ($val != $dict['title']) && ($val != $dict['url']))
                    $dict['text'] = $val;
                pushStruct($dict,$structs,$xostack,'dict');
                array_pop($xostack);
                if($dumpStacks)
                    {
                    echo $xoxoVals[$x]['type'] .' ' . $xoxoVals[$x]['tag'] .":\n";
                    var_dump($structs);
                    var_dump($xostack);
                    }
              }
            }
        }
    #echo "</pre>";
  while (count($structs) == 1 && getKind($structs) == 'list')
        $structs = $structs[0];
    return $structs;
}
?>
</nowiki></pre></code>
 
== xoxotest.php ==
<code><pre><nowiki>
<?
include("xoxolib.php");
function assertEqual($testname,$str1,$str2)
{
if ($str1 == $str2)
    echo "<h3>&#x221a; $testname </h3>";
else
    {
    echo "<h3><big>&#x2639;</big> $testname failed</h3>";
    echo "<dl><dt>expected</dt>\n<dd>$str1</dd>\n<dt>returned</dt>\n<dd>$str2</dd>\n<dl>";
   
    }
}
 
function assertArrayEqual($testname,$expected,$returned)
{
if ($expected == $returned)
    echo "<h3>&#x221a; $testname </h3>";
else
    {
    echo "<h3><big>&#x2639;</big> $testname failed</h3>";
    echo "<dl><dt>expected</dt>\n<dd><pre>";
    var_dump($expected);
    echo "</pre></dd>\n<dt>returned</dt>\n<dd><pre>";
    var_dump($returned);
    echo "</pre></dd>\n<dl>";
   
    }
}
 
function failIfEqual($testname,$str1,$str2)
{
if ($str1 != $str2)
    echo "<h3>&#x221a; $testname</h3>";
else
    {
    echo "<h3><big>&#x2639;</big> $testname failed</h3>";
    echo "<dl><dt>both were</dt><dd>$str1</dd><dl>";
   
    }
}
 
 
$l = array('1','2','3');
$html = toXOXO($l);
assertEqual('make xoxo from list','<ol class="xoxo"><li>1</li><li>2</li><li>3</li></ol>',$html);
 
$s = 'test';
$html = toXOXO($s);
assertEqual("make xoxo from a string",'<ol class="xoxo"><li>test</li></ol>',$html);
$htmlwrap = toXOXO($s,TRUE);
failIfEqual("make sure wrapped and unwrapped differ",html,htmlwrap);
assertEqual("make wrapped xoxo from a string",'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head profile=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><ol class="xoxo"><li>test</li></ol></body></html>',$htmlwrap);
$csswrap = toXOXO($s,TRUE,"reaptest.css");
assertEqual("make wrapped xoxo with css link from a string",'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head profile=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><style type="text/css" >@import "reaptest.css";</style></head><body><ol class="xoxo"><li>test</li></ol></body></html>',$csswrap);
$l2 = array('1',array('2','3'));
$html = toXOXO($l2);
assertEqual('make xoxo from nested list','<ol class="xoxo"><li>1</li><li><ol><li>2</li><li>3</li></ol></li></ol>',$html);
$d = array(test=>'1');
$html = toXOXO($d);
assertEqual('make xoxo from 1-element dictionary','<ol class="xoxo"><li><dl><dt>test</dt><dd>1</dd></dl></li></ol>',$html);
$d = array(test=>'1',name=>Kevin);
$html = toXOXO($d);
assertEqual('make xoxo from dictionary','<ol class="xoxo"><li><dl><dt>test</dt><dd>1</dd><dt>name</dt><dd>Kevin</dd></dl></li></ol>',$html);
$d = array('url'=>'http://example.com/more.xoxo','title'=>'sample url','type'=>"text/xml",'rel'=>'help','text'=>'an example');
$html = toXOXO($d);
assertEqual('make xoxo from dictionary with url','<ol class="xoxo"><li><a href="http://example.com/more.xoxo" title="sample url" rel="help" type="text/xml" >an example</a></li></ol>',$html);
$d = array('url'=>'http://example.com/more.xoxo','title'=>'sample url','type'=>"text/xml",'rel'=>'help','text'=>'an example','thing'=>'and another thing...');
$html = toXOXO($d);
assertEqual('make xoxo from dictionary with url and thing','<ol class="xoxo"><li><a href="http://example.com/more.xoxo" title="sample url" rel="help" type="text/xml" >an example</a><dl><dt>thing</dt><dd>and another thing...</dd></dl></li></ol>',$html);
$d = array('url'=>'http://example.com/more.xoxo','title'=>'sample url','type'=>"text/xml",'rel'=>'help','text'=>'an example','list'=>array('and', 'another','thing...'));
$html = toXOXO($d);
assertEqual('make xoxo from dictionary with url and list','<ol class="xoxo"><li><a href="http://example.com/more.xoxo" title="sample url" rel="help" type="text/xml" >an example</a><dl><dt>list</dt><dd><ol><li>and</li><li>another</li><li>thing...</li></ol></dd></dl></li></ol>',$html);
$l = array('3',array('a'=>'2'));
$html = toXOXO($l);
assertEqual('make xoxo from dict in list','<ol class="xoxo"><li>3</li><li><dl><dt>a</dt><dd>2</dd></dl></li></ol>',$html);
 
$l = array('3','2','1');
$html = toXOXO($l);
$newdl= fromXOXO($html);
assertArrayEqual('list to xoxo and back',$l,$newdl);
$l = array('1',array('a','b'));
$html = toXOXO($l);
$newdl= fromXOXO($html);
assertArrayEqual('list of lists to xoxo and back',$l,$newdl);
 
$l= array('3',array('a','2'),array('b',array('1',array('c','4'))));
$html = toXOXO($l);
$newdl= fromXOXO($html);
assertArrayEqual('list of list of lists to xoxo and back',$l,$newdl);
$d = array(test=>'1',name=>Kevin);
$html = toXOXO($d);
$newd= fromXOXO($html);
assertArrayEqual('dictionary to xoxo and back',$d,$newd);
 
$l = array('3',array('a'=>'2'),array('b'=>'1','c'=>'4'));
$html = toXOXO($l);
$newdl= fromXOXO($html);
assertArrayEqual('list of dicts to xoxo and back',$l,$newdl);
assertEqual('list of dicts to xoxo and back',$html,toXOXO($newdl));
$l = array('one'=>array('a'=>'2','b'=>'3'),'two'=>array('c'=>'4'));
$html = toXOXO($l);
$newdl= fromXOXO($html);
assertArrayEqual('dict of dicts to xoxo and back',$l,$newdl);
assertEqual('dict of dicts to xoxo and back',$html,toXOXO($newdl));
$l = array('one'=>array('a'=>'2','b'=>'3'),'url'=>'http://example.com');
$html = toXOXO($l);
$newdl= fromXOXO($html);
assertArrayEqual('dict of dicts with url to xoxo and back',$l,$newdl);
assertEqual('dict of dicts with url to xoxo and back',$html,toXOXO($newdl));
$d = array('test'=> array('1','2'),
'name'=> 'Kevin','nestlist'=> array('a',array('b','c')),
'nestdict'=>array('e'=>'6','f'=>'7'));
$html = toXOXO($d);
$newd= fromXOXO($html);
assertArrayEqual('dictionary of lists  to xoxo and back',$d,$newd);
 
$d=fromXOXO('<ol>bad<li><dl>worse<dt>good</dt><dd>buy</dd> now</dl></li></ol>');
assertArrayEqual('make sure text outside &lt;li&gt; etc is ignored',array(good=>buy),$d);
 
$l=fromXOXO('<ol><li>bad<dl><dt>good</dt><dd>buy</dd></dl>worse</li><li>bag<ol><li>OK</li></ol>fish</li></ol>');
assertArrayEqual('make sure text within &lt;li&gt; but outside a subcontainer is ignored',array(array(good=>buy),array('OK')),$l);
 
$xoxoSample= "<ol class='xoxo'>
  <li>
    <dl>
        <dt>text</dt>
        <dd>item 1</dd>
        <dt>description</dt>
        <dd> This item represents the main point we're trying to make.</dd>
        <dt>url</dt>
        <dd>http://example.com/more.xoxo</dd>
        <dt>title</dt>
        <dd>title of item 1</dd>
        <dt>type</dt>
        <dd>text/xml</dd>
        <dt>rel</dt>
        <dd>help</dd>
    </dl>
  </li>
</ol>";
$d = fromXOXO($xoxoSample);
$d2=array('text'=>'item 1',
    'description'=>" This item represents the main point we're trying to make.",
    'url'=>'http://example.com/more.xoxo',
    'title'=>'title of item 1',
    'type'=>'text/xml',
    'rel'=>'help');
assertArrayEqual('unmung some xoxo with spaces in and check result is right',$d2,$d);
 
$xoxoSample= "<ol class='xoxo'>
  <li>
    <dl>
        <dt>text</dt>
        <dd>item 1</dd>
        <dt>url</dt>
        <dd>http://example.com/more.xoxo</dd>
        <dt>title</dt>
        <dd>title of item 1</dd>
        <dt>type</dt>
        <dd>text/xml</dd>
        <dt>rel</dt>
        <dd>help</dd>
    </dl>
  </li>
</ol>";
$d = fromXOXO($xoxoSample);
$smartxoxoSample= "<ol class=\"xoxo\">
  <li><a href=\"http://example.com/more.xoxo\"
        title=\"title of item 1\"
        type=\"text/xml\"
        rel=\"help\">item 1</a>
<!-- note how the \"text\" property is simply the contents of the <a> element -->
  </li>
</ol>";
$d2 = fromXOXO($smartxoxoSample);
assertArrayEqual('unmung some xoxo with &lt;a href= rel= etc in and check result is right',$d,$d2);
$xoxoSample= "<ol class='xoxo'>
  <li>
    <dl>
        <dt>text</dt>
        <dd>item 1</dd>
        <dt>description</dt>
        <dd> This item represents the main point we're trying to make.</dd>
        <dt>url</dt>
        <dd>http://example.com/more.xoxo</dd>
        <dt>title</dt>
        <dd>title of item 1</dd>
        <dt>type</dt>
        <dd>text/xml</dd>
        <dt>rel</dt>
        <dd>help</dd>
    </dl>
  </li>
</ol>";
$d = fromXOXO($xoxoSample);
$smartxoxoSample= "<ol class=\"xoxo\">
  <li><a href=\"http://example.com/more.xoxo\"
        title=\"title of item 1\"
        type=\"text/xml\"
        rel=\"help\">item 1</a>
<!-- note how the \"text\" property is simply the contents of the <a> element -->
      <dl>
        <dt>description</dt>
          <dd> This item represents the main point we're trying to make.</dd>
      </dl>
  </li>
</ol>";
$d2 = fromXOXO($smartxoxoSample);
assertArrayEqual('unmung some xoxo with &lt;a href= rel= etc in and check result is right',$d,$d2);
 
$d=array('url'=>'http://example.com/more.xoxo','title'=>'sample url','type'=>"text/xml",'rel'=>'help','text'=>'an example');
$html=toXOXO($d);
assertArrayEqual('round trip url to href to url',$d,fromXOXO($html));
 
$d=array('url'=>'http://example.com/more.xoxo','title'=>'sample url','type'=>"text/xml",'rel'=>'help');
$html=toXOXO($d);
assertArrayEqual('round trip url to href to url (no text)',$d,fromXOXO($html));
 
$d=array('url'=>'http://example.com/more.xoxo');
$html=toXOXO($d);
assertArrayEqual('round trip url to href to url (just url)',$d,fromXOXO($html));
$kmattn=<<<ENDATTN
<ol class="xoxo"><li><a href="http://www.boingboing.net/" title="Boing Boing Blog" >Boing Boing Blog</a><dl><dt>alturls</dt><dd><ol><li><a href="http://boingboing.net/rss.xml" >xmlurl</a></li></ol></dd><dt>description</dt><dd>Boing Boing Blog</dd></dl></li><li><a href="http://www.financialcryptography.com/" title="Financial Cryptography" >Financial Cryptography</a><dl><dt>alturls</dt><dd><ol><li><a href="http://www.financialcryptography.com/mt/index.rdf" >xmlurl</a></li></ol></dd><dt>description</dt><dd>Financial Cryptography</dd></dl></li><li><a href="http://hublog.hubmed.org/" title="HubLog" >HubLog</a><dl><dt>alturls</dt><dd><ol><li><a href="http://hublog.hubmed.org/index.xml" >xmlurl</a></li><li><a href="http://hublog.hubmed.org/foaf.rdf" >foafurl</a></li></ol></dd><dt>description</dt><dd>HubLog</dd></dl></li></ol>
ENDATTN;
$d=fromXOXO($kmattn);
$newattn = toXOXO($d);
$d2=fromXOXO($newattn);
assertArrayEqual('attention double round-trip',$d,$d2);
assertEqual('attention triple round-trip',$newattn,toXOXO($d2));
assertEqual('attention one round-trip',$kmattn,$newattn);
$d=array(array(url=>"http://www.boingboing.net/",title=>"Boing Boing Blog","alturls"=>array(array("url"=>"http://boingboing.net/rss.xml","text"=>
"xmlurl")),"description"=>"Boing Boing Blog"),array(url=>"http://www.financialcryptography.com/",title=>"Financial Cryptography","alturls"=>array(array("url"=>"http://www.financialcryptography.com/mt/index.rdf","text"=>
"xmlurl")),"description"=>"Financial Cryptography"));
$attn=<<<ENDATTN
<ol class="xoxo"><li><a href="http://www.boingboing.net/" title="Boing Boing Blog" >Boing Boing Blog</a><dl><dt>alturls</dt><dd><ol><li><a href="http://boingboing.net/rss.xml" >xmlurl</a></li></ol></dd><dt>description</dt><dd>Boing Boing Blog</dd></dl></li><li><a href="http://www.financialcryptography.com/" title="Financial Cryptography" >Financial Cryptography</a><dl><dt>alturls</dt><dd><ol><li><a href="http://www.financialcryptography.com/mt/index.rdf" >xmlurl</a></li></ol></dd><dt>description</dt><dd>Financial Cryptography</dd></dl></li></ol>
ENDATTN;
assertEqual('attention encode',$attn,toXOXO($d));
 


?>
</nowiki></pre></code>
== other implementations ==
== other implementations ==
* [http://boxtheweb.mihopa.net/code/apis/xoxo2array.php.txt xoxo2array.php]
* [http://boxtheweb.mihopa.net/code/apis/xoxo2array.php.txt xoxo2array.php]
* [http://boxtheweb.mihopa.net/code/apis/array2xoxo.php.txt array2xoxo.php]
* [http://boxtheweb.mihopa.net/code/apis/array2xoxo.php.txt array2xoxo.php]
* [http://boxtheweb.mihopa.net/code/apis/#outlineclasses Outline Classes]
* [http://boxtheweb.mihopa.net/code/apis/#outlineclasses Outline Classes]

Latest revision as of 03:45, 26 October 2015

XOXO Sample Code

A whole bunch of open source (CC-by-2.0, Apache 2.0) sample code to read and write xoxo files in Python and Java (with Perl, PHP, ... to follow).

Also on github

Python

See xoxo-sample-code-python

Java

See xoxo-sample-code-java

PHP

See xoxo-sample-code-php

other implementations