Skip to content

Commit cd2fa42

Browse files
committed
(core) new library(openpg): open a file in the playground
Src-commit: 6bbc27c98ef682920e6b2f001339f900f36ab5f6
1 parent 1708fa9 commit cd2fa42

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

core/library/openpg.pl

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
:- module(openpg, [], [assertions, regtypes, fsyntax]).
2+
3+
:- doc(title, "Open a Ciao playground").
4+
:- doc(author, "The Ciao Development Team").
5+
6+
:- doc(module, "This module provides predicates to open documents
7+
using the Ciao Playground. It relies on @lib{opendoc} to open
8+
arbritrary URLs.
9+
10+
This is just a small client library. The playground code is
11+
distributed seperately in the @tt{ciao_playground} bundle.
12+
").
13+
14+
:- use_module(library(opendoc)).
15+
:- use_module(library(streams), [fixed_absolute_file_name/3]).
16+
17+
:- export(pgloc/1).
18+
:- regtype pgloc/1 # "Ciao Playground location:
19+
@begin{itemize}
20+
@item @tt{ciaolang}: alias for @tt{https://ciao-lang.org/playground}
21+
@item @tt{local}: alias for @tt{http://localhost:8001/playground}
22+
(development local playground, see @tt{ciao-serve-mt})
23+
@end{itemize}
24+
".
25+
pgloc(ciaolang).
26+
pgloc(local).
27+
28+
pg_url(ciaolang, 'https://ciao-lang.org/playground').
29+
pg_url(local, 'http://localhost:8001/playground').
30+
31+
:- export(openpg/1).
32+
:- pred openpg(PGLoc) # "Open a new Ciao Playground".
33+
openpg(PGLoc) :-
34+
openpg(PGLoc, '').
35+
36+
:- export(openpg/2).
37+
:- pred openpg(PGLoc, Path) # "Open a Ciao Playground from the
38+
specified location".
39+
40+
openpg(PGLoc, Path) :-
41+
( pg_url(PGLoc, URL) -> true
42+
; throw(error(bad_pgloc, openpg/2))
43+
),
44+
( Path = '' ->
45+
opendoc(URL)
46+
; PGLoc = local ->
47+
Path1 = ~fixed_absolute_file_name(Path, '.'),
48+
path_split(Path1, _, Name),
49+
create_site_symlink(Path1, Name),
50+
URL2 = ~atom_concat(URL, ~atom_concat('#/tmp/', Name)),
51+
opendoc(URL2)
52+
; throw(error(unsupported_path, openpg/2))
53+
).
54+
55+
% ---------------------------------------------------------------------------
56+
% TODO: implement a better way; use FileSystem API?
57+
58+
:- use_module(library(pathnames)).
59+
:- use_module(library(system),[copy_file/3]).
60+
:- use_module(library(system_extra),[mkpath/1]).
61+
62+
:- use_module(ciaobld(config_common), [site_root_dir/1]).
63+
64+
% Create symbolic link under site tmp
65+
create_site_symlink(OrigPath, DestName) :-
66+
SiteDir = ~site_root_dir,
67+
TmpSite = ~path_concat(SiteDir, 'tmp'),
68+
mkpath(TmpSite),
69+
DestPath = ~path_concat(TmpSite, DestName),
70+
copy_file(OrigPath, DestPath, [overwrite, symlink]).

0 commit comments

Comments
 (0)