DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:chem

This is an old revision of the document!


Chem plugin

Compatible with DokuWiki

2006/11/06 or later

plugin Format a molecular formula

Last updated on
2008-09-29
Provides
Syntax

The missing download url means that this extension cannot be installed via the Extension Manager. Please see Publishing a Plugin on dokuwiki.org. Recommended are public repository hosts like GitHub, GitLab or Bitbucket.

This extension has not been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues.

Tagged with chemistry, formula, science

Details and Download page

ZIP file
chem.zip (1KB)

This DokuWiki plugin formats a molecular formula easily.

Appearance C2H5O2-Na+
Without the plugin C<sub>2</sub>H<sub>5</sub>O<sub>2</sub><sup>-</sup>Na<sup>+</sup>
Using Chem plugin <chem>C2H5O2-Na+</chem>

The latest version supports to export Open Document format using ODT plugin.

Examples

C2H6

<chem>C2H6</chem>

Na2HPO4 12H2O

<chem>Na2HPO4 12H2O</chem>

C2H5O2-Na+

<chem>C2H5O2|-Na+</chem>    // | is a splitter.

Cl2H6N2Pt2+

<chem>Cl2H6N2Pt2+</chem>

Al2(SO4)3

<chem>Al2(SO4)3</chem>

another Version

The original plugin works very fine.

For those of you who have to write more chemical formulars and have some extra wishes like easier syntax, nuclid-number, and a nicer output in DokuWiki pages the following suggestion could help you.

Syntax:

92|235U3|2+

would appear as <html>23592<sub>32+</html>; the single numbers are optional so expressions like U2+, U2, 235U or 92|235U are recognized as well as all the brackets.

Unfortunately there are some limitations:

  • Due to many preg_matches the entering and leaving of DokuWiki editmenu takes quite long
  • :!: ODT-Support doesn't work - but that´s just due to a lack of regular expressions to get the formatting out. (May someone could give me a hand here)
  • :!: if you use ions and have multiple elements the shifting breaks as I just optimized it for one digit. (May someone could give me a hand here)
I'll work on it soon (in Apr, maybe ;-)iobataya 2010/04/04 22:23
I'd like to apply a new syntax for notation of isotopes in order to simplify either the DW syntax and the regular expressions.
  • Put on ^ before nucleons within a <chem> element. (→ superscript)
  • Put on _ before atomic number within a <chem> element. (→ subscript)
#EXAMPLE
^14_6C        (carbon-14)
^235_92U      (uranium-235)

What do you think ? — iobataya 2010/04/21 05:45

syntax.php:

<?php
/*
description : Plugin Chem: formatting chemical format
author      : Ikuo Obataya
email       : ikuo_obataya@symplus.co.jp
lastupdate  : 2008-09-29
license     : GPL 2 (http://www.gnu.org/licenses/gpl.html)
*/
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
class syntax_plugin_chem extends DokuWiki_Syntax_Plugin {
  function getInfo(){
    return array(
        'author' => 'Ikuo Obataya',
        'email'  => 'I.Obataya@gmail.com',
        'date'   => '2008-9-29',
        'name'   => 'Chemical Formula Plugin',
        'desc'   => 'Format chemical formula',
        'url'    => 'http://wiki.symplus.co.jp/computer/en/chem_plugin',
    );
}
 
  function getType(){ return 'formatting'; }
  function getAllowedTypes() { return array('formatting', 'substition', 'disabled'); }   
  function getSort(){ return 158; }
  function connectTo($mode) { $this->Lexer->addEntryPattern('<chem>(?=.*?</chem>)',$mode,'plugin_chem'); }
  function postConnect() { $this->Lexer->addExitPattern('</chem>','plugin_chem'); }
 
  function handle($match, $state, $pos, &$handler){
    switch ($state) {
        case DOKU_LEXER_ENTER     :return array($state, '');
        case DOKU_LEXER_UNMATCHED :return array($state, $match);
        case DOKU_LEXER_EXIT      :return array($state, '');
    }
    return array();
  }
 
