<?php if (!defined('PmWiki')) exit();
/*
# This adds a range of markup extensions to PmWiki 2. It combines into
# a single script a whole lot of separate items from PmWiki 1:
#    prevent WikiWords with Wiki`Word markup
#    '`.' (invisible stop) and '` ' (nonbreaking space)
#   '`-' (en dash) and `' (right quote)
#   fractions (quarter, half, three quarters)
#   em dash, plus or minus, and minus
#   left and right arrows
#   ellipsis ...
#   angle brackets
#   long vowels (macrons)
#   '/cite/' and ';small caps;'
#   [^footnote text^] and [^#^] to list footnotes
#   Q: and A: markups and Z; drop caps
#   {abbr|abbreviations} and [def|definitions]
#   =|centred text and =>right aligned text
#   {=sticky note comments=}
#   !run-in heads!and text
#   tidy :: used merely to indent
#   :: for multiple <dd> per <dt>
#   teaser markups T[:*#] Name and (:para Name#id:)
#   lazy web links (an alternative to the one from Pm)
#   automatic smart quotes in text and link text
#   page tool-tip format

    Version 2.0.16 (beta release nearly stable)

    Copyright 2004, 2005 John Rankin (john.rankin@affinity.co.nz)
    This program is free software; 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.
    
    lazy web links Copyright 2004 Patrick R. Michaud (pmichaud@pobox.com)  
*/

