<?php if (!defined('PmWiki')) exit();
/*
 * SourceBlock - Yet another source code syntax highlighter for PmWiki 2.x
 * Copyright 2005-2008 by D.Faure (dfaure@cpan.org)
 *
 * Geshi module written by and (C) Nigel McNie (oracle.shinoda@gmail.com)
 *
 * SourceHistory - A source code history keeper for PmWiki 2.x,
 * by Lionel Aubert, terrain.net@gmail.com, www.sarl-terrain.net
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * See http://www.pmwiki.org/wiki/Cookbook/SourceHistory for info.
 */
$RecipeInfo['Source']['Version'] = '2008-12-07';

Markup('sourcehistory', '>markupend',
  "/\\(:sourcehistory(\\s+.*?)?\\s*:\\)[^\\S\n]*\\[([=@])(.*?)\\2\\]/sei",
  "SourceHistoryMarkup(\$pagename, PSS('$1'), PSS('$3'))");
Markup('sourcehistoryend', '>sourcehistory',
  "/\\(:sourcehistory(\\s+.*?)?\\s*:\\)[^\\S\n]*\n(.*?)\\(:sourcee?nd:\\)/sei",
  "SourceHistoryMarkup(\$pagename, PSS('$1'), PSS('$2'))");

function SourceHistoryMarkup($pagename, $args, $block) {
// copie à peine modifiée du code de D. Faure, SourceBlockMarkup
	
//insère un code formaté pmwiki	
	
  global $SourceHistoryParams, $GESHI_PATH, $HTMLStylesFmt, $GeshiStyles,
         $GeshiConfig, $SourceHistoryFmt, $SourceHistoryDivNumber,
         $EnableSourceHistoryKeywordLinks;
  SDVA($SourceHistoryParams, array('header' => '', 'footer' => ''));
  $opt = array_merge((array)$SourceHistoryParams, ParseArgs($args));
  $getcode = SourceBlockGetCodeInit($pagename, $opt);
  if(!@in_array('trim', (array)$opt['-'])) $block = trim($block);
  if(@$opt['lang']) {
    include_once($GESHI_PATH . "/geshi.php");
    # undo PmWiki's htmlspecialchars conversion
    $block = str_replace(array('<:vspace>', '&lt;', '&gt;', '&amp;'),
                          array('', '<', '>', '&'), $block);
    $geshi =& new GeSHi($block, $opt['lang'], GESHI_LANG_ROOT);
    $geshi->enable_classes();
    SDVA($GeshiStyles, array(
    'code'  => 'font-family: monospace; font-weight: normal;',
    'line1' => 'font-family: monospace; color: black; font-weight: normal;',
    'line2' => 'font-weight: bold;',
    ));
    $geshi->set_code_style($GeshiStyles['code']);
    $geshi->set_line_style($GeshiStyles['line1'], $GeshiStyles['line2']);
    $geshi->set_header_type(GESHI_HEADER_DIV);
    if(@$opt['tabwidth']) $geshi->set_tab_width($opt['tabwidth']);
    if(@in_array('strict', (array)$opt['']))
      $geshi->enable_strict_mode();
    if(@in_array('linenum', (array)$opt['']))
      $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);
    if(@$opt['linenum']) {
      $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);
      $geshi->start_line_numbers_at($opt['linenum']);
    }
    if(@$opt['highlight']) {
      $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);
      $geshi->highlight_lines_extra(preg_split("/[\\s,;]+/", $opt['highlight']));
    }
    if(@$opt['encoding']) $geshi->set_encoding($opt['encoding']);
    $geshi->set_header_content($opt['header']);
    $geshi->set_footer_content($opt['footer']);
    $links = IsEnabled($EnableSourceHistoryKeywordLinks, false);
    if(    @in_array('link', (array)$opt['-'])) $links = false;
    elseif(@in_array('link', (array)$opt['+'])) $links = true;
    $geshi->enable_keyword_links($links);
    if($GeshiConfig) $GeshiConfig($pagename, $geshi, $args);
    @$HTMLStylesFmt['geshi_'.$geshi->overall_class.'_'.$geshi->overall_id] = $geshi->get_stylesheet();
    $txt = $geshi->parse_code();
    if($geshi->error()) $txt = $geshi->error();
  } else {
    $lines = explode("\n", $block);
    $count = count($lines);
    $linenum = "";
    if(@in_array('linenum', (array)$opt[''])) $linenum = "<ol>";
    elseif(@$opt['linenum']) $linenum = "<ol start=" . $opt['linenum'] . ">";
    $txt = "";
    for($i = 0; $i < $count; $i++)
      $txt .= ($linenum ? "<li>" : "") . $lines[$i] . ($linenum ? "</li>" : "<br/>");
    $txt = "<div>{$linenum}{$txt}" . ($linenum ? "</ol>" : "") . "</div>";
  }
  
  SDV($SourceHistoryFmt, "
<div class='sourcehistory \$class' id='\$id'>
  <div class='sourcehistorytext'>\$txt</div>
  <div class='sourcehistorylink'>\$url</div>
</div>
");

// Insertion of new code (SourceHistory)
	$name = PageVar($pagename, '$Name');
	$dern = PageVar($lastmodified, '$LastModified');

	//The name of the wiki file is converted : upcase to lowcase with underline
	$standardname = str_replace('.', '', substr($pagename, strpos($pagename, '.')));
	$standardname = ereg_replace('[A-Z]', ' ' . strtolower("\\0"), $standardname);
	$standardname = str_replace(' ', '_', trim(strtolower($standardname)));

	// Here the new files will be located in /prog directory, and the name of the file will include a 'inc.php' extension
	$filename = "prog/$standardname.inc.php";
	if( ! $fp = fopen($filename, 'w')) {
		echo "<br />&Eacute;chec de l'ouverture du fichier $filename";
	} else {
		fputs($fp, $block);
		fclose($fp);
	}
	// End of the new SourceHistory code

  SDV($HTMLStylesFmt['sourcehistory'], "
.sourcehistorylink {
  text-align: right;
  font-size: smaller;
}
.sourcehistorytext {
  padding: 0.5em;
  border: 1px solid #808080;
  color: #000000;
  background-color: #f1f0ed;
}
.sourcehistorytext div {
  font-family: monospace;
  font-size: small;
  line-height: 1;
  height: 1%;
}
.sourcehistorytext div.head,
.sourcehistorytext div.foot {
  font: italic medium serif;
  padding: 0.5em;
}
");

// l'insertion marche ici, mais est vide

  return Keep(str_replace(
    array('$class', '$id', '$url', '$txt'),
    array(@$opt['class'],
          IsEnabled($opt['id'], 'sourcehistory'.$SourceHistoryDivNumber),
          $getcode ? SourceBlockGetCodeHandler($pagename, $opt, $block) : '',
          $txt),
    $SourceHistoryFmt));
    
// l'insertion ne marche pas ici
}


/* svg de l'insertion
//global $pagename;
//global $lastmodified;
$name = PageVar($pagename, '$Name');
//echo "<br />Nom du fich : $name";
$dern = PageVar($lastmodified, '$LastModified');
//echo "<br />Dern modif du fich : $dern";

//$n = PageVar('$Name', $pagename);
//$n = PageVar('$LastModified', $lastmodified);
//$standardname = str_replace('.', '', substr($pagename, strpos($pagename, '.')));
$standardname = str_replace('.', '', substr($pagename, strpos($pagename, '.')));
$nom1 = ereg_replace('[A-Z]', ' ' . strtolower("\\0"), $standardname);
$nom2 = ereg_replace('([A-Z])', '_' . strtolower("\0"), $standardname);
$nom3 = ereg_replace("(^[^A-Z])([A-Z])", "\\0_\\1", $standardname);
$nom4 = ereg_replace("^[^A-Z]([A-Z])", "\\0_\\1", $standardname);
//echo "<br />noms 1, 2, 3, 4 : $nom1, $nom2, $nom3, $nom4";

$standardname = str_replace(' ', '_', trim(strtolower($nom1)));
//$standardname = ereg_replace("([A-Z]) ", "\\1", ucwords(strtolower(ereg_replace("[A-Z]"," \\0", $standardname))));
//$standardname = ereg_replace("([A-Za-z])([0-9])", "\\1 \\2", $standardname);

$filename = "prog/$standardname.inc.php";
if (file_exists($filename)) {
    //echo "$filename a été modifié le : " . date ("F d Y H:i:s.", filemtime($filename));
}
if( ! $fp = fopen($filename, 'w')) {
	//echo "<br />&Eacute;chec de l'ouverture du fichier $filename";
} else {
	fputs($fp, $block);
	fclose($fp);
}
*/