123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147(*********************************************************************************)(* Stog *)(* *)(* Copyright (C) 2012-2015 INRIA All rights reserved. *)(* Author: Maxence Guesdon, INRIA Saclay *)(* *)(* This program is free software; you can redistribute it and/or modify *)(* it under the terms of the GNU General Public License as *)(* published by the Free Software Foundation, version 3 of the License. *)(* *)(* This program is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU General Public License for more details. *)(* *)(* You should have received a copy of the GNU General Public *)(* License along with this program; if not, write to the Free Software *)(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA *)(* 02111-1307 USA *)(* *)(* As a special exception, you have permission to link this program *)(* with the OCaml compiler and distribute executables, as long as you *)(* follow the requirements of the GNU GPL in regard to all of the *)(* software in the executable aside from the OCaml compiler. *)(* *)(* Contact: Maxence.Guesdon@inria.fr *)(* *)(*********************************************************************************)(** *)moduleS=Cohttp_lwt_unix.ServeropenStog.UrlopenStog.TypesopenTypesopenRunlet(>>=)=Lwt.bindletclient_js="stog_server_client.js";;letdefault_css="stog-server-style.css";;letrespond_cssbody=letheaders=Cohttp.Header.init_with"Content-Type""text/css"inS.respond_string~headers~status:`OK~body()letrespond_jsbody=letheaders=Cohttp.Header.init_with"Content-Type""text/javascript"inS.respond_string~headers~status:`OK~body()letrespond_server_client_js=respond_jsFiles.server_client_jsletrespond_default_css=respond_cssFiles.server_style_cssletpreview_filestog=function|[file]whenfile=client_js->respond_server_client_js|path->letrecitertree=function[]->S.respond_file~fname:""()|[f]->ifStog.Types.Str_set.memftree.filesthenletfname=Filename.concatstog.stog_dir(String.concatFilename.dir_seppath)inS.respond_file~fname()else((* maybe this was a generated file in stog output directory *)letfname=Filename.concatstog.stog_outdir(String.concatFilename.dir_seppath)inprerr_endline("fname="^fname);S.respond_file~fname())|d::q->matchtrySome(Stog.Types.Str_map.finddtree.dirs)withNot_found->NonewithSometree->itertreeq|None->(* maybe this was a generated file in stog output directory *)letfname=Filename.concatstog.stog_outdir(String.concatFilename.dir_seppath)inprerr_endline("fname="^fname);S.respond_file~fname()initerstog.stog_filespathlethandle_preview~http_url~ws_urlcurrent_statereqpath=match!current_statewithNone->Lwt.fail(Failure"No state yet!")|Somestate->matchlets_path="/"^(String.concat"/"path)inletpath=Stog.Path.of_strings_pathintrySome(Stog.Types.doc_by_pathstate.stogpath)with_->trySome(Stog.Types.doc_by_pathstate.stog(Stog.Path.of_string(s_path^"/index.html")))with_->NonewithSome(doc_id,doc)->letbody=matchdoc.doc_outwithNone->String.concat"\n"["Document not computed yet";String.concat"\n"state.stog_errors;String.concat"\n"state.stog_warnings;]|Somexmls->letdoc_path=Stog.Path.to_stringdoc.doc_pathinlettitle=Printf.sprintf"Loading preview of %s"doc_pathinletscript_url=Stog.Url.appendhttp_url.pub[client_js]inlethttp_root_url=letpath=matchList.rev(Stog.Url.pathhttp_url.pub)with_::q->List.revq|[]->[]inleturl=Stog.Url.with_pathhttp_url.pubpathinStog.Url.to_stringurlin"<?xml version=\"1.0\" encoding=\"utf-8\"?><html xmlns=\"http://www.w3.org/1999/xhtml\"><header>
<meta charset=\"utf-8\"/>
<meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=utf-8\"/>
<title>"^title^"</title>"^"<script type=\"text/javascript\">
stog_server = {
wsUrl: '"^(Stog.Url.to_stringws_url.pub)^"',
doc: '"^doc_path^"', httpUrl: '"^http_root_url^"' };
</script>"^"<script src=\""^(Stog.Url.to_stringscript_url)^"\"
type=\"text/javascript\"> </script>"^"</header><body><h1>"^title^"</h1></body></html>"inletheaders=Cohttp.Header.init_with"Content-Type""application/xhtml+xml; charset=utf-8"inS.respond_string~headers~status:`OK~body()|None->preview_filestate.stogpathletnew_stog_sessionstogstog_base_url=letactive_cons=ref[]inletcurrent_state=refNoneinletstog=(* if modifying another field, update also Run.refresh *){stogwithStog.Types.stog_base_url}inleton_update=Ws.send_patchactive_consinleton_error=Ws.send_errorsactive_consinlet_watcher=Run.watchstogcurrent_state~on_update~on_errorin(current_state,active_cons)