deno.land / x / deno_temme@v1.0.0 / temme / procedures.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// deno-lint-ignore-file no-explicit-anyimport { cheerio } from '../deps.ts'import { Capture, Literal } from '../parser/mod.ts'
import invariant from './invariant.ts'
import { CaptureResult } from './CaptureResult.ts'import { ASSIGN_PROCEDURE_NAME, DEFAULT_PROCEDURE_NAME } from './constants.ts'import { isCapture } from './utils.ts'
export interface ProcedureFn { (result: CaptureResult, node: any, ...args: any[]): void}
function text(result: CaptureResult, node: any, capture: Capture) { result.add(capture, node.text())}
function html(result: CaptureResult, node: any, capture: Capture) { result.add(capture, node.html())}
function node(result: CaptureResult, node: any, capture: Capture) { result.add(capture, cheerio.load(node))}
/** Try to capture text within a node's text. * This content function can have three forms: * 1. find('before-string', $capture) Try to capture the text after 'before-string' * 2. find($capture, 'after-string') Try to capture the text before 'after-string' * 3. find('pre', $capture, 'post') Try to capture the text between 'pre' and 'post' * */function find(result: CaptureResult, node: cheerio.CheerioAPI, ...args: (string | Capture)[]) { const invalidArgs = 'Invalid arguments received by match(...)' const s = node.text() if (args.length === 2) { const [before, after] = args invariant( (typeof before === 'string' && isCapture(after)) || (isCapture(before) && typeof after === 'string'), invalidArgs, ) if (typeof before === 'string') { const capture = after as Capture const i = s.indexOf(before) if (i === -1) { return } result.add(capture, s.substring(i + before.length)) } else { const capture = before as Capture const i = s.indexOf(after as string) if (i === -1) { return } result.add(capture, s.substring(0, i)) } } else { invariant(args.length === 3, invalidArgs) const [before, capture, after] = args as [string, Capture, string] invariant( typeof before === 'string' && isCapture(capture) && typeof after === 'string', invalidArgs, ) const i = s.indexOf(before) if (i === -1) { return } const j = s.indexOf(after, i + before.length) if (j === -1) { return } result.add(capture, s.substring(i + before.length, j)) }}
function assign(result: CaptureResult, _: any, capture: Capture, value: Literal) { result.forceAdd(capture, value)}
export const defaultProcedureDict: Record<string, ProcedureFn> = { [DEFAULT_PROCEDURE_NAME]: text, [ASSIGN_PROCEDURE_NAME]: assign, html, node, find,}
export function defineProcedure(name: string, fn: ProcedureFn) { defaultProcedureDict[name] = fn}
deno_temme

Version Info

Tagged at
a year ago