<?php if (!defined('PmWiki')) exit();
/*  Copyright 2005-2011 Patrick R. Michaud (pmichaud@pobox.com)
    This file is countglyphs.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 recipe adds {$Glyphs} and {$Words} markup, which are replaced
    by the number of non-space characters or words that have been generated
    prior to the line containing the markup.

    To use this recipe, simply copy it into the cookbook/ directory and
    add the following line to a local customization file.

        include_once('cookbook/countglyphs.php');
    
    Script maintained by Petko Yotov www.pmwiki.org/petko
*/
$RecipeInfo['CountGlyphs']['Version'] = '20170801';

$GlyphsToken = $KeepToken.'glyphs'.$KeepToken;
$WordsToken = $KeepToken.'words'.$KeepToken;

Markup('words-glyphs', '<{$var}', 
  '/{\\$(Words|Glyphs)}/', 'WordGlyph');
  
function WordGlyph($m) {
  global $GlyphsToken, $WordsToken, $countglyphs;
  $var = $m[1] . 'Token';
  $countglyphs=1;
  return $$var;
}

$MarkupFrameBase['posteval']['glyphs'] = 
   "if (\$GLOBALS['countglyphs']) {
      \$dehtml = preg_replace('/<.*?>/', '', \$out);
      \$out = str_replace('$GlyphsToken', 
        strlen(preg_replace('/\\\\s+/', '', \$dehtml)),
        \$out);
      \$out = str_replace('$WordsToken',
        count(preg_split('/\\\\s+/', \$dehtml, -1, PREG_SPLIT_NO_EMPTY)),
        \$out);
    }";