<?php if (!defined('PmWiki')) exit();
/*
    whitespace.php for pmwiki 2
    copyright 2007 Hans Bracker, for intuitive spacing and 
    preserving white spaces.
    In addition:
    Set $EnableCodeWrap = 1; for line wrapping code and preformatted text
    (entered with @@...@@ and [@....@] markup)
    Use style 'mono' for monospaced font:
    %mono% or >>mono<<
    Use >>-<< for empty line with half height
*/
$RecipeInfo['LiteralWhiteSpace']['Version'] = '2024-04-25';

# set $EnableCodeWrap = 1; for wrapping code and pre lines
SDV($EnableCodeWrap,0);

# set line height (used in injected css)
SDV($LineHeight, 1.3);
$HalfLineHeight = 0.5 * (float)$LineHeight;

# honour linebreaks
$HTMLPNewline = '<br />'; 

# spaces at beginning of lines, and continuing list elements   
Markup('^ws', '<^img', '/^(\\s+)(.*?)/', "WSIndent2");
function WSIndent2($m) {
    $i = $m[1];
    global $MarkupFrame, $HTMLPNewline;
    $icol = strlen($i);
    for($depth = count(@$MarkupFrame[0]['cs']); $depth > 0; $depth--)
    if (@$MarkupFrame[0]['is'][$depth] == $icol) {
        $MarkupFrame[0]['idep'] = $depth;
        $MarkupFrame[0]['icol'] = $icol;
        return '';
    }
    $sp = str_repeat('&ensp;',$icol);
    if (@empty($MarkupFrame[0]['cs']))
        return "<:p,1>$sp";
    else return "<:p,1>$HTMLPNewline$sp";
}

# convert spaces, but not in html tags
Markup('spacing', '>style', "/(\\s{2,7})(?![^<]*>)/", "WSReplace");
function WSReplace($m) {
  $i = $m[1];
  $icol = strlen($i);
  return str_repeat('&ensp;', $icol);
}

# honour empty lines, height set with variable in injected css
Markup(':vspace', 'fulltext','/<:vspace>/',"<p class='vspace'>&nbsp;</p>");
# half height empty line spacer >>-<<
Markup('>>-<<','inline','/&gt;&gt;-&lt;&lt;/',"<p class='halfvspace'&nbsp;></p>");
# tabs
Markup('\t','inline',"/\\t/",'<span class="indent"></span>');

# vertical spacing css rules for class='vspace' and class='halfline', and adjusting p and span
$HTMLStylesFmt['vspace'] = "  
  .halfvspace { height:".$HalfLineHeight."em; line-height:".$HalfLineHeight.";}
  .vspace { min-height:".$LineHeight."em; line-height:".$LineHeight."; margin-top:0; } 
  p, span { line-height:".$LineHeight."; margin-top:0; padding:0; }
  ";

# use mono style class for monospaced font (outside of @@...@@ and [@...@])  
$HTMLStylesFmt['mono'] = 
  ".mono { font-family: Lucida Console, monospace; } ";

##========   pre and code line wrap styles =====##  
# wrap lines of preformatted text and code
if($EnableCodeWrap==1) { 
  # whitespace wrap (perhaps copy styles to css stylesheet)
  $HTMLStylesFmt['prewrap'] = " pre.escaped, code.escaped { white-space: pre-wrap; } ";
}