@manuscripts/transform 1.0.0

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 (315) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +193 -0
  3. package/dist/cjs/errors.js +17 -0
  4. package/dist/cjs/index.js +27 -0
  5. package/dist/cjs/jats/importer/index.js +24 -0
  6. package/dist/cjs/jats/importer/jats-body-dom-parser.js +650 -0
  7. package/dist/cjs/jats/importer/jats-body-transformations.js +269 -0
  8. package/dist/cjs/jats/importer/jats-comments.js +131 -0
  9. package/dist/cjs/jats/importer/jats-front-parser.js +341 -0
  10. package/dist/cjs/jats/importer/jats-journal-meta-parser.js +77 -0
  11. package/dist/cjs/jats/importer/jats-parser-utils.js +145 -0
  12. package/dist/cjs/jats/importer/jats-reference-parser.js +180 -0
  13. package/dist/cjs/jats/importer/parse-jats-article.js +237 -0
  14. package/dist/cjs/jats/index.js +40 -0
  15. package/dist/cjs/jats/jats-exporter.js +1569 -0
  16. package/dist/cjs/jats/jats-versions.js +41 -0
  17. package/dist/cjs/lib/attributes.js +26 -0
  18. package/dist/cjs/lib/html.js +48 -0
  19. package/dist/cjs/lib/styled-content.js +28 -0
  20. package/dist/cjs/lib/table-cell-styles.js +52 -0
  21. package/dist/cjs/lib/utils.js +66 -0
  22. package/dist/cjs/mathjax/index.js +41 -0
  23. package/dist/cjs/mathjax/mathjax-packages.js +20 -0
  24. package/dist/cjs/mathjax/mathml-to-svg.js +56 -0
  25. package/dist/cjs/mathjax/tex-to-mathml.js +49 -0
  26. package/dist/cjs/mathjax/tex-to-svg.js +59 -0
  27. package/dist/cjs/schema/groups.js +31 -0
  28. package/dist/cjs/schema/index.js +195 -0
  29. package/dist/cjs/schema/marks.js +159 -0
  30. package/dist/cjs/schema/nodes/attribution.js +32 -0
  31. package/dist/cjs/schema/nodes/bibliography_element.js +44 -0
  32. package/dist/cjs/schema/nodes/bibliography_item.js +101 -0
  33. package/dist/cjs/schema/nodes/bibliography_section.js +46 -0
  34. package/dist/cjs/schema/nodes/blockquote_element.js +62 -0
  35. package/dist/cjs/schema/nodes/caption.js +52 -0
  36. package/dist/cjs/schema/nodes/caption_title.js +51 -0
  37. package/dist/cjs/schema/nodes/citation.js +52 -0
  38. package/dist/cjs/schema/nodes/comment.js +26 -0
  39. package/dist/cjs/schema/nodes/cross_reference.js +58 -0
  40. package/dist/cjs/schema/nodes/doc.js +21 -0
  41. package/dist/cjs/schema/nodes/equation.js +55 -0
  42. package/dist/cjs/schema/nodes/equation_element.js +52 -0
  43. package/dist/cjs/schema/nodes/figcaption.js +31 -0
  44. package/dist/cjs/schema/nodes/figure.js +52 -0
  45. package/dist/cjs/schema/nodes/figure_element.js +78 -0
  46. package/dist/cjs/schema/nodes/footnote.js +67 -0
  47. package/dist/cjs/schema/nodes/footnotes_element.js +54 -0
  48. package/dist/cjs/schema/nodes/footnotes_section.js +45 -0
  49. package/dist/cjs/schema/nodes/graphical_abstract_section.js +46 -0
  50. package/dist/cjs/schema/nodes/hard_break.js +30 -0
  51. package/dist/cjs/schema/nodes/highlight_marker.js +53 -0
  52. package/dist/cjs/schema/nodes/inline_equation.js +58 -0
  53. package/dist/cjs/schema/nodes/inline_footnote.js +51 -0
  54. package/dist/cjs/schema/nodes/keywords_element.js +52 -0
  55. package/dist/cjs/schema/nodes/keywords_section.js +46 -0
  56. package/dist/cjs/schema/nodes/link.js +61 -0
  57. package/dist/cjs/schema/nodes/list.js +121 -0
  58. package/dist/cjs/schema/nodes/listing.js +73 -0
  59. package/dist/cjs/schema/nodes/listing_element.js +52 -0
  60. package/dist/cjs/schema/nodes/manuscript.js +46 -0
  61. package/dist/cjs/schema/nodes/missing_figure.js +50 -0
  62. package/dist/cjs/schema/nodes/paragraph.js +63 -0
  63. package/dist/cjs/schema/nodes/placeholder.js +49 -0
  64. package/dist/cjs/schema/nodes/placeholder_element.js +48 -0
  65. package/dist/cjs/schema/nodes/pullquote_element.js +65 -0
  66. package/dist/cjs/schema/nodes/section.js +94 -0
  67. package/dist/cjs/schema/nodes/section_label.js +30 -0
  68. package/dist/cjs/schema/nodes/section_title.js +48 -0
  69. package/dist/cjs/schema/nodes/table.js +70 -0
  70. package/dist/cjs/schema/nodes/table_col.js +57 -0
  71. package/dist/cjs/schema/nodes/table_element.js +62 -0
  72. package/dist/cjs/schema/nodes/table_row.js +123 -0
  73. package/dist/cjs/schema/nodes/text.js +21 -0
  74. package/dist/cjs/schema/nodes/toc_element.js +52 -0
  75. package/dist/cjs/schema/nodes/toc_section.js +46 -0
  76. package/dist/cjs/schema/types.js +17 -0
  77. package/dist/cjs/tests.js +30 -0
  78. package/dist/cjs/transformer/builders.js +305 -0
  79. package/dist/cjs/transformer/bundles-data.js +36 -0
  80. package/dist/cjs/transformer/bundles.js +32 -0
  81. package/dist/cjs/transformer/decode.js +619 -0
  82. package/dist/cjs/transformer/document-object-types.js +31 -0
  83. package/dist/cjs/transformer/encode.js +545 -0
  84. package/dist/cjs/transformer/filename.js +27 -0
  85. package/dist/cjs/transformer/footnote-category.js +20 -0
  86. package/dist/cjs/transformer/footnotes-order.js +59 -0
  87. package/dist/cjs/transformer/highlight-markers.js +126 -0
  88. package/dist/cjs/transformer/html.js +414 -0
  89. package/dist/cjs/transformer/id.js +28 -0
  90. package/dist/cjs/transformer/index.js +54 -0
  91. package/dist/cjs/transformer/labels.js +85 -0
  92. package/dist/cjs/transformer/manuscript-dependencies.js +74 -0
  93. package/dist/cjs/transformer/model-map.js +26 -0
  94. package/dist/cjs/transformer/models.js +17 -0
  95. package/dist/cjs/transformer/node-names.js +42 -0
  96. package/dist/cjs/transformer/node-title.js +87 -0
  97. package/dist/cjs/transformer/node-types.js +62 -0
  98. package/dist/cjs/transformer/object-types.js +57 -0
  99. package/dist/cjs/transformer/project-bundle.js +86 -0
  100. package/dist/cjs/transformer/section-category.js +222 -0
  101. package/dist/cjs/transformer/serializer.js +23 -0
  102. package/dist/cjs/transformer/shared-data.js +31 -0
  103. package/dist/cjs/transformer/tei-grobid-importer.js +121 -0
  104. package/dist/cjs/transformer/timestamp.js +20 -0
  105. package/dist/cjs/transformer/update-identifiers.js +93 -0
  106. package/dist/cjs/types.js +2 -0
  107. package/dist/es/errors.js +12 -0
  108. package/dist/es/index.js +9 -0
  109. package/dist/es/jats/importer/index.js +16 -0
  110. package/dist/es/jats/importer/jats-body-dom-parser.js +644 -0
  111. package/dist/es/jats/importer/jats-body-transformations.js +266 -0
  112. package/dist/es/jats/importer/jats-comments.js +124 -0
  113. package/dist/es/jats/importer/jats-front-parser.js +335 -0
  114. package/dist/es/jats/importer/jats-journal-meta-parser.js +70 -0
  115. package/dist/es/jats/importer/jats-parser-utils.js +139 -0
  116. package/dist/es/jats/importer/jats-reference-parser.js +177 -0
  117. package/dist/es/jats/importer/parse-jats-article.js +229 -0
  118. package/dist/es/jats/index.js +18 -0
  119. package/dist/es/jats/jats-exporter.js +1561 -0
  120. package/dist/es/jats/jats-versions.js +36 -0
  121. package/dist/es/lib/attributes.js +22 -0
  122. package/dist/es/lib/html.js +43 -0
  123. package/dist/es/lib/styled-content.js +23 -0
  124. package/dist/es/lib/table-cell-styles.js +47 -0
  125. package/dist/es/lib/utils.js +59 -0
  126. package/dist/es/mathjax/index.js +12 -0
  127. package/dist/es/mathjax/mathjax-packages.js +17 -0
  128. package/dist/es/mathjax/mathml-to-svg.js +52 -0
  129. package/dist/es/mathjax/tex-to-mathml.js +45 -0
  130. package/dist/es/mathjax/tex-to-svg.js +55 -0
  131. package/dist/es/schema/groups.js +27 -0
  132. package/dist/es/schema/index.js +178 -0
  133. package/dist/es/schema/marks.js +156 -0
  134. package/dist/es/schema/nodes/attribution.js +29 -0
  135. package/dist/es/schema/nodes/bibliography_element.js +41 -0
  136. package/dist/es/schema/nodes/bibliography_item.js +98 -0
  137. package/dist/es/schema/nodes/bibliography_section.js +42 -0
  138. package/dist/es/schema/nodes/blockquote_element.js +58 -0
  139. package/dist/es/schema/nodes/caption.js +49 -0
  140. package/dist/es/schema/nodes/caption_title.js +48 -0
  141. package/dist/es/schema/nodes/citation.js +48 -0
  142. package/dist/es/schema/nodes/comment.js +23 -0
  143. package/dist/es/schema/nodes/cross_reference.js +55 -0
  144. package/dist/es/schema/nodes/doc.js +18 -0
  145. package/dist/es/schema/nodes/equation.js +52 -0
  146. package/dist/es/schema/nodes/equation_element.js +49 -0
  147. package/dist/es/schema/nodes/figcaption.js +28 -0
  148. package/dist/es/schema/nodes/figure.js +49 -0
  149. package/dist/es/schema/nodes/figure_element.js +74 -0
  150. package/dist/es/schema/nodes/footnote.js +63 -0
  151. package/dist/es/schema/nodes/footnotes_element.js +50 -0
  152. package/dist/es/schema/nodes/footnotes_section.js +41 -0
  153. package/dist/es/schema/nodes/graphical_abstract_section.js +42 -0
  154. package/dist/es/schema/nodes/hard_break.js +27 -0
  155. package/dist/es/schema/nodes/highlight_marker.js +49 -0
  156. package/dist/es/schema/nodes/inline_equation.js +55 -0
  157. package/dist/es/schema/nodes/inline_footnote.js +47 -0
  158. package/dist/es/schema/nodes/keywords_element.js +49 -0
  159. package/dist/es/schema/nodes/keywords_section.js +42 -0
  160. package/dist/es/schema/nodes/link.js +58 -0
  161. package/dist/es/schema/nodes/list.js +117 -0
  162. package/dist/es/schema/nodes/listing.js +69 -0
  163. package/dist/es/schema/nodes/listing_element.js +49 -0
  164. package/dist/es/schema/nodes/manuscript.js +43 -0
  165. package/dist/es/schema/nodes/missing_figure.js +47 -0
  166. package/dist/es/schema/nodes/paragraph.js +59 -0
  167. package/dist/es/schema/nodes/placeholder.js +46 -0
  168. package/dist/es/schema/nodes/placeholder_element.js +45 -0
  169. package/dist/es/schema/nodes/pullquote_element.js +61 -0
  170. package/dist/es/schema/nodes/section.js +90 -0
  171. package/dist/es/schema/nodes/section_label.js +26 -0
  172. package/dist/es/schema/nodes/section_title.js +44 -0
  173. package/dist/es/schema/nodes/table.js +67 -0
  174. package/dist/es/schema/nodes/table_col.js +54 -0
  175. package/dist/es/schema/nodes/table_element.js +59 -0
  176. package/dist/es/schema/nodes/table_row.js +120 -0
  177. package/dist/es/schema/nodes/text.js +18 -0
  178. package/dist/es/schema/nodes/toc_element.js +49 -0
  179. package/dist/es/schema/nodes/toc_section.js +42 -0
  180. package/dist/es/schema/types.js +16 -0
  181. package/dist/es/tests.js +28 -0
  182. package/dist/es/transformer/builders.js +261 -0
  183. package/dist/es/transformer/bundles-data.js +8 -0
  184. package/dist/es/transformer/bundles.js +27 -0
  185. package/dist/es/transformer/decode.js +608 -0
  186. package/dist/es/transformer/document-object-types.js +28 -0
  187. package/dist/es/transformer/encode.js +535 -0
  188. package/dist/es/transformer/filename.js +23 -0
  189. package/dist/es/transformer/footnote-category.js +16 -0
  190. package/dist/es/transformer/footnotes-order.js +53 -0
  191. package/dist/es/transformer/highlight-markers.js +120 -0
  192. package/dist/es/transformer/html.js +407 -0
  193. package/dist/es/transformer/id.js +23 -0
  194. package/dist/es/transformer/index.js +38 -0
  195. package/dist/es/transformer/labels.js +81 -0
  196. package/dist/es/transformer/manuscript-dependencies.js +64 -0
  197. package/dist/es/transformer/model-map.js +22 -0
  198. package/dist/es/transformer/models.js +16 -0
  199. package/dist/es/transformer/node-names.js +39 -0
  200. package/dist/es/transformer/node-title.js +82 -0
  201. package/dist/es/transformer/node-types.js +55 -0
  202. package/dist/es/transformer/object-types.js +52 -0
  203. package/dist/es/transformer/project-bundle.js +77 -0
  204. package/dist/es/transformer/section-category.js +210 -0
  205. package/dist/es/transformer/serializer.js +17 -0
  206. package/dist/es/transformer/shared-data.js +1 -0
  207. package/dist/es/transformer/tei-grobid-importer.js +115 -0
  208. package/dist/es/transformer/timestamp.js +16 -0
  209. package/dist/es/transformer/update-identifiers.js +87 -0
  210. package/dist/es/types.js +1 -0
  211. package/dist/types/errors.d.ts +6 -0
  212. package/dist/types/index.d.ts +9 -0
  213. package/dist/types/jats/importer/index.d.ts +16 -0
  214. package/dist/types/jats/importer/jats-body-dom-parser.d.ts +24 -0
  215. package/dist/types/jats/importer/jats-body-transformations.d.ts +31 -0
  216. package/dist/types/jats/importer/jats-comments.d.ts +26 -0
  217. package/dist/types/jats/importer/jats-front-parser.d.ts +62 -0
  218. package/dist/types/jats/importer/jats-journal-meta-parser.d.ts +37 -0
  219. package/dist/types/jats/importer/jats-parser-utils.d.ts +23 -0
  220. package/dist/types/jats/importer/jats-reference-parser.d.ts +23 -0
  221. package/dist/types/jats/importer/parse-jats-article.d.ts +30 -0
  222. package/dist/types/jats/index.d.ts +18 -0
  223. package/dist/types/jats/jats-exporter.d.ts +72 -0
  224. package/dist/types/jats/jats-versions.d.ts +23 -0
  225. package/dist/types/lib/attributes.d.ts +20 -0
  226. package/dist/types/lib/html.d.ts +17 -0
  227. package/dist/types/lib/styled-content.d.ts +22 -0
  228. package/dist/types/lib/table-cell-styles.d.ts +27 -0
  229. package/dist/types/lib/utils.d.ts +26 -0
  230. package/dist/types/mathjax/index.d.ts +3 -0
  231. package/dist/types/mathjax/mathjax-packages.d.ts +16 -0
  232. package/dist/types/mathjax/mathml-to-svg.d.ts +17 -0
  233. package/dist/types/mathjax/tex-to-mathml.d.ts +17 -0
  234. package/dist/types/mathjax/tex-to-svg.d.ts +17 -0
  235. package/dist/types/schema/groups.d.ts +22 -0
  236. package/dist/types/schema/index.d.ts +65 -0
  237. package/dist/types/schema/marks.d.ts +27 -0
  238. package/dist/types/schema/nodes/attribution.d.ts +21 -0
  239. package/dist/types/schema/nodes/bibliography_element.d.ts +26 -0
  240. package/dist/types/schema/nodes/bibliography_item.d.ts +36 -0
  241. package/dist/types/schema/nodes/bibliography_section.d.ts +26 -0
  242. package/dist/types/schema/nodes/blockquote_element.d.ts +28 -0
  243. package/dist/types/schema/nodes/caption.d.ts +25 -0
  244. package/dist/types/schema/nodes/caption_title.d.ts +25 -0
  245. package/dist/types/schema/nodes/citation.d.ts +27 -0
  246. package/dist/types/schema/nodes/comment.d.ts +31 -0
  247. package/dist/types/schema/nodes/cross_reference.d.ts +26 -0
  248. package/dist/types/schema/nodes/doc.d.ts +17 -0
  249. package/dist/types/schema/nodes/equation.d.ts +28 -0
  250. package/dist/types/schema/nodes/equation_element.d.ts +29 -0
  251. package/dist/types/schema/nodes/figcaption.d.ts +21 -0
  252. package/dist/types/schema/nodes/figure.d.ts +25 -0
  253. package/dist/types/schema/nodes/figure_element.d.ts +44 -0
  254. package/dist/types/schema/nodes/footnote.d.ts +32 -0
  255. package/dist/types/schema/nodes/footnotes_element.d.ts +28 -0
  256. package/dist/types/schema/nodes/footnotes_section.d.ts +26 -0
  257. package/dist/types/schema/nodes/graphical_abstract_section.d.ts +26 -0
  258. package/dist/types/schema/nodes/hard_break.d.ts +21 -0
  259. package/dist/types/schema/nodes/highlight_marker.d.ts +29 -0
  260. package/dist/types/schema/nodes/inline_equation.d.ts +28 -0
  261. package/dist/types/schema/nodes/inline_footnote.d.ts +27 -0
  262. package/dist/types/schema/nodes/keywords_element.d.ts +27 -0
  263. package/dist/types/schema/nodes/keywords_section.d.ts +26 -0
  264. package/dist/types/schema/nodes/link.d.ts +26 -0
  265. package/dist/types/schema/nodes/list.d.ts +39 -0
  266. package/dist/types/schema/nodes/listing.d.ts +33 -0
  267. package/dist/types/schema/nodes/listing_element.d.ts +29 -0
  268. package/dist/types/schema/nodes/manuscript.d.ts +23 -0
  269. package/dist/types/schema/nodes/missing_figure.d.ts +23 -0
  270. package/dist/types/schema/nodes/paragraph.d.ts +30 -0
  271. package/dist/types/schema/nodes/placeholder.d.ts +26 -0
  272. package/dist/types/schema/nodes/placeholder_element.d.ts +25 -0
  273. package/dist/types/schema/nodes/pullquote_element.d.ts +28 -0
  274. package/dist/types/schema/nodes/section.d.ts +36 -0
  275. package/dist/types/schema/nodes/section_label.d.ts +22 -0
  276. package/dist/types/schema/nodes/section_title.d.ts +22 -0
  277. package/dist/types/schema/nodes/table.d.ts +27 -0
  278. package/dist/types/schema/nodes/table_col.d.ts +24 -0
  279. package/dist/types/schema/nodes/table_element.d.ts +35 -0
  280. package/dist/types/schema/nodes/table_row.d.ts +41 -0
  281. package/dist/types/schema/nodes/text.d.ts +17 -0
  282. package/dist/types/schema/nodes/toc_element.d.ts +27 -0
  283. package/dist/types/schema/nodes/toc_section.d.ts +26 -0
  284. package/dist/types/schema/types.d.ts +45 -0
  285. package/dist/types/tests.d.ts +16 -0
  286. package/dist/types/transformer/builders.d.ts +74 -0
  287. package/dist/types/transformer/bundles-data.d.ts +3 -0
  288. package/dist/types/transformer/bundles.d.ts +3 -0
  289. package/dist/types/transformer/decode.d.ts +38 -0
  290. package/dist/types/transformer/document-object-types.d.ts +17 -0
  291. package/dist/types/transformer/encode.d.ts +29 -0
  292. package/dist/types/transformer/filename.d.ts +16 -0
  293. package/dist/types/transformer/footnote-category.d.ts +17 -0
  294. package/dist/types/transformer/footnotes-order.d.ts +23 -0
  295. package/dist/types/transformer/highlight-markers.d.ts +26 -0
  296. package/dist/types/transformer/html.d.ts +36 -0
  297. package/dist/types/transformer/id.d.ts +20 -0
  298. package/dist/types/transformer/index.d.ts +38 -0
  299. package/dist/types/transformer/labels.d.ts +24 -0
  300. package/dist/types/transformer/manuscript-dependencies.d.ts +12 -0
  301. package/dist/types/transformer/model-map.d.ts +19 -0
  302. package/dist/types/transformer/models.d.ts +48 -0
  303. package/dist/types/transformer/node-names.d.ts +17 -0
  304. package/dist/types/transformer/node-title.d.ts +19 -0
  305. package/dist/types/transformer/node-types.d.ts +22 -0
  306. package/dist/types/transformer/object-types.d.ts +29 -0
  307. package/dist/types/transformer/project-bundle.d.ts +30 -0
  308. package/dist/types/transformer/section-category.d.ts +28 -0
  309. package/dist/types/transformer/serializer.d.ts +19 -0
  310. package/dist/types/transformer/shared-data.d.ts +2 -0
  311. package/dist/types/transformer/tei-grobid-importer.d.ts +20 -0
  312. package/dist/types/transformer/timestamp.d.ts +16 -0
  313. package/dist/types/transformer/update-identifiers.d.ts +23 -0
  314. package/dist/types/types.d.ts +2 -0
  315. package/package.json +89 -0
