<?php
/*
author: Timotheus Pokorra (timotheus@pokorra.de)
file: content.php
autumn 2002
feel free to use and modify
no guarantuee
*/
?>

<?php
    function showContentParagraph(&$paragraph, &$book, $printword, $realParagraph=1)
    {
		$output="";
        if ($paragraph != 0)
            foreach($paragraph as $item)
            {
                if ($item['type'] == 'struct')
                {
                     $realp = 0;
                     if ($realParagraph == -1)
                          $realp = -1;
                     $output .= showContentParagraph($item['value'], $book, $printword, $realp);
                }
                if ($item['type'] == 'pre')
				{
					$output .= spaceline($printword,1,0,0);
                    $output .= "<pre>".MakeHTMLConform($item['value'])."</pre>";
					$output .= spaceline($printword,0,0,0);
				}
                if ($item['type'] == 'bold')
				{
                    $output .= "<b>".MakeHTMLConform($item['value'])."</b>";
				}
                if ($item['type'] == 'command')
				{
                   if ($printword)
					 echo "*** todo check ***";
					if ($item['readfile'])
						readfile("http://".$_SERVER["HTTP_HOST"].$item['readfile']."&printword=".$printword);
                }
				if ($item['type'] == 'text')
                {   
				   $longtext = MakeHTMLConform($item['value']);
				   if ((!(strpos($longtext, "<TABLE") === false)|| !(strpos($longtext, "<table") === false))
					 && $printword)
						$output .= "*** todo table ***";
				   $output .= $longtext;
				   
                }
				if ($item['type'] == 'http')
                   $output .= "<a href='".$item['value']."' target=_blank>".$item['value']."</a>";
                if ($item['type'] == 'br')
					$output .= spaceline($printword,1,0,1);
				if ($item['type'] == 'keyword')
                {
                   if ($realParagraph >= 0)
                   {
                       $book['keywords'][$item['id']]['chapter'][] = $book['currentchapter'];
                       $occurence = $book['keywords'][$item['id']]['occurence']++;
					   if ($item['show'] && $item['long'])
							$output .= '<b>'.$item['long'].'</b> (';
					   else if ($item['show'] && $item['plural'])
							$output .= '<b>'.$item['id'].'s</b>';
					   else if ($item['show'])
							$output .= '<b>'.$item['id'].'</b>';
					   
						$showlinkkeyword = true;
						if (!printword && $showlinkkeyword)
						{
							 $output .= "<a name='#keyword".$item['id'].$occurence.
								"'></a><a href='#keyword".$item['id'].
                                 "'>".$item['value']."</a>";
						}
						else
						{
							$output .= "<a name='#keyword".$item['id'].$occurence.
								"'></a>".$item['value'];
						}
						if ($item['show'] && $item['long'])
							$output .= ")";
                   }
                   else $output .= $item['value'];
                }
                if ($item['type'] == 'source')
                {
                   $source = $book['sources'][$item['id']];
				   if (!$printword)
				   {
					  $link_a = "<a href='#src".$item['id']."'>";
					  $link_b = "</a>";
				   }
				   else
				   {
						$link_a = "";
						$link_b = "";
				   }
				   $output .= "[".$link_a.$item['id'];
                   if ($item['page'] != '')
                   {
					  if (strlen($item['page']) > 10)
						  $output .= ", see ".$item['page'];
                      else if (strpos($item['page'], ',') > 0
                          || strpos($item['page'], '-') > 0|| strpos($item['page'], '+') > 0)
                          $output .= ", pp. ".$item['page'];
                      else
                          $output .= ", p. ".$item['page'];
                   }
                   $output .= $link_b."]";
                }
                if ($item['type'] == 'figurelink')
                {
                   $figure = $book['figures'][$item['id']];
				   if (!$printword)
						$output .= "<a href='#fig".$figure['num']."'>fig. ".$figure['num']."</a>";
					else
						$output .= "fig. ".$figure['num'];

                }
                if ($item['type'] == 'figure')
                {
                   $figure = $book['figures'][$item['id']];
				   $text = "fig. ".$figure['num'].".: ".$figure['title'];
					$output .= spaceline($printword,1,1,2);	
                   if ($printword)
					 $output .= "*** todo figure ***";
				   $output .= "<a name='fig".$figure['num']."'><img src='".
                        $figure['src']."' border='0' alt='".$text."'><br>".
                        $text."</a>";
					$output .= spaceline($printword,1,1,2);
                }
            }
        if ($realParagraph > 0)
		{
			echo $output.spaceline($printword,0,0,2);
		}
		else
			return $output;
    }

    function showContentChapter(&$chapter, &$book, $printword)
    {
        echo "<a name='".$chapter['chapterNum']."'></a>\n";

		if ($chapter['chapterDepth'] == 1)
		{
			echo spaceline($printword,1,0,0);
		}	
        if ($chapter['chapterDepth'] >= 0 && $chapter['chapterDepth'] <= 4)
        {
             echo '<h'.($chapter['chapterDepth']).'>';
			 
			 $title = "";
			 if ($chapter['chapterDepth'] == 1)
				$title .= pagebreak($printword);
             if ($chapter['chapterDepth']<4 || $chapter['pagebreak'] == 1)
				$title .= $chapter['chapterNum'].' ';
			 $title .= showContentParagraph($chapter['title'], $book, $printword, 0);
			 if ($chapter['chapterDepth'] == 1)
				 $title=strtoupper($title);
			 echo $title;
			 echo '</h'.($chapter['chapterDepth']).">\n";
        }
        else
        {
			echo spaceline($printword,1,0,0);
            echo '<b>'.spaceline($printword,0,0,0);
            echo showContentParagraph($chapter['title'], $book, $printword, 0);
            echo "</b>".spaceline($printword,1,0,1);
        }
        if ($chapter['struct'] != 0)
            foreach($chapter['struct'] as $item)
            {
                if ($item['type'] == 'chapter')
                   showContentChapter($item['value'], $book, $printword);
                if ($item['type'] == 'paragraph')
                  showContentParagraph($item['value'], $book, $printword);
            }
    }


    function showContent(&$book, $printword)
    {
        foreach($book['chapters'] as $chapter)
        {
            showContentChapter($chapter, $book, $printword);
        }
    }
?>