src/Pure/General/url.ML
author wenzelm
Mon, 09 Dec 2013 12:16:52 +0100
changeset 54702 3daeba5130f0
parent 29606 fedb8be05f24
child 58854 b979c781c2db
permissions -rw-r--r--
added document antiquotation @{url}, which produces formal markup for LaTeX and PIDE;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6639
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/General/url.ML
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
     2
    Author:     Markus Wenzel, TU Muenchen
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
     3
21515
43d55165b282 Comment: see RFC 2396 for relative URI syntax.
aspinall
parents: 21503
diff changeset
     4
Basic URLs, see RFC 1738 and RFC 2396.
6639
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
     5
*)
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
     6
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
     7
signature URL =
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
     8
sig
14909
988b4342ed1f tuned representation; added RemoteFile;
wenzelm
parents: 8806
diff changeset
     9
  datatype T =
988b4342ed1f tuned representation; added RemoteFile;
wenzelm
parents: 8806
diff changeset
    10
    File of Path.T |
988b4342ed1f tuned representation; added RemoteFile;
wenzelm
parents: 8806
diff changeset
    11
    RemoteFile of string * Path.T |
988b4342ed1f tuned representation; added RemoteFile;
wenzelm
parents: 8806
diff changeset
    12
    Http of string * Path.T |
988b4342ed1f tuned representation; added RemoteFile;
wenzelm
parents: 8806
diff changeset
    13
    Ftp of string * Path.T
6639
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    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
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    19
end;
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    20
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    21
structure Url: URL =
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    22
struct
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    23
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    24
(* type url *)
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    25
14909
988b4342ed1f tuned representation; added RemoteFile;
wenzelm
parents: 8806
diff changeset
    26
datatype T =
988b4342ed1f tuned representation; added RemoteFile;
wenzelm
parents: 8806
diff changeset
    27
  File of Path.T |
988b4342ed1f tuned representation; added RemoteFile;
wenzelm
parents: 8806
diff changeset
    28
  RemoteFile of string * Path.T |
988b4342ed1f tuned representation; added RemoteFile;
wenzelm
parents: 8806
diff changeset
    29
  Http of string * Path.T |
988b4342ed1f tuned representation; added RemoteFile;
wenzelm
parents: 8806
diff changeset
    30
  Ftp of string * Path.T;
6639
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    31
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    32
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    33
(* append *)
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    34
14909
988b4342ed1f tuned representation; added RemoteFile;
wenzelm
parents: 8806
diff changeset
    35
fun append (File p)            (File p') = File (Path.append p p')
988b4342ed1f tuned representation; added RemoteFile;
wenzelm
parents: 8806
diff changeset
    36
  | append (RemoteFile (h, p)) (File p') = RemoteFile (h, Path.append p p')
988b4342ed1f tuned representation; added RemoteFile;
wenzelm
parents: 8806
diff changeset
    37
  | append (Http (h, p))       (File p') = Http (h, Path.append p p')
988b4342ed1f tuned representation; added RemoteFile;
wenzelm
parents: 8806
diff changeset
    38
  | append (Ftp (h, p))        (File p') = Ftp (h, Path.append p p')
6639
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    39
  | append _ url = url;
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    40
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    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
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    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
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    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 (RemoteFile (h, p)) = "file://" ^ h ^ implode_path p
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21515
diff changeset
    48
  | implode_url (Http (h, p)) = "http://" ^ h ^ implode_path p
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
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    50
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    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
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    53
14918
9f30a1238090 improved RemoteFile;
wenzelm
parents: 14909
diff changeset
    54
local
9f30a1238090 improved RemoteFile;
wenzelm
parents: 14909
diff changeset
    55
16195
wenzelm
parents: 15627
diff changeset
    56
val scan_host =
23784
75e6b9dd5336 Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents: 21858
diff changeset
    57
  (Scan.many1 (fn s => s <> "/" andalso Symbol.is_regular s) >> implode) --|
