This repository has been archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurlfix.scm
executable file
·69 lines (58 loc) · 1.69 KB
/
urlfix.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
#!/usr/local/bin/gosh
; fix common url issues
; currently just repairs missing trailing slash
; todo - urlencode
(use gauche.parseopt) ; command-line
(use file.filter) ; file-filter-fold
(use srfi-13) ; string-scan
(include "lib/dir.scm")
(define (main args)
(let-args (cdr args)
((h "h|help" => (cut help (car args)))
(d "d|dot-files")
(f "f|file=s")
(t "t|type=s")
(v "v|verbose")
. restargs)
(if (not h)
(let ((urls (if f
(fix-file f)
(fix-dir (current-directory)
:type t :dot-files d :verbose v))))
(print #"Fixed ~urls urls")))))
(define (help file)
(print "Replace text in file or current directory (and below).")
(dir-help))
(define (fix-dir path :rest args)
(apply dir-info path args)
(directory-fold path
(lambda (file result)
(+ result (fix-file file)))
0
:lister
(lambda (dir seed)
(values (apply filter-dir dir args)
seed))))
(define (fix-file file)
(guard (e (else
(print #"Error processing ~file")
(print (condition-message e))
0))
(file-filter-fold fix-fold 0
:input file :output file
:temporary-file #t
:leave-unchanged #t)))
(define (fix-fold line count out)
(let ((match (rxmatch
#/\.(((ar)|(at)|(au)|(be)|(ca)|(cl)|(com)|(de)|(dk)|(es)|(fr)|(hk)|(id)|(ie)|(it)|(jp)|(kh)|(kr)|(mo)|(mu)|(mx)|(my)|(nl)|(no)|(nz)|(ph)|(pl)|(pt)|(se)|(sg)|(th)|(tt)|(tw)|(ua)|(uk)|(vn)))"/
line)))
(if (not match)
(begin
(write-string line out)
(newline out)
count)
(let ((new (string-append (match 'before 1) (match 1) "/" (match 'after 1))))
(print new)
(write-string new out)
(newline out)
(+ 1 count)))))