  function render($mode, &$renderer, $data) {
    if($mode == 'xhtml' || $mode=='odt'){
      list($state, $match) = $data;
      switch ($state) {
        case DOKU_LEXER_ENTER:break;
        case DOKU_LEXER_UNMATCHED:
          if($mode=='xhtml'){
            // xhtml
            $renderer->doc .= $this->getChemFormat($match);
          }
		  if($mode == 'odt'){
            // Open document format
            $renderer->doc.= $this->getOdtChemFormat($match);
          }
          break;
        case DOKU_LEXER_EXIT:break;
      }
      return true;
    }
    return false;
  }
 
   function getChemFormat($raw){
    $pattern = array(	"/([A-Z][a-z]?|\)|\])([1-9][0-9]*)[\|](([\-\+][0-9]*)|([0-9]*[\-\+]))/",	/* sub+ sup: Al2|2+ , H2 , )2 , ]2 */
						"/([A-Ze][a-z]?|\)|\])(([\-\+][0-9]*)|([0-9]*[\-\+]))/",					/* only sup: Al2- , H2+ , )2+ , ]2- */
						"/([A-Z][a-z]?|\)|\])([1-9][0-9]*)/",										/* only sub: Al2 , H2 , )2 , ]2 */
						"/([0-9]*)[\|]([0-9]*)([A-Z][a-z]?)/",										/* sup + sub: 92|235U */
						"/([0-9]*)([A-Z][a-z]?)/",													/* only sup: 235U */
 
						"/Neutron/",
						"/Elektron/",
						"/Proton/",
 
						"/alpha/",
						"/beta/",
						"/gamma/");
    $replace = array(	"\${1}<sub style='position: relative; font-family: Arial, Helvetica, sans-serif; font-size: 70%;'>\${2}</sub><sup style='position: relative; left: -0.5em;font-family: Arial, Helvetica, sans-serif; font-size: 70%;'>\${3}</sup>",
						"\${1}<sup>\${2}</sup>",
						"\${1}<sub>\${2}</sub>",
						"<sup style='position: relative; right: -1.0em;font-family: Arial, Helvetica, sans-serif; font-size: 70%;'>\${2}</sup><sub style='position: relative; font-family: Arial, Helvetica, sans-serif; font-size: 70%;'>\${1}</sub>\${3}",
						"<sup>\${1}</sup>\${2}",
 
						"<sup style='position: relative; right: -0.5em;font-family: Arial, Helvetica, sans-serif; font-size: 70%;'>1</sup><sub style='position: relative; font-family: Arial, Helvetica, sans-serif; font-size: 70%;'>0</sub>n",
						"<sup style='position: relative; right: -1.0em;font-family: Arial, Helvetica, sans-serif; font-size: 70%;'>0</sup><sub style='position: relative; font-family: Arial, Helvetica, sans-serif; font-size: 70%;'>-1</sub>e",
						"<sup style='position: relative; right: -1.0em;font-family: Arial, Helvetica, sans-serif; font-size: 70%;'>0</sup><sub style='position: relative; font-family: Arial, Helvetica, sans-serif; font-size: 70%;'>+1</sub>p",
 
						"<sub style='position: relative; right: -0.5em;font-family: Arial, Helvetica, sans-serif; font-size: 70%;'>2</sub><sup style='position: relative; font-family: Arial, Helvetica, sans-serif; font-size: 70%;'>4</sup>He",
						"e<sup>-</sup>",
						"&gamma;");
    return preg_replace($pattern,$replace,$raw);
  }
 
 
  function getOdtChemFormat($raw){
    $c = $this->getChemFormat($raw);
    $pattern = array(	"/<sup([^>]*)>([^<]+)<\/sup>/",
						"/<sub([^>]*)>([^<]+)<\/sub>/");
    $replace = array(	"<text:span text:style-name=\"sup\">\${2}</text:span>",
						"<text:span text:style-name=\"sub\">\${2}</text:span>"); 
    return preg_replace($pattern,$replace,$c);
  }
 
 
 }
?>
plugin/chem.1291672212.txt.gz · Last modified: 2010-12-06 22:50 by iobataya

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki