| author | blanchet | 
| Tue, 22 May 2018 17:15:02 +0200 | |
| changeset 68250 | c45067867860 | 
| parent 68226 | 5ce62512de35 | 
| child 72511 | 460d743010bc | 
| permissions | -rw-r--r-- | 
| 6639 | 1  | 
(* Title: Pure/General/url.ML  | 
2  | 
Author: Markus Wenzel, TU Muenchen  | 
|
3  | 
||
| 21515 | 4  | 
Basic URLs, see RFC 1738 and RFC 2396.  | 
| 6639 | 5  | 
*)  | 
6  | 
||
7  | 
signature URL =  | 
|
8  | 
sig  | 
|
| 14909 | 9  | 
datatype T =  | 
10  | 
File of Path.T |  | 
|
11  | 
Http of string * Path.T |  | 
|
| 68226 | 12  | 
Https of string * Path.T |  | 
| 14909 | 13  | 
Ftp of string * Path.T  | 
| 6639 | 14  | 
val append: T -> T -> T  | 
| 
21858
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
15  | 
val implode: T -> string  | 
| 
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
16  | 
val explode: string -> T  | 
| 
54702
 
3daeba5130f0
added document antiquotation @{url}, which produces formal markup for LaTeX and PIDE;
 
wenzelm 
parents: 
29606 
diff
changeset
 | 
17  | 
val pretty: T -> Pretty.T  | 
| 
 
3daeba5130f0
added document antiquotation @{url}, which produces formal markup for LaTeX and PIDE;
 
wenzelm 
parents: 
29606 
diff
changeset
 | 
18  | 
val print: T -> string  | 
| 6639 | 19  | 
end;  | 
20  | 
||
21  | 
structure Url: URL =  | 
|
22  | 
struct  | 
|
23  | 
||
24  | 
(* type url *)  | 
|
25  | 
||
| 14909 | 26  | 
datatype T =  | 
27  | 
File of Path.T |  | 
|
28  | 
Http of string * Path.T |  | 
|
| 68226 | 29  | 
Https of string * Path.T |  | 
| 14909 | 30  | 
Ftp of string * Path.T;  | 
| 6639 | 31  | 
|
32  | 
||
33  | 
(* append *)  | 
|
34  | 
||
| 64776 | 35  | 
fun append (File p) (File p') = File (Path.append p p')  | 
36  | 
| append (Http (h, p)) (File p') = Http (h, Path.append p p')  | 
|
| 68226 | 37  | 
| append (Https (h, p)) (File p') = Https (h, Path.append p p')  | 
| 64776 | 38  | 
| append (Ftp (h, p)) (File p') = Ftp (h, Path.append p p')  | 
| 6639 | 39  | 
| append _ url = url;  | 
40  | 
||
41  | 
||
| 
21858
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
42  | 
(* implode *)  | 
| 6639 | 43  | 
|
| 
21858
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
44  | 
fun implode_path p = if Path.is_current p then "" else Path.implode p;  | 
| 6639 | 45  | 
|
| 
21858
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
46  | 
fun implode_url (File p) = implode_path p  | 
| 
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
47  | 
| implode_url (Http (h, p)) = "http://" ^ h ^ implode_path p  | 
| 68226 | 48  | 
| implode_url (Https (h, p)) = "https://" ^ h ^ implode_path p  | 
| 
21858
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
49  | 
| implode_url (Ftp (h, p)) = "ftp://" ^ h ^ implode_path p;  | 
| 6639 | 50  | 
|
51  | 
||
| 
21858
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
52  | 
(* explode *)  | 
| 6639 | 53  | 
|
| 14918 | 54  | 
local  | 
55  | 
||
| 16195 | 56  | 
val scan_host =  | 
| 58854 | 57  | 
(Scan.many1 (fn s => s <> "/" andalso Symbol.not_eof s) >> implode) --|  | 
| 14918 | 58  | 
Scan.ahead ($$ "/" || Scan.one Symbol.is_eof);  | 
59  | 
||
| 58854 | 60  | 
val scan_path = Scan.many Symbol.not_eof >> (Path.explode o implode);  | 
61  | 
val scan_path_root = Scan.many Symbol.not_eof >> (Path.explode o implode o cons "/");  | 
|
| 6639 | 62  | 
|
63  | 
val scan_url =  | 
|
| 68226 | 64  | 
Scan.unless  | 
65  | 
(Scan.this_string "file:" || Scan.this_string "http:" ||  | 
|
66  | 
Scan.this_string "https:" || Scan.this_string "ftp:") scan_path >> File ||  | 
|
| 
15175
 
