1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3 <script:module xmlns:script="http://openoffice.org/2000/script" script:name="Main" script:language="StarBasic">' Matt McCutchen's SuperbChemistry for OpenOffice, version 1
5 ' Applies superscript and subscript formatting to chemical formulas in text.
8 ' C12345 ==> C_{12345}
10 ' Cl- ==> Cl^-
11 ' Fe3+ ==> Fe^{3+}
12 ' C1232+ ==> C_{123}^{2+}
13 ' N2- ==> N^{2-}
14 ' Exception for O and ): NO3- ==> NO_3^-, Fe(OH)2- ==> Fe(OH)_2^-
15 ' But still O12 ==> O_{12}
16 ' 4+ ==> 4+ (not a superscript by itself)
18 ' Regular expression replace in the document,
19 ' creating superscripts if superb > 0 or subscripts if superb < 0.
20 ' Used by SuperbChemistry.
21 sub SuperbReplace(doc as object, searchStr as string, replaceStr as string, superb as integer)
24 rd = doc.createReplaceDescriptor()
26 rd.SearchRegularExpression = true
27 rd.setSearchString(searchStr)
28 rd.setReplaceString(replaceStr)
30 if superb <> 0 then
31 dim replaceAttrs(1) as new com.sun.star.beans.PropertyValue
32 replaceAttrs(0).Name = "CharEscapement"
34 replaceAttrs(0).Value = 33
36 replaceAttrs(0).Value = -9
38 replaceAttrs(1).Name = "CharEscapementHeight"
39 replaceAttrs(1).Value = 58
40 rd.setReplaceAttributes(replaceAttrs)
47 ' Formats the current document
50 ' Mark candidate superscripts so we know they follow letters or ).
51 SuperbReplace(ThisComponent, "[A-Za-z)][0-9]*[-+−]", "&@l@", 0)
53 ' O and ) grab a single digit. Block it off from becoming a superscript.
54 SuperbReplace(ThisComponent, "[O)][0-9]", "&@n@", 0)
56 ' Real minus signs in superscripts.
57 SuperbReplace(ThisComponent, "-@l@", "−@l@", 0)
59 ' Make superscripts: at most one digit.
60 SuperbReplace(ThisComponent, "[0-9]?[−+]@l@", "@q@&", 1)
62 ' Remove the O and ) markers.
63 SuperbReplace(ThisComponent, "@n@", "", 0)
65 ' Mark off subscripts: as many digits as we can still grab.
66 SuperbReplace(ThisComponent, "[A-Za-z)][0-9]+", "&@n@", 0)
68 ' Make subscripts.
69 SuperbReplace(ThisComponent, "[0-9]+@n@", "&", -1)
71 ' Clean up all markers.
72 SuperbReplace(ThisComponent, "@[lnq]@", "", 0)