123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175(*********************************************************************************)(* Stog *)(* *)(* Copyright (C) 2012-2024 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 *)(* *)(*********************************************************************************)(* An asymptote plugin.
Example of use:
<asy src="foo.asy" outfile="foo.svg">
asymptote code
</asy>
It calls [asy -f svg -o outfile src] to create a SVG file.
If [outfile] attribute is given, then the given name is used to
store the result file and an <img> tag is used to display
the svg file. Else the resulting svg file is included and the
generated file is removed.
Source code is found in the file indicated with the [src] attribute
if present. Else it is the content of the [<asy>] node.
An additional attribute, [args], is handled to pass extra arguments
to the [asy] command.
The dvisvgm tool must be installed.
*)moduleXR=Xtmpl.RewritemoduleXml=Xtmpl.XmlopenStogletconcat_code=letf?locb=functionXR.Dcode->Buffer.add_stringbcode.Xtmpl.Types.text|xml->letmsg=Xtmpl.Types.loc_sprintfloc"XML code in Asymptote code: %s"(XR.to_string[xml])infailwithmsginfun?locxmls->letb=Buffer.create256inList.iter(f?locb)xmls;Buffer.contentsb;;letfun_asystogenv?locattssubs=letcode=concat_codesubsinlet(stog,path)=Engine.get_pathstogenvinlet(_,doc)=Types.doc_by_pathstogpathinletdoc_dir=Filename.dirnamedoc.Types.doc_srcinlettyp=XR.opt_att_cdata~def:"svg"atts("","type")inletid_prefix=XR.get_att_cdataatts("","prefix-svg-ids")inlet(stog,infile,finalize_src)=matchXR.get_att_cdataatts("","src")withNone->letf=Filename.temp_file"stog"".asy"inStog_base.Misc.file_of_string~file:fcode;(stog,f,(fun()->tryUnix.unlinkfwith_->()))|Somef->letf=ifFilename.is_relativefthenFilename.concatdoc_dirfelsefinletstog=Plug.add_depstogdoc(Types.Filef)in(stog,f,fun()->())intrylet(outfile,abs_outfile,inc,finalize_outfile)=matchXR.get_att_cdataatts("","outfile")withNone->iftyp<>"svg"thenfailwith(Xtmpl.Types.loc_sprintfloc"<asy>: please specify outfile attribute if file type is not 'svg'");letf=Filename.temp_file"stog_asy"".svg"in(f,f,true,(fun()->tryUnix.unlinkfwith_->()))|Somef->letabsf=ifFilename.is_relativefthenFilename.concatstog.Types.stog_outdir(Filename.concatdoc_dirf)elsefin(f,absf,false,fun()->())inletargs=XR.opt_att_cdata~def:""atts("","args")inStog_base.Misc.safe_mkdir(Filename.dirnameabs_outfile);letcom=Printf.sprintf"asy -f %s %s -o %s %s"(Filename.quotetyp)args(Filename.(quote(chop_extensionabs_outfile)))(Filename.quoteinfile)inletxml=matchSys.commandcomwith0->Log.debug(funm->m"ASY: command ok: %s"com);ifincthenbeginletxmldoc=XR.doc_from_fileabs_outfileinletxml=xmldoc.XR.elementsinletxml=matchid_prefixwithNone->xml|Someprefix->List.map(Svg.prefix_svg_idsprefix)xmlinxmlendelsebeginletatts=XR.atts_remove("","args")(XR.atts_remove("","outfile")(XR.atts_remove("","type")(XR.atts_remove("","prefix-svg-ids")(XR.atts_remove("","src")atts))))inletatts=XR.atts_one~atts("","src")[XR.cdataoutfile]in[XR.node("","img")~atts[]]end|_->Log.err(funm->m?loc"Command failed: %s"com);[]infinalize_outfile();finalize_src();(stog,xml)withFailuremsg->Log.err(funm->m?loc"%s"msg);(stog,[]);;let()=Plug.register_html_base_rule("","asy")fun_asy;;