nnbb 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (218) hide show
  1. package/.dockerignore +1 -0
  2. package/.env.local +2 -0
  3. package/.eslintrc.json +15 -0
  4. package/.github/stale.yml +16 -0
  5. package/.github/workflows/codeql-analysis.yml +73 -0
  6. package/.github/workflows/docker-ghcr.yaml +59 -0
  7. package/.prettierrc +9 -0
  8. package/.vscode/launch.json +28 -0
  9. package/.vscode/settings.json +6 -0
  10. package/Dockerfile +38 -0
  11. package/LICENSE +21 -0
  12. package/README.md +16 -0
  13. package/blog.config.js +454 -0
  14. package/components/Ackee.js +83 -0
  15. package/components/AdBlockDetect.js +40 -0
  16. package/components/AlgoliaSearchModal.js +250 -0
  17. package/components/Artalk.js +30 -0
  18. package/components/Busuanzi.js +26 -0
  19. package/components/ChatBase.js +19 -0
  20. package/components/Collapse.tsx +134 -0
  21. package/components/Comment.js +161 -0
  22. package/components/CommonHead.tsx +101 -0
  23. package/components/CommonScript.js +125 -0
  24. package/components/CusdisComponent.js +35 -0
  25. package/components/CustomContextMenu.js +221 -0
  26. package/components/DebugPanel.js +134 -0
  27. package/components/DisableCopy.js +21 -0
  28. package/components/Draggable.js +167 -0
  29. package/components/Equation.js +31 -0
  30. package/components/ExternalPlugins.js +75 -0
  31. package/components/ExternalScript.js +29 -0
  32. package/components/FacebookMessenger.js +255 -0
  33. package/components/FacebookPage.js +34 -0
  34. package/components/Fireworks.js +210 -0
  35. package/components/FlipCard.js +56 -0
  36. package/components/FlutteringRibbon.js +322 -0
  37. package/components/FullScreenButton.js +48 -0
  38. package/components/Giscus.js +33 -0
  39. package/components/Gitalk.js +42 -0
  40. package/components/GoogleAdsense.js +111 -0
  41. package/components/Gtag.js +18 -0
  42. package/components/HeroIcons.tsx +321 -0
  43. package/components/KatexReact.js +53 -0
  44. package/components/LazyImage.js +95 -0
  45. package/components/Live2D.js +52 -0
  46. package/components/Loading.js +20 -0
  47. package/components/Mark.js +28 -0
  48. package/components/NProgress.ts +8 -0
  49. package/components/Nest.js +124 -0
  50. package/components/NotionIcon.js +20 -0
  51. package/components/NotionPage.tsx +206 -0
  52. package/components/Player.js +54 -0
  53. package/components/PrismMac.js +236 -0
  54. package/components/QrCode.js +34 -0
  55. package/components/Ribbon.js +98 -0
  56. package/components/Sakura.js +192 -0
  57. package/components/Select.js +40 -0
  58. package/components/ShareBar.js +29 -0
  59. package/components/ShareButtons.js +403 -0
  60. package/components/SideBarDrawer.js +50 -0
  61. package/components/StarrySky.js +130 -0
  62. package/components/Tabs.js +64 -0
  63. package/components/ThemeSwitch.js +67 -0
  64. package/components/Twikoo.js +27 -0
  65. package/components/TwikooCommentCount.js +22 -0
  66. package/components/TwikooCommentCounter.js +78 -0
  67. package/components/TwikooRecentComments.js +11 -0
  68. package/components/Utterances.js +35 -0
  69. package/components/VConsole.js +76 -0
  70. package/components/ValineComponent.js +59 -0
  71. package/components/ValineCount.js +6 -0
  72. package/components/ValinePanel.js +3 -0
  73. package/components/Vercel.tsx +54 -0
  74. package/components/WWAds.js +18 -0
  75. package/components/WalineComponent.js +83 -0
  76. package/components/WebMention.js +173 -0
  77. package/components/Webwhiz.js +17 -0
  78. package/components/WordCount.js +73 -0
  79. package/hooks/useToggleClickOutSide.ts +32 -0
  80. package/hooks/useWindowSize.ts +30 -0
  81. package/lib/algolia.js +108 -0
  82. package/lib/busuanzi.js +99 -0
  83. package/lib/cache/cacheManager.ts +49 -0
  84. package/lib/cache/localFileCache.ts +56 -0
  85. package/lib/cache/memoryMache.ts +20 -0
  86. package/lib/cache/mongoDbCache.ts +70 -0
  87. package/lib/cache/types.ts +5 -0
  88. package/lib/font.js +46 -0
  89. package/lib/global.tsx +129 -0
  90. package/lib/gtag.js +17 -0
  91. package/lib/mailchimp.js +49 -0
  92. package/lib/memorize.js +0 -0
  93. package/lib/mhchem.js +1696 -0
  94. package/lib/notion/getAllCategories.ts +51 -0
  95. package/lib/notion/getAllPageIds.ts +51 -0
  96. package/lib/notion/getAllPosts.js +68 -0
  97. package/lib/notion/getAllTags.ts +43 -0
  98. package/lib/notion/getNotionData.ts +340 -0
  99. package/lib/notion/getPageInfoOfPostPage.ts +58 -0
  100. package/lib/notion/getPageProperties.ts +203 -0
  101. package/lib/notion/getPageTableOfContents.ts +107 -0
  102. package/lib/notion/getPostBlocks.ts +147 -0
  103. package/lib/notion/mapImage.ts +130 -0
  104. package/lib/notion/types.ts +125 -0
  105. package/lib/notion.js +2 -0
  106. package/lib/robots.txt.js +25 -0
  107. package/lib/rss.js +63 -0
  108. package/lib/sitemap.xml.js +67 -0
  109. package/lib/utils.js +212 -0
  110. package/next-env.d.ts +5 -0
  111. package/next-i18next.config.js +7 -0
  112. package/next-sitemap.config.js +11 -0
  113. package/next.config.js +124 -0
  114. package/package.json +92 -0
  115. package/pages/404.tsx +40 -0
  116. package/pages/[prefix]/[slug].tsx +123 -0
  117. package/pages/[prefix]/index.tsx +223 -0
  118. package/pages/_app.js +59 -0
  119. package/pages/_document.js +42 -0
  120. package/pages/api/subscribe.js +22 -0
  121. package/pages/archive/index.tsx +79 -0
  122. package/pages/category/[category]/index.tsx +87 -0
  123. package/pages/category/[category]/page/[page].tsx +103 -0
  124. package/pages/category/index.tsx +43 -0
  125. package/pages/index.tsx +88 -0
  126. package/pages/page/[page].tsx +93 -0
  127. package/pages/search/[keyword]/index.tsx +162 -0
  128. package/pages/search/[keyword]/page/[page].tsx +166 -0
  129. package/pages/search/index.tsx +69 -0
  130. package/pages/sitemap.xml.js +70 -0
  131. package/pages/tag/[tag]/index.tsx +73 -0
  132. package/pages/tag/[tag]/page/[page].tsx +87 -0
  133. package/pages/tag/index.tsx +42 -0
  134. package/postcss.config.js +6 -0
  135. package/public/ads.txt +1 -0
  136. package/public/avatar.png +0 -0
  137. package/public/avatar.svg +11 -0
  138. package/public/bg_image.jpg +0 -0
  139. package/public/css/all.min.css +9 -0
  140. package/public/css/custom.css +8 -0
  141. package/public/css/img-shadow.css +5 -0
  142. package/public/css/prism-mac-style.css +58 -0
  143. package/public/favicon.ico +0 -0
  144. package/public/favicon.svg +9 -0
  145. package/public/js/cusdis.es.js +107 -0
  146. package/public/js/custom.js +1 -0
  147. package/public/locales/en/common.json +44 -0
  148. package/public/locales/en/menu.json +9 -0
  149. package/public/locales/en/nav.json +11 -0
  150. package/public/locales/zh-CN/common.json +44 -0
  151. package/public/locales/zh-CN/menu.json +9 -0
  152. package/public/locales/zh-CN/nav.json +9 -0
  153. package/public/webfonts/fa-brands-400.ttf +0 -0
  154. package/public/webfonts/fa-brands-400.woff2 +0 -0
  155. package/public/webfonts/fa-regular-400.ttf +0 -0
  156. package/public/webfonts/fa-regular-400.woff2 +0 -0
  157. package/public/webfonts/fa-solid-900.ttf +0 -0
  158. package/public/webfonts/fa-solid-900.woff2 +0 -0
  159. package/public/webfonts/fa-v4compatibility.ttf +0 -0
  160. package/public/webfonts/fa-v4compatibility.woff2 +0 -0
  161. package/styles/animate.css +503 -0
  162. package/styles/globals.css +183 -0
  163. package/styles/notion.css +2064 -0
  164. package/styles/nprogress.css +84 -0
  165. package/styles/prism-theme.css +119 -0
  166. package/styles/utility-patterns.css +79 -0
  167. package/tailwind.config.js +37 -0
  168. package/theme/index.ts +6 -0
  169. package/theme/types/@theme-components.d.ts +29 -0
  170. package/theme/useLayout.ts +41 -0
  171. package/theme/utils.ts +108 -0
  172. package/themes/innocent/package.json +7 -0
  173. package/themes/innocent/theme.config.js +1 -0
  174. package/themes/nobelium/components/Announcement.tsx +27 -0
  175. package/themes/nobelium/components/ArticleFooter.tsx +39 -0
  176. package/themes/nobelium/components/ArticleInfo.tsx +58 -0
  177. package/themes/nobelium/components/ArticleLock.tsx +86 -0
  178. package/themes/nobelium/components/BlogArchiveItem.js +41 -0
  179. package/themes/nobelium/components/BlogListBar.js +39 -0
  180. package/themes/nobelium/components/BlogListPage.tsx +67 -0
  181. package/themes/nobelium/components/BlogListScroll.tsx +96 -0
  182. package/themes/nobelium/components/BlogPost.tsx +33 -0
  183. package/themes/nobelium/components/DarkModeButton.tsx +50 -0
  184. package/themes/nobelium/components/ExampleRecentComments.js +35 -0
  185. package/themes/nobelium/components/Footer.tsx +35 -0
  186. package/themes/nobelium/components/JumpToTopButton.tsx +39 -0
  187. package/themes/nobelium/components/LanguageSwitchButton.tsx +58 -0
  188. package/themes/nobelium/components/MenuItemCollapse.tsx +92 -0
  189. package/themes/nobelium/components/MenuItemDrop.tsx +83 -0
  190. package/themes/nobelium/components/Nav/Nav.module.css +50 -0
  191. package/themes/nobelium/components/Nav/Nav.tsx +187 -0
  192. package/themes/nobelium/components/RandomPostButton.tsx +31 -0
  193. package/themes/nobelium/components/SearchButton.tsx +31 -0
  194. package/themes/nobelium/components/SearchInput.tsx +94 -0
  195. package/themes/nobelium/components/SearchNavBar.js +19 -0
  196. package/themes/nobelium/components/SideBar.js +83 -0
  197. package/themes/nobelium/components/SvgIcon.js +29 -0
  198. package/themes/nobelium/components/TagItem.js +13 -0
  199. package/themes/nobelium/components/Tags.tsx +44 -0
  200. package/themes/nobelium/components/Title.js +19 -0
  201. package/themes/nobelium/index.tsx +28 -0
  202. package/themes/nobelium/layout/LayoutBase.tsx +79 -0
  203. package/themes/nobelium/pages/Archive.tsx +30 -0
  204. package/themes/nobelium/pages/Category.tsx +43 -0
  205. package/themes/nobelium/pages/Home.tsx +22 -0
  206. package/themes/nobelium/pages/PageNotFound.tsx +15 -0
  207. package/themes/nobelium/pages/Post.tsx +34 -0
  208. package/themes/nobelium/pages/PostList.tsx +74 -0
  209. package/themes/nobelium/pages/Search.tsx +65 -0
  210. package/themes/nobelium/pages/Tag.tsx +42 -0
  211. package/themes/nobelium/providers/index.tsx +60 -0
  212. package/themes/nobelium/stores/index.tsx +42 -0
  213. package/themes/nobelium/theme.config.ts +17 -0
  214. package/themes/nobelium/types/index.ts +10 -0
  215. package/tsconfig.json +29 -0
  216. package/types/index.ts +1 -0
  217. package/types/page.ts +102 -0
  218. package/vercel.json +5 -0
