@readium/shared 1.2.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 (150) hide show
  1. package/LICENSE +28 -0
  2. package/README.MD +27 -0
  3. package/dist/index.js +2536 -0
  4. package/dist/index.umd.cjs +2 -0
  5. package/package.json +73 -0
  6. package/src/fetcher/Fetcher.ts +37 -0
  7. package/src/fetcher/HttpFetcher.ts +122 -0
  8. package/src/fetcher/Resource.ts +37 -0
  9. package/src/fetcher/index.ts +2 -0
  10. package/src/index.ts +4 -0
  11. package/src/opds/Acquisition.ts +54 -0
  12. package/src/opds/Availability.ts +61 -0
  13. package/src/opds/Copies.ts +43 -0
  14. package/src/opds/Holds.ts +43 -0
  15. package/src/opds/Price.ts +51 -0
  16. package/src/opds/index.ts +5 -0
  17. package/src/publication/BelongsTo.ts +51 -0
  18. package/src/publication/Contributor.ts +129 -0
  19. package/src/publication/GuidedNavigation.ts +176 -0
  20. package/src/publication/Link.ts +332 -0
  21. package/src/publication/LocalizedString.ts +74 -0
  22. package/src/publication/Locator.ts +225 -0
  23. package/src/publication/LocatorCollection.ts +133 -0
  24. package/src/publication/Manifest.ts +256 -0
  25. package/src/publication/Metadata.ts +329 -0
  26. package/src/publication/Properties.ts +44 -0
  27. package/src/publication/Publication.ts +148 -0
  28. package/src/publication/PublicationCollection.ts +144 -0
  29. package/src/publication/ReadingProgression.ts +28 -0
  30. package/src/publication/Subject.ts +115 -0
  31. package/src/publication/encryption/Encryption.ts +68 -0
  32. package/src/publication/encryption/Properties.ts +20 -0
  33. package/src/publication/encryption/index.ts +2 -0
  34. package/src/publication/epub/EPUBLayout.ts +10 -0
  35. package/src/publication/epub/Presentation.ts +16 -0
  36. package/src/publication/epub/Properties.ts +28 -0
  37. package/src/publication/epub/Publication.ts +51 -0
  38. package/src/publication/epub/index.ts +4 -0
  39. package/src/publication/html/DomRange.ts +63 -0
  40. package/src/publication/html/DomRangePoint.ts +64 -0
  41. package/src/publication/html/Locations.ts +129 -0
  42. package/src/publication/html/index.ts +3 -0
  43. package/src/publication/index.ts +19 -0
  44. package/src/publication/opds/Properties.ts +84 -0
  45. package/src/publication/opds/Publication.ts +14 -0
  46. package/src/publication/opds/index.ts +2 -0
  47. package/src/publication/presentation/Metadata.ts +19 -0
  48. package/src/publication/presentation/Presentation.ts +155 -0
  49. package/src/publication/presentation/Properties.ts +70 -0
  50. package/src/publication/presentation/index.ts +3 -0
  51. package/src/publication/services/content/Content.ts +36 -0
  52. package/src/publication/services/content/ContentTokenizer.ts +62 -0
  53. package/src/publication/services/content/Iterator.ts +46 -0
  54. package/src/publication/services/content/element/attributes.ts +44 -0
  55. package/src/publication/services/content/element/element.ts +118 -0
  56. package/src/publication/services/content/element/index.ts +3 -0
  57. package/src/publication/services/content/element/text_role.ts +60 -0
  58. package/src/publication/services/content/index.ts +5 -0
  59. package/src/publication/services/content/iterators/HTMLResourceContentIterator.ts +571 -0
  60. package/src/publication/services/content/iterators/PDFTextContentIterator.ts +184 -0
  61. package/src/publication/services/content/iterators/PublicationContentIterator.ts +144 -0
  62. package/src/publication/services/content/iterators/helpers.ts +146 -0
  63. package/src/publication/services/content/iterators/index.ts +3 -0
  64. package/src/publication/services/index.ts +1 -0
  65. package/src/util/JSONParse.ts +31 -0
  66. package/src/util/Language.ts +5 -0
  67. package/src/util/URITemplate.ts +83 -0
  68. package/src/util/index.ts +5 -0
  69. package/src/util/mediatype/MediaType.ts +563 -0
  70. package/src/util/mediatype/index.ts +1 -0
  71. package/src/util/tokenizer/TextTokenizer.ts +126 -0
  72. package/src/util/tokenizer/Tokenizer.ts +8 -0
  73. package/src/util/tokenizer/index.ts +2 -0
  74. package/src/util/tokenizer/tokenize-english/README.md +7 -0
  75. package/src/util/tokenizer/tokenize-english/abbreviations.js +52 -0
  76. package/src/util/tokenizer/tokenize-english/index.d.ts +3 -0
  77. package/src/util/tokenizer/tokenize-english/index.js +243 -0
  78. package/src/util/tokenizer/tokenize-english/utils.js +16 -0
  79. package/src/util/tokenizer/tokenize-text/README.MD +6 -0
  80. package/src/util/tokenizer/tokenize-text/index.d.ts +52 -0
  81. package/src/util/tokenizer/tokenize-text/index.js +256 -0
  82. package/src/util/tokenizer/tokenize-text/tokens.js +100 -0
  83. package/types/src/fetcher/Fetcher.d.ts +27 -0
  84. package/types/src/fetcher/HttpFetcher.d.ts +28 -0
  85. package/types/src/fetcher/Resource.d.ts +15 -0
  86. package/types/src/fetcher/index.d.ts +2 -0
  87. package/types/src/index.d.ts +4 -0
  88. package/types/src/opds/Acquisition.d.ts +26 -0
  89. package/types/src/opds/Availability.d.ts +32 -0
  90. package/types/src/opds/Copies.d.ts +22 -0
  91. package/types/src/opds/Holds.d.ts +22 -0
  92. package/types/src/opds/Price.d.ts +29 -0
  93. package/types/src/opds/index.d.ts +5 -0
  94. package/types/src/publication/BelongsTo.d.ts +23 -0
  95. package/types/src/publication/Contributor.d.ts +60 -0
  96. package/types/src/publication/GuidedNavigation.d.ts +70 -0
  97. package/types/src/publication/Link.d.ts +128 -0
  98. package/types/src/publication/LocalizedString.d.ts +48 -0
  99. package/types/src/publication/Locator.d.ts +101 -0
  100. package/types/src/publication/LocatorCollection.d.ts +54 -0
  101. package/types/src/publication/Manifest.d.ts +59 -0
  102. package/types/src/publication/Metadata.d.ts +101 -0
  103. package/types/src/publication/Properties.d.ts +28 -0
  104. package/types/src/publication/Publication.d.ts +53 -0
  105. package/types/src/publication/PublicationCollection.d.ts +34 -0
  106. package/types/src/publication/ReadingProgression.d.ts +9 -0
  107. package/types/src/publication/Subject.d.ts +56 -0
  108. package/types/src/publication/encryption/Encryption.d.ts +33 -0
  109. package/types/src/publication/encryption/Properties.d.ts +10 -0
  110. package/types/src/publication/encryption/index.d.ts +2 -0
  111. package/types/src/publication/epub/EPUBLayout.d.ts +5 -0
  112. package/types/src/publication/epub/Presentation.d.ts +7 -0
  113. package/types/src/publication/epub/Properties.d.ts +14 -0
  114. package/types/src/publication/epub/Publication.d.ts +19 -0
  115. package/types/src/publication/epub/index.d.ts +4 -0
  116. package/types/src/publication/html/DomRange.d.ts +40 -0
  117. package/types/src/publication/html/DomRangePoint.d.ts +36 -0
  118. package/types/src/publication/html/Locations.d.ts +45 -0
  119. package/types/src/publication/html/index.d.ts +3 -0
  120. package/types/src/publication/index.d.ts +19 -0
  121. package/types/src/publication/opds/Properties.d.ts +38 -0
  122. package/types/src/publication/opds/Publication.d.ts +6 -0
  123. package/types/src/publication/opds/index.d.ts +2 -0
  124. package/types/src/publication/presentation/Metadata.d.ts +6 -0
  125. package/types/src/publication/presentation/Presentation.d.ts +95 -0
  126. package/types/src/publication/presentation/Properties.d.ts +32 -0
  127. package/types/src/publication/presentation/index.d.ts +3 -0
  128. package/types/src/publication/services/content/Content.d.ts +20 -0
  129. package/types/src/publication/services/content/ContentTokenizer.d.ts +19 -0
  130. package/types/src/publication/services/content/Iterator.d.ts +38 -0
  131. package/types/src/publication/services/content/element/attributes.d.ts +31 -0
  132. package/types/src/publication/services/content/element/element.d.ts +107 -0
  133. package/types/src/publication/services/content/element/index.d.ts +3 -0
  134. package/types/src/publication/services/content/element/text_role.d.ts +53 -0
  135. package/types/src/publication/services/content/index.d.ts +5 -0
  136. package/types/src/publication/services/content/iterators/HTMLResourceContentIterator.d.ts +30 -0
  137. package/types/src/publication/services/content/iterators/PDFTextContentIterator.d.ts +60 -0
  138. package/types/src/publication/services/content/iterators/PublicationContentIterator.d.ts +59 -0
  139. package/types/src/publication/services/content/iterators/helpers.d.ts +10 -0
  140. package/types/src/publication/services/content/iterators/index.d.ts +3 -0
  141. package/types/src/publication/services/index.d.ts +1 -0
  142. package/types/src/util/JSONParse.d.ts +11 -0
  143. package/types/src/util/Language.d.ts +1 -0
  144. package/types/src/util/URITemplate.d.ts +24 -0
  145. package/types/src/util/index.d.ts +5 -0
  146. package/types/src/util/mediatype/MediaType.d.ts +148 -0
  147. package/types/src/util/mediatype/index.d.ts +1 -0
  148. package/types/src/util/tokenizer/TextTokenizer.d.ts +39 -0
  149. package/types/src/util/tokenizer/Tokenizer.d.ts +6 -0
  150. package/types/src/util/tokenizer/index.d.ts +2 -0
