123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125(*
* Copyright (c) 2018 Thomas Gazagnaire <thomas@gazagnaire.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)letsrc=Logs.Src.create"ocaml-mdx"moduleLexer_mdx=Lexer_mdxmoduleLog=(valLogs.src_logsrc:Logs.LOG)moduleOutput=OutputmoduleCram=CrammoduleDeprecated=DeprecatedmoduleDocument=DocumentmoduleToplevel=ToplevelmodulePart=PartmoduleBlock=BlockmoduleMli_parser=Mli_parsermoduleCompat=CompatmoduleUtil=UtilmodulePrelude=PreludemoduleSyntax=SyntaxmoduleLabel=LabelmoduleDep=DepmoduleOcaml_env=Ocaml_envmoduleStable_printer =Stable_printerincludeDocumentopenUtil.Result.Infixletsection_of_line=function|Section s->Somes|Text_->None|Blockb->b.sectionletfilter_sectionre(t:t)=matchList.filter(funl->matchsection_of_linelwith|None->false|Some(_,s)->Re.execpres)twith|[]->None|l->Somelletparsel=letresults=List.map(function|`Textt->Ok(Textt)|`Sections->Ok(Sections)|`Blockrb->let* b=Block.from_rawrbinOk (Blockb))linletok,errors=Util.Result.List.splitresultsinmatcherrorswith[]->Okok|_->Error(List.concaterrors)letparse_lexbufsyntaxMisc.{string;lexbuf}=match syntax with|Syntax.Mli->Mli_parser.parse_mlistring|Syntax.Mld-> Mli_parser.parse_mldstring|Markdown->Util.Result.to_error_list@@Lexer_mdx.markdown_tokenlexbuf>>=parse|Cram ->Util.Result.to_error_list@@Lexer_mdx.cram_tokenlexbuf>>=parseletparse_file syntaxf=Misc.load_file~filename:f|>parse_lexbufsyntaxletof_stringsyntaxs=match syntaxwith|Syntax.Mli->Mli_parser.parse_mlis|Syntax.Mld->Mli_parser.parse_mlds|Syntax.Markdown|Syntax.Cram->Misc.{lexbuf=Lexing.from_strings;string=s}|>parse_lexbufsyntaxletdump_lineppf(l:line)=match lwith|Blockb->Fmt.pfppf"Block %a"Block.dumpb|Section(d,s)->Fmt.pfppf"Section (%d, %S)"ds|Texts->Fmt.pfppf"Text %S"sletdump=Fmt.Dump.listdump_linetypeexpect_result=Identical|Differsletrun_str~syntax~ffile=letl=Misc.load_file~filename:fileinlet+items=parse_lexbufsyntaxlinLog.debug(funl->l"run @[%a@]"dumpitems);letcorrected=fl.stringitemsinlethas_changed=corrected<>l.stringinletresult=ifhas_changedthenDifferselse Identicalin(result,corrected)letwrite_file~outfilecontent=letoc=open_out_binoutfile inoutput_string occontent;close_out ocletrun_to_stdout ?(syntax=Syntax.Markdown)~finfile=let+_,corrected =run_str~syntax~finfileinprint_string correctedletrun_to_file?(syntax=Syntax.Markdown)~f~outfileinfile=let+_,corrected =run_str~syntax~finfileinwrite_file ~outfilecorrectedletrun ?(syntax =Syntax.Markdown)?(force_output=false)~finfile=letoutfile=infile^".corrected" inlet+test_result,corrected=run_str~syntax~finfileinmatch(force_output,test_result)with|true,_|false,Differs->write_file~outfilecorrected|false,Identical ->ifSys.file_existsoutfilethenSys.removeoutfile