GuntherRademacher / rr

RR - Railroad Diagram Generator
Apache License 2.0
467 stars 51 forks source link

[question] batch mode via http request? #35

Closed apb2006 closed 4 months ago

apb2006 commented 4 months ago

I am using rr in batch mode to process a set of ebnf files as suggested in the readme. java -jar rr.war grammar.ebnf It works fine but is a little slow when processing many files due to the Java startup time.

Is there a way to POST an ebnf (and maybe options) to the RR server and directly get back the xhtml doc? I am looking for something similar to the REx web page https://www.bottlecaps.de/rex/

mingodad commented 4 months ago

See here https://meimporta.eu/tree-sitter/json2ebnf.html how I did following the indications from @GuntherRademacher :

<form method="POST" action="https://bottlecaps.de/rr/ui" enctype="multipart/form-data" target="_blank">
<input type="hidden" name="name" value="compilation">
<input type="hidden" name="task" value="VIEW">
<input type="hidden" name="options" value="factoring">
<input type="hidden" name="options" value="eliminaterecursion">
<input type="hidden" name="options" value="inline">
<input type="hidden" name="options" value="keep">
<input type="hidden" name="uri" value="">
<p>
Or press <input type="submit" value="Show Railroad Diagram"> to automatically send it to https://www.bottlecaps.de/rr/ui .
</p>
<textarea id="gramarEBNF" name="text"></textarea>
</form>
GuntherRademacher commented 4 months ago

See here https://meimporta.eu/tree-sitter/json2ebnf.html

Note that the base domain bottlecaps.de is not currently usable, but subdomains are (though now in IPv6-space). So several URLs on that page are broken and should be prefixed with www.

GuntherRademacher commented 4 months ago

Is there a way to POST an ebnf (and maybe options) to the RR server and directly get back the xhtml doc?

It is not really made for this, but in my tests scripts (for Windows) I have something like this:

set port=8001
set url=http://localhost:%port%

:: start local RR server
start java -jar rr.war -gui -port:%port%

:: process EBNF to XHTML
curl -s -F text=@%1 -o %~n1.xhtml -F task=VIEW -F frame=diagram -F width=992 %url%/ui

:: stop local RR server
curl -s %url%/stop | grep -v "^OK$"

which does about the same as:

:: process EBNF to XHTML
java -jar rr.war -out:%~n1.xhtml %1

Options might be added using the options argument.

I hope you can do it like that, running RR locally. Please do not involve my server in any automated build processes...

apb2006 commented 4 months ago

Thanks @mingodad and @GuntherRademacher

I hope you can do it like that, running RR locally. Please do not involve my server in any automated build processes...

Understood.

This BaseX Xquery is working for me

(:~ local RR server :)
declare variable $rr-server := "http://localhost:7777/rr/ui";

(:~ post form data in $fields to $url :)
declare function local:post-form($fields as map(*),$url as xs:string)
as item(){
  let $names:= map:keys($fields) 
  let $req:=<http:request method='POST'>
              <http:multipart media-type='multipart/form-data'>{
                $names!(
                  <http:header name='content-disposition' value='form-data; name="{.} "'/>,
                  <http:body media-type='application/octet-stream'/>
                )
             }</http:multipart>
           </http:request>
  let $result:= http:send-request($req,$url,$names!$fields(.))
  return if($result[1]/@status eq "200")
         then $result[2]
         else error(xs:QName("local:post-form"),$result[1]/@message)        
};

let $ebnf:=file:read-text("..")
let $fields:=map{"text":$ebnf,
                           "task":"VIEW",
                           "frame":"diagram",
                           "width":992}
let $rr:=local:post-form($fields,$rr-server)
let $t:=xslt:transform-text($rr,"toc.xsl")
return file:write-text("..",$t)