$HTMLStylesFmt['extend'] = "
a.createlink { color: red; }
#wikitext { line-height: 1.4em; }
#wikitext sub, #wikitext sup { line-height: 0; }
div.footnote { 
    width: 160px; 
    border-bottom: 1px solid blue;
	margin-bottom: 0.5em;
}
p.footnote {
	text-indent: -1em;
	margin-right: 3em;
	margin-left: 3em;
	margin-top: 0px;
	margin-bottom: 0.5em;
	font-size: smaller;
}
p.qanda:first-letter {
    float: left;
    font-family: Old English, Georgia, serif;
    font-size: 290%;
    line-height: 0.85em;
    margin-right: 0.3em;
    margin-bottom:-0.25em;
}
p.drop:first-letter {
    float: left;
    font-family: Old English, Georgia, serif;
    font-size: 290%;
    line-height: 0.85em;
    margin-right: 0.1em;
    margin-bottom:-0.25em;
}
del { color: red; }
ins { background-color: yellow; }
span.stickynote {
    font-size: smaller;
    float: right;
    padding: 8px;
    margin-left: 10px;
    margin-bottom: 10px;
    width: 160px;
    border-top: 1px dotted;
    border-bottom: 1px dotted;
    text-align: center;
    color: navy;
    background-color: #dcdcdc;
}
dfn  { font-style: normal; }
abbr { font-style: italic; }
abbr, dfn.definition { border-bottom: 1px dotted; }
h5.runin { display: run-in; font-size: 100%; border: none; }
div.figure {
    border: thin silver solid;
    padding: 0.3em;
}
div.figure p {
    text-align: center;
    font-style: italic;
    font-size: smaller;
    padding-top: 0.2em;
    margin: 0;
}
dd { margin-bottom: 0.5em }
b.selfreference { color: #e66e31; border-bottom: 1px dotted; }
";

## require 2 lower and upper case characters for a WikiWord
$WikiWordPattern = '[[:upper:]][[:alnum:]]*(?:[[:upper:]][[:lower:]0-9][[:lower:]0-9]|[[:lower:]0-9][[:lower:]0-9][[:upper:]]|[[:lower:]0-9][[:upper:]]+[[:lower:]0-9])[[:alnum:]]*';

#### escape character (backtick) ####
## prevent WikiWords with Wiki`Word  and `WikiWord markups
Markup("`A",'>links','/([[:alnum:].\/])?`([[:upper:]])/','$1$2');

## '`.' (invisible stop)
Markup("`.",'>links',"/`\./",'');

## '`-' (en dash) 
Markup("`-",'inline',"/`-/",'&ndash;');

## '` ' (nonbreaking space)
Markup("`s",'inline',"/`\s/",'&nbsp;');

## '`:' (middot)
Markup("`:",'inline',"/`:/",'&middot;');

## simple fractions
Markup("1/4",'inline',"/`1\/?4/",'&#188;');
Markup("1/2",'inline',"/`1\/?2/",'&#189;');
Markup("3/4",'inline',"/`3\/?4/",'&#190;');

## em dash, en dash, plus or minus, and minus
Markup("--",'inline',"/--([^-&])/",'&mdash;$1');
Markup("d-d",'>links',"/(\\d)-(\\d)/",'$1&ndash;$2');
Markup("+-",'<-d',"/\+\/?-/",'&plusmn;');
Markup("-d",'>d-d',"/([^[:alpha:]])-(\\d)/",'$1&minus;$2');

## left and right arrows
Markup("<-",'<lsa',"/&lt;-/",'&larr;');
Markup("->",'<rsa',"/-&gt;/",'&rarr;');

## ellipsis ...
Markup("...",'inline',"/\.\.\./",'&hellip;');

## angle brackets
Markup("aquo",'>links',"/&lt;&lt;(.*?)&gt;&gt;/",'&laquo;$1&raquo;');
Markup("lsa",'>aquo',"/&lt;(.*?)\|/",'&lsaquo;$1|');
Markup("rsa",'>aquo',"/\|(.*?)&gt;/",'|$1&rsaquo;');

## long vowels (macrons)
$LongVowels = array (
    'A' => '&#256;',
    'a' => '&#257;',
    'E' => '&#274;',
    'e' => '&#275;',
    'I' => '&#298;',
    'i' => '&#299;',
    'O' => '&#332;',
    'o' => '&#333;',
    'U' => '&#362;',
    'u' => '&#363;'
);
Markup("mac",'>&',"/(?:&|{)([AaEeIiOoUu])(?:m;|})/e","Macron('$1')");

function Macron($vowel) {
  global $LongVowels;
  return $LongVowels[$vowel];
}

#### inline markups ####
## '/cite/'
Markup("'/",'inline',"/'\/(.*?)\/'/",'<cite>$1</cite>');

## ';small caps;'
Markup("';",'inline',"/';(.*?);'/",
    '<span style=\'font-variant: small-caps\'>$1</span>');

## [^footnote text^] and a style to tidy line spacing
Markup("[^",'>links','/\[\^(.*?)\^\]/e',"Footnote('$1')");
Markup("^[^",'block','/^\[\^#\^\]$/e',"'<:block>'.Footnote('#')");

function Footnote($foottext) {
  static $fncount, $fntext;
  if ($foottext == "#") {
     $fncount = 0;
     $r = "<div class='footnote'>&nbsp;</div>$fntext";
     $fntext = '';
  } else {
     $fncount++;
     $r = "<sup><a href='#fn$fncount'>$fncount</a></sup>";
     $foottext = stripslashes($foottext);
     $fntext .= "<p class='footnote'><a name='fn$fncount' id='fn$fncount'><sup>$fncount</sup></a> $foottext</p>";
  }
  return $r;
}

## Q: and A: markup
Markup('q&a','<block','/^([QA]):(.*)$/','<:block><p class=\'qanda\'>$1$2</p>');

## dropcaps markup
Markup('A;','block','/^([[:upper:]]);(.*)$/','<:block><p class=\'drop\'>$1$2</p>');

## {abbr|abbreviations}, {:term:definitions} & right aligned text
if ($action=="print" || $action=="publish") {
    Markup("{|}",'>links',"/\{(.*?)\|(.*?\}?)\}/",'$1 ($2)');
    Markup("{:}",'<mac',"/\{:((?:\[\[[^\]]+\]\])?\{?[^:\}]*?\}?):(.*?\}?)\}/e","PSS('$1').' ('.Keep('$2').')'");
    Markup('^=>','block','/^=&gt;(.*)$/','<:block>');
    $hide = 2;
} else {
    Markup("{|}",'>links',"/\{(.*?)\|(.*?\}?)\}/",'<abbr title=\'$2\'>$1</abbr>');
    Markup("{:}",'<mac',"/\{:((?:\[\[[^\]]+\]\])?\{?[^:\}]*?\}?):(.*?\}?)\}/e",
        "'<dfn title='.Keep(PSS(DfnTitle('$2','$1'))).'>'.PSS('$1').'</dfn>'");
    Markup('^=>','block','/^=&gt;(.*)$/',
        '<:block><p style=\'text-align: right\'>$1</p>');
}

function DfnTitle($title,$text) {
    $title = str_replace('"','&quot;',$title);
    $title = (strstr($title,"'")) ? '"'.$title.'"' : "'$title'";
    return (preg_match('/^[[:alnum:]].*$/',$text)) ? 
        "$title class='definition'" : $title;
}

## centred text
Markup('^=|','block','/^=\|(.*)$/',
    '<:block><p style=\'text-align: center\'>$1</p>');

## {+insertions+}, {-deletions-} and {=sticky notes=}
SDV($hide, isset($HTTP_GET_VARS['hide']) ? $HTTP_GET_VARS['hide'] : 0);
$pgnum = isset($HTTP_GET_VARS['p']) ? "&p=".$HTTP_GET_VARS['p'] : '';
if ($hide) {
    Markup("{+",'inline',"/{\+(.*?)\+}/",'$1');
    Markup("{-",'inline',"/{\-(.*?)\-}/",'');
    Markup("{=",'inline',"/{=(.*?)=}/",'');
    if ($hide==1) Markup('revisions','<${fmt}','/\(:revisions:\)/',
        '[[{$Name}?hide=0'.$pgnum.' | Show revisions]]');
    else Markup('revisions','directives','/\(:revisions:\)/','');
} else { 
    Markup("{=",'inline',"/{=(.*?)=}/",'<span class=\'stickynote\'>$1</span>');
    Markup('revisions','<${fmt}','/\\(:revisions:\)/',
        '[[{$Name}?hide=1'.$pgnum.' | Hide revisions]]');
}

## run-in heads
Markup('^!!','<^!','/^!([^!]+)!(.*?)$/',
   '<:block><h5 class=\'runin\'>$1.</h5><p> $2</p>');

## figure captions
Markup('fig','<links',
    "/^=figure\s+((?:\[\[)?.*?$ImgExtPattern\"([^\"]*)\"(?:\]\])?)\s*(.*?)$/e",
    "'<:block><div class=\'figure\'><p>'.PSS('$1').'</p><p>'.
    (('$3'=='') ? PSS('$2') : PSS('$3')).'</p></div>'");

## tidy :: used merely to indent
Markup('^::2: :','<^: :2->','/^(:+)(:[^:]+)$/','$1 $2');
Markup('^: :2->','<block','/^(:+)\\s+:/e',
    "str_replace(':','-','$1').'&gt;'");

## :: for multiple <dd> per <dt>
Markup('::','<block','/^(:+.*?:)(.*?::.*?)+$/e',
    "PSS('$1').str_replace('::','</dd><dd>',PSS('$2'))");

## teaser markups T[:*#] Name and (:para Name#id:)
Markup('para','directives',"/\(:para\s+(.+?)(?:#([^:\s]+))?:\)/e",
    "TeaseParagraph(\$pagename,'$1','$2')");
Markup('tfl','directives',"/^T([:*#]+)\s*(\[\[.+?\]\])/e",
    "TeaserFL(\$pagename,'$1','$2')");
Markup('tww','directives',
    "/^T([:*#]+)\s*((?:$GroupPattern([\/.]))?$WikiWordPattern)/e",
    "Teaser(\$pagename,'$1','$2')");
SDV($ParaBadAnchorFmt,"'''\$Anchor''' \$[not found in] \$PageName\n");

function TeaserFL($pagename,$markup,$linkword) {
  return "$markup$linkword: " .
        TeaseParagraph($pagename,FLRef($linkword),'');
}

function FLRef($linkword) {
  $l = preg_replace('/\\s*\\|[^\\]]+/','',$linkword);
  $l = preg_replace('/[^\\]]+-+&gt;\\s*/','',$l);
  return $l;
}

function Teaser($pagename,$markup,$linkword) {
  return "$markup$linkword: " . TeaseParagraph($pagename,$linkword,'');
}

function TeaseParagraph($pagename,$teasername,$teaseranch) {
  global $ParaBadAnchorFmt;
  $tname = MakePageName($pagename,$teasername);
  if ($tname==$pagename) return '';
  $tpage=RetrieveAuthPage($tname,'read',false,'');
  $ttext=$tpage['text'];
  $tgroup = FmtPageName('$Group',$tname);
  if ($teaseranch=='')
      $tpara = CleanParagraph($pagename,$tgroup,
                    substr($ttext,0,strpos($ttext."\n","\n")));
  elseif (preg_match("/\\[\\[#+$teaseranch\\]\\]\\n?([^\\n]+)/",$ttext,$m))
      $tpara = CleanParagraph($pagename,$tgroup,$m[1]);
  else
      $tpara = str_replace('$Anchor',$teaseranch,
                    FmtPageName($ParaBadAnchorFmt,$tname));
  return $tpara;
}

function CleanParagraph($pagename,$group,$para) {
  global $GroupPattern,$WikiWordPattern;
  $pgroup = FmtPageName('$Group',$pagename);
  $p = preg_replace("/^[#*!]+\s*/","",$para);
  $p = preg_replace("/^:.*?:/","",$p);
  $p = preg_replace("/`\\..*?$/","...",$p);
  $p = preg_replace("/\\[@(.*?)@\\]/","@@[=$1=]@@",$p);
  $p = preg_replace("/\\[=(.*?)=\\]/e",'Keep(PSS("$1"))',$p);
  $p = preg_replace("/\\[\\[#.*?\\]\\]/","",$p);
  $p = preg_replace("/\\b(($GroupPattern([\\/.]))?$WikiWordPattern)/e",
          'QualifyWLink($pgroup,$group,"$1")',$p);
  $p = preg_replace("/\\[\\[(.*?)\\]\\]/e",
          "'[['.QualifyFLink('$pgroup','$group','$1').']]'",$p);
  return FmtPageName(preg_replace("/{(\\$.*?)}/",'$1',$p),$pagename);
}

function QualifyWLink($pgroup,$group,$link) {
  $g = ($pgroup==$group) ? '' : "$group/";
  return (preg_match("/[.\\/]/",$link)) ? $link : "$g$link";
}

function QualifyFLink($pgroup,$group,$link) {
  $g = ($pgroup==$group) ? '' : "$group/";
  $l = FLRef($link);
  return (preg_match("/[.\\/]/",$l)) ? $link : 
            str_replace("$l","$g$l",$link);
}

## lazy web links (an alternative to the one from Pm)
Markup('lazyweb','<wikilink',
  "/\\bwww\\.[^\\s$UrlExcludeChars]*[^\\s.,?!$UrlExcludeChars]/e",
  "Keep(MakeLink(\$pagename,'http://$0','$0'),'L')");

## bare wikilinks with spaced wikiwords
Markup('wikilink','>@wikilink',"/\\b($GroupPattern([\\/.]))?($WikiWordPattern)/e",
  "Keep(WikiLinkSpaced(\$pagename,'$0'),'L')");
  
$SpaceWikiWords = 1;
$AsSpacedFunction = 'SpaceWikiWords';
$SpaceWikiWordsFunction = 'SpaceWikiWords';
$RecentChangesFmt = array(
  'Main.AllRecentChanges' => 
    '* [[$Group.$Title]]  . . . $CurrentTime by $AuthorLink',
  '$Group.RecentChanges' =>
    '* [[$Group/$Title]]  . . . $CurrentTime by $AuthorLink');
$DefaultPageTextFmt = 'Describe [[$Group/$Title]] here.';
$FPLByGroupIFmt = "<dd><a href='\$PageUrl'>\$Title</a></dd>";
$StopList = array(
		'A',
		'An',
		'And',
		'But',
		'By',
		'For',
		'From',
		'In',
		'Is',
		'It',
		'Of',
		'On',
		'Or',
		'The',
		'To',
            );
$UnspacedList = array(
        'Mac ',
        'Mc ',
        'Pm Wiki',
        'Side Bar'
            );

function SpaceWikiWords($text) {
  global $StopList,$UnspacedList;
#  $text = AsSpaced($text);
  $text = preg_replace('/([[:lower:]])([[:upper:]\\d])/','$1 $2',$text);
  $text = preg_replace('/([[:upper:]\\d])([[:upper:]][[:lower:]\\d])/',
    '$1 $2',$text);
  foreach((array)$StopList as $s)
    $text = preg_replace("/(\\s$s\\s)/e","strtolower('$1')",$text);
  foreach((array)$UnspacedList as $u)
    $text = str_replace($u,str_replace(' ','',$u),$text);
  return $text;
}

function WikiLinkSpaced($pagename,$word) {
  global $LinkWikiWords,$SpaceWikiWords,$SpaceWikiWordsFunction;
  $word = ($SpaceWikiWords) ? $SpaceWikiWordsFunction($word) : $word;
  $text = preg_replace('!.*/!','',$word);
  if (!$LinkWikiWords) return $text;
  return MakeLink($pagename,$word,$text);
}

## smart quotes
Markup('<n>','<squo',"/\s?\n\s*([^<]+?>)/",' $1');
Markup('squo','>style',"/(<.*?>['\"]*)|(.?['\"]+)/e",
    "BypassHTML(PSS('$1'),PSS('$2'))");
Markup('sq|','>inline',"/(\\[\\[[^|\\]]+\\|)(.*?)(\\]\\])/e",
  "'$1'.SmartenLinkText(PSS('$2')).'$3'");
Markup('sq->','>inline',"/(\\[\\[)([^\\]]+?)(-+&gt;.*?\\]\\])/e",
  "'$1'.SmartenLinkText(PSS('$2')).'$3'");

function SmartenLinkText($txt) {
  global $LinkPattern,$UrlExcludeChars,$ImgExtPattern;
  if (!preg_match("/($LinkPattern)([^$UrlExcludeChars]+$ImgExtPattern)/",$txt)) 
        $txt = preg_replace("/(<.*?>['\"]*)|(.?['\"]+)/e",
                "BypassHTML(PSS('$1'),PSS('$2'))",$txt);
  return $txt;
}

function BypassHTML($hstring,$qstring) {
  if ($qstring=='') {
     $qstring = preg_replace("/.*>/",'',$hstring);
     $hstr = preg_replace("/>.*/",'>',$hstring);
     if ($qstring=='') return $hstr;
     else { if (strstr($hstr,"</")) $qstring = "`" . $qstring;
            return $hstr . SmartenQuotes($qstring); }
  }
  else return SmartenQuotes($qstring);
}

function SmartenQuotes($chars) {
  $s = 0;  $r = '';
  if ($chars[0] =="'" || $chars[0] == '"') {
      $quotes = $chars;
      $char = '';
  } else {
      $quotes = substr($chars,1);
      $char = $chars[0];
      if (strstr("0123456789",$char)) {
         $p = ($quotes[0]=="'") ? "p" : "P";
         $r = "$char&$p" . "rime;";
         $s = 1;
         $char = "`";
      }
  }
  $hands = array('l','r');
  if ($char=="" || strstr(" =-[(",$char)) $hi = 0;
  else $hi = 1;
  if ($char=="`") $char = "";
  $r .= $char;
  $prevq = "x";
  for ($i=$s;$i<strlen($quotes);$i++) {
      $q = ($quotes[$i]=="'") ? "s" : "d";
      if ($q==$prevq) $hi = 1 - $hi;
      $r .= "&$hands[$hi]$q" . "quo;";
      $prevq = $q;
  }
  return $r;
}

## page self-reference format and tool-tip format
SDV($WikiStylePattern,'%%|%[A-Za-z][-,=:#\\w\\s\'"]*%');
$LinkCleanser = array(
    '/`\..*?$/' => '...',
    "/\\[\\[([^|\\]]+)\\|\\s*(.*?)\\]\\]($SuffixPattern)/e" =>
            "MakeLink(\$pagename,PSS('$1'),PSS('$2'),'$3','\$LinkText')",
    "/\\[\\[([^\\]]+?)\\s*-+&gt;\\s*(.*?)\\]\\]($SuffixPattern)/e" =>
            "MakeLink(\$pagename,PSS('$2'),PSS('$1'),'$3','\$LinkText')",
    '/\\[\\[#([A-Za-z][-.:\\w]*)\\]\\]/' => "",
    "/\\[\\[(.*?)\\]\\]($SuffixPattern)/e" =>
            "MakeLink(\$pagename,PSS('$1'),NULL,'$2','\$LinkText')",
    '/[\\[\\{](.*?)\\|(.*?)[\\]\\}]/' => '$1',
    "/`(($GroupPattern([\\/.]))?($WikiWordPattern))/" => '$1'
            );
$oLinkPageFunction = $LinkFunctions['<:page>'];
$LinkFunctions['<:page>'] = 'LinkPageTitle';
$LinkPageSelfFmt = "<b class='selfreference'>\$LinkText</b>";
$LinkPageExistsTitleFmt = 
    "<a class='wikilink' href='\$LinkUrl' title=\$ToolTip>\$LinkText</a>";

function LinkPageTitle($pagename,$imap,$path,$title,$txt,$fmt=NULL) {
    global  $oLinkPageFunction,$LinkPageExistsTitleFmt,$UrlExcludeChars;
    if ($fmt!='') 
        return $oLinkPageFunction($pagename,$imap,$path,$title,$txt,$fmt);
    if (preg_match("/^([^#?]+)(?:#([^\\s$UrlExcludeChars]*))?$/",$path,$match)) {
        $tgtname = MakePageName($pagename,$match[1]); $anch=@$match[2];
        if (PageExists($tgtname) && $tgtname!=$pagename) {
            $title = TitleParagraph($tgtname,$anch);
            if ($title) { 
                $title = (strstr($title,"'")) ? '"'.$title.'"' : "'$title'";
                $fmt = str_replace('$ToolTip',$title,$LinkPageExistsTitleFmt);
            }
        }
    }
    return $oLinkPageFunction($pagename,$imap,$path,$title,$txt,$fmt);
}

function TitleParagraph($pagename,$anch) {
    global $LinkCleanser, $WikiStylePattern, $ParaBadAnchorFmt;
    $refpage = ReadPage($pagename); $para = '';
    $title = ($anch=='') ? 
        preg_match("/^(?:!+|:.*?:)\\s*(?:\\[\\[#.*?\\]\\])?([^\\n]+)/",
                $refpage['text'],$match) :
        preg_match("/\\[\\[#+$anch\\]\\]\\n?([^\\n]+)/",
                $refpage['text'],$match);
    if ($title) {
        $para = preg_replace("/!.*?$/",'',$match[1]);
        $para = preg_replace("/(''+|@@)(.*?)\\1/",'$2',$para);
        $para = preg_replace("/'([-_^;+\\/])(.*?)\\1'/",'$2',$para);
        $para = preg_replace("/\\[([@=]|[-+]+)(.*?)\\1\\]/",'$2',$para);
        $para = preg_replace("/$WikiStylePattern/",'',$para);
        foreach ($LinkCleanser as $p => $c) $para = preg_replace($p,$c,$para);
        $para = str_replace('"','&quot;',str_replace('`','',$para));
    } elseif ($anch!='')  $para = str_replace("'''","'",
    str_replace('$Anchor',$anch,FmtPageName($ParaBadAnchorFmt,$pagename)));
    return $para;
}
## ||table attributes
Markup('^||','>^||||','/^\\|\\|(.*)$/e',
  "PZZ(\$GLOBALS['BlockMarkups']['table'][0] = PSS('<table '. QuoteAttrs('$1') . '>'))");

function QuoteAttrs($attr) {
  return preg_replace('/([a-zA-Z])\\s*=\\s*([^\'"]\\S*)/',"\$1='\$2'",$attr);
}
/*
# superseded items
## category markup
SDV($CategoryGroup,'Category');
Markup('[[!','<[[','/\[\[!(.*?)\]\]/',"[[$CategoryGroup/$1]]");
*/
?>