package/lib/mhchem.js ADDED
@@ -0,0 +1,1696 @@
1
+ /* eslint-disable */
2
+ /* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */
3
+ /* vim: set ts=2 et sw=2 tw=80: */
4
+
5
+ /*************************************************************
6
+ *
7
+ * KaTeX mhchem.js
8
+ *
9
+ * This file implements a KaTeX version of mhchem version 3.3.0.
10
+ * It is adapted from MathJax/extensions/TeX/mhchem.js
11
+ * It differs from the MathJax version as follows:
12
+ * 1. The interface is changed so that it can be called from KaTeX, not MathJax.
13
+ * 2. \rlap and \llap are replaced with \mathrlap and \mathllap.
14
+ * 3. Four lines of code are edited in order to use \raisebox instead of \raise.
15
+ * 4. The reaction arrow code is simplified. All reaction arrows are rendered
16
+ * using KaTeX extensible arrows instead of building non-extensible arrows.
17
+ * 5. \tripledash vertical alignment is slightly adjusted.
18
+ *
19
+ * This code, as other KaTeX code, is released under the MIT license.
20
+ *
21
+ * /*************************************************************
22
+ *
23
+ * MathJax/extensions/TeX/mhchem.js
24
+ *
25
+ * Implements the \ce command for handling chemical formulas
26
+ * from the mhchem LaTeX package.
27
+ *
28
+ * ---------------------------------------------------------------------
29
+ *
30
+ * Copyright (c) 2011-2015 The MathJax Consortium
31
+ * Copyright (c) 2015-2018 Martin Hensel
32
+ *
33
+ * Licensed under the Apache License, Version 2.0 (the "License");
34
+ * you may not use this file except in compliance with the License.
35
+ * You may obtain a copy of the License at
36
+ *
37
+ * http://www.apache.org/licenses/LICENSE-2.0
38
+ *
39
+ * Unless required by applicable law or agreed to in writing, software
40
+ * distributed under the License is distributed on an "AS IS" BASIS,
41
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42
+ * See the License for the specific language governing permissions and
43
+ * limitations under the License.
44
+ */
45
+
46
+ //
47
+ // Coding Style
48
+ // - use '' for identifiers that can by minified/uglified
49
+ // - use "" for strings that need to stay untouched
50
+
51
+ // version: "3.3.0" for MathJax and KaTeX
52
+
53
+
54
+ // Add \ce, \pu, and \tripledash to the KaTeX macros.
55
+
56
+ katex.__defineMacro("\\ce", function(context) {
57
+ return chemParse(context.consumeArgs(1)[0], "ce")
58
+ });
59
+
60
+ katex.__defineMacro("\\pu", function(context) {
61
+ return chemParse(context.consumeArgs(1)[0], "pu");
62
+ });
63
+
64
+ // Needed for \bond for the ~ forms
65
+ // Raise by 2.56mu, not 2mu. We're raising a hyphen-minus, U+002D, not
66
+ // a mathematical minus, U+2212. So we need that extra 0.56.
67
+ katex.__defineMacro("\\tripledash", "{\\vphantom{-}\\raisebox{2.56mu}{$\\mkern2mu"
68
+ + "\\tiny\\text{-}\\mkern1mu\\text{-}\\mkern1mu\\text{-}\\mkern2mu$}}");
69
+
70
+ import katex from "katex";
71
+
72
+ //
73
+ // This is the main function for handing the \ce and \pu commands.
74
+ // It takes the argument to \ce or \pu and returns the corresponding TeX string.
75
+ //
76
+
77
+ var chemParse = function (tokens, stateMachine) {
78
+ // Recreate the argument string from KaTeX's array of tokens.
79
+ var str = "";
80
+ var expectedLoc = tokens.length && tokens[tokens.length - 1].loc.start
81
+ for (var i = tokens.length - 1; i >= 0; i--) {
82
+ if(tokens[i].loc.start > expectedLoc) {
83
+ // context.consumeArgs has eaten a space.
84
+ str += " ";
85
+ expectedLoc = tokens[i].loc.start;
86
+ }
87
+ str += tokens[i].text;
88
+ expectedLoc += tokens[i].text.length;
89
+ }
90
+ var tex = texify.go(mhchemParser.go(str, stateMachine));
91
+ return tex;
92
+ };
93
+
94
+ //
95
+ // Core parser for mhchem syntax (recursive)
96
+ //
97
+ /** @type {MhchemParser} */
98
+ var mhchemParser = {
99
+ //
100
+ // Parses mchem \ce syntax
101
+ //
102
+ // Call like
103
+ // go("H2O");
104
+ //
105
+ go: function (input, stateMachine) {
106
+ if (!input) { return []; }
107
+ if (stateMachine === undefined) { stateMachine = 'ce'; }
108
+ var state = '0';
109
+
110
+ //
111
+ // String buffers for parsing:
112
+ //
113
+ // buffer.a == amount
114
+ // buffer.o == element
115
+ // buffer.b == left-side superscript
116
+ // buffer.p == left-side subscript
117
+ // buffer.q == right-side subscript
118
+ // buffer.d == right-side superscript
119
+ //
120
+ // buffer.r == arrow
121
+ // buffer.rdt == arrow, script above, type
122
+ // buffer.rd == arrow, script above, content
123
+ // buffer.rqt == arrow, script below, type
124
+ // buffer.rq == arrow, script below, content
125
+ //
126
+ // buffer.text_
127
+ // buffer.rm
128
+ // etc.
129
+ //
130
+ // buffer.parenthesisLevel == int, starting at 0
131
+ // buffer.sb == bool, space before
132
+ // buffer.beginsWithBond == bool
133
+ //
134
+ // These letters are also used as state names.
135
+ //
136
+ // Other states:
137
+ // 0 == begin of main part (arrow/operator unlikely)
138
+ // 1 == next entity
139
+ // 2 == next entity (arrow/operator unlikely)
140
+ // 3 == next atom
141
+ // c == macro
142
+ //
143
+ /** @type {Buffer} */
144
+ var buffer = {};
145
+ buffer['parenthesisLevel'] = 0;
146
+
147
+ input = input.replace(/\n/g, " ");
148
+ input = input.replace(/[\u2212\u2013\u2014\u2010]/g, "-");
149
+ input = input.replace(/[\u2026]/g, "...");
150
+
151
+ //
152
+ // Looks through mhchemParser.transitions, to execute a matching action
153
+ // (recursive)
154
+ //
155
+ var lastInput;
156
+ var watchdog = 10;
157
+ /** @type {ParserOutput[]} */
158
+ var output = [];
159
+ while (true) {
160
+ if (lastInput !== input) {
161
+ watchdog = 10;
162
+ lastInput = input;
163
+ } else {
164
+ watchdog--;
165
+ }
166
+ //
167
+ // Find actions in transition table
168
+ //
169
+ var machine = mhchemParser.stateMachines[stateMachine];
170
+ var t = machine.transitions[state] || machine.transitions['*'];
171
+ iterateTransitions:
172
+ for (var i=0; i<t.length; i++) {
173
+ var matches = mhchemParser.patterns.match_(t[i].pattern, input);
174
+ if (matches) {
175
+ //
176
+ // Execute actions
177
+ //
178
+ var task = t[i].task;
179
+ for (var iA=0; iA<task.action_.length; iA++) {
180
+ var o;
181
+ //
182
+ // Find and execute action
183
+ //
184
+ if (machine.actions[task.action_[iA].type_]) {
185
+ o = machine.actions[task.action_[iA].type_](buffer, matches.match_, task.action_[iA].option);
186
+ } else if (mhchemParser.actions[task.action_[iA].type_]) {
187
+ o = mhchemParser.actions[task.action_[iA].type_](buffer, matches.match_, task.action_[iA].option);
188
+ } else {
189
+ throw ["MhchemBugA", "mhchem bug A. Please report. (" + task.action_[iA].type_ + ")"]; // Trying to use non-existing action
190
+ }
191
+ //
192
+ // Add output
193
+ //
194
+ mhchemParser.concatArray(output, o);
195
+ }
196
+ //
197
+ // Set next state,
198
+ // Shorten input,
199
+ // Continue with next character
200
+ // (= apply only one transition per position)
201
+ //
202
+ state = task.nextState || state;
203
+ if (input.length > 0) {
204
+ if (!task.revisit) {
205
+ input = matches.remainder;
206
+ }
207
+ if (!task.toContinue) {
208
+ break iterateTransitions;
209
+ }
210
+ } else {
211
+ return output;
212
+ }
213
+ }
214
+ }
215
+ //
216
+ // Prevent infinite loop
217
+ //
218
+ if (watchdog <= 0) {
219
+ throw ["MhchemBugU", "mhchem bug U. Please report."]; // Unexpected character
220
+ }
221
+ }
222
+ },
223
+ concatArray: function (a, b) {
224
+ if (b) {
225
+ if (Array.isArray(b)) {
226
+ for (var iB=0; iB<b.length; iB++) {
227
+ a.push(b[iB]);
228
+ }
229
+ } else {
230
+ a.push(b);
231
+ }
232
+ }
233
+ },
234
+
235
+ patterns: {
236
+ //
237
+ // Matching patterns
238
+ // either regexps or function that return null or {match_:"a", remainder:"bc"}
239
+ //
240
+ patterns: {
241
+ // property names must not look like integers ("2") for correct property traversal order, later on
242
+ 'empty': /^$/,
243
+ 'else': /^./,
244
+ 'else2': /^./,
245
+ 'space': /^\s/,
246
+ 'space A': /^\s(?=[A-Z\\$])/,
247
+ 'space$': /^\s$/,
248
+ 'a-z': /^[a-z]/,
249
+ 'x': /^x/,
250
+ 'x$': /^x$/,
251
+ 'i$': /^i$/,
252
+ 'letters': /^(?:[a-zA-Z\u03B1-\u03C9\u0391-\u03A9?@]|(?:\\(?:alpha|beta|gamma|delta|epsilon|zeta|eta|theta|iota|kappa|lambda|mu|nu|xi|omicron|pi|rho|sigma|tau|upsilon|phi|chi|psi|omega|Gamma|Delta|Theta|Lambda|Xi|Pi|Sigma|Upsilon|Phi|Psi|Omega)(?:\s+|\{\}|(?![a-zA-Z]))))+/,
253
+ '\\greek': /^\\(?:alpha|beta|gamma|delta|epsilon|zeta|eta|theta|iota|kappa|lambda|mu|nu|xi|omicron|pi|rho|sigma|tau|upsilon|phi|chi|psi|omega|Gamma|Delta|Theta|Lambda|Xi|Pi|Sigma|Upsilon|Phi|Psi|Omega)(?:\s+|\{\}|(?![a-zA-Z]))/,
254
+ 'one lowercase latin letter $': /^(?:([a-z])(?:$|[^a-zA-Z]))$/,
255
+ '$one lowercase latin letter$ $': /^\$(?:([a-z])(?:$|[^a-zA-Z]))\$$/,
256
+ 'one lowercase greek letter $': /^(?:\$?[\u03B1-\u03C9]\$?|\$?\\(?:alpha|beta|gamma|delta|epsilon|zeta|eta|theta|iota|kappa|lambda|mu|nu|xi|omicron|pi|rho|sigma|tau|upsilon|phi|chi|psi|omega)\s*\$?)(?:\s+|\{\}|(?![a-zA-Z]))$/,
257
+ 'digits': /^[0-9]+/,
258
+ '-9.,9': /^[+\-]?(?:[0-9]+(?:[,.][0-9]+)?|[0-9]*(?:\.[0-9]+))/,
259
+ '-9.,9 no missing 0': /^[+\-]?[0-9]+(?:[.,][0-9]+)?/,
260
+ '(-)(9.,9)(e)(99)': function (input) {
261
+ var m = input.match(/^(\+\-|\+\/\-|\+|\-|\\pm\s?)?([0-9]+(?:[,.][0-9]+)?|[0-9]*(?:\.[0-9]+))?(\((?:[0-9]+(?:[,.][0-9]+)?|[0-9]*(?:\.[0-9]+))\))?(?:([eE]|\s*(\*|x|\\times|\u00D7)\s*10\^)([+\-]?[0-9]+|\{[+\-]?[0-9]+\}))?/);
262
+ if (m && m[0]) {
263
+ return { match_: m.splice(1), remainder: input.substr(m[0].length) };
264
+ }
265
+ return null;
266
+ },
267
+ '(-)(9)^(-9)': function (input) {
268
+ var m = input.match(/^(\+\-|\+\/\-|\+|\-|\\pm\s?)?([0-9]+(?:[,.][0-9]+)?|[0-9]*(?:\.[0-9]+)?)\^([+\-]?[0-9]+|\{[+\-]?[0-9]+\})/);
269
+ if (m && m[0]) {
270
+ return { match_: m.splice(1), remainder: input.substr(m[0].length) };
271
+ }
272
+ return null;
273
+ },
274
+ 'state of aggregation $': function (input) { // ... or crystal system
275
+ var a = mhchemParser.patterns.findObserveGroups(input, "", /^\([a-z]{1,3}(?=[\),])/, ")", ""); // (aq), (aq,$\infty$), (aq, sat)
276
+ if (a && a.remainder.match(/^($|[\s,;\)\]\}])/)) { return a; } // AND end of 'phrase'
277
+ var m = input.match(/^(?:\((?:\\ca\s?)?\$[amothc]\$\))/); // OR crystal system ($o$) (\ca$c$)
278
+ if (m) {
279
+ return { match_: m[0], remainder: input.substr(m[0].length) };
280
+ }
281
+ return null;
282
+ },
283
+ '_{(state of aggregation)}$': /^_\{(\([a-z]{1,3}\))\}/,
284
+ '{[(': /^(?:\\\{|\[|\()/,
285
+ ')]}': /^(?:\)|\]|\\\})/,
286
+ ', ': /^[,;]\s*/,
287
+ ',': /^[,;]/,
288
+ '.': /^[.]/,
289
+ '. ': /^([.\u22C5\u00B7\u2022])\s*/,
290
+ '...': /^\.\.\.(?=$|[^.])/,
291
+ '* ': /^([*])\s*/,
292
+ '^{(...)}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "^{", "", "", "}"); },
293
+ '^($...$)': function (input) { return mhchemParser.patterns.findObserveGroups(input, "^", "$", "$", ""); },
294
+ '^a': /^\^([0-9]+|[^\\_])/,
295
+ '^\\x{}{}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "^", /^\\[a-zA-Z]+\{/, "}", "", "", "{", "}", "", true); },
296
+ '^\\x{}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "^", /^\\[a-zA-Z]+\{/, "}", ""); },
297
+ '^\\x': /^\^(\\[a-zA-Z]+)\s*/,
298
+ '^(-1)': /^\^(-?\d+)/,
299
+ '\'': /^'/,
300
+ '_{(...)}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "_{", "", "", "}"); },
301
+ '_($...$)': function (input) { return mhchemParser.patterns.findObserveGroups(input, "_", "$", "$", ""); },
302
+ '_9': /^_([+\-]?[0-9]+|[^\\])/,
303
+ '_\\x{}{}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "_", /^\\[a-zA-Z]+\{/, "}", "", "", "{", "}", "", true); },
304
+ '_\\x{}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "_", /^\\[a-zA-Z]+\{/, "}", ""); },
305
+ '_\\x': /^_(\\[a-zA-Z]+)\s*/,
306
+ '^_': /^(?:\^(?=_)|\_(?=\^)|[\^_]$)/,
307
+ '{}': /^\{\}/,
308
+ '{...}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "", "{", "}", ""); },
309
+ '{(...)}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "{", "", "", "}"); },
310
+ '$...$': function (input) { return mhchemParser.patterns.findObserveGroups(input, "", "$", "$", ""); },
311
+ '${(...)}$': function (input) { return mhchemParser.patterns.findObserveGroups(input, "${", "", "", "}$"); },
312
+ '$(...)$': function (input) { return mhchemParser.patterns.findObserveGroups(input, "$", "", "", "$"); },
313
+ '=<>': /^[=<>]/,
314
+ '#': /^[#\u2261]/,
315
+ '+': /^\+/,
316
+ '-$': /^-(?=[\s_},;\]/]|$|\([a-z]+\))/, // -space -, -; -] -/ -$ -state-of-aggregation
317
+ '-9': /^-(?=[0-9])/,
318
+ '- orbital overlap': /^-(?=(?:[spd]|sp)(?:$|[\s,;\)\]\}]))/,
319
+ '-': /^-/,
320
+ 'pm-operator': /^(?:\\pm|\$\\pm\$|\+-|\+\/-)/,
321
+ 'operator': /^(?:\+|(?:[\-=<>]|<<|>>|\\approx|\$\\approx\$)(?=\s|$|-?[0-9]))/,
322
+ 'arrowUpDown': /^(?:v|\(v\)|\^|\(\^\))(?=$|[\s,;\)\]\}])/,
323
+ '\\bond{(...)}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "\\bond{", "", "", "}"); },
324
+ '->': /^(?:<->|<-->|->|<-|<=>>|<<=>|<=>|[\u2192\u27F6\u21CC])/,
325
+ 'CMT': /^[CMT](?=\[)/,
326
+ '[(...)]': function (input) { return mhchemParser.patterns.findObserveGroups(input, "[", "", "", "]"); },
327
+ '1st-level escape': /^(&|\\\\|\\hline)\s*/,
328
+ '\\,': /^(?:\\[,\ ;:])/, // \\x - but output no space before
329
+ '\\x{}{}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "", /^\\[a-zA-Z]+\{/, "}", "", "", "{", "}", "", true); },
330
+ '\\x{}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "", /^\\[a-zA-Z]+\{/, "}", ""); },
331
+ '\\ca': /^\\ca(?:\s+|(?![a-zA-Z]))/,
332
+ '\\x': /^(?:\\[a-zA-Z]+\s*|\\[_&{}%])/,
333
+ 'orbital': /^(?:[0-9]{1,2}[spdfgh]|[0-9]{0,2}sp)(?=$|[^a-zA-Z])/, // only those with numbers in front, because the others will be formatted correctly anyway
334
+ 'others': /^[\/~|]/,
335
+ '\\frac{(...)}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "\\frac{", "", "", "}", "{", "", "", "}"); },
336
+ '\\overset{(...)}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "\\overset{", "", "", "}", "{", "", "", "}"); },
337
+ '\\underset{(...)}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "\\underset{", "", "", "}", "{", "", "", "}"); },
338
+ '\\underbrace{(...)}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "\\underbrace{", "", "", "}_", "{", "", "", "}"); },
339
+ '\\color{(...)}0': function (input) { return mhchemParser.patterns.findObserveGroups(input, "\\color{", "", "", "}"); },
340
+ '\\color{(...)}{(...)}1': function (input) { return mhchemParser.patterns.findObserveGroups(input, "\\color{", "", "", "}", "{", "", "", "}"); },
341
+ '\\color(...){(...)}2': function (input) { return mhchemParser.patterns.findObserveGroups(input, "\\color", "\\", "", /^(?=\{)/, "{", "", "", "}"); },
342
+ '\\ce{(...)}': function (input) { return mhchemParser.patterns.findObserveGroups(input, "\\ce{", "", "", "}"); },
343
+ 'oxidation$': /^(?:[+-][IVX]+|\\pm\s*0|\$\\pm\$\s*0)$/,
344
+ 'd-oxidation$': /^(?:[+-]?\s?[IVX]+|\\pm\s*0|\$\\pm\$\s*0)$/, // 0 could be oxidation or charge
345
+ 'roman numeral': /^[IVX]+/,
346
+ '1/2$': /^[+\-]?(?:[0-9]+|\$[a-z]\$|[a-z])\/[0-9]+(?:\$[a-z]\$|[a-z])?$/,
347
+ 'amount': function (input) {
348
+ var match;
349
+ // e.g. 2, 0.5, 1/2, -2, n/2, +; $a$ could be added later in parsing
350
+ match = input.match(/^(?:(?:(?:\([+\-]?[0-9]+\/[0-9]+\)|[+\-]?(?:[0-9]+|\$[a-z]\$|[a-z])\/[0-9]+|[+\-]?[0-9]+[.,][0-9]+|[+\-]?\.[0-9]+|[+\-]?[0-9]+)(?:[a-z](?=\s*[A-Z]))?)|[+\-]?[a-z](?=\s*[A-Z])|\+(?!\s))/);
351
+ if (match) {
352
+ return { match_: match[0], remainder: input.substr(match[0].length) };
353
+ }
354
+ var a = mhchemParser.patterns.findObserveGroups(input, "", "$", "$", "");
355
+ if (a) { // e.g. $2n-1$, $-$
356
+ match = a.match_.match(/^\$(?:\(?[+\-]?(?:[0-9]*[a-z]?[+\-])?[0-9]*[a-z](?:[+\-][0-9]*[a-z]?)?\)?|\+|-)\$$/);
357
+ if (match) {
358
+ return { match_: match[0], remainder: input.substr(match[0].length) };
359
+ }
360
+ }
361
+ return null;
362
+ },
363
+ 'amount2': function (input) { return this['amount'](input); },
364
+ '(KV letters),': /^(?:[A-Z][a-z]{0,2}|i)(?=,)/,
365
+ 'formula$': function (input) {
366
+ if (input.match(/^\([a-z]+\)$/)) { return null; } // state of aggregation = no formula
367
+ var match = input.match(/^(?:[a-z]|(?:[0-9\ \+\-\,\.\(\)]+[a-z])+[0-9\ \+\-\,\.\(\)]*|(?:[a-z][0-9\ \+\-\,\.\(\)]+)+[a-z]?)$/);
368
+ if (match) {
369
+ return { match_: match[0], remainder: input.substr(match[0].length) };
370
+ }
371
+ return null;
372
+ },
373
+ 'uprightEntities': /^(?:pH|pOH|pC|pK|iPr|iBu)(?=$|[^a-zA-Z])/,
374
+ '/': /^\s*(\/)\s*/,
375
+ '//': /^\s*(\/\/)\s*/,
376
+ '*': /^\s*[*.]\s*/
377
+ },
378
+ findObserveGroups: function (input, begExcl, begIncl, endIncl, endExcl, beg2Excl, beg2Incl, end2Incl, end2Excl, combine) {
379
+ /** @type {{(input: string, pattern: string | RegExp): string | string[] | null;}} */
380
+ var _match = function (input, pattern) {
381
+ if (typeof pattern === "string") {
382
+ if (input.indexOf(pattern) !== 0) { return null; }
383
+ return pattern;
384
+ } else {
385
+ var match = input.match(pattern);
386
+ if (!match) { return null; }
387
+ return match[0];
388
+ }
389
+ };
390
+ /** @type {{(input: string, i: number, endChars: string | RegExp): {endMatchBegin: number, endMatchEnd: number} | null;}} */
391
+ var _findObserveGroups = function (input, i, endChars) {
392
+ var braces = 0;
393
+ while (i < input.length) {
394
+ var a = input.charAt(i);
395
+ var match = _match(input.substr(i), endChars);
396
+ if (match !== null && braces === 0) {
397
+ return { endMatchBegin: i, endMatchEnd: i + match.length };
398
+ } else if (a === "{") {
399
+ braces++;
400
+ } else if (a === "}") {
401
+ if (braces === 0) {
402
+ throw ["ExtraCloseMissingOpen", "Extra close brace or missing open brace"];
403
+ } else {
404
+ braces--;
405
+ }
406
+ }
407
+ i++;
408
+ }
409
+ if (braces > 0) {
410
+ return null;
411
+ }
412
+ return null;
413
+ };
414
+ var match = _match(input, begExcl);
415
+ if (match === null) { return null; }
416
+ input = input.substr(match.length);
417
+ match = _match(input, begIncl);
418
+ if (match === null) { return null; }
419
+ var e = _findObserveGroups(input, match.length, endIncl || endExcl);
420
+ if (e === null) { return null; }
421
+ var match1 = input.substring(0, (endIncl ? e.endMatchEnd : e.endMatchBegin));
422
+ if (!(beg2Excl || beg2Incl)) {
423
+ return {
424
+ match_: match1,
425
+ remainder: input.substr(e.endMatchEnd)
426
+ };
427
+ } else {
428
+ var group2 = this.findObserveGroups(input.substr(e.endMatchEnd), beg2Excl, beg2Incl, end2Incl, end2Excl);
429
+ if (group2 === null) { return null; }
430
+ /** @type {string[]} */
431
+ var matchRet = [match1, group2.match_];
432
+ return {
433
+ match_: (combine ? matchRet.join("") : matchRet),
434
+ remainder: group2.remainder
435
+ };
436
+ }
437
+ },
438
+
439
+ //
440
+ // Matching function
441
+ // e.g. match("a", input) will look for the regexp called "a" and see if it matches
442
+ // returns null or {match_:"a", remainder:"bc"}
443
+ //
444
+ match_: function (m, input) {
445
+ var pattern = mhchemParser.patterns.patterns[m];
446
+ if (pattern === undefined) {
447
+ throw ["MhchemBugP", "mhchem bug P. Please report. (" + m + ")"]; // Trying to use non-existing pattern
448
+ } else if (typeof pattern === "function") {
449
+ return mhchemParser.patterns.patterns[m](input); // cannot use cached var pattern here, because some pattern functions need this===mhchemParser
450
+ } else { // RegExp
451
+ var match = input.match(pattern);
452
+ if (match) {
453
+ var mm;
454
+ if (match[2]) {
455
+ mm = [ match[1], match[2] ];
456
+ } else if (match[1]) {
457
+ mm = match[1];
458
+ } else {
459
+ mm = match[0];
460
+ }
461
+ return { match_: mm, remainder: input.substr(match[0].length) };
462
+ }
463
+ return null;
464
+ }
465
+ }
466
+ },
467
+
468
+ //
469
+ // Generic state machine actions
470
+ //
471
+ actions: {
472
+ 'a=': function (buffer, m) { buffer.a = (buffer.a || "") + m; },
473
+ 'b=': function (buffer, m) { buffer.b = (buffer.b || "") + m; },
474
+ 'p=': function (buffer, m) { buffer.p = (buffer.p || "") + m; },
475
+ 'o=': function (buffer, m) { buffer.o = (buffer.o || "") + m; },
476
+ 'q=': function (buffer, m) { buffer.q = (buffer.q || "") + m; },
477
+ 'd=': function (buffer, m) { buffer.d = (buffer.d || "") + m; },
478
+ 'rm=': function (buffer, m) { buffer.rm = (buffer.rm || "") + m; },
479
+ 'text=': function (buffer, m) { buffer.text_ = (buffer.text_ || "") + m; },
480
+ 'insert': function (buffer, m, a) { return { type_: a }; },
481
+ 'insert+p1': function (buffer, m, a) { return { type_: a, p1: m }; },
482
+ 'insert+p1+p2': function (buffer, m, a) { return { type_: a, p1: m[0], p2: m[1] }; },
483
+ 'copy': function (buffer, m) { return m; },
484
+ 'rm': function (buffer, m) { return { type_: 'rm', p1: m || ""}; },
485
+ 'text': function (buffer, m) { return mhchemParser.go(m, 'text'); },
486
+ '{text}': function (buffer, m) {
487
+ var ret = [ "{" ];
488
+ mhchemParser.concatArray(ret, mhchemParser.go(m, 'text'));
489
+ ret.push("}");
490
+ return ret;
491
+ },
492
+ 'tex-math': function (buffer, m) { return mhchemParser.go(m, 'tex-math'); },
493
+ 'tex-math tight': function (buffer, m) { return mhchemParser.go(m, 'tex-math tight'); },
494
+ 'bond': function (buffer, m, k) { return { type_: 'bond', kind_: k || m }; },
495
+ 'color0-output': function (buffer, m) { return { type_: 'color0', color: m[0] }; },
496
+ 'ce': function (buffer, m) { return mhchemParser.go(m); },
497
+ '1/2': function (buffer, m) {
498
+ /** @type {ParserOutput[]} */
499
+ var ret = [];
500
+ if (m.match(/^[+\-]/)) {
501
+ ret.push(m.substr(0, 1));
502
+ m = m.substr(1);
503
+ }
504
+ var n = m.match(/^([0-9]+|\$[a-z]\$|[a-z])\/([0-9]+)(\$[a-z]\$|[a-z])?$/);
505
+ n[1] = n[1].replace(/\$/g, "");
506
+ ret.push({ type_: 'frac', p1: n[1], p2: n[2] });
507
+ if (n[3]) {
508
+ n[3] = n[3].replace(/\$/g, "");
509
+ ret.push({ type_: 'tex-math', p1: n[3] });
510
+ }
511
+ return ret;
512
+ },
513
+ '9,9': function (buffer, m) { return mhchemParser.go(m, '9,9'); }
514
+ },
515
+ //
516
+ // createTransitions
517
+ // convert { 'letter': { 'state': { action_: 'output' } } } to { 'state' => [ { pattern: 'letter', task: { action_: [{type_: 'output'}] } } ] }
518
+ // with expansion of 'a|b' to 'a' and 'b' (at 2 places)
519
+ //
520
+ createTransitions: function (o) {
521
+ var pattern, state;
522
+ /** @type {string[]} */
523
+ var stateArray;
524
+ var i;
525
+ //
526
+ // 1. Collect all states
527
+ //
528
+ /** @type {Transitions} */
529
+ var transitions = {};
530
+ for (pattern in o) {
531
+ for (state in o[pattern]) {
532
+ stateArray = state.split("|");
533
+ o[pattern][state].stateArray = stateArray;
534
+ for (i=0; i<stateArray.length; i++) {
535
+ transitions[stateArray[i]] = [];
536
+ }
537
+ }
538
+ }
539
+ //
540
+ // 2. Fill states
541
+ //
542
+ for (pattern in o) {
543
+ for (state in o[pattern]) {
544
+ stateArray = o[pattern][state].stateArray || [];
545
+ for (i=0; i<stateArray.length; i++) {
546
+ //
547
+ // 2a. Normalize actions into array: 'text=' ==> [{type_:'text='}]
548
+ // (Note to myself: Resolving the function here would be problematic. It would need .bind (for *this*) and currying (for *option*).)
549
+ //
550
+ /** @type {any} */
551
+ var p = o[pattern][state];
552
+ if (p.action_) {
553
+ p.action_ = [].concat(p.action_);
554
+ for (var k=0; k<p.action_.length; k++) {
555
+ if (typeof p.action_[k] === "string") {
556
+ p.action_[k] = { type_: p.action_[k] };
557
+ }
558
+ }
559
+ } else {
560
+ p.action_ = [];
561
+ }
562
+ //
563
+ // 2.b Multi-insert
564
+ //
565
+ var patternArray = pattern.split("|");
566
+ for (var j=0; j<patternArray.length; j++) {
567
+ if (stateArray[i] === '*') { // insert into all
568
+ for (var t in transitions) {
569
+ transitions[t].push({ pattern: patternArray[j], task: p });
570
+ }
571
+ } else {
572
+ transitions[stateArray[i]].push({ pattern: patternArray[j], task: p });
573
+ }
574
+ }
575
+ }
576
+ }
577
+ }
578
+ return transitions;
579
+ },
580
+ stateMachines: {}
581
+ };
582
+
583
+ //
584
+ // Definition of state machines
585
+ //
586
+ mhchemParser.stateMachines = {
587
+ //
588
+ // \ce state machines
589
+ //
590
+ //#region ce
591
+ 'ce': { // main parser
592
+ transitions: mhchemParser.createTransitions({
593
+ 'empty': {
594
+ '*': { action_: 'output' } },
595
+ 'else': {
596
+ '0|1|2': { action_: 'beginsWithBond=false', revisit: true, toContinue: true } },
597
+ 'oxidation$': {
598
+ '0': { action_: 'oxidation-output' } },
599
+ 'CMT': {
600
+ 'r': { action_: 'rdt=', nextState: 'rt' },
601
+ 'rd': { action_: 'rqt=', nextState: 'rdt' } },
602
+ 'arrowUpDown': {
603
+ '0|1|2|as': { action_: [ 'sb=false', 'output', 'operator' ], nextState: '1' } },
604
+ 'uprightEntities': {
605
+ '0|1|2': { action_: [ 'o=', 'output' ], nextState: '1' } },
606
+ 'orbital': {
607
+ '0|1|2|3': { action_: 'o=', nextState: 'o' } },
608
+ '->': {
609
+ '0|1|2|3': { action_: 'r=', nextState: 'r' },
610
+ 'a|as': { action_: [ 'output', 'r=' ], nextState: 'r' },
611
+ '*': { action_: [ 'output', 'r=' ], nextState: 'r' } },
612
+ '+': {
613
+ 'o': { action_: 'd= kv', nextState: 'd' },
614
+ 'd|D': { action_: 'd=', nextState: 'd' },
615
+ 'q': { action_: 'd=', nextState: 'qd' },
616
+ 'qd|qD': { action_: 'd=', nextState: 'qd' },
617
+ 'dq': { action_: [ 'output', 'd=' ], nextState: 'd' },
618
+ '3': { action_: [ 'sb=false', 'output', 'operator' ], nextState: '0' } },
619
+ 'amount': {
620
+ '0|2': { action_: 'a=', nextState: 'a' } },
621
+ 'pm-operator': {
622
+ '0|1|2|a|as': { action_: [ 'sb=false', 'output', { type_: 'operator', option: '\\pm' } ], nextState: '0' } },
623
+ 'operator': {
624
+ '0|1|2|a|as': { action_: [ 'sb=false', 'output', 'operator' ], nextState: '0' } },
625
+ '-$': {
626
+ 'o|q': { action_: [ 'charge or bond', 'output' ], nextState: 'qd' },
627
+ 'd': { action_: 'd=', nextState: 'd' },
628
+ 'D': { action_: [ 'output', { type_: 'bond', option: "-" } ], nextState: '3' },
629
+ 'q': { action_: 'd=', nextState: 'qd' },
630
+ 'qd': { action_: 'd=', nextState: 'qd' },
631
+ 'qD|dq': { action_: [ 'output', { type_: 'bond', option: "-" } ], nextState: '3' } },
632
+ '-9': {
633
+ '3|o': { action_: [ 'output', { type_: 'insert', option: 'hyphen' } ], nextState: '3' } },
634
+ '- orbital overlap': {
635
+ 'o': { action_: [ 'output', { type_: 'insert', option: 'hyphen' } ], nextState: '2' },
636
+ 'd': { action_: [ 'output', { type_: 'insert', option: 'hyphen' } ], nextState: '2' } },
637
+ '-': {
638
+ '0|1|2': { action_: [ { type_: 'output', option: 1 }, 'beginsWithBond=true', { type_: 'bond', option: "-" } ], nextState: '3' },
639
+ '3': { action_: { type_: 'bond', option: "-" } },
640
+ 'a': { action_: [ 'output', { type_: 'insert', option: 'hyphen' } ], nextState: '2' },
641
+ 'as': { action_: [ { type_: 'output', option: 2 }, { type_: 'bond', option: "-" } ], nextState: '3' },
642
+ 'b': { action_: 'b=' },
643
+ 'o': { action_: { type_: '- after o/d', option: false }, nextState: '2' },
644
+ 'q': { action_: { type_: '- after o/d', option: false }, nextState: '2' },
645
+ 'd|qd|dq': { action_: { type_: '- after o/d', option: true }, nextState: '2' },
646
+ 'D|qD|p': { action_: [ 'output', { type_: 'bond', option: "-" } ], nextState: '3' } },
647
+ 'amount2': {
648
+ '1|3': { action_: 'a=', nextState: 'a' } },
649
+ 'letters': {
650
+ '0|1|2|3|a|as|b|p|bp|o': { action_: 'o=', nextState: 'o' },
651
+ 'q|dq': { action_: ['output', 'o='], nextState: 'o' },
652
+ 'd|D|qd|qD': { action_: 'o after d', nextState: 'o' } },
653
+ 'digits': {
654
+ 'o': { action_: 'q=', nextState: 'q' },
655
+ 'd|D': { action_: 'q=', nextState: 'dq' },
656
+ 'q': { action_: [ 'output', 'o=' ], nextState: 'o' },
657
+ 'a': { action_: 'o=', nextState: 'o' } },
658
+ 'space A': {
659
+ 'b|p|bp': {} },
660
+ 'space': {
661
+ 'a': { nextState: 'as' },
662
+ '0': { action_: 'sb=false' },
663
+ '1|2': { action_: 'sb=true' },
664
+ 'r|rt|rd|rdt|rdq': { action_: 'output', nextState: '0' },
665
+ '*': { action_: [ 'output', 'sb=true' ], nextState: '1'} },
666
+ '1st-level escape': {
667
+ '1|2': { action_: [ 'output', { type_: 'insert+p1', option: '1st-level escape' } ] },
668
+ '*': { action_: [ 'output', { type_: 'insert+p1', option: '1st-level escape' } ], nextState: '0' } },
669
+ '[(...)]': {
670
+ 'r|rt': { action_: 'rd=', nextState: 'rd' },
671
+ 'rd|rdt': { action_: 'rq=', nextState: 'rdq' } },
672
+ '...': {
673
+ 'o|d|D|dq|qd|qD': { action_: [ 'output', { type_: 'bond', option: "..." } ], nextState: '3' },
674
+ '*': { action_: [ { type_: 'output', option: 1 }, { type_: 'insert', option: 'ellipsis' } ], nextState: '1' } },
675
+ '. |* ': {
676
+ '*': { action_: [ 'output', { type_: 'insert', option: 'addition compound' } ], nextState: '1' } },
677
+ 'state of aggregation $': {
678
+ '*': { action_: [ 'output', 'state of aggregation' ], nextState: '1' } },
679
+ '{[(': {
680
+ 'a|as|o': { action_: [ 'o=', 'output', 'parenthesisLevel++' ], nextState: '2' },
681
+ '0|1|2|3': { action_: [ 'o=', 'output', 'parenthesisLevel++' ], nextState: '2' },
682
+ '*': { action_: [ 'output', 'o=', 'output', 'parenthesisLevel++' ], nextState: '2' } },
683
+ ')]}': {
684
+ '0|1|2|3|b|p|bp|o': { action_: [ 'o=', 'parenthesisLevel--' ], nextState: 'o' },
685
+ 'a|as|d|D|q|qd|qD|dq': { action_: [ 'output', 'o=', 'parenthesisLevel--' ], nextState: 'o' } },
686
+ ', ': {
687
+ '*': { action_: [ 'output', 'comma' ], nextState: '0' } },
688
+ '^_': { // ^ and _ without a sensible argument
689
+ '*': { } },
690
+ '^{(...)}|^($...$)': {
691
+ '0|1|2|as': { action_: 'b=', nextState: 'b' },
692
+ 'p': { action_: 'b=', nextState: 'bp' },
693
+ '3|o': { action_: 'd= kv', nextState: 'D' },
694
+ 'q': { action_: 'd=', nextState: 'qD' },
695
+ 'd|D|qd|qD|dq': { action_: [ 'output', 'd=' ], nextState: 'D' } },
696
+ '^a|^\\x{}{}|^\\x{}|^\\x|\'': {
697
+ '0|1|2|as': { action_: 'b=', nextState: 'b' },
698
+ 'p': { action_: 'b=', nextState: 'bp' },
699
+ '3|o': { action_: 'd= kv', nextState: 'd' },
700
+ 'q': { action_: 'd=', nextState: 'qd' },
701
+ 'd|qd|D|qD': { action_: 'd=' },
702
+ 'dq': { action_: [ 'output', 'd=' ], nextState: 'd' } },
703
+ '_{(state of aggregation)}$': {
704
+ 'd|D|q|qd|qD|dq': { action_: [ 'output', 'q=' ], nextState: 'q' } },
705
+ '_{(...)}|_($...$)|_9|_\\x{}{}|_\\x{}|_\\x': {
706
+ '0|1|2|as': { action_: 'p=', nextState: 'p' },
707
+ 'b': { action_: 'p=', nextState: 'bp' },
708
+ '3|o': { action_: 'q=', nextState: 'q' },
709
+ 'd|D': { action_: 'q=', nextState: 'dq' },
710
+ 'q|qd|qD|dq': { action_: [ 'output', 'q=' ], nextState: 'q' } },
711
+ '=<>': {
712
+ '0|1|2|3|a|as|o|q|d|D|qd|qD|dq': { action_: [ { type_: 'output', option: 2 }, 'bond' ], nextState: '3' } },
713
+ '#': {
714
+ '0|1|2|3|a|as|o': { action_: [ { type_: 'output', option: 2 }, { type_: 'bond', option: "#" } ], nextState: '3' } },
715
+ '{}': {
716
+ '*': { action_: { type_: 'output', option: 1 }, nextState: '1' } },
717
+ '{...}': {
718
+ '0|1|2|3|a|as|b|p|bp': { action_: 'o=', nextState: 'o' },
719
+ 'o|d|D|q|qd|qD|dq': { action_: [ 'output', 'o=' ], nextState: 'o' } },
720
+ '$...$': {
721
+ 'a': { action_: 'a=' }, // 2$n$
722
+ '0|1|2|3|as|b|p|bp|o': { action_: 'o=', nextState: 'o' }, // not 'amount'
723
+ 'as|o': { action_: 'o=' },
724
+ 'q|d|D|qd|qD|dq': { action_: [ 'output', 'o=' ], nextState: 'o' } },
725
+ '\\bond{(...)}': {
726
+ '*': { action_: [ { type_: 'output', option: 2 }, 'bond' ], nextState: "3" } },
727
+ '\\frac{(...)}': {
728
+ '*': { action_: [ { type_: 'output', option: 1 }, 'frac-output' ], nextState: '3' } },
729
+ '\\overset{(...)}': {
730
+ '*': { action_: [ { type_: 'output', option: 2 }, 'overset-output' ], nextState: '3' } },
731
+ '\\underset{(...)}': {
732
+ '*': { action_: [ { type_: 'output', option: 2 }, 'underset-output' ], nextState: '3' } },
733
+ '\\underbrace{(...)}': {
734
+ '*': { action_: [ { type_: 'output', option: 2 }, 'underbrace-output' ], nextState: '3' } },
735
+ '\\color{(...)}{(...)}1|\\color(...){(...)}2': {
736
+ '*': { action_: [ { type_: 'output', option: 2 }, 'color-output' ], nextState: '3' } },
737
+ '\\color{(...)}0': {
738
+ '*': { action_: [ { type_: 'output', option: 2 }, 'color0-output' ] } },
739
+ '\\ce{(...)}': {
740
+ '*': { action_: [ { type_: 'output', option: 2 }, 'ce' ], nextState: '3' } },
741
+ '\\,': {
742
+ '*': { action_: [ { type_: 'output', option: 1 }, 'copy' ], nextState: '1' } },
743
+ '\\x{}{}|\\x{}|\\x': {
744
+ '0|1|2|3|a|as|b|p|bp|o|c0': { action_: [ 'o=', 'output' ], nextState: '3' },
745
+ '*': { action_: ['output', 'o=', 'output' ], nextState: '3' } },
746
+ 'others': {
747
+ '*': { action_: [ { type_: 'output', option: 1 }, 'copy' ], nextState: '3' } },
748
+ 'else2': {
749
+ 'a': { action_: 'a to o', nextState: 'o', revisit: true },
750
+ 'as': { action_: [ 'output', 'sb=true' ], nextState: '1', revisit: true },
751
+ 'r|rt|rd|rdt|rdq': { action_: [ 'output' ], nextState: '0', revisit: true },
752
+ '*': { action_: [ 'output', 'copy' ], nextState: '3' } }
753
+ }),
754
+ actions: {
755
+ 'o after d': function (buffer, m) {
756
+ var ret;
757
+ if ((buffer.d || "").match(/^[0-9]+$/)) {
758
+ var tmp = buffer.d;
759
+ buffer.d = undefined;
760
+ ret = this['output'](buffer);
761
+ buffer.b = tmp;
762
+ } else {
763
+ ret = this['output'](buffer);
764
+ }
765
+ mhchemParser.actions['o='](buffer, m);
766
+ return ret;
767
+ },
768
+ 'd= kv': function (buffer, m) {
769
+ buffer.d = m;
770
+ buffer.dType = 'kv';
771
+ },
772
+ 'charge or bond': function (buffer, m) {
773
+ if (buffer['beginsWithBond']) {
774
+ /** @type {ParserOutput[]} */
775
+ var ret = [];
776
+ mhchemParser.concatArray(ret, this['output'](buffer));
777
+ mhchemParser.concatArray(ret, mhchemParser.actions['bond'](buffer, m, "-"));
778
+ return ret;
779
+ } else {
780
+ buffer.d = m;
781
+ }
782
+ },
783
+ '- after o/d': function (buffer, m, isAfterD) {
784
+ var c1 = mhchemParser.patterns.match_('orbital', buffer.o || "");
785
+ var c2 = mhchemParser.patterns.match_('one lowercase greek letter $', buffer.o || "");
786
+ var c3 = mhchemParser.patterns.match_('one lowercase latin letter $', buffer.o || "");
787
+ var c4 = mhchemParser.patterns.match_('$one lowercase latin letter$ $', buffer.o || "");
788
+ var hyphenFollows = m==="-" && ( c1 && c1.remainder==="" || c2 || c3 || c4 );
789
+ if (hyphenFollows && !buffer.a && !buffer.b && !buffer.p && !buffer.d && !buffer.q && !c1 && c3) {
790
+ buffer.o = '$' + buffer.o + '$';
791
+ }
792
+ /** @type {ParserOutput[]} */
793
+ var ret = [];
794
+ if (hyphenFollows) {
795
+ mhchemParser.concatArray(ret, this['output'](buffer));
796
+ ret.push({ type_: 'hyphen' });
797
+ } else {
798
+ c1 = mhchemParser.patterns.match_('digits', buffer.d || "");
799
+ if (isAfterD && c1 && c1.remainder==='') {
800
+ mhchemParser.concatArray(ret, mhchemParser.actions['d='](buffer, m));
801
+ mhchemParser.concatArray(ret, this['output'](buffer));
802
+ } else {
803
+ mhchemParser.concatArray(ret, this['output'](buffer));
804
+ mhchemParser.concatArray(ret, mhchemParser.actions['bond'](buffer, m, "-"));
805
+ }
806
+ }
807
+ return ret;
808
+ },
809
+ 'a to o': function (buffer) {
810
+ buffer.o = buffer.a;
811
+ buffer.a = undefined;
812
+ },
813
+ 'sb=true': function (buffer) { buffer.sb = true; },
814
+ 'sb=false': function (buffer) { buffer.sb = false; },
815
+ 'beginsWithBond=true': function (buffer) { buffer['beginsWithBond'] = true; },
816
+ 'beginsWithBond=false': function (buffer) { buffer['beginsWithBond'] = false; },
817
+ 'parenthesisLevel++': function (buffer) { buffer['parenthesisLevel']++; },
818
+ 'parenthesisLevel--': function (buffer) { buffer['parenthesisLevel']--; },
819
+ 'state of aggregation': function (buffer, m) {
820
+ return { type_: 'state of aggregation', p1: mhchemParser.go(m, 'o') };
821
+ },
822
+ 'comma': function (buffer, m) {
823
+ var a = m.replace(/\s*$/, '');
824
+ var withSpace = (a !== m);
825
+ if (withSpace && buffer['parenthesisLevel'] === 0) {
826
+ return { type_: 'comma enumeration L', p1: a };
827
+ } else {
828
+ return { type_: 'comma enumeration M', p1: a };
829
+ }
830
+ },
831
+ 'output': function (buffer, m, entityFollows) {
832
+ // entityFollows:
833
+ // undefined = if we have nothing else to output, also ignore the just read space (buffer.sb)
834
+ // 1 = an entity follows, never omit the space if there was one just read before (can only apply to state 1)
835
+ // 2 = 1 + the entity can have an amount, so output a\, instead of converting it to o (can only apply to states a|as)
836
+ /** @type {ParserOutput | ParserOutput[]} */
837
+ var ret;
838
+ if (!buffer.r) {
839
+ ret = [];
840
+ if (!buffer.a && !buffer.b && !buffer.p && !buffer.o && !buffer.q && !buffer.d && !entityFollows) {
841
+ //ret = [];
842
+ } else {
843
+ if (buffer.sb) {
844
+ ret.push({ type_: 'entitySkip' });
845
+ }
846
+ if (!buffer.o && !buffer.q && !buffer.d && !buffer.b && !buffer.p && entityFollows!==2) {
847
+ buffer.o = buffer.a;
848
+ buffer.a = undefined;
849
+ } else if (!buffer.o && !buffer.q && !buffer.d && (buffer.b || buffer.p)) {
850
+ buffer.o = buffer.a;
851
+ buffer.d = buffer.b;
852
+ buffer.q = buffer.p;
853
+ buffer.a = buffer.b = buffer.p = undefined;
854
+ } else {
855
+ if (buffer.o && buffer.dType==='kv' && mhchemParser.patterns.match_('d-oxidation$', buffer.d || "")) {
856
+ buffer.dType = 'oxidation';
857
+ } else if (buffer.o && buffer.dType==='kv' && !buffer.q) {
858
+ buffer.dType = undefined;
859
+ }
860
+ }
861
+ ret.push({
862
+ type_: 'chemfive',
863
+ a: mhchemParser.go(buffer.a, 'a'),
864
+ b: mhchemParser.go(buffer.b, 'bd'),
865
+ p: mhchemParser.go(buffer.p, 'pq'),
866
+ o: mhchemParser.go(buffer.o, 'o'),
867
+ q: mhchemParser.go(buffer.q, 'pq'),
868
+ d: mhchemParser.go(buffer.d, (buffer.dType === 'oxidation' ? 'oxidation' : 'bd')),
869
+ dType: buffer.dType
870
+ });
871
+ }
872
+ } else { // r
873
+ /** @type {ParserOutput[]} */
874
+ var rd;
875
+ if (buffer.rdt === 'M') {
876
+ rd = mhchemParser.go(buffer.rd, 'tex-math');
877
+ } else if (buffer.rdt === 'T') {
878
+ rd = [ { type_: 'text', p1: buffer.rd || "" } ];
879
+ } else {
880
+ rd = mhchemParser.go(buffer.rd);
881
+ }
882
+ /** @type {ParserOutput[]} */
883
+ var rq;
884
+ if (buffer.rqt === 'M') {
885
+ rq = mhchemParser.go(buffer.rq, 'tex-math');
886
+ } else if (buffer.rqt === 'T') {
887
+ rq = [ { type_: 'text', p1: buffer.rq || ""} ];
888
+ } else {
889
+ rq = mhchemParser.go(buffer.rq);
890
+ }
891
+ ret = {
892
+ type_: 'arrow',
893
+ r: buffer.r,
894
+ rd: rd,
895
+ rq: rq
896
+ };
897
+ }
898
+ for (var p in buffer) {
899
+ if (p !== 'parenthesisLevel' && p !== 'beginsWithBond') {
900
+ delete buffer[p];
901
+ }
902
+ }
903
+ return ret;
904
+ },
905
+ 'oxidation-output': function (buffer, m) {
906
+ var ret = [ "{" ];
907
+ mhchemParser.concatArray(ret, mhchemParser.go(m, 'oxidation'));
908
+ ret.push("}");
909
+ return ret;
910
+ },
911
+ 'frac-output': function (buffer, m) {
912
+ return { type_: 'frac-ce', p1: mhchemParser.go(m[0]), p2: mhchemParser.go(m[1]) };
913
+ },
914
+ 'overset-output': function (buffer, m) {
915
+ return { type_: 'overset', p1: mhchemParser.go(m[0]), p2: mhchemParser.go(m[1]) };
916
+ },
917
+ 'underset-output': function (buffer, m) {
918
+ return { type_: 'underset', p1: mhchemParser.go(m[0]), p2: mhchemParser.go(m[1]) };
919
+ },
920
+ 'underbrace-output': function (buffer, m) {
921
+ return { type_: 'underbrace', p1: mhchemParser.go(m[0]), p2: mhchemParser.go(m[1]) };
922
+ },
923
+ 'color-output': function (buffer, m) {
924
+ return { type_: 'color', color1: m[0], color2: mhchemParser.go(m[1]) };
925
+ },
926
+ 'r=': function (buffer, m) { buffer.r = m; },
927
+ 'rdt=': function (buffer, m) { buffer.rdt = m; },
928
+ 'rd=': function (buffer, m) { buffer.rd = m; },
929
+ 'rqt=': function (buffer, m) { buffer.rqt = m; },
930
+ 'rq=': function (buffer, m) { buffer.rq = m; },
931
+ 'operator': function (buffer, m, p1) { return { type_: 'operator', kind_: (p1 || m) }; }
932
+ }
933
+ },
934
+ 'a': {
935
+ transitions: mhchemParser.createTransitions({
936
+ 'empty': {
937
+ '*': {} },
938
+ '1/2$': {
939
+ '0': { action_: '1/2' } },
940
+ 'else': {
941
+ '0': { nextState: '1', revisit: true } },
942
+ '$(...)$': {
943
+ '*': { action_: 'tex-math tight', nextState: '1' } },
944
+ ',': {
945
+ '*': { action_: { type_: 'insert', option: 'commaDecimal' } } },
946
+ 'else2': {
947
+ '*': { action_: 'copy' } }
948
+ }),
949
+ actions: {}
950
+ },
951
+ 'o': {
952
+ transitions: mhchemParser.createTransitions({
953
+ 'empty': {
954
+ '*': {} },
955
+ '1/2$': {
956
+ '0': { action_: '1/2' } },
957
+ 'else': {
958
+ '0': { nextState: '1', revisit: true } },
959
+ 'letters': {
960
+ '*': { action_: 'rm' } },
961
+ '\\ca': {
962
+ '*': { action_: { type_: 'insert', option: 'circa' } } },
963
+ '\\x{}{}|\\x{}|\\x': {
964
+ '*': { action_: 'copy' } },
965
+ '${(...)}$|$(...)$': {
966
+ '*': { action_: 'tex-math' } },
967
+ '{(...)}': {
968
+ '*': { action_: '{text}' } },
969
+ 'else2': {
970
+ '*': { action_: 'copy' } }
971
+ }),
972
+ actions: {}
973
+ },
974
+ 'text': {
975
+ transitions: mhchemParser.createTransitions({
976
+ 'empty': {
977
+ '*': { action_: 'output' } },
978
+ '{...}': {
979
+ '*': { action_: 'text=' } },
980
+ '${(...)}$|$(...)$': {
981
+ '*': { action_: 'tex-math' } },
982
+ '\\greek': {
983
+ '*': { action_: [ 'output', 'rm' ] } },
984
+ '\\,|\\x{}{}|\\x{}|\\x': {
985
+ '*': { action_: [ 'output', 'copy' ] } },
986
+ 'else': {
987
+ '*': { action_: 'text=' } }
988
+ }),
989
+ actions: {
990
+ 'output': function (buffer) {
991
+ if (buffer.text_) {
992
+ /** @type {ParserOutput} */
993
+ var ret = { type_: 'text', p1: buffer.text_ };
994
+ for (var p in buffer) { delete buffer[p]; }
995
+ return ret;
996
+ }
997
+ }
998
+ }
999
+ },
1000
+ 'pq': {
1001
+ transitions: mhchemParser.createTransitions({
1002
+ 'empty': {
1003
+ '*': {} },
1004
+ 'state of aggregation $': {
1005
+ '*': { action_: 'state of aggregation' } },
1006
+ 'i$': {
1007
+ '0': { nextState: '!f', revisit: true } },
1008
+ '(KV letters),': {
1009
+ '0': { action_: 'rm', nextState: '0' } },
1010
+ 'formula$': {
1011
+ '0': { nextState: 'f', revisit: true } },
1012
+ '1/2$': {
1013
+ '0': { action_: '1/2' } },
1014
+ 'else': {
1015
+ '0': { nextState: '!f', revisit: true } },
1016
+ '${(...)}$|$(...)$': {
1017
+ '*': { action_: 'tex-math' } },
1018
+ '{(...)}': {
1019
+ '*': { action_: 'text' } },
1020
+ 'a-z': {
1021
+ 'f': { action_: 'tex-math' } },
1022
+ 'letters': {
1023
+ '*': { action_: 'rm' } },
1024
+ '-9.,9': {
1025
+ '*': { action_: '9,9' } },
1026
+ ',': {
1027
+ '*': { action_: { type_: 'insert+p1', option: 'comma enumeration S' } } },
1028
+ '\\color{(...)}{(...)}1|\\color(...){(...)}2': {
1029
+ '*': { action_: 'color-output' } },
1030
+ '\\color{(...)}0': {
1031
+ '*': { action_: 'color0-output' } },
1032
+ '\\ce{(...)}': {
1033
+ '*': { action_: 'ce' } },
1034
+ '\\,|\\x{}{}|\\x{}|\\x': {
1035
+ '*': { action_: 'copy' } },
1036
+ 'else2': {
1037
+ '*': { action_: 'copy' } }
1038
+ }),
1039
+ actions: {
1040
+ 'state of aggregation': function (buffer, m) {
1041
+ return { type_: 'state of aggregation subscript', p1: mhchemParser.go(m, 'o') };
1042
+ },
1043
+ 'color-output': function (buffer, m) {
1044
+ return { type_: 'color', color1: m[0], color2: mhchemParser.go(m[1], 'pq') };
1045
+ }
1046
+ }
1047
+ },
1048
+ 'bd': {
1049
+ transitions: mhchemParser.createTransitions({
1050
+ 'empty': {
1051
+ '*': {} },
1052
+ 'x$': {
1053
+ '0': { nextState: '!f', revisit: true } },
1054
+ 'formula$': {
1055
+ '0': { nextState: 'f', revisit: true } },
1056
+ 'else': {
1057
+ '0': { nextState: '!f', revisit: true } },
1058
+ '-9.,9 no missing 0': {
1059
+ '*': { action_: '9,9' } },
1060
+ '.': {
1061
+ '*': { action_: { type_: 'insert', option: 'electron dot' } } },
1062
+ 'a-z': {
1063
+ 'f': { action_: 'tex-math' } },
1064
+ 'x': {
1065
+ '*': { action_: { type_: 'insert', option: 'KV x' } } },
1066
+ 'letters': {
1067
+ '*': { action_: 'rm' } },
1068
+ '\'': {
1069
+ '*': { action_: { type_: 'insert', option: 'prime' } } },
1070
+ '${(...)}$|$(...)$': {
1071
+ '*': { action_: 'tex-math' } },
1072
+ '{(...)}': {
1073
+ '*': { action_: 'text' } },
1074
+ '\\color{(...)}{(...)}1|\\color(...){(...)}2': {
1075
+ '*': { action_: 'color-output' } },
1076
+ '\\color{(...)}0': {
1077
+ '*': { action_: 'color0-output' } },
1078
+ '\\ce{(...)}': {
1079
+ '*': { action_: 'ce' } },
1080
+ '\\,|\\x{}{}|\\x{}|\\x': {
1081
+ '*': { action_: 'copy' } },
1082
+ 'else2': {
1083
+ '*': { action_: 'copy' } }
1084
+ }),
1085
+ actions: {
1086
+ 'color-output': function (buffer, m) {
1087
+ return { type_: 'color', color1: m[0], color2: mhchemParser.go(m[1], 'bd') };
1088
+ }
1089
+ }
1090
+ },
1091
+ 'oxidation': {
1092
+ transitions: mhchemParser.createTransitions({
1093
+ 'empty': {
1094
+ '*': {} },
1095
+ 'roman numeral': {
1096
+ '*': { action_: 'roman-numeral' } },
1097
+ '${(...)}$|$(...)$': {
1098
+ '*': { action_: 'tex-math' } },
1099
+ 'else': {
1100
+ '*': { action_: 'copy' } }
1101
+ }),
1102
+ actions: {
1103
+ 'roman-numeral': function (buffer, m) { return { type_: 'roman numeral', p1: m || "" }; }
1104
+ }
1105
+ },
1106
+ 'tex-math': {
1107
+ transitions: mhchemParser.createTransitions({
1108
+ 'empty': {
1109
+ '*': { action_: 'output' } },
1110
+ '\\ce{(...)}': {
1111
+ '*': { action_: [ 'output', 'ce' ] } },
1112
+ '{...}|\\,|\\x{}{}|\\x{}|\\x': {
1113
+ '*': { action_: 'o=' } },
1114
+ 'else': {
1115
+ '*': { action_: 'o=' } }
1116
+ }),
1117
+ actions: {
1118
+ 'output': function (buffer) {
1119
+ if (buffer.o) {
1120
+ /** @type {ParserOutput} */
1121
+ var ret = { type_: 'tex-math', p1: buffer.o };
1122
+ for (var p in buffer) { delete buffer[p]; }
1123
+ return ret;
1124
+ }
1125
+ }
1126
+ }
1127
+ },
1128
+ 'tex-math tight': {
1129
+ transitions: mhchemParser.createTransitions({
1130
+ 'empty': {
1131
+ '*': { action_: 'output' } },
1132
+ '\\ce{(...)}': {
1133
+ '*': { action_: [ 'output', 'ce' ] } },
1134
+ '{...}|\\,|\\x{}{}|\\x{}|\\x': {
1135
+ '*': { action_: 'o=' } },
1136
+ '-|+': {
1137
+ '*': { action_: 'tight operator' } },
1138
+ 'else': {
1139
+ '*': { action_: 'o=' } }
1140
+ }),
1141
+ actions: {
1142
+ 'tight operator': function (buffer, m) { buffer.o = (buffer.o || "") + "{"+m+"}"; },
1143
+ 'output': function (buffer) {
1144
+ if (buffer.o) {
1145
+ /** @type {ParserOutput} */
1146
+ var ret = { type_: 'tex-math', p1: buffer.o };
1147
+ for (var p in buffer) { delete buffer[p]; }
1148
+ return ret;
1149
+ }
1150
+ }
1151
+ }
1152
+ },
1153
+ '9,9': {
1154
+ transitions: mhchemParser.createTransitions({
1155
+ 'empty': {
1156
+ '*': {} },
1157
+ ',': {
1158
+ '*': { action_: 'comma' } },
1159
+ 'else': {
1160
+ '*': { action_: 'copy' } }
1161
+ }),
1162
+ actions: {
1163
+ 'comma': function () { return { type_: 'commaDecimal' }; }
1164
+ }
1165
+ },
1166
+ //#endregion
1167
+ //
1168
+ // \pu state machines
1169
+ //
1170
+ //#region pu
1171
+ 'pu': {
1172
+ transitions: mhchemParser.createTransitions({
1173
+ 'empty': {
1174
+ '*': { action_: 'output' } },
1175
+ 'space$': {
1176
+ '*': { action_: [ 'output', 'space' ] } },
1177
+ '{[(|)]}': {
1178
+ '0|a': { action_: 'copy' } },
1179
+ '(-)(9)^(-9)': {
1180
+ '0': { action_: 'number^', nextState: 'a' } },
1181
+ '(-)(9.,9)(e)(99)': {
1182
+ '0': { action_: 'enumber', nextState: 'a' } },
1183
+ 'space': {
1184
+ '0|a': {} },
1185
+ 'pm-operator': {
1186
+ '0|a': { action_: { type_: 'operator', option: '\\pm' }, nextState: '0' } },
1187
+ 'operator': {
1188
+ '0|a': { action_: 'copy', nextState: '0' } },
1189
+ '//': {
1190
+ 'd': { action_: 'o=', nextState: '/' } },
1191
+ '/': {
1192
+ 'd': { action_: 'o=', nextState: '/' } },
1193
+ '{...}|else': {
1194
+ '0|d': { action_: 'd=', nextState: 'd' },
1195
+ 'a': { action_: [ 'space', 'd=' ], nextState: 'd' },
1196
+ '/|q': { action_: 'q=', nextState: 'q' } }
1197
+ }),
1198
+ actions: {
1199
+ 'enumber': function (buffer, m) {
1200
+ /** @type {ParserOutput[]} */
1201
+ var ret = [];
1202
+ if (m[0] === "+-" || m[0] === "+/-") {
1203
+ ret.push("\\pm ");
1204
+ } else if (m[0]) {
1205
+ ret.push(m[0]);
1206
+ }
1207
+ if (m[1]) {
1208
+ mhchemParser.concatArray(ret, mhchemParser.go(m[1], 'pu-9,9'));
1209
+ if (m[2]) {
1210
+ if (m[2].match(/[,.]/)) {
1211
+ mhchemParser.concatArray(ret, mhchemParser.go(m[2], 'pu-9,9'));
1212
+ } else {
1213
+ ret.push(m[2]);
1214
+ }
1215
+ }
1216
+ m[3] = m[4] || m[3];
1217
+ if (m[3]) {
1218
+ m[3] = m[3].trim();
1219
+ if (m[3] === "e" || m[3].substr(0, 1) === "*") {
1220
+ ret.push({ type_: 'cdot' });
1221
+ } else {
1222
+ ret.push({ type_: 'times' });
1223
+ }
1224
+ }
1225
+ }
1226
+ if (m[3]) {
1227
+ ret.push("10^{"+m[5]+"}");
1228
+ }
1229
+ return ret;
1230
+ },
1231
+ 'number^': function (buffer, m) {
1232
+ /** @type {ParserOutput[]} */
1233
+ var ret = [];
1234
+ if (m[0] === "+-" || m[0] === "+/-") {
1235
+ ret.push("\\pm ");
1236
+ } else if (m[0]) {
1237
+ ret.push(m[0]);
1238
+ }
1239
+ mhchemParser.concatArray(ret, mhchemParser.go(m[1], 'pu-9,9'));
1240
+ ret.push("^{"+m[2]+"}");
1241
+ return ret;
1242
+ },
1243
+ 'operator': function (buffer, m, p1) { return { type_: 'operator', kind_: (p1 || m) }; },
1244
+ 'space': function () { return { type_: 'pu-space-1' }; },
1245
+ 'output': function (buffer) {
1246
+ /** @type {ParserOutput | ParserOutput[]} */
1247
+ var ret;
1248
+ var md = mhchemParser.patterns.match_('{(...)}', buffer.d || "");
1249
+ if (md && md.remainder === '') { buffer.d = md.match_; }
1250
+ var mq = mhchemParser.patterns.match_('{(...)}', buffer.q || "");
1251
+ if (mq && mq.remainder === '') { buffer.q = mq.match_; }
1252
+ if (buffer.d) {
1253
+ buffer.d = buffer.d.replace(/\u00B0C|\^oC|\^{o}C/g, "{}^{\\circ}C");
1254
+ buffer.d = buffer.d.replace(/\u00B0F|\^oF|\^{o}F/g, "{}^{\\circ}F");
1255
+ }
1256
+ if (buffer.q) { // fraction
1257
+ buffer.q = buffer.q.replace(/\u00B0C|\^oC|\^{o}C/g, "{}^{\\circ}C");
1258
+ buffer.q = buffer.q.replace(/\u00B0F|\^oF|\^{o}F/g, "{}^{\\circ}F");
1259
+ var b5 = {
1260
+ d: mhchemParser.go(buffer.d, 'pu'),
1261
+ q: mhchemParser.go(buffer.q, 'pu')
1262
+ };
1263
+ if (buffer.o === '//') {
1264
+ ret = { type_: 'pu-frac', p1: b5.d, p2: b5.q };
1265
+ } else {
1266
+ ret = b5.d;
1267
+ if (b5.d.length > 1 || b5.q.length > 1) {
1268
+ ret.push({ type_: ' / ' });
1269
+ } else {
1270
+ ret.push({ type_: '/' });
1271
+ }
1272
+ mhchemParser.concatArray(ret, b5.q);
1273
+ }
1274
+ } else { // no fraction
1275
+ ret = mhchemParser.go(buffer.d, 'pu-2');
1276
+ }
1277
+ for (var p in buffer) { delete buffer[p]; }
1278
+ return ret;
1279
+ }
1280
+ }
1281
+ },
1282
+ 'pu-2': {
1283
+ transitions: mhchemParser.createTransitions({
1284
+ 'empty': {
1285
+ '*': { action_: 'output' } },
1286
+ '*': {
1287
+ '*': { action_: [ 'output', 'cdot' ], nextState: '0' } },
1288
+ '\\x': {
1289
+ '*': { action_: 'rm=' } },
1290
+ 'space': {
1291
+ '*': { action_: [ 'output', 'space' ], nextState: '0' } },
1292
+ '^{(...)}|^(-1)': {
1293
+ '1': { action_: '^(-1)' } },
1294
+ '-9.,9': {
1295
+ '0': { action_: 'rm=', nextState: '0' },
1296
+ '1': { action_: '^(-1)', nextState: '0' } },
1297
+ '{...}|else': {
1298
+ '*': { action_: 'rm=', nextState: '1' } }
1299
+ }),
1300
+ actions: {
1301
+ 'cdot': function () { return { type_: 'tight cdot' }; },
1302
+ '^(-1)': function (buffer, m) { buffer.rm += "^{"+m+"}"; },
1303
+ 'space': function () { return { type_: 'pu-space-2' }; },
1304
+ 'output': function (buffer) {
1305
+ /** @type {ParserOutput | ParserOutput[]} */
1306
+ var ret = [];
1307
+ if (buffer.rm) {
1308
+ var mrm = mhchemParser.patterns.match_('{(...)}', buffer.rm || "");
1309
+ if (mrm && mrm.remainder === '') {
1310
+ ret = mhchemParser.go(mrm.match_, 'pu');
1311
+ } else {
1312
+ ret = { type_: 'rm', p1: buffer.rm };
1313
+ }
1314
+ }
1315
+ for (var p in buffer) { delete buffer[p]; }
1316
+ return ret;
1317
+ }
1318
+ }
1319
+ },
1320
+ 'pu-9,9': {
1321
+ transitions: mhchemParser.createTransitions({
1322
+ 'empty': {
1323
+ '0': { action_: 'output-0' },
1324
+ 'o': { action_: 'output-o' } },
1325
+ ',': {
1326
+ '0': { action_: [ 'output-0', 'comma' ], nextState: 'o' } },
1327
+ '.': {
1328
+ '0': { action_: [ 'output-0', 'copy' ], nextState: 'o' } },
1329
+ 'else': {
1330
+ '*': { action_: 'text=' } }
1331
+ }),
1332
+ actions: {
1333
+ 'comma': function () { return { type_: 'commaDecimal' }; },
1334
+ 'output-0': function (buffer) {
1335
+ /** @type {ParserOutput[]} */
1336
+ var ret = [];
1337
+ buffer.text_ = buffer.text_ || "";
1338
+ if (buffer.text_.length > 4) {
1339
+ var a = buffer.text_.length % 3;
1340
+ if (a === 0) { a = 3; }
1341
+ for (var i=buffer.text_.length-3; i>0; i-=3) {
1342
+ ret.push(buffer.text_.substr(i, 3));
1343
+ ret.push({ type_: '1000 separator' });
1344
+ }
1345
+ ret.push(buffer.text_.substr(0, a));
1346
+ ret.reverse();
1347
+ } else {
1348
+ ret.push(buffer.text_);
1349
+ }
1350
+ for (var p in buffer) { delete buffer[p]; }
1351
+ return ret;
1352
+ },
1353
+ 'output-o': function (buffer) {
1354
+ /** @type {ParserOutput[]} */
1355
+ var ret = [];
1356
+ buffer.text_ = buffer.text_ || "";
1357
+ if (buffer.text_.length > 4) {
1358
+ var a = buffer.text_.length - 3;
1359
+ for (var i=0; i<a; i+=3) {
1360
+ ret.push(buffer.text_.substr(i, 3));
1361
+ ret.push({ type_: '1000 separator' });
1362
+ }
1363
+ ret.push(buffer.text_.substr(i));
1364
+ } else {
1365
+ ret.push(buffer.text_);
1366
+ }
1367
+ for (var p in buffer) { delete buffer[p]; }
1368
+ return ret;
1369
+ }
1370
+ }
1371
+ }
1372
+ //#endregion
1373
+ };
1374
+
1375
+ //
1376
+ // texify: Take MhchemParser output and convert it to TeX
1377
+ //
1378
+ /** @type {Texify} */
1379
+ var texify = {
1380
+ go: function (input, isInner) { // (recursive, max 4 levels)
1381
+ if (!input) { return ""; }
1382
+ var res = "";
1383
+ var cee = false;
1384
+ for (var i=0; i < input.length; i++) {
1385
+ var inputi = input[i];
1386
+ if (typeof inputi === "string") {
1387
+ res += inputi;
1388
+ } else {
1389
+ res += texify._go2(inputi);
1390
+ if (inputi.type_ === '1st-level escape') { cee = true; }
1391
+ }
1392
+ }
1393
+ if (!isInner && !cee && res) {
1394
+ res = "{" + res + "}";
1395
+ }
1396
+ return res;
1397
+ },
1398
+ _goInner: function (input) {
1399
+ if (!input) { return input; }
1400
+ return texify.go(input, true);
1401
+ },
1402
+ _go2: function (buf) {
1403
+ /** @type {undefined | string} */
1404
+ var res;
1405
+ switch (buf.type_) {
1406
+ case 'chemfive':
1407
+ res = "";
1408
+ var b5 = {
1409
+ a: texify._goInner(buf.a),
1410
+ b: texify._goInner(buf.b),
1411
+ p: texify._goInner(buf.p),
1412
+ o: texify._goInner(buf.o),
1413
+ q: texify._goInner(buf.q),
1414
+ d: texify._goInner(buf.d)
1415
+ };
1416
+ //
1417
+ // a
1418
+ //
1419
+ if (b5.a) {
1420
+ if (b5.a.match(/^[+\-]/)) { b5.a = "{"+b5.a+"}"; }
1421
+ res += b5.a + "\\,";
1422
+ }
1423
+ //
1424
+ // b and p
1425
+ //
1426
+ if (b5.b || b5.p) {
1427
+ res += "{\\vphantom{X}}";
1428
+ res += "^{\\hphantom{"+(b5.b||"")+"}}_{\\hphantom{"+(b5.p||"")+"}}";
1429
+ res += "{\\vphantom{X}}";
1430
+ res += "^{\\smash[t]{\\vphantom{2}}\\mathllap{"+(b5.b||"")+"}}";
1431
+ res += "_{\\vphantom{2}\\mathllap{\\smash[t]{"+(b5.p||"")+"}}}";
1432
+ }
1433
+ //
1434
+ // o
1435
+ //
1436
+ if (b5.o) {
1437
+ if (b5.o.match(/^[+\-]/)) { b5.o = "{"+b5.o+"}"; }
1438
+ res += b5.o;
1439
+ }
1440
+ //
1441
+ // q and d
1442
+ //
1443
+ if (buf.dType === 'kv') {
1444
+ if (b5.d || b5.q) {
1445
+ res += "{\\vphantom{X}}";
1446
+ }
1447
+ if (b5.d) {
1448
+ res += "^{"+b5.d+"}";
1449
+ }
1450
+ if (b5.q) {
1451
+ res += "_{\\smash[t]{"+b5.q+"}}";
1452
+ }
1453
+ } else if (buf.dType === 'oxidation') {
1454
+ if (b5.d) {
1455
+ res += "{\\vphantom{X}}";
1456
+ res += "^{"+b5.d+"}";
1457
+ }
1458
+ if (b5.q) {
1459
+ res += "{\\vphantom{X}}";
1460
+ res += "_{\\smash[t]{"+b5.q+"}}";
1461
+ }
1462
+ } else {
1463
+ if (b5.q) {
1464
+ res += "{\\vphantom{X}}";
1465
+ res += "_{\\smash[t]{"+b5.q+"}}";
1466
+ }
1467
+ if (b5.d) {
1468
+ res += "{\\vphantom{X}}";
1469
+ res += "^{"+b5.d+"}";
1470
+ }
1471
+ }
1472
+ break;
1473
+ case 'rm':
1474
+ res = "\\mathrm{"+buf.p1+"}";
1475
+ break;
1476
+ case 'text':
1477
+ if (buf.p1.match(/[\^_]/)) {
1478
+ buf.p1 = buf.p1.replace(" ", "~").replace("-", "\\text{-}");
1479
+ res = "\\mathrm{"+buf.p1+"}";
1480
+ } else {
1481
+ res = "\\text{"+buf.p1+"}";
1482
+ }
1483
+ break;
1484
+ case 'roman numeral':
1485
+ res = "\\mathrm{"+buf.p1+"}";
1486
+ break;
1487
+ case 'state of aggregation':
1488
+ res = "\\mskip2mu "+texify._goInner(buf.p1);
1489
+ break;
1490
+ case 'state of aggregation subscript':
1491
+ res = "\\mskip1mu "+texify._goInner(buf.p1);
1492
+ break;
1493
+ case 'bond':
1494
+ res = texify._getBond(buf.kind_);
1495
+ if (!res) {
1496
+ throw ["MhchemErrorBond", "mhchem Error. Unknown bond type (" + buf.kind_ + ")"];
1497
+ }
1498
+ break;
1499
+ case 'frac':
1500
+ var c = "\\frac{" + buf.p1 + "}{" + buf.p2 + "}";
1501
+ res = "\\mathchoice{\\textstyle"+c+"}{"+c+"}{"+c+"}{"+c+"}";
1502
+ break;
1503
+ case 'pu-frac':
1504
+ var d = "\\frac{" + texify._goInner(buf.p1) + "}{" + texify._goInner(buf.p2) + "}";
1505
+ res = "\\mathchoice{\\textstyle"+d+"}{"+d+"}{"+d+"}{"+d+"}";
1506
+ break;
1507
+ case 'tex-math':
1508
+ res = buf.p1 + " ";
1509
+ break;
1510
+ case 'frac-ce':
1511
+ res = "\\frac{" + texify._goInner(buf.p1) + "}{" + texify._goInner(buf.p2) + "}";
1512
+ break;
1513
+ case 'overset':
1514
+ res = "\\overset{" + texify._goInner(buf.p1) + "}{" + texify._goInner(buf.p2) + "}";
1515
+ break;
1516
+ case 'underset':
1517
+ res = "\\underset{" + texify._goInner(buf.p1) + "}{" + texify._goInner(buf.p2) + "}";
1518
+ break;
1519
+ case 'underbrace':
1520
+ res = "\\underbrace{" + texify._goInner(buf.p1) + "}_{" + texify._goInner(buf.p2) + "}";
1521
+ break;
1522
+ case 'color':
1523
+ res = "{\\color{" + buf.color1 + "}{" + texify._goInner(buf.color2) + "}}";
1524
+ break;
1525
+ case 'color0':
1526
+ res = "\\color{" + buf.color + "}";
1527
+ break;
1528
+ case 'arrow':
1529
+ var b6 = {
1530
+ rd: texify._goInner(buf.rd),
1531
+ rq: texify._goInner(buf.rq)
1532
+ };
1533
+ var arrow = "\\x" + texify._getArrow(buf.r);
1534
+ if (b6.rq) { arrow += "[{" + b6.rq + "}]"; }
1535
+ if (b6.rd) {
1536
+ arrow += "{" + b6.rd + "}";
1537
+ } else {
1538
+ arrow += "{}";
1539
+ }
1540
+ res = arrow;
1541
+ break;
1542
+ case 'operator':
1543
+ res = texify._getOperator(buf.kind_);
1544
+ break;
1545
+ case '1st-level escape':
1546
+ res = buf.p1+" "; // &, \\\\, \\hlin
1547
+ break;
1548
+ case 'space':
1549
+ res = " ";
1550
+ break;
1551
+ case 'entitySkip':
1552
+ res = "~";
1553
+ break;
1554
+ case 'pu-space-1':
1555
+ res = "~";
1556
+ break;
1557
+ case 'pu-space-2':
1558
+ res = "\\mkern3mu ";
1559
+ break;
1560
+ case '1000 separator':
1561
+ res = "\\mkern2mu ";
1562
+ break;
1563
+ case 'commaDecimal':
1564
+ res = "{,}";
1565
+ break;
1566
+ case 'comma enumeration L':
1567
+ res = "{"+buf.p1+"}\\mkern6mu ";
1568
+ break;
1569
+ case 'comma enumeration M':
1570
+ res = "{"+buf.p1+"}\\mkern3mu ";
1571
+ break;
1572
+ case 'comma enumeration S':
1573
+ res = "{"+buf.p1+"}\\mkern1mu ";
1574
+ break;
1575
+ case 'hyphen':
1576
+ res = "\\text{-}";
1577
+ break;
1578
+ case 'addition compound':
1579
+ res = "\\,{\\cdot}\\,";
1580
+ break;
1581
+ case 'electron dot':
1582
+ res = "\\mkern1mu \\bullet\\mkern1mu ";
1583
+ break;
1584
+ case 'KV x':
1585
+ res = "{\\times}";
1586
+ break;
1587
+ case 'prime':
1588
+ res = "\\prime ";
1589
+ break;
1590
+ case 'cdot':
1591
+ res = "\\cdot ";
1592
+ break;
1593
+ case 'tight cdot':
1594
+ res = "\\mkern1mu{\\cdot}\\mkern1mu ";
1595
+ break;
1596
+ case 'times':
1597
+ res = "\\times ";
1598
+ break;
1599
+ case 'circa':
1600
+ res = "{\\sim}";
1601
+ break;
1602
+ case '^':
1603
+ res = "uparrow";
1604
+ break;
1605
+ case 'v':
1606
+ res = "downarrow";
1607
+ break;
1608
+ case 'ellipsis':
1609
+ res = "\\ldots ";
1610
+ break;
1611
+ case '/':
1612
+ res = "/";
1613
+ break;
1614
+ case ' / ':
1615
+ res = "\\,/\\,";
1616
+ break;
1617
+ default:
1618
+ assertNever(buf);
1619
+ throw ["MhchemBugT", "mhchem bug T. Please report."]; // Missing texify rule or unknown MhchemParser output
1620
+ }
1621
+ assertString(res);
1622
+ return res;
1623
+ },
1624
+ _getArrow: function (a) {
1625
+ switch (a) {
1626
+ case "->": return "rightarrow";
1627
+ case "\u2192": return "rightarrow";
1628
+ case "\u27F6": return "rightarrow";
1629
+ case "<-": return "leftarrow";
1630
+ case "<->": return "leftrightarrow";
1631
+ case "<-->": return "rightleftarrows";
1632
+ case "<=>": return "rightleftharpoons";
1633
+ case "\u21CC": return "rightleftharpoons";
1634
+ case "<=>>": return "rightequilibrium";
1635
+ case "<<=>": return "leftequilibrium";
1636
+ default:
1637
+ assertNever(a);
1638
+ throw ["MhchemBugT", "mhchem bug T. Please report."];
1639
+ }
1640
+ },
1641
+ _getBond: function (a) {
1642
+ switch (a) {
1643
+ case "-": return "{-}";
1644
+ case "1": return "{-}";
1645
+ case "=": return "{=}";
1646
+ case "2": return "{=}";
1647
+ case "#": return "{\\equiv}";
1648
+ case "3": return "{\\equiv}";
1649
+ case "~": return "{\\tripledash}";
1650
+ case "~-": return "{\\mathrlap{\\raisebox{-.1em}{$-$}}\\raisebox{.1em}{$\\tripledash$}}";
1651
+ case "~=": return "{\\mathrlap{\\raisebox{-.2em}{$-$}}\\mathrlap{\\raisebox{.2em}{$\\tripledash$}}-}";
1652
+ case "~--": return "{\\mathrlap{\\raisebox{-.2em}{$-$}}\\mathrlap{\\raisebox{.2em}{$\\tripledash$}}-}";
1653
+ case "-~-": return "{\\mathrlap{\\raisebox{-.2em}{$-$}}\\mathrlap{\\raisebox{.2em}{$-$}}\\tripledash}";
1654
+ case "...": return "{{\\cdot}{\\cdot}{\\cdot}}";
1655
+ case "....": return "{{\\cdot}{\\cdot}{\\cdot}{\\cdot}}";
1656
+ case "->": return "{\\rightarrow}";
1657
+ case "<-": return "{\\leftarrow}";
1658
+ case "<": return "{<}";
1659
+ case ">": return "{>}";
1660
+ default:
1661
+ assertNever(a);
1662
+ throw ["MhchemBugT", "mhchem bug T. Please report."];
1663
+ }
1664
+ },
1665
+ _getOperator: function (a) {
1666
+ switch (a) {
1667
+ case "+": return " {}+{} ";
1668
+ case "-": return " {}-{} ";
1669
+ case "=": return " {}={} ";
1670
+ case "<": return " {}<{} ";
1671
+ case ">": return " {}>{} ";
1672
+ case "<<": return " {}\\ll{} ";
1673
+ case ">>": return " {}\\gg{} ";
1674
+ case "\\pm": return " {}\\pm{} ";
1675
+ case "\\approx": return " {}\\approx{} ";
1676
+ case "$\\approx$": return " {}\\approx{} ";
1677
+ case "v": return " \\downarrow{} ";
1678
+ case "(v)": return " \\downarrow{} ";
1679
+ case "^": return " \\uparrow{} ";
1680
+ case "(^)": return " \\uparrow{} ";
1681
+ default:
1682
+ assertNever(a);
1683
+ throw ["MhchemBugT", "mhchem bug T. Please report."];
1684
+ }
1685
+ }
1686
+ };
1687
+
1688
+ //
1689
+ // Helpers for code anaylsis
1690
+ // Will show type error at calling position
1691
+ //
1692
+ /** @param {number} a */
1693
+ function assertNever(a) {}
1694
+ /** @param {string} a */
1695
+ function assertString(a) {}
1696
+