@@ -0,0 +1,59 @@
1
+ import { Resource } from "../../../../fetcher/Resource";
2
+ import { Locator } from "../../../Locator";
3
+ import { Publication } from "../../../Publication";
4
+ import { Iterator } from "../Iterator";
5
+ import { ContentElement } from "../element";
6
+ export type ResourceContentIteratorFactory = (resource: Resource, locator: Locator) => Iterator | null;
7
+ export declare class PublicationContentIterator extends Iterator {
8
+ /**
9
+ * The [Publication] which will be iterated through.
10
+ */
11
+ private publication;
12
+ /**
13
+ * Starting [Locator] in the publication.
14
+ */
15
+ private startLocator;
16
+ /**
17
+ * List of [ResourceContentIteratorFactory] which will be used to create the
18
+ * iterator for each resource. The factories are tried in order until there's a match.
19
+ */
20
+ private resourceContentIteratorFactories;
21
+ private _currentIterator;
22
+ private currentElement;
23
+ constructor(
24
+ /**
25
+ * The [Publication] which will be iterated through.
26
+ */
27
+ publication: Publication,
28
+ /**
29
+ * Starting [Locator] in the publication.
30
+ */
31
+ startLocator: Locator | undefined,
32
+ /**
33
+ * List of [ResourceContentIteratorFactory] which will be used to create the
34
+ * iterator for each resource. The factories are tried in order until there's a match.
35
+ */
36
+ resourceContentIteratorFactories: ResourceContentIteratorFactory[]);
37
+ hasPrevious(): Promise<boolean>;
38
+ previous(): ContentElement;
39
+ hasNext(): Promise<boolean>;
40
+ next(): ContentElement;
41
+ private nextIn;
42
+ /**
43
+ * Returns the [Iterator] for the current [Resource] in the reading order.
44
+ */
45
+ private get currentIterator();
46
+ /**
47
+ * Returns the first iterator starting at [startLocator] or the beginning of the publication.
48
+ */
49
+ private initialIterator;
50
+ /**
51
+ * Returns the next resource iterator in the given [direction], starting from [fromIndex].
52
+ */
53
+ private nextIteratorIn;
54
+ /**
55
+ * Loads the iterator at the given [index] in the reading order.
56
+ * The [location] will be used to compute the starting [Locator] for the iterator.
57
+ */
58
+ private loadIteratorAt;
59
+ }
@@ -0,0 +1,10 @@
1
+ import { LocatorText } from "../../../Locator";
2
+ export declare function isInlineTag(n: Node): boolean;
3
+ export declare function srcRelativeToHref(e: Element, base?: string | URL): string | null;
4
+ export declare const isBlank: (s: string | null) => boolean;
5
+ export declare const trimUnicodeSpace: (s: string) => string;
6
+ export declare const trimUnicodeSpaceStart: (s: string) => string;
7
+ export declare const trimUnicodeSpaceEnd: (s: string) => string;
8
+ export declare const trimmingTextLocator: (text: string, before?: string) => LocatorText;
9
+ export declare function elementLanguage(e: HTMLElement | null): string | null;
10
+ export declare function appendNormalizedWhitespace(accum: string, text: string): string;
@@ -0,0 +1,3 @@
1
+ export * from './PublicationContentIterator';
2
+ export * from './PDFTextContentIterator';
3
+ export * from './HTMLResourceContentIterator';
@@ -0,0 +1 @@
1
+ export * from './content';
@@ -0,0 +1,11 @@
1
+ /** Parses the given array (or undefined it it's not an array) */
2
+ export declare function arrayfromJSON(json: any): Array<any> | undefined;
3
+ export declare function arrayfromJSONorString(json: any): Array<any> | undefined;
4
+ /** Parses the given key and returns a Date (or undefined if it’s not a string) */
5
+ export declare function datefromJSON(json: any): Date | undefined;
6
+ /** Parses a numeric value, but returns undefined if it is not a positive number. */
7
+ export declare function numberfromJSON(json: any): number | undefined;
8
+ /** Parses a numeric value, but returns undefined if it is not a positive number. */
9
+ export declare function positiveNumberfromJSON(json: any): number | undefined;
10
+ /** Converts a Set of a string to a string Array object */
11
+ export declare function setToArray(obj: Set<string>): Array<string>;
@@ -0,0 +1 @@
1
+ export type Language = string;
@@ -0,0 +1,24 @@
1
+ /** A lightweight implementation of URI Template (RFC 6570).
2
+ *
3
+ * Only handles simple cases, fitting Readium's use cases.
4
+ * See https://tools.ietf.org/html/rfc6570
5
+ */
6
+ export declare class URITemplate {
7
+ uri: string;
8
+ /**
9
+ * List of URI template parameter keys, if the [Link] is templated.
10
+ */
11
+ parameters: Set<string>;
12
+ constructor(uri: string);
13
+ /**
14
+ * List of URI template parameter keys, if the [Link] is templated.
15
+ */
16
+ private getParameters;
17
+ /** Expands the URI by replacing the template variables by the given parameters.
18
+ * Any extra parameter is appended as query parameters.
19
+ * See RFC 6570 on URI template: https://tools.ietf.org/html/rfc6570
20
+ */
21
+ expand(parameters: {
22
+ [param: string]: string;
23
+ }): string;
24
+ }
@@ -0,0 +1,5 @@
1
+ export * from './mediatype';
2
+ export * from './JSONParse';
3
+ export * from './URITemplate';
4
+ export * from './Language';
5
+ export * from './tokenizer';
@@ -0,0 +1,148 @@
1
+ type ParametersMap = {
2
+ [param: string]: string;
3
+ };
4
+ /** Represents a string media type.
5
+ * `MediaType` handles:
6
+ * - components parsing – eg. type, subtype and parameters,
7
+ * - media types comparison.
8
+ */
9
+ export declare class MediaType {
10
+ /** The type component, e.g. `application` in `application/epub+zip`. */
11
+ type: string;
12
+ /** The subtype component, e.g. `epub+zip` in `application/epub+zip`. */
13
+ subtype: string;
14
+ /** The parameters in the media type, such as `charset=utf-8`. */
15
+ parameters: ParametersMap;
16
+ /** The string representation of this media type. */
17
+ string: string;
18
+ /** Encoding as declared in the `charset` parameter, if there's any. */
19
+ encoding?: string;
20
+ /** A human readable name identifying the media type, which may be presented to the user. */
21
+ name?: string;
22
+ /** The default file extension to use for this media type. */
23
+ fileExtension?: string;
24
+ /** Creates a MediaType object. */
25
+ constructor(values: {
26
+ mediaType: string;
27
+ name?: string;
28
+ fileExtension?: string;
29
+ });
30
+ static parse(values: {
31
+ mediaType: string;
32
+ name?: string;
33
+ fileExtension?: string;
34
+ }): MediaType;
35
+ /** Structured syntax suffix, e.g. `+zip` in `application/epub+zip`.
36
+ * Gives a hint on the underlying structure of this media type.
37
+ * See. https://tools.ietf.org/html/rfc6838#section-4.2.8
38
+ */
39
+ get structuredSyntaxSuffix(): string | undefined;
40
+ /** Parameter values might or might not be case-sensitive, depending on the semantics of
41
+ * the parameter name.
42
+ * https://tools.ietf.org/html/rfc2616#section-3.7
43
+ *
44
+ * The character set names may be up to 40 characters taken from the printable characters
45
+ * of US-ASCII. However, no distinction is made between use of upper and lower case
46
+ * letters.
47
+ * https://www.iana.org/assignments/character-sets/character-sets.xhtml
48
+ */
49
+ get charset(): string | undefined;
50
+ /** Returns whether the given `other` media type is included in this media type.
51
+ * For example, `text/html` contains `text/html;charset=utf-8`.
52
+ * - `other` must match the parameters in the `parameters` property, but extra parameters
53
+ * are ignored.
54
+ * - Order of parameters is ignored.
55
+ * - Wildcards are supported, meaning that `image/*` contains `image/png`
56
+ */
57
+ contains(other: MediaType | string): boolean;
58
+ /** Returns whether this media type and `other` are the same, ignoring parameters that
59
+ * are not in both media types.
60
+ * For example, `text/html` matches `text/html;charset=utf-8`, but `text/html;charset=ascii`
61
+ * doesn't. This is basically like `contains`, but working in both direction.
62
+ */
63
+ matches(other: MediaType | string): boolean;
64
+ /**
65
+ * Returns whether this media type matches any of the [others] media types.
66
+ */
67
+ matchesAny(...others: MediaType[] | string[]): boolean;
68
+ /** Checks the MediaType equals another one (comparing their string) */
69
+ equals(other: MediaType): boolean;
70
+ /** Returns whether this media type is structured as a ZIP archive. */
71
+ get isZIP(): boolean;
72
+ /** Returns whether this media type is structured as a JSON file. */
73
+ get isJSON(): boolean;
74
+ /** Returns whether this media type is of an OPDS feed. */
75
+ get isOPDS(): boolean;
76
+ /** Returns whether this media type is of an HTML document. */
77
+ get isHTML(): boolean;
78
+ /** Returns whether this media type is of a bitmap image, so excluding vectorial formats. */
79
+ get isBitmap(): boolean;
80
+ /** Returns whether this media type is of an audio clip. */
81
+ get isAudio(): boolean;
82
+ /** Returns whether this media type is of a video clip. */
83
+ get isVideo(): boolean;
84
+ /** Returns whether this media type is of a Readium Web Publication Manifest. */
85
+ get isRWPM(): boolean;
86
+ /** Returns whether this media type is of a publication file. */
87
+ get isPublication(): boolean;
88
+ static get AAC(): MediaType;
89
+ static get ACSM(): MediaType;
90
+ static get AIFF(): MediaType;
91
+ static get AVI(): MediaType;
92
+ static get BINARY(): MediaType;
93
+ static get BMP(): MediaType;
94
+ static get CBZ(): MediaType;
95
+ static get CSS(): MediaType;
96
+ static get DIVINA(): MediaType;
97
+ static get DIVINA_MANIFEST(): MediaType;
98
+ static get EPUB(): MediaType;
99
+ static get GIF(): MediaType;
100
+ static get GZ(): MediaType;
101
+ static get HTML(): MediaType;
102
+ static get JAVASCRIPT(): MediaType;
103
+ static get JPEG(): MediaType;
104
+ static get JSON(): MediaType;
105
+ static get LCP_LICENSE_DOCUMENT(): MediaType;
106
+ static get LCP_PROTECTED_AUDIOBOOK(): MediaType;
107
+ static get LCP_PROTECTED_PDF(): MediaType;
108
+ static get LCP_STATUS_DOCUMENT(): MediaType;
109
+ static get LPF(): MediaType;
110
+ static get MP3(): MediaType;
111
+ static get MPEG(): MediaType;
112
+ static get NCX(): MediaType;
113
+ static get OGG(): MediaType;
114
+ static get OGV(): MediaType;
115
+ static get OPDS1(): MediaType;
116
+ static get OPDS1_ENTRY(): MediaType;
117
+ static get OPDS2(): MediaType;
118
+ static get OPDS2_PUBLICATION(): MediaType;
119
+ static get OPDS_AUTHENTICATION(): MediaType;
120
+ static get OPUS(): MediaType;
121
+ static get OTF(): MediaType;
122
+ static get PDF(): MediaType;
123
+ static get PNG(): MediaType;
124
+ static get READIUM_AUDIOBOOK(): MediaType;
125
+ static get READIUM_AUDIOBOOK_MANIFEST(): MediaType;
126
+ static get READIUM_CONTENT_DOCUMENT(): MediaType;
127
+ static get READIUM_GUIDED_NAVIGATION_DOCUMENT(): MediaType;
128
+ static get READIUM_POSITION_LIST(): MediaType;
129
+ static get READIUM_WEBPUB(): MediaType;
130
+ static get READIUM_WEBPUB_MANIFEST(): MediaType;
131
+ static get SMIL(): MediaType;
132
+ static get SVG(): MediaType;
133
+ static get TEXT(): MediaType;
134
+ static get TIFF(): MediaType;
135
+ static get TTF(): MediaType;
136
+ static get W3C_WPUB_MANIFEST(): MediaType;
137
+ static get WAV(): MediaType;
138
+ static get WEBM_AUDIO(): MediaType;
139
+ static get WEBM_VIDEO(): MediaType;
140
+ static get WEBP(): MediaType;
141
+ static get WOFF(): MediaType;
142
+ static get WOFF2(): MediaType;
143
+ static get XHTML(): MediaType;
144
+ static get XML(): MediaType;
145
+ static get ZAB(): MediaType;
146
+ static get ZIP(): MediaType;
147
+ }
148
+ export {};
@@ -0,0 +1 @@
1
+ export * from './MediaType';
@@ -0,0 +1,39 @@
1
+ import { Language } from "../Language";
2
+ import { Tokenizer } from "./Tokenizer";
3
+ export type Range = [number, number];
4
+ /**
5
+ * A tokenizer splitting a String into range tokens (e.g. words, sentences, etc.).
6
+ */
7
+ export type TextTokenizer = Tokenizer<string, Range>;
8
+ /**
9
+ * A text token unit which can be used with a [TextTokenizer].
10
+ */
11
+ export declare enum TextUnit {
12
+ Word = "word",
13
+ Sentence = "sentence",
14
+ Paragraph = "paragraph"
15
+ }
16
+ export declare const DefaultTextContentTokenizer: (language: Language | null, unit: TextUnit) => TextTokenizer;
17
+ /**
18
+ * A [TextTokenizer] using the Intl.Segmenter API.
19
+ * Very aware of language-specific rules since it uses ICU behind the scenes.
20
+ */
21
+ export declare class IntlTextTokenizer implements TextTokenizer {
22
+ private unit;
23
+ private segmenter;
24
+ constructor(language: Language | null, unit: TextUnit);
25
+ tokenize(data: string): Range[];
26
+ }
27
+ /**
28
+ * A [TextTokenizer] using a naive approach to splitting text into tokens.
29
+ * This is a fallback for browsers that don't support Intl.Segmenter.
30
+ * It works mainly on English and similar languages. Don't use unless necessary.
31
+ */
32
+ export declare class NaiveTextTokenizer {
33
+ private unit;
34
+ private tokenizer;
35
+ private isEnglish;
36
+ constructor(language: Language | null, unit: TextUnit);
37
+ tokenize(data: string): Range[];
38
+ }
39
+ export declare const speakableToken: (token: string) => string | null;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * A tokenizer splits a piece of data [D] into a list of [T] tokens.
3
+ */
4
+ export interface Tokenizer<D, T> {
5
+ tokenize(data: D): T[];
6
+ }
@@ -0,0 +1,2 @@
1
+ export * from './Tokenizer';
2
+ export * from './TextTokenizer';