diff --git a/src/doci.xqm b/src/doci.xqm index d853645..f0c7f12 100644 --- a/src/doci.xqm +++ b/src/doci.xqm @@ -4,12 +4,12 @@ @author andy bunce :) module namespace doci = 'urn:doci'; +(: default line seperator if none found in text :) +declare variable $doci:default-separator:=file:line-separator(); declare record doci:doci( lines as xs:string+, - length as xs:integer, - separator? as xs:string, - starts? as xs:integer+ + separator? as xs:string ); declare record doci:line( @@ -41,37 +41,89 @@ declare record doci:Range( end as doci:Position ); +(: +@see https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_didChange +:) +declare record doci:TextDocumentContentChangeEvent( + text as xs:string, + range? as doci:Range +); + (: create new doci from string :) declare function doci:build($text as xs:string) as doci:doci{ let $ls:=doci:separator($text ) - let $lines:=tokenize($text, '(\r\n?|\n\r?)') - let $starts:=hof:scan-left($lines, 0, - fn($res,$line){$res+string-length($ls)+string-length($line)} - ) + let $lines:= doci:split-lines($text) + return doci:doci( lines:= $lines, - length:= string-length($text), - separator:=$ls, - starts:= $starts + separator:=$ls ) }; -(: line separator, assume all same:) +(: return full text :) +declare function doci:text($doci as doci:doci) +as xs:string{ + string-join($doci?lines,$doci?separator) +}; +(: number of lines:) +declare function doci:lines($doci as doci:doci) +as xs:string{ + $doci?lines=>count() +}; + +(: detect line separator, assumes all the same:) declare function doci:separator($text as xs:string) as xs:string?{ switch () { case contains($text," ") return " " case contains($text," ") return " " case contains($text," ") return " " - default return () + default return $doci:default-separator } }; +(: apply change:) +declare function doci:change($doci as doci:doci,$change as doci:TextDocumentContentChangeEvent) +as doci:doci{ + let $lines:= doci:split-lines($change?text) + return if(empty($change?range)) + then doci:doci($lines,$doci?separator) + else let $range:=$change?range + let $sline:=$range?start?line + let $eline:=$range?end?line + return switch(){ + case $sline eq $eline and count($lines) eq 1 + return let $line:=$doci?lines[$sline+1] + let $new:=substring($line,1,$range?start?character) + || $lines || + substring($line,1+$range?end?character) + + let $ulines:=( $doci?lines[position() le $sline], + $new, + $doci?lines[position() gt 1+$sline] + ) + return doci:doci($ulines, $doci?separator) + + default return error(#doci:change,"oh") + } +}; + +declare function doci:split-lines($text as xs:string) +as xs:string+{ + if($text eq "") + then $text + else tokenize($text, '(\r\n?|\n\r?)') +}; (: line from pos :) declare function doci:lineAt($doci as doci:doci,$pos as xs:integer) -{ -let $line:=do-until( +as doci:line { +let $starts:=hof:scan-left($doci?lines, 0, + fn($res,$line){$res+string-length($doci?seperator)+string-length($line)} + ) +let $line:=if($pos gt $doci?length) + then error(#doci:range,"pos beyond range") + else do-until( {"min":1,"max":count($doci?lines)}, fn($r){ let $mid:=round(($r?min+$r?max) div 2,0,"away-from-zero") @@ -81,5 +133,12 @@ let $line:=do-until( }, fn($r){$r?max eq $r?min} )?max - return $line -}; \ No newline at end of file + + return doci:line( + number:= $line, + text:= $doci?lines[$line], + from:= $doci?starts[$line], + to:= -1 + ) +}; + diff --git a/test/doci.xq b/test/doci.xq index f1e97e4..64eb4fd 100644 --- a/test/doci.xq +++ b/test/doci.xq @@ -7,4 +7,4 @@ ddddd `; -doci:build($long) +doci:build($long)?length \ No newline at end of file diff --git a/test/resources/doc1.txt b/test/resources/doc1.txt new file mode 100644 index 0000000..ad947a8 --- /dev/null +++ b/test/resources/doc1.txt @@ -0,0 +1,529 @@ +xquery version '3.1'; +(:~ +It requires the Pdfbox jars to be on the classpath, or a EXPath package (xar) installation. +
style: Roman, Decimal etc, start: the index to start from (default 1) and prefix: an optional string to prefix to the page label e.g "Vol1:"$pdf to filesystem at $savepath , returns $savepath :)
+declare function pdfbox:pdf-save($pdf as item(),$savepath as xs:string)
+as xs:string{
+ PDDocument:save($pdf, File:new($savepath)),$savepath
+};
+
+(:~ Create binary representation (xs:base64Binary) of $pdf object
+@param $pdf pdf object, created by pdfbox:open
+@see #pdfbox:open
+@see #pdfbox:with-pdf
+:)
+declare function pdfbox:binary($pdf as item())
+as xs:base64Binary{
+ let $bytes:=Q{java:java.io.ByteArrayOutputStream}new()
+ let $_:=PDDocument:save($pdf, $bytes)
+ return Q{java:java.io.ByteArrayOutputStream}toByteArray($bytes)
+ =>convert:integers-to-base64()
+};
+
+(:~ Release any resources related to $pdf:)
+declare function pdfbox:close($pdf as item())
+as empty-sequence(){
+ (# db:wrapjava void #) {
+ PDDocument:close($pdf)
+ }
+};
+
+(:~ Number of pages in PDF:)
+declare function pdfbox:number-of-pages($pdf as item())
+as xs:integer{
+ PDDocument:getNumberOfPages($pdf)
+};
+
+(:~ Pdf page as image (zero is cover)
+options.format="bmp jpg png gif" etc, options.scale= 1 is 72 dpi?? :)
+declare function pdfbox:page-render($pdf as item(),$pageNo as xs:integer,$options as map(*))
+as xs:base64Binary{
+ let $options := map:merge(($options,map{"format":"jpg","scale":1}))
+ let $bufferedImage := PDFRenderer:new($pdf)
+ =>PDFRenderer:renderImage($pageNo,$options?scale)
+ let $bytes := Q{java:java.io.ByteArrayOutputStream}new()
+ let $_ := Q{java:javax.imageio.ImageIO}write($bufferedImage ,$options?format, $bytes)
+ return Q{java:java.io.ByteArrayOutputStream}toByteArray($bytes)
+ =>convert:integers-to-base64()
+
+};
+
+
+(:~ Defines a map from property names to evaluation method.
+ Keys are property names,
+ values are sequences of functions to get property value starting from a $pdf object.
+:)
+declare %private variable $pdfbox:property-map:=map{
+ "#pages": pdfbox:number-of-pages#1,
+
+ "#bookmarks": pdfbox:number-of-bookmarks#1,
+
+ "#labels": pdfbox:number-of-labels#1,
+
+ "specification":pdfbox:specification#1,
+
+ "title": (PDDocument:getDocumentInformation#1,
+ PDDocumentInformation:getTitle#1) ,
+
+ "author": (PDDocument:getDocumentInformation#1,
+ PDDocumentInformation:getAuthor#1 ),
+
+ "creator": (PDDocument:getDocumentInformation#1,
+ PDDocumentInformation:getCreator#1),
+
+ "producer": (PDDocument:getDocumentInformation#1,
+ PDDocumentInformation:getProducer#1),
+
+ "subject": (PDDocument:getDocumentInformation#1,
+ PDDocumentInformation:getSubject#1),
+
+ "keywords": (PDDocument:getDocumentInformation#1,
+ PDDocumentInformation:getKeywords#1),
+
+ "creationDate": (PDDocument:getDocumentInformation#1,
+ PDDocumentInformation:getCreationDate#1,
+ pdfbox:gregToISO#1),
+
+ "modificationDate": (PDDocument:getDocumentInformation#1,
+ PDDocumentInformation:getModificationDate#1,
+ pdfbox:gregToISO#1),
+
+ "labels": pdfbox:labels-as-string#1
+};
+
+(:~ Defined property names, sorted :)
+declare function pdfbox:property-names()
+as xs:string*{
+ $pdfbox:property-map=>map:keys()=>sort()
+};
+
+(:~ Return the value of $property for $pdf :)
+declare function pdfbox:property($pdf as item(),$property as xs:string)
+as item()*{
+ let $fns:= $pdfbox:property-map($property)
+ return if(exists($fns))
+ then fold-left($fns,
+ $pdf,
+ function($result,$this as function(*)){$result!$this(.)})
+ else error(xs:QName('pdfbox:property'),concat("Property '",$property,"' not defined."))
+};
+
+
+(:~ summary CSV style info for named $properties for PDFs in $pdfpaths
+@see https://docs.basex.org/main/CSV_Functions#xquery
+:)
+declare function pdfbox:report($pdfpaths as item()*, $properties as xs:string*)
+as map(*){
+ map{"names": array{"path",$properties},
+
+ "records": for $path in $pdfpaths
+ let $name:=if($path instance of xs:base64Binary) then "binary" else $path
+ return try{
+ let $pdf:=pdfbox:open($path)
+ return (fold-left($properties,
+ array{$name},
+ function($result as array(*),$prop as xs:string){
+ array:append($result, string(pdfbox:property($pdf, $prop)))}
+ ), pdfbox:close($pdf)
+ )
+ } catch *{
+ fold-left($properties,
+ array{$name},
+ function($result as array(*),$prop as xs:string){
+ array:append($result, "#ERROR")}
+ )
+ }
+
+ }
+};
+
+(:~ summary CSV style info for all properties for $pdfpaths
+:)
+declare function pdfbox:report($pdfpaths as xs:string*)
+as map(*){
+ pdfbox:report($pdfpaths,pdfbox:property-names())
+};
+
+(:~ Convenience function to save report() data to file :)
+declare function pdfbox:report-save($data as map(*),$dest as xs:string)
+as empty-sequence(){
+ let $opts := map { "format":"xquery", "header":"yes", "separator" : "," }
+ return file:write-text($dest,csv:serialize($data,$opts))
+};
+
+(:~ The number of outline items defined in $pdf :)
+declare function pdfbox:number-of-bookmarks($pdf as item())
+as xs:integer{
+ let $xml:=pdfbox:outline-xml($pdf)
+ return count($xml//bookmark)
+};
+
+(:~ XMP metadata as "RDF" document
+@note usually rdf:RDF root, but sometimes x:xmpmeta
+:)
+declare function pdfbox:metadata($pdf as item())
+as document-node(element(*))?
+{
+ let $m:=PDDocument:getDocumentCatalog($pdf)
+ =>PDDocumentCatalog:getMetadata()
+ return if(exists($m))
+ then
+ let $is:=PDMetadata:exportXMPMetadata($m)
+ return pdfbox:do-until(
+ map{"n":0,"data":""},
+
+ function($input,$pos ) { pdfbox:read-stream($is,$input?data)},
+
+ function($output,$pos) { $output?n eq -1 }
+ )?data=>parse-xml()
+ else ()
+};
+
+(:~ read next block from XMP stream :)
+declare %private function pdfbox:read-stream($is,$read as xs:string)
+as map(*){
+ let $blen:=4096
+ let $buff:=Q{java:java.util.Arrays}copyOf(array{xs:byte(0)},$blen)
+ let $n:= COSInputStream:read($is,$buff,xs:int(0),xs:int($blen))
+ let $data:=convert:integers-to-base64(subsequence($buff,1,$n))=>convert:binary-to-string()
+ return map{"n":$n, "data": $read || $data}
+};
+
+(:~ Return outline for $pdf as map()* :)
+declare function pdfbox:outline($pdf as item())
+as map(*)*{
+ (# db:wrapjava some #) {
+ let $outline:=
+ PDDocument:getDocumentCatalog($pdf)
+ =>PDDocumentCatalog:getDocumentOutline()
+
+ return if(exists($outline))
+ then pdfbox:outline($pdf,PDOutlineItem:getFirstChild($outline))
+ else ()
+ }
+};
+
+(:~ return bookmark info for children of $outlineItem as seq of maps :)
+declare %private function pdfbox:outline($pdf as item(),$outlineItem as item()?)
+as map(*)*{
+ let $find as map(*):=pdfbox:outline_($pdf ,$outlineItem)
+ return map:get($find,"list")
+};
+
+(:~ outline helper. BaseX bug 10.7? error if inlined in outline :)
+declare %private function pdfbox:outline_($pdf as item(),$outlineItem as item()?)
+as map(*){
+ pdfbox:do-until(
+
+ map{"list":(),"this":$outlineItem},
+
+ function($input,$pos ) {
+ let $bookmark:=$input?this
+ let $bk:=map{
+ "index": PDOutlineItem:findDestinationPage($bookmark,$pdf)=>pdfbox:find-page($pdf),
+ "title": (# db:checkstrings #) {PDOutlineItem:getTitle($bookmark)}
+ }
+
+ let $bk:= if(PDOutlineItem:hasChildren($bookmark))
+ then let $kids:=pdfbox:outline($pdf,PDOutlineItem:getFirstChild($bookmark))
+ return map:merge(($bk,map:entry("children",$kids)))
+ else $bk
+ return map{
+ "list": ($input?list, $bk),
+ "this": PDOutlineItem:getNextSibling($input?this)}
+ },
+
+ function($output,$pos) { empty($output?this) }
+ )
+};
+
+(:~ PDF outline in xml format :)
+declare function pdfbox:outline-xml($pdf as item())
+as element(outline)?{
+ let $outline:=pdfbox:outline($pdf)
+ return if(exists($outline))
+ then