@@ -350,6 +350,15 @@ pub struct Compiler {
350350}
351351
352352impl Compiler {
353+ fn apply_root ( & mut self , root : Option < PathBuf > ) -> PyResult < ( ) > {
354+ if let Some ( root) = root {
355+ self . world
356+ . set_root ( root)
357+ . map_err ( |msg| PyRuntimeError :: new_err ( msg. to_string ( ) ) ) ?;
358+ }
359+ Ok ( ( ) )
360+ }
361+
353362 fn apply_input ( & mut self , input : Option < Input > ) -> PyResult < ( ) > {
354363 if let Some ( input) = input {
355364 self . world
@@ -516,7 +525,7 @@ impl Compiler {
516525
517526 /// Compile a typst file to PDF
518527 #[ allow( clippy:: too_many_arguments) ]
519- #[ pyo3( name = "compile" , signature = ( input = None , output = None , format = None , ppi = None , sys_inputs = SysInputsOption :: Keep , pdf_standards = Vec :: new( ) ) ) ]
528+ #[ pyo3( name = "compile" , signature = ( input = None , output = None , format = None , ppi = None , sys_inputs = SysInputsOption :: Keep , pdf_standards = Vec :: new( ) , root = None ) ) ]
520529 fn py_compile (
521530 & mut self ,
522531 py : Python < ' _ > ,
@@ -526,7 +535,9 @@ impl Compiler {
526535 ppi : Option < f32 > ,
527536 #[ pyo3( from_py_with = extract_sys_inputs_option) ] sys_inputs : SysInputsOption ,
528537 #[ pyo3( from_py_with = extract_pdf_standards) ] pdf_standards : Vec < typst_pdf:: PdfStandard > ,
538+ root : Option < PathBuf > ,
529539 ) -> PyResult < Py < PyAny > > {
540+ self . apply_root ( root) ?;
530541 self . apply_input ( input) ?;
531542 self . apply_sys_inputs ( sys_inputs) ;
532543 if let Some ( output) = output {
@@ -592,7 +603,7 @@ impl Compiler {
592603
593604 /// Compile a typst file and return both result and warnings
594605 #[ allow( clippy:: too_many_arguments) ]
595- #[ pyo3( name = "compile_with_warnings" , signature = ( input = None , output = None , format = None , ppi = None , sys_inputs = SysInputsOption :: Keep , pdf_standards = Vec :: new( ) ) ) ]
606+ #[ pyo3( name = "compile_with_warnings" , signature = ( input = None , output = None , format = None , ppi = None , sys_inputs = SysInputsOption :: Keep , pdf_standards = Vec :: new( ) , root = None ) ) ]
596607 fn py_compile_with_warnings (
597608 & mut self ,
598609 py : Python < ' _ > ,
@@ -602,7 +613,9 @@ impl Compiler {
602613 ppi : Option < f32 > ,
603614 #[ pyo3( from_py_with = extract_sys_inputs_option) ] sys_inputs : SysInputsOption ,
604615 #[ pyo3( from_py_with = extract_pdf_standards) ] pdf_standards : Vec < typst_pdf:: PdfStandard > ,
616+ root : Option < PathBuf > ,
605617 ) -> PyResult < Py < PyAny > > {
618+ self . apply_root ( root) ?;
606619 self . apply_input ( input) ?;
607620 self . apply_sys_inputs ( sys_inputs) ;
608621 let result = py
@@ -656,15 +669,17 @@ impl Compiler {
656669 }
657670
658671 /// Query a typst document
659- #[ pyo3( name = "query" , signature = ( selector, field = None , one = false , format = None ) ) ]
672+ #[ pyo3( name = "query" , signature = ( selector, field = None , one = false , format = None , root = None ) ) ]
660673 fn py_query (
661674 & mut self ,
662675 py : Python < ' _ > ,
663676 selector : & str ,
664677 field : Option < & str > ,
665678 one : bool ,
666679 format : Option < & str > ,
680+ root : Option < PathBuf > ,
667681 ) -> PyResult < Py < PyAny > > {
682+ self . apply_root ( root) ?;
668683 py. detach ( || self . query ( selector, field, one, format) )
669684 . map ( |s| PyString :: new ( py, & s) . into ( ) )
670685 }
@@ -713,6 +728,7 @@ fn compile(
713728 ppi,
714729 SysInputsOption :: Keep ,
715730 pdf_standards,
731+ None ,
716732 )
717733}
718734
@@ -759,6 +775,7 @@ fn compile_with_warnings(
759775 ppi,
760776 SysInputsOption :: Keep ,
761777 pdf_standards,
778+ None ,
762779 )
763780}
764781
@@ -801,7 +818,7 @@ fn py_query(
801818 sys_inputs,
802819 package_path,
803820 ) ?;
804- compiler. py_query ( py, selector, field, one, format)
821+ compiler. py_query ( py, selector, field, one, format, None )
805822}
806823
807824/// Python binding to typst
0 commit comments