<?php if (!defined('PmWiki')) exit();
/*  Copyright 2007 Patrick R. Michaud (pmichaud@pobox.com)
    This file is confluence.php; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published
    by the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.  

    This script adds Confluence markup to PmWiki.  To activate this
    script, simply place this script in the cookbook/ directory
    and add the line below to a local customization file:

        include_once('cookbook/confluence.php');

*/

$RecipeInfo['Confluence']['Version'] = '2007-04-13';

$HTMLStylesFmt['confluence'] = '
  table.confluence 
    { border-collapse:collapse; }

  table.confluence td,
  table.confluence th
    { border:1px solid #cccccc; padding:2px; }

  div.panel { border-width:1px; border-color: #3c78b5; }
  div.panelTitle { 
    border-top:0px; border-left:0px; border-right:0px; 
    background-color:#f0f0f0;
    text-align: center;
    font-weight: bold;
  }
';

## h1. heading
Markup('^h1.', '^!',
  '/^h([1-6])\.\\s?(.*)$/',
  '<:block,1><h$1>$2</h$1>');

## {anchor:...} - define anchor
Markup('{anchor:', '>[[',
  '/(?>\\{anchor:([A-Za-z][-.:\\w]*))\\}/e',
  "Keep(TrackAnchors('$1') ? '' : \"<a name='$1' id='$1'></a>\", 'L')");

## [link]
Markup('[','<mailto',
  '/(?>\\[\\s*(.*?)\\])/e',
  "Keep(MakeLink(\$pagename,PSS('$1')),'L')");

## [text | link]
Markup('[|', '<[',
  '/(?>\\[([^|\\]]*)\\|\\s*)(.*?)\\s*\\]/e',
  "Keep(MakeLink(\$pagename,PSS('$2'),PSS('$1')),'L')");

## [#anchor] - jump to anchor
Markup('[#', '<[', '/(?>\\[#([A-Za-z][-.:\\w]*))\\]/e',
  "Keep(MakeLink(\$pagename, '(#)$1'), 'L')");

## [^attach.xls]  - link to attachment
Markup('[^', '<[',
  '/\\[\\^\\s*(.*?)\\]/e',
  "Keep(MakeLink(\$pagename, PSS('(Attach:)$1')), 'L')");

## [mailto:...]
Markup('[mailto:', '<[',
  '/\\[(mailto:([^\\]]+))\\]/e',
  "Keep(MakeLink(\$pagename, PSS('$1'), PSS('$2')), 'L')");

## Tables
Markup('|-table', '>^||',
  '/^\\|(.+)\\|\\s*$/e',
  "FormatTableRow(PSS('$0'), '\\|')");
$BlockMarkups['table'] = 
  array("<table class='confluence'>",'','</table>',0);

## Bold
Markup('*', 'inline',
  '/(?<!\\*)\\*(?>(\\S.*?)\\*)(?<!\\s\\*)(?!\\*)/',
  '<b>$1</b>');

## {newwindow:}
Markup('{newwindow}', '<links',
  '/\\{newwindow:[^}]*\\}(.*?)\\{newwindow\\}/',
  '%newwin%[[$1]]%%');

## {section}
Markup('{section}', '<table',
  '/^\\s*\\{section(:(.*?))?\\}/',
  "(:table width='100%' $2:)");

## {column}
Markup('{column}', 'fulltext',
  '/^\\s*\\{column(:(.*?))?\\}(.*?)^\\s*\\{column\\}/sm',
  '(:cell $2:)$3');


## {panel}
Markup('{panel}', 'fulltext',
  '/\\s*\\{panel(:(.*?))?\\}(.*?)\\{panel\\}/se',
  "ConfluencePanel(\$pagename, PSS('$2'), PSS('$3'))");

function ConfluencePanel($pagename, $options, $text) {
  $style = '';
  foreach(explode('|', $options) as $o) {
    if (!preg_match('/^\\s*(\\w+)=(.*)$/', $o, $match)) continue;
    $v = str_replace("'", "&#39;", $match[2]);
    switch ($match[1]) {
      case 'title': $title = $v; break;
      case 'borderStyle': $style .= " border-style:$v;"; break;
      case 'borderColor': $style .= " border-color:$v;"; break;
      case 'borderWidth': $style .= " border-width:$v;"; break;
      case 'bgColor'    : $style .= " background-color:$v;"; break;
    }
  }
  if ($style) { $style = "style='$style'"; }
  if (@$title) { $title = "<div class='panelTitle'>$title</div>"; }
  return "<div class='panel' $style>$title$text</div>";
}