b62f7b493360
Fix file:/// and file://localhost/ to give absolute paths
 
aspinall 
parents: 
15174 
diff
changeset
 | 
67  | 
Scan.this_string "file:///" |-- scan_path_root >> File ||  | 
| 
 
b62f7b493360
Fix file:/// and file://localhost/ to give absolute paths
 
aspinall 
parents: 
15174 
diff
changeset
 | 
68  | 
Scan.this_string "file://localhost/" |-- scan_path_root >> File ||  | 
| 64776 | 69  | 
Scan.this_string "file://" |-- scan_host -- scan_path  | 
70  | 
>> (fn (h, p) => File (Path.append (Path.named_root h) p)) ||  | 
|
| 21503 | 71  | 
Scan.this_string "file:/" |-- scan_path_root >> File ||  | 
| 16195 | 72  | 
Scan.this_string "http://" |-- scan_host -- scan_path >> Http ||  | 
| 68226 | 73  | 
Scan.this_string "https://" |-- scan_host -- scan_path >> Https ||  | 
| 16195 | 74  | 
Scan.this_string "ftp://" |-- scan_host -- scan_path >> Ftp;  | 
| 14918 | 75  | 
|
76  | 
in  | 
|
| 6639 | 77  | 
|
| 
21858
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
78  | 
fun explode_url s = Symbol.scanner "Malformed URL" scan_url (Symbol.explode s);  | 
| 6639 | 79  | 
|
80  | 
end;  | 
|
| 14918 | 81  | 
|
| 
21858
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
82  | 
|
| 
54702
 
3daeba5130f0
added document antiquotation @{url}, which produces formal markup for LaTeX and PIDE;
 
wenzelm 
parents: 
29606 
diff
changeset
 | 
83  | 
(* print *)  | 
| 
 
3daeba5130f0
added document antiquotation @{url}, which produces formal markup for LaTeX and PIDE;
 
wenzelm 
parents: 
29606 
diff
changeset
 | 
84  | 
|
| 
 
3daeba5130f0
added document antiquotation @{url}, which produces formal markup for LaTeX and PIDE;
 
wenzelm 
parents: 
29606 
diff
changeset
 | 
85  | 
val pretty = Pretty.mark_str o `Markup.url o implode_url;  | 
| 
 
3daeba5130f0
added document antiquotation @{url}, which produces formal markup for LaTeX and PIDE;
 
wenzelm 
parents: 
29606 
diff
changeset
 | 
86  | 
|
| 
61877
 
276ad4354069
renamed Pretty.str_of to Pretty.unformatted_string_of to emphasize its meaning;
 
wenzelm 
parents: 
58854 
diff
changeset
 | 
87  | 
val print = Pretty.unformatted_string_of o pretty;  | 
| 
54702
 
3daeba5130f0
added document antiquotation @{url}, which produces formal markup for LaTeX and PIDE;
 
wenzelm 
parents: 
29606 
diff
changeset
 | 
88  | 
|
| 
 
3daeba5130f0
added document antiquotation @{url}, which produces formal markup for LaTeX and PIDE;
 
wenzelm 
parents: 
29606 
diff
changeset
 | 
89  | 
|
| 
21858
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
90  | 
(*final declarations of this structure!*)  | 
| 
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
91  | 
val implode = implode_url;  | 
| 
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
92  | 
val explode = explode_url;  | 
| 
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
93  | 
|
| 14918 | 94  | 
end;  |