MediaWiki

From Jmol
Revision as of 21:53, 28 March 2006 by NicolasVervelle (talk | contribs)
Jump to navigation Jump to search

MediaWiki is an open source wiki engine licensed under the GNU General Public License. It is used by many websites, including this one.

The description of Jmol Mediawiki Extension can be found here.

The source code of the current version of the extension (development version) is :

<?php
$wgExtensionFunctions[] = "wfJmolExtensionDevelop";
$currentTag = "";
$appletContents = "";
$appletScript = "";
$appletName = "";
$appletSize = "";
$appletColor = "";
$appletText = "";

function wfJmolExtensionDevelop() {
  global $wgParser;
  $wgParser->setHook( "jmolDevelop", "renderJmolDevelop" );
  $wgParser->setHook( "jmolButton", "renderJmolButton" );
}

function startElement( $parser, $name, $attrs ) {
  global $currentTag;

  $currentTag = $name;
}

function endElement( $parser, $name ) {
  global $currentTag;

  $currentTag = "";
}

function characterData( $parser, $data ) {
  global $currentTag;
  global $appletContents;
  global $appletScript;
  global $appletName;
  global $appletSize;
  global $appletColor;
  global $appletText;

  switch ($currentTag) {
  case "CONTENTS":
    $data = trim($data);
    if ($data != "") {
      $appletContents = $appletContents . "\n" . $data;
    }
    break;
  case "SCRIPT":
    $appletScript = $data;
    break;
  case "NAME":
    $appletName = $data;
    break;
  case "SIZE":
    $appletSize = $data;
    break;
  case "COLOR":
    $appletColor = $data;
    break;
  case "TEXT":
    $appletText = $data;
    break;
  default:
    break;
  }
}

function renderJmolDevelop( $input ) {
  global $appletContents;
  global $appletScript;
  global $appletName;
  global $appletSize;
  global $appletColor;

  resetValues();
  $xmlParser = xml_parser_create();
  xml_set_element_handler($xmlParser, "startElement", "endElement");
  xml_set_character_data_handler($xmlParser, "characterData");
  $input = "<jmolApplet>$input<jmolApplet>";
  if (!xml_parse($xmlParser, $input)) {
    die(sprintf(
      "XML error: %s at line %d",
      xml_error_string(xml_get_error_code($xmlParser)),
      xml_get_current_line_number($xmlParser)));
  }
  xml_parser_free($xml_parser);
  $appletContents = trim($appletContents);
  $appletContents = preg_replace("/\t/", " ", $appletContents);
  $appletContents = preg_replace("/\n/", "\\n'+\n'", $appletContents);
  $output = $output .
    "<script src='./extensions/jmol/Jmol.js'></script>\n".
    "<script>\n" .
    "jmolInitialize('./extensions/jmol');\n".
    "jmolCheckBrowser('popup', './extensions/jmol/browsercheck', 'onClick');\n".
    "jmolSetAppletColor('$appletColor');\n".
    "jmolAppletInline($appletSize, \n'$appletContents', \n'$appletScript', \n'$appletName');\n" .
    "jmolBr();\n" .
    "</script>\n"
    ;
  return $output;
}

function renderJmolButton( $input ) {
  global $appletScript;
  global $appletName;
  global $appletText;

  resetValues();
  $xmlParser = xml_parser_create();
  xml_set_element_handler($xmlParser, "startElement", "endElement");
  xml_set_character_data_handler($xmlParser, "characterData");
  $input = "<jmolApplet>$input<jmolApplet>";
  if (!xml_parse($xmlParser, $input)) {
    die(sprintf(
      "XML error: %s at line %d",
      xml_error_string(xml_get_error_code($xmlParser)),
      xml_get_current_line_number($xmlParser)));
  }
  xml_parser_free($xml_parser);
  $output = $output .
    "<script>\n" .
    "jmolSetTarget('$appletName');\n" .
    "jmolButton('$appletScript', '$appletText');\n" .
    "</script>\n"
    ;
  return $output;
}

function resetValues() {
  global $currentTag;
  global $appletContents;
  global $appletScript;
  global $appletName;
  global $appletSize;
  global $appletColor;

  $currentTag = "";
  $appletContents = "";
  $appletScript = "set spin Y 10; spin on";
  $appletName = "FahWiki";
  $appletSize = "400";
  $appletColor = "black";
  $appletText = "";
}

?>