9
9
import warnings
10
10
import tempfile
11
11
import typst
12
- from bs4 import BeautifulSoup
13
12
from pathlib import Path
14
13
from subprocess import PIPE , Popen , SubprocessError
15
14
from typing import Any , Optional , Union
23
22
AVAILABLE_THEMES ,
24
23
DEFAULT_THEME ,
25
24
MIN_NPM_VERSION ,
25
+ bundler_output_dir
26
26
)
27
27
from pyobsplot .widget import ObsplotWidget
28
28
@@ -400,65 +400,24 @@ def save_to_file(self, path: str, res: HTML) -> None:
400
400
with open (path , "w" , encoding = "utf-8" ) as f :
401
401
self .render_typst (str (res .data ), f .name )
402
402
403
- @staticmethod
404
- def shift_svg (svg ):
405
- soup = BeautifulSoup (str (svg ), "xml" )
406
- svg = soup .svg
407
- if "viewBox" in svg .attrs :
408
- x , y , width , height = map (int , svg .attrs ["viewBox" ].split ())
409
- if x != 0 or y != 0 :
410
- g = soup .new_tag ("g" , transform = f"translate({ - x } , { - y } )" )
411
- g .extend (svg .contents )
412
- svg .clear ()
413
- svg .append (g )
414
- svg .attrs ["viewBox" ] = f"0 0 { width } { height } "
415
- return str (svg )
416
-
417
403
def render_typst (self , html : str , path : str ) -> None :
418
404
path_obj = Path (path )
419
405
ext = "" .join (path_obj .suffixes )
420
- stem = str (path_obj .name ).removesuffix ("" .join (path_obj .suffixes ))
421
406
422
407
with tempfile .TemporaryDirectory () as tmpdirname :
423
- soup = BeautifulSoup (html , "xml" )
424
- figure = soup .find ("figure" , recursive = False )
425
- swatches = []
426
- plots = []
427
- for i , swatch in enumerate (figure .find_all ("div" , recursive = False )):
428
- new_swatch = []
429
- for j , svg in enumerate (swatch .find_all ("svg" , recursive = True )):
430
- with open (f"{ tmpdirname } /{ stem } _{ i } _{ j } .svg" , "w" ) as f :
431
- f .write (ObsplotTypstCreator .shift_svg (str (svg )))
432
- new_swatch .append (
433
- {"file" : f"{ stem } _{ i } _{ j } .svg" , "width" : svg .attrs ["width" ], "height" : svg .attrs ["height" ], "text" : svg .next_sibling }
434
- )
435
- swatches .append (new_swatch )
436
- for i , svg in enumerate (figure .find_all ("svg" , recursive = False )):
437
- with open (f"{ tmpdirname } /{ stem } _{ i } .svg" , "w" , encoding = 'utf-8' ) as f :
438
- f .write (ObsplotTypstCreator .shift_svg (str (svg )))
439
- plots .append ({"file" : f"{ stem } _{ i } .svg" , "width" : svg .attrs ["width" ], "height" : svg .attrs ["height" ]})
440
- max_width = max (int (svg ["width" ]) for svg in plots )
441
- typeset = (
442
- f'#set text(\n font: "{ self .font } ",\n size: { self .font_size } pt,\n fallback: false)\n '
443
- + f"#set page(\n width: { max_width + 2 * self .margin } pt,\n height: auto,\n margin: (x: { self .margin } pt, y: { self .margin } pt),\n )\n "
444
- )
445
- if title := figure .find ("h2" ):
446
- typeset += f"= { title .text } "
447
- if subtitle := figure .find ("h3" ):
448
- typeset += f"\n { subtitle .text } "
449
- typeset += "\n \n "
450
- for swatch in swatches :
451
- typeset += "#{\n set align(horizon)\n stack(\n dir: ltr,\n spacing: 10pt,\n "
452
- for el in swatch :
453
- typeset += f' image("{ el ["file" ]} ", width: { el ["width" ]} pt),\n '
454
- typeset += f' "{ el ["text" ]} ",\n '
455
- typeset += ")}\n \n "
456
- typeset += "#v(-10pt)\n " .join ([f'#image("{ plot ["file" ]} ", width: { plot ["width" ]} pt)\n ' for plot in plots ])
457
-
458
- if caption := figure .find ("figcaption" ):
459
- typeset += f"\n { caption .text } "
460
-
461
- with open (f"{ tmpdirname } /{ stem } .typ" , "w" ) as f :
462
- f .write (typeset )
463
-
464
- typst .compile (f"{ tmpdirname } /{ stem } .typ" , output = path , ppi = self .dpi , format = ext [1 :])
408
+ with open (f"{ tmpdirname } /jsdom.html" , "w" ) as f :
409
+ f .write (html )
410
+ shutil .copy (bundler_output_dir / "template.typ" , f"{ tmpdirname } /template.typ" )
411
+ with open (f"{ tmpdirname } /input.typ" , "w" ) as f :
412
+ f .write (f"""
413
+ #import "template.typ": obsplot
414
+
415
+ #show: obsplot(
416
+ "jsdom.html",
417
+ margin: { self .margin } 4pt,
418
+ font: "{ self .font } ",
419
+ font-size: { self .font_size } pt,
420
+ )
421
+ """ )
422
+
423
+ typst .compile (f"{ tmpdirname } /input.typ" , output = path , ppi = self .dpi , format = ext [1 :])
0 commit comments