@@ -0,0 +1,608 @@
1
+ /*!
2
+ * © 2019 Atypon Systems LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ var __rest = (this && this.__rest) || function (s, e) {
17
+ var t = {};
18
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
19
+ t[p] = s[p];
20
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
21
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
22
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
23
+ t[p[i]] = s[p[i]];
24
+ }
25
+ return t;
26
+ };
27
+ import { ObjectTypes, } from '@manuscripts/manuscripts-json-schema';
28
+ import debug from 'debug';
29
+ import { DOMParser } from 'prosemirror-model';
30
+ import { MissingElement } from '../errors';
31
+ import { schema, } from '../schema';
32
+ import { insertHighlightMarkers } from './highlight-markers';
33
+ import { generateNodeID } from './id';
34
+ import { ExtraObjectTypes, hasObjectType, isCommentAnnotation, isManuscript, } from './object-types';
35
+ import { chooseSectionLableName, chooseSectionNodeType, chooseSecType, guessSectionCategory, } from './section-category';
36
+ import { timestamp } from './timestamp';
37
+ const warn = debug('manuscripts-transform');
38
+ const parser = DOMParser.fromSchema(schema);
39
+ export const getModelData = (model) => {
40
+ const { _rev, _deleted, updatedAt, createdAt, sessionID } = model, data = __rest(model, ["_rev", "_deleted", "updatedAt", "createdAt", "sessionID"]);
41
+ return data;
42
+ };
43
+ export const getModelsByType = (modelMap, objectType) => {
44
+ const output = [];
45
+ for (const model of modelMap.values()) {
46
+ if (model.objectType === objectType) {
47
+ output.push(model);
48
+ }
49
+ }
50
+ return output;
51
+ };
52
+ export const sortSectionsByPriority = (a, b) => a.priority === b.priority ? 0 : Number(a.priority) - Number(b.priority);
53
+ const getSections = (modelMap) => getModelsByType(modelMap, ObjectTypes.Section).sort(sortSectionsByPriority);
54
+ export const isManuscriptNode = (model) => model !== null;
55
+ const isParagraphElement = hasObjectType(ObjectTypes.ParagraphElement);
56
+ const isFootnote = hasObjectType(ObjectTypes.Footnote);
57
+ const hasParentSection = (id) => (section) => section.path &&
58
+ section.path.length > 1 &&
59
+ section.path[section.path.length - 2] === id;
60
+ export class Decoder {
61
+ createCommentsNode(model) {
62
+ const comments = [];
63
+ for (const comment of this.getComments(model)) {
64
+ comments.push(this.decode(comment));
65
+ }
66
+ return comments;
67
+ }
68
+ getComments(model) {
69
+ return Array.from(this.modelMap.values()).filter((c) => isCommentAnnotation(c) && c.target === model._id);
70
+ }
71
+ extractListing(model) {
72
+ if (model.listingID) {
73
+ const listingModel = this.getModel(model.listingID);
74
+ let listing;
75
+ if (listingModel) {
76
+ listing = this.decode(listingModel);
77
+ }
78
+ else if (this.allowMissingElements) {
79
+ listing = schema.nodes.placeholder.create({
80
+ id: model.listingID,
81
+ label: 'A listing',
82
+ });
83
+ }
84
+ else {
85
+ throw new MissingElement(model.listingID);
86
+ }
87
+ return listing;
88
+ }
89
+ }
90
+ constructor(modelMap, allowMissingElements = false) {
91
+ this.creators = {
92
+ [ObjectTypes.BibliographyElement]: (data) => {
93
+ var _a;
94
+ const model = data;
95
+ const referenceIDs = (_a = model.containedObjectIDs) === null || _a === void 0 ? void 0 : _a.filter((i) => i.startsWith('MPBibliographyItem'));
96
+ const references = [];
97
+ referenceIDs === null || referenceIDs === void 0 ? void 0 : referenceIDs.forEach((id) => {
98
+ const referenceModel = this.getModel(id);
99
+ if (referenceModel) {
100
+ return references.push(this.decode(referenceModel));
101
+ }
102
+ });
103
+ if (!references.length) {
104
+ references.push(schema.nodes.placeholder.createAndFill());
105
+ }
106
+ return schema.nodes.bibliography_element.createChecked({
107
+ id: model._id,
108
+ contents: '',
109
+ paragraphStyle: model.paragraphStyle,
110
+ }, references);
111
+ },
112
+ [ObjectTypes.BibliographyItem]: (data) => {
113
+ const model = data;
114
+ return schema.nodes.bibliography_item.create({
115
+ id: model._id,
116
+ type: model.type,
117
+ author: model.author,
118
+ issued: model.issued,
119
+ containerTitle: model['container-title'],
120
+ volume: model.volume,
121
+ issue: model.issue,
122
+ supplement: model.supplement,
123
+ doi: model.DOI,
124
+ page: model.page,
125
+ title: model.title,
126
+ literal: model.literal,
127
+ });
128
+ },
129
+ [ExtraObjectTypes.PlaceholderElement]: (data) => {
130
+ const model = data;
131
+ return schema.nodes.placeholder_element.create({
132
+ id: model._id,
133
+ });
134
+ },
135
+ [ObjectTypes.Figure]: (data) => {
136
+ const model = data;
137
+ return schema.nodes.figure.create({
138
+ id: model._id,
139
+ contentType: model.contentType,
140
+ src: model.src,
141
+ position: model.position,
142
+ comments: this.createCommentsNode(model),
143
+ });
144
+ },
145
+ [ObjectTypes.FigureElement]: (data) => {
146
+ const model = data;
147
+ const paragraphIDs = model.containedObjectIDs.filter((i) => i.startsWith('MPParagraphElement'));
148
+ const figureIDs = model.containedObjectIDs.filter((i) => i.startsWith('MPFigure') || i.startsWith('MPMissingFigure'));
149
+ const paragraphs = [];
150
+ paragraphIDs.forEach((id) => {
151
+ const paragraphModel = this.getModel(id);
152
+ if (paragraphModel) {
153
+ return paragraphs.push(this.decode(paragraphModel));
154
+ }
155
+ });
156
+ const figures = [];
157
+ figureIDs.forEach((id) => {
158
+ const figureModel = this.getModel(id);
159
+ if (!figureModel) {
160
+ return figures.push(schema.nodes.placeholder.create({
161
+ id,
162
+ label: 'A figure',
163
+ }));
164
+ }
165
+ return figures.push(this.decode(figureModel));
166
+ });
167
+ if (!figures.length) {
168
+ figures.push(schema.nodes.figure.createAndFill());
169
+ }
170
+ const figcaption = this.getFigcaption(model);
171
+ const content = [...paragraphs, ...figures, figcaption];
172
+ const listing = this.extractListing(model);
173
+ if (listing) {
174
+ content.push(listing);
175
+ }
176
+ else {
177
+ const listing = schema.nodes.listing.create();
178
+ content.push(listing);
179
+ }
180
+ return schema.nodes.figure_element.createChecked({
181
+ id: model._id,
182
+ figureLayout: model.figureLayout,
183
+ label: model.label,
184
+ figureStyle: model.figureStyle,
185
+ alignment: model.alignment,
186
+ sizeFraction: model.sizeFraction,
187
+ suppressCaption: Boolean(model.suppressCaption),
188
+ suppressTitle: Boolean(model.suppressTitle === undefined ? true : model.suppressTitle),
189
+ attribution: model.attribution,
190
+ alternatives: model.alternatives,
191
+ comments: this.createCommentsNode(model),
192
+ }, content);
193
+ },
194
+ [ObjectTypes.Equation]: (data) => {
195
+ const model = data;
196
+ return schema.nodes.equation.createChecked({
197
+ id: model._id,
198
+ MathMLStringRepresentation: model.MathMLStringRepresentation,
199
+ SVGStringRepresentation: model.SVGStringRepresentation,
200
+ TeXRepresentation: model.TeXRepresentation,
201
+ });
202
+ },
203
+ [ObjectTypes.EquationElement]: (data) => {
204
+ const model = data;
205
+ const equationModel = this.getModel(model.containedObjectID);
206
+ let equation;
207
+ if (equationModel) {
208
+ equation = this.decode(equationModel);
209
+ }
210
+ else if (this.allowMissingElements) {
211
+ equation = schema.nodes.placeholder.create({
212
+ id: model.containedObjectID,
213
+ label: 'An equation',
214
+ });
215
+ }
216
+ else {
217
+ throw new MissingElement(model.containedObjectID);
218
+ }
219
+ const figcaption = this.getFigcaption(model);
220
+ return schema.nodes.equation_element.createChecked({
221
+ id: model._id,
222
+ suppressCaption: Boolean(model.suppressCaption),
223
+ suppressTitle: Boolean(model.suppressTitle === undefined ? true : model.suppressTitle),
224
+ }, [equation, figcaption]);
225
+ },
226
+ [ObjectTypes.FootnotesElement]: (data) => {
227
+ const model = data;
228
+ const collateByKind = model.collateByKind || 'footnote';
229
+ const footnotesOfKind = [];
230
+ for (const model of this.modelMap.values()) {
231
+ if (isFootnote(model) && model.kind === collateByKind) {
232
+ const footnote = this.parseContents(model.contents || '<div></div>', undefined, this.getComments(model), {
233
+ topNode: schema.nodes.footnote.create({
234
+ id: model._id,
235
+ kind: model.kind,
236
+ comments: this.createCommentsNode(model),
237
+ }),
238
+ });
239
+ footnotesOfKind.push(footnote);
240
+ }
241
+ }
242
+ return schema.nodes.footnotes_element.create({
243
+ id: model._id,
244
+ kind: model.collateByKind,
245
+ paragraphStyle: model.paragraphStyle,
246
+ }, footnotesOfKind);
247
+ },
248
+ [ObjectTypes.Footnote]: (data) => {
249
+ const model = data;
250
+ return schema.nodes.footnote.create({
251
+ id: model._id,
252
+ kind: model.kind,
253
+ comments: this.createCommentsNode(model),
254
+ });
255
+ },
256
+ [ObjectTypes.KeywordsElement]: (data) => {
257
+ const model = data;
258
+ return schema.nodes.keywords_element.create({
259
+ id: model._id,
260
+ contents: model.contents
261
+ ? model.contents.replace(/\s+xmlns=".+?"/, '')
262
+ : '',
263
+ paragraphStyle: model.paragraphStyle,
264
+ });
265
+ },
266
+ [ObjectTypes.ListElement]: (data) => {
267
+ const model = data;
268
+ switch (model.elementType) {
269
+ case 'ol':
270
+ return this.parseContents(model.contents || '<ol></ol>', undefined, this.getComments(model), {
271
+ topNode: schema.nodes.ordered_list.create({
272
+ id: model._id,
273
+ paragraphStyle: model.paragraphStyle,
274
+ comments: this.createCommentsNode(model),
275
+ }),
276
+ });
277
+ case 'ul':
278
+ return this.parseContents(model.contents || '<ul></ul>', undefined, this.getComments(model), {
279
+ topNode: schema.nodes.bullet_list.create({
280
+ id: model._id,
281
+ paragraphStyle: model.paragraphStyle,
282
+ }),
283
+ });
284
+ default:
285
+ throw new Error('Unknown list element type');
286
+ }
287
+ },
288
+ [ObjectTypes.Listing]: (data) => {
289
+ const model = data;
290
+ return schema.nodes.listing.createChecked({
291
+ id: model._id,
292
+ contents: model.contents,
293
+ language: model.language,
294
+ languageKey: model.languageKey,
295
+ comments: this.createCommentsNode(model),
296
+ });
297
+ },
298
+ [ObjectTypes.ListingElement]: (data) => {
299
+ const model = data;
300
+ const listingModel = this.getModel(model.containedObjectID);
301
+ let listing;
302
+ if (listingModel) {
303
+ listing = this.decode(listingModel);
304
+ }
305
+ else if (this.allowMissingElements) {
306
+ listing = schema.nodes.placeholder.create({
307
+ id: model.containedObjectID,
308
+ label: 'A listing',
309
+ });
310
+ }
311
+ else {
312
+ throw new MissingElement(model.containedObjectID);
313
+ }
314
+ const figcaption = this.getFigcaption(model);
315
+ return schema.nodes.listing_element.createChecked({
316
+ id: model._id,
317
+ suppressCaption: model.suppressCaption,
318
+ suppressTitle: Boolean(model.suppressTitle === undefined ? true : model.suppressTitle),
319
+ comments: this.createCommentsNode(model),
320
+ }, [listing, figcaption]);
321
+ },
322
+ [ObjectTypes.MissingFigure]: (data) => {
323
+ const model = data;
324
+ return schema.nodes.missing_figure.create({
325
+ id: model._id,
326
+ position: model.position,
327
+ });
328
+ },
329
+ [ObjectTypes.ParagraphElement]: (data) => {
330
+ const model = data;
331
+ return this.parseContents(model.contents || '<p></p>', undefined, this.getComments(model), {
332
+ topNode: schema.nodes.paragraph.create({
333
+ id: model._id,
334
+ paragraphStyle: model.paragraphStyle,
335
+ placeholder: model.placeholderInnerHTML,
336
+ comments: this.createCommentsNode(model),
337
+ }),
338
+ });
339
+ },
340
+ [ObjectTypes.QuoteElement]: (data) => {
341
+ const model = data;
342
+ switch (model.quoteType) {
343
+ case 'block':
344
+ return this.parseContents(model.contents || '<p></p>', undefined, this.getComments(model), {
345
+ topNode: schema.nodes.blockquote_element.create({
346
+ id: model._id,
347
+ paragraphStyle: model.paragraphStyle,
348
+ placeholder: model.placeholderInnerHTML,
349
+ }),
350
+ });
351
+ case 'pull':
352
+ return this.parseContents(model.contents || '<p></p>', undefined, this.getComments(model), {
353
+ topNode: schema.nodes.pullquote_element.create({
354
+ id: model._id,
355
+ paragraphStyle: model.paragraphStyle,
356
+ placeholder: model.placeholderInnerHTML,
357
+ }),
358
+ });
359
+ default:
360
+ throw new Error('Unknown block type');
361
+ }
362
+ },
363
+ [ObjectTypes.Section]: (data) => {
364
+ const model = data;
365
+ const isKeywordsSection = model.category === 'MPSectionCategory:keywords';
366
+ const elements = [];
367
+ if (model.elementIDs) {
368
+ for (const id of model.elementIDs) {
369
+ const element = this.getModel(id);
370
+ if (element) {
371
+ if (isKeywordsSection && isParagraphElement(element)) {
372
+ continue;
373
+ }
374
+ elements.push(element);
375
+ }
376
+ else if (this.allowMissingElements) {
377
+ const placeholderElement = {
378
+ _id: id,
379
+ containerID: model._id,
380
+ elementType: 'p',
381
+ objectType: ExtraObjectTypes.PlaceholderElement,
382
+ createdAt: timestamp(),
383
+ updatedAt: timestamp(),
384
+ };
385
+ elements.push(placeholderElement);
386
+ }
387
+ else {
388
+ throw new MissingElement(id);
389
+ }
390
+ }
391
+ }
392
+ const elementNodes = elements
393
+ .map(this.decode)
394
+ .filter(isManuscriptNode);
395
+ const sectionTitleNode = model.title
396
+ ? this.parseContents(model.title, 'h1', this.getComments(model), {
397
+ topNode: schema.nodes.section_title.create(),
398
+ })
399
+ : schema.nodes.section_title.create();
400
+ let sectionLabelNode = undefined;
401
+ if (model.label) {
402
+ sectionLabelNode = this.parseContents(model.label, 'label', this.getComments(model), {
403
+ topNode: schema.nodes.section_label.create(),
404
+ });
405
+ }
406
+ const nestedSections = getSections(this.modelMap)
407
+ .filter(hasParentSection(model._id))
408
+ .map(this.creators[ObjectTypes.Section]);
409
+ const sectionCategory = model.category || guessSectionCategory(elements);
410
+ const sectionNodeType = chooseSectionNodeType(sectionCategory);
411
+ const content = (sectionLabelNode
412
+ ? [sectionLabelNode, sectionTitleNode]
413
+ : [sectionTitleNode])
414
+ .concat(elementNodes)
415
+ .concat(nestedSections);
416
+ const sectionNode = sectionNodeType.createAndFill({
417
+ id: model._id,
418
+ category: sectionCategory,
419
+ titleSuppressed: model.titleSuppressed,
420
+ pageBreakStyle: model.pageBreakStyle,
421
+ generatedLabel: model.generatedLabel,
422
+ comments: this.createCommentsNode(model),
423
+ }, content);
424
+ if (!sectionNode) {
425
+ console.error(model);
426
+ throw new Error('Invalid content for section ' + model._id);
427
+ }
428
+ return sectionNode;
429
+ },
430
+ [ObjectTypes.Table]: (data) => {
431
+ const model = data;
432
+ return this.parseContents(model.contents, undefined, this.getComments(model), {
433
+ topNode: schema.nodes.table.create({
434
+ id: model._id,
435
+ comments: this.createCommentsNode(model),
436
+ }),
437
+ });
438
+ },
439
+ [ObjectTypes.TableElement]: (data) => {
440
+ const model = data;
441
+ const tableModel = this.getModel(model.containedObjectID);
442
+ let table;
443
+ if (tableModel) {
444
+ table = this.decode(tableModel);
445
+ }
446
+ else if (this.allowMissingElements) {
447
+ table = schema.nodes.placeholder.create({
448
+ id: model.containedObjectID,
449
+ label: 'A table',
450
+ });
451
+ }
452
+ else {
453
+ throw new MissingElement(model.containedObjectID);
454
+ }
455
+ const figcaption = this.getFigcaption(model);
456
+ const content = [table, figcaption];
457
+ if (model.listingID) {
458
+ const listingModel = this.getModel(model.listingID);
459
+ let listing;
460
+ if (listingModel) {
461
+ listing = this.decode(listingModel);
462
+ }
463
+ else if (this.allowMissingElements) {
464
+ listing = schema.nodes.placeholder.create({
465
+ id: model.listingID,
466
+ label: 'A listing',
467
+ });
468
+ }
469
+ else {
470
+ throw new MissingElement(model.listingID);
471
+ }
472
+ content.push(listing);
473
+ }
474
+ else {
475
+ const listing = schema.nodes.listing.create();
476
+ content.push(listing);
477
+ }
478
+ return schema.nodes.table_element.createChecked({
479
+ id: model._id,
480
+ table: model.containedObjectID,
481
+ suppressCaption: model.suppressCaption,
482
+ suppressTitle: Boolean(model.suppressTitle === undefined ? true : model.suppressTitle),
483
+ suppressFooter: model.suppressFooter,
484
+ suppressHeader: model.suppressHeader,
485
+ tableStyle: model.tableStyle,
486
+ paragraphStyle: model.paragraphStyle,
487
+ comments: this.createCommentsNode(model),
488
+ }, content);
489
+ },
490
+ [ObjectTypes.TOCElement]: (data) => {
491
+ const model = data;
492
+ return schema.nodes.toc_element.create({
493
+ id: model._id,
494
+ contents: model.contents
495
+ ? model.contents.replace(/\s+xmlns=".+?"/, '')
496
+ : '',
497
+ paragraphStyle: model.paragraphStyle,
498
+ });
499
+ },
500
+ [ObjectTypes.CommentAnnotation]: (data) => {
501
+ const model = data;
502
+ return schema.nodes.comment.create({
503
+ id: model._id,
504
+ contents: model.contents,
505
+ selector: model.selector,
506
+ target: model.target,
507
+ });
508
+ },
509
+ };
510
+ this.decode = (model) => {
511
+ if (!this.creators[model.objectType]) {
512
+ warn(`No converter for ${model.objectType}`);
513
+ return null;
514
+ }
515
+ return this.creators[model.objectType](model);
516
+ };
517
+ this.getModel = (id) => this.modelMap.get(id);
518
+ this.createArticleNode = (manuscriptID) => {
519
+ let rootSections = getSections(this.modelMap).filter((section) => !section.path || section.path.length <= 1);
520
+ rootSections = this.addGeneratedLabels(rootSections);
521
+ const rootSectionNodes = rootSections
522
+ .map(this.decode)
523
+ .filter(isManuscriptNode);
524
+ if (!rootSectionNodes.length) {
525
+ rootSectionNodes.push(schema.nodes.section.createAndFill({
526
+ id: generateNodeID(schema.nodes.section),
527
+ }));
528
+ }
529
+ return schema.nodes.manuscript.create({
530
+ id: manuscriptID || this.getManuscriptID(),
531
+ }, rootSectionNodes);
532
+ };
533
+ this.addGeneratedLabels = (sections) => {
534
+ const nextLableCount = { Appendix: 1 };
535
+ return sections.map((section) => {
536
+ if (section.generatedLabel) {
537
+ const secType = section.category
538
+ ? chooseSecType(section.category)
539
+ : undefined;
540
+ if (secType === 'appendices') {
541
+ section.label = `${chooseSectionLableName(secType)} ${nextLableCount['Appendix']}`;
542
+ nextLableCount['Appendix'] += 1;
543
+ }
544
+ else {
545
+ delete section.label;
546
+ }
547
+ }
548
+ return section;
549
+ });
550
+ };
551
+ this.parseContents = (contents, wrapper, commentAnnotations = [], options) => {
552
+ const contentsWithComments = commentAnnotations.length
553
+ ? insertHighlightMarkers(contents, commentAnnotations)
554
+ : contents;
555
+ const wrappedContents = wrapper
556
+ ? `<${wrapper}>${contentsWithComments}</${wrapper}>`
557
+ : contentsWithComments;
558
+ const html = wrappedContents.trim();
559
+ if (!html.length) {
560
+ throw new Error('No HTML to parse');
561
+ }
562
+ const template = document.createElement('template');
563
+ template.innerHTML = html;
564
+ if (!template.content.firstChild) {
565
+ throw new Error('No content could be parsed');
566
+ }
567
+ return parser.parse(template.content.firstChild, options);
568
+ };
569
+ this.getManuscriptID = () => {
570
+ for (const item of this.modelMap.values()) {
571
+ if (isManuscript(item)) {
572
+ return item._id;
573
+ }
574
+ }
575
+ };
576
+ this.getFigcaption = (model) => {
577
+ const titleNode = schema.nodes.caption_title.create();
578
+ const captionTitle = model.title
579
+ ? this.parseContents(model.title, 'caption_title', this.getComments(model), {
580
+ topNode: titleNode,
581
+ })
582
+ : titleNode;
583
+ if (model.caption && /<\/?[a-z][\s\S]*>/i.test(model.caption)) {
584
+ const captionDoc = document.createElement('div');
585
+ captionDoc.innerHTML = model.caption;
586
+ const content = [captionTitle];
587
+ const paragraphs = captionDoc.querySelectorAll('p');
588
+ for (const paragraph of paragraphs) {
589
+ const captionNode = schema.nodes.caption.create();
590
+ const caption = this.parseContents(paragraph.outerHTML, 'caption', this.getComments(model), {
591
+ topNode: captionNode,
592
+ });
593
+ content.push(caption);
594
+ }
595
+ return schema.nodes.figcaption.create({}, content);
596
+ }
597
+ const captionNode = schema.nodes.caption.create();
598
+ const caption = model.caption
599
+ ? this.parseContents(model.caption, 'caption', this.getComments(model), {
600
+ topNode: captionNode,
601
+ })
602
+ : captionNode;
603
+ return schema.nodes.figcaption.create({}, [captionTitle, caption]);
604
+ };
605
+ this.modelMap = modelMap;
606
+ this.allowMissingElements = allowMissingElements;
607
+ }
608
+ }
@@ -0,0 +1,28 @@
1
+ /*!
2
+ * © 2019 Atypon Systems LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { ObjectTypes } from '@manuscripts/manuscripts-json-schema';
17
+ export const documentObjectTypes = [
18
+ ObjectTypes.BibliographyElement,
19
+ ObjectTypes.EquationElement,
20
+ ObjectTypes.FigureElement,
21
+ ObjectTypes.ListElement,
22
+ ObjectTypes.ListingElement,
23
+ ObjectTypes.Manuscript,
24
+ ObjectTypes.ParagraphElement,
25
+ ObjectTypes.Section,
26
+ ObjectTypes.TableElement,
27
+ ObjectTypes.TOCElement,
28
+ ];