Commit | Line | Data |
---|---|---|
071359bb MM |
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 | |
4 | ' | |
5 | ' Applies superscript and subscript formatting to chemical formulas in text. | |
6 | ' | |
7 | ' Examples: | |
8 | ' C12345 ==> C_{12345} | |
9 | ' H+ ==> H^+ | |
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) | |
17 | ||
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) | |
22 | ||
23 | dim rd as object | |
24 | rd = doc.createReplaceDescriptor() | |
25 | ||
26 | rd.SearchRegularExpression = true | |
27 | rd.setSearchString(searchStr) | |
28 | rd.setReplaceString(replaceStr) | |
29 | ||
30 | if superb <> 0 then | |
31 | dim replaceAttrs(1) as new com.sun.star.beans.PropertyValue | |
32 | replaceAttrs(0).Name = "CharEscapement" | |
33 | if superb > 0 then | |
34 | replaceAttrs(0).Value = 33 | |
35 | else | |
36 | replaceAttrs(0).Value = -9 | |
37 | end if | |
38 | replaceAttrs(1).Name = "CharEscapementHeight" | |
39 | replaceAttrs(1).Value = 58 | |
40 | rd.setReplaceAttributes(replaceAttrs) | |
41 | end if | |
42 | ||
43 | doc.replaceAll(rd) | |
44 | ||
45 | end sub | |
46 | ||
47 | ' Formats the current document | |
48 | sub FormatDocument | |
49 | ||
50 | ' Mark candidate superscripts so we know they follow letters or ). | |
51 | SuperbReplace(ThisComponent, "[A-Za-z)][0-9]*[-+−]", "&@l@", 0) | |
52 | ||
53 | ' O and ) grab a single digit. Block it off from becoming a superscript. | |
54 | SuperbReplace(ThisComponent, "[O)][0-9]", "&@n@", 0) | |
55 | ||
56 | ' Real minus signs in superscripts. | |
57 | SuperbReplace(ThisComponent, "-@l@", "−@l@", 0) | |
58 | ||
59 | ' Make superscripts: at most one digit. | |
60 | SuperbReplace(ThisComponent, "[0-9]?[−+]@l@", "@q@&", 1) | |
61 | ||
62 | ' Remove the O and ) markers. | |
63 | SuperbReplace(ThisComponent, "@n@", "", 0) | |
64 | ||
65 | ' Mark off subscripts: as many digits as we can still grab. | |
66 | SuperbReplace(ThisComponent, "[A-Za-z)][0-9]+", "&@n@", 0) | |
67 | ||
68 | ' Make subscripts. | |
69 | SuperbReplace(ThisComponent, "[0-9]+@n@", "&", -1) | |
70 | ||
71 | ' Clean up all markers. | |
72 | SuperbReplace(ThisComponent, "@[lnq]@", "", 0) | |
73 | ||
74 | end sub | |
75 | ||
76 | </script:module> |