====== Chem plugin ======
---- plugin ----
description: Format a molecular formula
author : Ikuo Obataya
email : ikuo_obataya@symplus.co.jp
type : syntax
lastupdate : 2008-09-29
compatible : 2006/11/06 or later
depends :
conflicts :
similar :
tags : formula chemistry science
----
[[http://www.symplus.com/legacy/wiki/en:Chem%20plugin|Details and Download]] page
^ ZIP file ^
|{{http://wiki.symplus.co.jp/lib/exe/fetch.php/computer/source/chem.zip|chem.zip}} (1KB)|
This DokuWiki plugin formats a molecular formula easily.
^ Appearance |**C2H5O2-Na+**|
^ Without the plugin |%%C2H5O2-Na+%%|
^ Using Chem plugin |C2H5O2-Na+|
The latest version supports to export Open Document format using [[plugin:odt|ODT]] plugin.
=====Examples=====
C2H6
C2H6
Na2HPO4 12H2O
Na2HPO4 12H2O
C2H5O2-Na+
C2H5O2|-Na+ // | is a splitter.
Cl2H6N2Pt2+
Cl2H6N2Pt2+
Al2(SO4)3
Al2(SO4)3
====== 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 easyer syntax, nuclid-number and a nicer output in DW-pages the following suggestion could help you.
__Syntax__: 92|235U3|2+ would appear as 2359232+; the single numbers are optional so expressions like U2+, U2, 235U or 92|235U are recognized as well as all the brackets.
__Unfortunatelly there are some limitations__:
* Due to many preg_matches the entering and leaving of DW-editmenu takes quite long
* :!: ODT-Support doesn´t work - but that´s just due to a lack of regular expressions to get the formating 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)
__syntax.php__:
'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('(?=.*?)',$mode,'plugin_chem'); }
function postConnect() { $this->Lexer->addExitPattern('','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}\${2}\${3}",
"\${1}\${2}",
"\${1}\${2}",
"\${2}\${1}\${3}",
"\${1}\${2}",
"10n",
"0-1e",
"0+1p",
"24He",
"e-",
"γ");
return preg_replace($pattern,$replace,$raw);
}
function getOdtChemFormat($raw){
$c = $this->getChemFormat($raw);
$pattern = array( "/]*)>([^<]+)<\/sup>/",
"/]*)>([^<]+)<\/sub>/");
$replace = array( "\${2}",
"\${2}");
return preg_replace($pattern,$replace,$c);
}
}
?>