123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170(*********************************************************************************)(* 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 *)(* *)(*********************************************************************************)(** *)openStog.Types;;moduleXR=Xtmpl.RewritemoduleXml=Xtmpl.Xmlletmodule_name="extern";;letrc_filestog=Stog.Plug.plugin_config_filestogmodule_name;;moduleW=Ocf.Wrappertypeaction={types:stringlist[@ocfW.listW.string,[]][@ocf.doc"list of document types concerned"];name:string[@ocfW.string,"cat"][@ocf.doc"name of run level in which to apply the command"];command:string[@ocfW.string,"cat"][@ocf.doc"command, taking XML document in input and outputting the new XML document"];}[@@ocf]typedata={actions:actionlist[@ocfW.listaction_wrapper,[]];}[@@ocf]letgroupdata=letoption_t=Ocf.optiondata_wrapperdatainletg=Ocf.as_groupoption_tin(g,option_t)letload_configstog=let(group,t)=groupdefault_datainletrc_file=rc_filestoginifnot(Sys.file_existsrc_file)thenOcf.to_filegrouprc_file;tryOcf.from_filegrouprc_file;Ocf.gettwith|Ocf.Errore->failwith(Ocf.string_of_errore);;letapply_to_doctypescommandstogdoc_id=letdoc=Stog.Types.docstogdoc_idinmatchList.memdoc.doc_typetypeswithfalse->None|true->letin_file=Filename.temp_file"stog"".xml.in"inletout_file=(Filename.chop_extensionin_file)^".out"intryletxml=matchdoc.doc_outwith|None->doc.doc_body|Somexml->xmlinStog_base.Misc.file_of_string~file:in_file(XR.to_stringxml);letcom=Printf.sprintf"cat %s | %s > %s"(Filename.quotein_file)command(Filename.quoteout_file)inletrm()=trySys.removein_filewith_->();trySys.removeout_filewith_->()inmatchSys.commandcomwith0->letxmldoc=XR.doc_from_fileout_fileinletxml=xmldoc.Xml.elementsinrm();letdoc={docwithdoc_out=Somexml}inSome(doc_id,doc)|n->failwith(Printf.sprintf"Command exited with %d: %s"ncom)withe->letmsg=matchewith|Failuremsg|Sys_errormsg->msg|_->Printexc.to_stringeinStog.Log.err(funm->m"Doc %S: %s"(Stog.Path.to_stringdoc.doc_path)msg);None(** FIXME: parallelize this when we'll use lwt everywhere *)letapplytypescommandenvstogdocs=letdocs=List.map(apply_to_doctypescommandstog)(Stog.Types.Doc_set.elementsdocs)inList.fold_left(funstog->function|None->stog|Some(doc_id,doc)->Stog.Types.set_docstogdoc_iddoc)stogdocsletlevel_fun_of_actiona=(a.name,Stog.Engine.Fun_stog(applya.typesa.command))letlevel_funsstog=letconfig=load_configstoginList.maplevel_fun_of_actionconfig.actions;;letdefault_levels=Stog.Types.Str_map.emptyletmake_modulestog?levels()=letlevel_funs=level_funsstoginletlevels=Stog.Html.mk_levelsmodule_namelevel_funsdefault_levels?levels()inletmoduleM=structtypedata=unitletmodul={Stog.Engine.mod_name=module_name;mod_levels=levels;mod_data=()}typecache_data=unitletcache_load_stogdatadoct=dataletcache_store_stogdatadoc=()endin(moduleM:Stog.Engine.Module);;letfstog=letlevels=trySome(Stog.Types.Str_map.findmodule_namestog.Stog.Types.stog_levels)withNot_found->Noneinmake_modulestog?levels();;let()=Stog.Engine.register_modulemodule_namef;;