14918
9f30a1238090 improved RemoteFile;
wenzelm
parents: 14909
diff changeset
    58
  Scan.ahead ($$ "/" || Scan.one Symbol.is_eof);
9f30a1238090 improved RemoteFile;
wenzelm
parents: 14909
diff changeset
    59
23784
75e6b9dd5336 Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents: 21858
diff changeset
    60
val scan_path = Scan.many Symbol.is_regular >> (Path.explode o implode);
75e6b9dd5336 Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents: 21858
diff changeset
    61
val scan_path_root = Scan.many Symbol.is_regular >> (Path.explode o implode o cons "/");
6639
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    62
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    63
val scan_url =
14909
988b4342ed1f tuned representation; added RemoteFile;
wenzelm
parents: 8806
diff changeset
    64
  Scan.unless (Scan.this_string "file:" ||
988b4342ed1f tuned representation; added RemoteFile;
wenzelm
parents: 8806
diff changeset
    65
    Scan.this_string "http:" || Scan.this_string "ftp:") scan_path >> File ||
15175
b62f7b493360 Fix file:/// and file://localhost/ to give absolute paths
aspinall
parents: 15174
diff changeset
    66
  Scan.this_string "file:///" |-- scan_path_root >> File ||
b62f7b493360 Fix file:/// and file://localhost/ to give absolute paths
aspinall
parents: 15174
diff changeset
    67
  Scan.this_string "file://localhost/" |-- scan_path_root >> File ||
16195
wenzelm
parents: 15627
diff changeset
    68
  Scan.this_string "file://" |-- scan_host -- scan_path >> RemoteFile ||
21503
c4ea7e8c3937 Accept URLs of form file:/home... also.
aspinall
parents: 19305
diff changeset
    69
  Scan.this_string "file:/" |-- scan_path_root >> File ||
16195
wenzelm
parents: 15627
diff changeset
    70
  Scan.this_string "http://" |-- scan_host -- scan_path >> Http ||
wenzelm
parents: 15627
diff changeset
    71
  Scan.this_string "ftp://" |-- scan_host -- scan_path >> Ftp;
14918
9f30a1238090 improved RemoteFile;
wenzelm
parents: 14909
diff changeset
    72
9f30a1238090 improved RemoteFile;
wenzelm
parents: 14909
diff changeset
    73
in
6639
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    74
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21515
diff changeset
    75
fun explode_url s = Symbol.scanner "Malformed URL" scan_url (Symbol.explode s);
6639
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    76
d399db16964c Basic URLs.
wenzelm
parents:
diff changeset
    77
end;
14918
9f30a1238090 improved RemoteFile;
wenzelm
parents: 14909
diff changeset
    78
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21515
diff changeset
    79
54702
3daeba5130f0 added document antiquotation @{url}, which produces formal markup for LaTeX and PIDE;
wenzelm
parents: 29606
diff changeset
    80
(* print *)
3daeba5130f0 added document antiquotation @{url}, which produces formal markup for LaTeX and PIDE;
wenzelm
parents: 29606
diff changeset
    81
3daeba5130f0 added document antiquotation @{url}, which produces formal markup for LaTeX and PIDE;
wenzelm
parents: 29606
diff changeset
    82
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
    83
3daeba5130f0 added document antiquotation @{url}, which produces formal markup for LaTeX and PIDE;
wenzelm
parents: 29606
diff changeset
    84
val print = Pretty.str_of o pretty;
3daeba5130f0 added document antiquotation @{url}, which produces formal markup for LaTeX and PIDE;
wenzelm
parents: 29606
diff changeset
    85
3daeba5130f0 added document antiquotation @{url}, which produces formal markup for LaTeX and PIDE;
wenzelm
parents: 29606
diff changeset
    86
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21515
diff changeset
    87
(*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
    88
val implode = implode_url;
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21515
diff changeset
    89
val explode = explode_url;
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21515
diff changeset
    90
14918
9f30a1238090 improved RemoteFile;
wenzelm
parents: 14909
diff changeset
    91
end;