This repository has been archived by the owner on Dec 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrfi-count-html-tags.scm
69 lines (57 loc) · 2.25 KB
/
srfi-count-html-tags.scm
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
;;; Display how many times each HTML tag is used in the given SRFIs.
(import (scheme base)
(scheme char)
(scheme cxr)
(scheme file)
(scheme process-context)
(scheme write)
(srfi 1)
(srfi 69)
(srfi 95))
(cond-expand (chibi (import (chibi io) (chibi string)))
(gauche (import (srfi 13) (gauche base))))
(import (chibi html-parser))
(define make-set make-hash-table)
(define set-elems hash-table-keys)
(define (add-to-set elem set) (hash-table-set! set elem #t) set)
(define (symbol<? a b) (string<? (symbol->string a) (symbol->string b)))
(define (hash-table-increment! table key)
(hash-table-update!/default table key (lambda (x) (+ x 1)) 0))
(define (symbol-prefix? prefix sym)
(string-prefix? prefix (symbol->string sym)))
(define (displayln . xs)
(for-each display xs)
(newline))
(define (tag-body elem)
(cond ((not (pair? (cdr elem))) '())
((and (pair? (cadr elem)) (eqv? '@ (caadr elem)))
(cddr elem))
(else (cdr elem))))
(define (tag-names-fold elem kons knil)
(let do-elem ((elem elem) (acc knil))
(if (not (pair? elem)) acc
(let do-list ((elems (tag-body elem)) (acc (kons (car elem) acc)))
(if (null? elems) acc
(do-list (cdr elems) (do-elem (car elems) acc)))))))
(define (count-html-file-tags! html-file counts)
(let* ((html (call-with-input-file html-file port->string))
(sxml (call-with-input-string html html->sxml)))
(tag-names-fold sxml
(lambda (tag counts)
(unless (symbol-prefix? "*" tag)
(hash-table-increment! counts tag))
counts)
counts)))
(define (main html-files)
(let ((counts (make-hash-table)))
(for-each (lambda (html-file)
(count-html-file-tags! html-file counts))
html-files)
(for-each (lambda (tag-name)
(let ((count (hash-table-ref counts tag-name)))
(displayln (list tag-name count))))
(sort (hash-table-keys counts)
(lambda (tag-a tag-b)
(> (hash-table-ref counts tag-a)
(hash-table-ref counts tag-b)))))))
(main (cdr (command-line)))