<?php if (!defined('PmWiki')) exit();
/*  Copyright 2016 Christopher J. Cox (chriscox@endlessnow.com)
    This file is ptvpopups.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.  

    Using a definition list of ptvs and values, search for words matching
    a ptv and present the ptv's value when the word is hovered over.
    To use this script, simply place it in the cookbook/ directory and add
    the line

        include_once('cookbook/definitionptvs.php');

    to a local customization file.

    This script based on ideas from {(allptvs)} markup in the Power Tools
    recipe.
*/


$RecipeInfo['PTVPopups']['Version'] = '2015-07-26';
// match text equal to a ptv and present hoverable definition using ptv's value
$MarkupExpr['ptvpopups'] = 'PTVPopups($pagename, $args[0])';
function PTVPopups($pagename, $list) {
  global $PCache, $HTMLStylesFmt; 
  $plist = (isset($list)) ? explode(',',$list) : array($pagename);
  foreach($plist as $pn) {
    $pn = MakePageName($pagename, $pn);
    PageTextVar($pn, '');
    // Double underscores now mean space
    Markup('__', '>inline', '/__/', ' ');
    foreach($PCache[$pn] as $key => $val) {
      if (substr($key,0,3)!="=p_") continue;
      $word=substr($key,3);
      $phrase=preg_replace('/__/', ' ', $word);
      if ($phrase != $word) {
        $searchword = $phrase . '|' . $word;
      } else {
        $searchword = $word;
      }
      $val=MarkupToUnstyled($pagename, $val);
      $val=preg_replace("/'/",'',$val);
      // If word is all uppercase (like an acronym), do not do case insensitive search     
      //  Prevents words like "it" being matched up with a term called "IT"
      if (preg_match('/[^A-Z]/',$word)) {  
        $i='i';
      } else {
        $i='';
      }
      Markup('searchptv'.$word, '<restore',
        "/\\b($searchword)\\b(?![^<]*>)/$i",
        "<span class='ptvpop' def='$val'>$1</span>");
      $HTMLStylesFmt["ptvpopspan"] =
        "#wikitext .ptvpop { border-bottom: 1px dashed #000; }";
      $HTMLStylesFmt["ptvpopsearch"] =
        "#wikitext .ptvpop:hover:after { background: rgba(20,0,0,0.85) none repeat scroll 0 0;border-radius: 6px;color: #fff;content: attr(def);margin-left:10px;margin-top:-10px;padding:3px 6px 3px 6px;position: absolute;max-width:300px;font-size:small; }";
    }
  }
}

?>