1
1
import { Entry } from "./entry" ;
2
- import { load } from "cheerio" ;
2
+ import { CheerioAPI , load } from "cheerio" ;
3
3
import { SubjectContainer } from "./subjectcontainer" ;
4
4
import { defaultSubjectShorts } from "../utility" ;
5
5
@@ -9,6 +9,31 @@ import { defaultSubjectShorts } from "../utility";
9
9
export class TimeTable implements SubjectContainer {
10
10
public subjectShorts = new Map < string , string > ( defaultSubjectShorts ) ;
11
11
12
+ /**
13
+ * Parse the `TimeTable` from HTML in a custom way
14
+ *
15
+ * @param html The html object from where the data is to be extracted
16
+ * @returns a new `TimeTable` instance
17
+ *
18
+ * @example
19
+ * ```js
20
+ * // use custom html-handler
21
+ * TimeTable.htmlHandler = (html) => {
22
+ * var div = html("my-div")
23
+ * var data = div.text()
24
+ *
25
+ * return new TimeTable(...)
26
+ * }
27
+ * ```
28
+ *
29
+ * @example
30
+ * ```js
31
+ * // reset html-handler to default
32
+ * TimeTable.htmlHandler = undefined
33
+ * ```
34
+ */
35
+ public static htmlHandler : ( html : CheerioAPI ) => TimeTable ;
36
+
12
37
constructor ( public readonly entries : Entry [ ] ) {
13
38
entries . forEach ( ( e ) => e . registerSubjectShorts ( this . subjectShorts ) ) ;
14
39
}
@@ -103,8 +128,11 @@ export class TimeTable implements SubjectContainer {
103
128
* @param rawHtml The raw html string
104
129
* @returns A new `TimeTable` resource
105
130
*/
106
- static fromHtml ( rawHtml : string ) {
131
+ public static fromHtml ( rawHtml : string ) {
107
132
const $ = load ( rawHtml ) ;
133
+ if ( TimeTable . htmlHandler !== undefined ) {
134
+ return TimeTable . htmlHandler ( $ ) ;
135
+ }
108
136
109
137
const centers = $ ( "center" ) ;
110
138
const entries : Entry [ ] = [ ] ;
@@ -133,9 +161,10 @@ export class TimeTable implements SubjectContainer {
133
161
) ;
134
162
135
163
for ( const row of $ ( center ) . find ( "tr" ) ) {
136
- if ( $ ( row ) . find ( "th" ) . length !== 0 ) continue ;
164
+ const rowCheer = $ ( row ) ;
165
+ if ( rowCheer . find ( "th" ) . length !== 0 ) continue ;
137
166
138
- const columns = $ ( row ) . find ( "td" ) ;
167
+ const columns = rowCheer . find ( "td" ) ;
139
168
140
169
let period = $ ( columns [ 1 ] ) . text ( ) ;
141
170
if ( period . includes ( "-" ) ) {
0 commit comments