22
33/* jsdom rendering */
44
5- import * as Plot from "@observablehq/plot" ;
6- import * as d3 from "d3" ;
7- import * as http from "node:http" ;
8- import { JSDOM } from "jsdom" ;
9- import { generate_plot } from "./plot.js" ;
5+ import * as Plot from "@observablehq/plot"
6+ import * as d3 from "d3"
7+
8+ import * as http from "node:http"
9+ import { JSDOM } from "jsdom"
10+ import { generate_plot } from "./plot.js"
1011
1112// Create and initialize jsdom
12- const jsdom = new JSDOM ( "" ) ;
13- global . window = jsdom . window ;
14- global . document = jsdom . window . document ;
15- global . Event = jsdom . window . Event ;
16- global . Node = jsdom . window . Node ;
17- global . NodeList = jsdom . window . NodeList ;
18- global . HTMLCollection = jsdom . window . HTMLCollection ;
13+ const jsdom = new JSDOM ( "" )
14+ global . window = jsdom . window
15+ global . document = jsdom . window . document
16+ global . Event = jsdom . window . Event
17+ global . Node = jsdom . window . Node
18+ global . NodeList = jsdom . window . NodeList
19+ global . HTMLCollection = jsdom . window . HTMLCollection
1920// Make Plot and d3 available in js()
20- global . d3 = d3 ;
21- global . Plot = Plot ;
21+ global . d3 = d3
22+ global . Plot = Plot
2223
2324// jsdom plot generator
2425function jsdom_plot ( request ) {
25- request = JSON . parse ( request ) ;
26- let el = generate_plot ( request [ "spec" ] , "jsdom" ) ;
26+ request = JSON . parse ( request )
27+ let el = generate_plot ( request [ "spec" ] , "jsdom" )
2728
2829 // foreground color
29- const bg = { light : "#FFFFFF" , dark : "#000000" , current : "transparent" } ;
30+ const bg = { light : "#FFFFFF" , dark : "#000000" , current : "transparent" }
3031 // background color
31- const fg = { light : "#000000" , dark : "#FFFFFF" , current : "currentColor" } ;
32+ const fg = { light : "#000000" , dark : "#FFFFFF" , current : "currentColor" }
3233 // caption color
33- const caption = { light : "#777777" , dark : "#888888" , current : "currentColor" } ;
34- const theme = request [ "theme" ] ;
34+ const caption = {
35+ light : "#777777" ,
36+ dark : "#888888" ,
37+ current : "currentColor" ,
38+ }
39+ const theme = request [ "theme" ]
3540
3641 for ( const svg of el . tagName . toLowerCase ( ) === "svg"
3742 ? [ el ]
@@ -40,93 +45,93 @@ function jsdom_plot(request) {
4045 "http://www.w3.org/2000/xmlns/" ,
4146 "xmlns" ,
4247 "http://www.w3.org/2000/svg"
43- ) ;
48+ )
4449 svg . setAttributeNS (
4550 "http://www.w3.org/2000/xmlns/" ,
4651 "xmlns:xlink" ,
4752 "http://www.w3.org/1999/xlink"
48- ) ;
53+ )
4954 // theming
50- svg . style . color ||= fg [ theme ] ;
51- svg . style . backgroundColor ||= bg [ theme ] ;
55+ svg . style . color ||= fg [ theme ]
56+ svg . style . backgroundColor ||= bg [ theme ]
5257 }
5358 for ( const figure of el . tagName . toLowerCase ( ) === "figure"
5459 ? [ el ]
5560 : el . querySelectorAll ( "figure" ) ) {
56- figure . style . padding ||= "0px 5px 5px 5px" ;
61+ figure . style . padding ||= "0px 5px 5px 5px"
5762 // theming
58- figure . style . color ||= fg [ theme ] ;
59- figure . style . backgroundColor ||= bg [ theme ] ;
63+ figure . style . color ||= fg [ theme ]
64+ figure . style . backgroundColor ||= bg [ theme ]
6065 // pass colors to typst via attributes
61- figure . setAttribute ( "typstbg" , bg [ theme ] ) ;
62- figure . setAttribute ( "typstfg" , fg [ theme ] ) ;
63- figure . setAttribute ( "typstcaption" , caption [ theme ] ) ;
66+ figure . setAttribute ( "typstbg" , bg [ theme ] )
67+ figure . setAttribute ( "typstfg" , fg [ theme ] )
68+ figure . setAttribute ( "typstcaption" , caption [ theme ] )
6469 for ( const h2 of figure . querySelectorAll ( "h2" ) ) {
65- h2 . style . lineHeight = "28px" ;
66- h2 . style . fontSize = "20px" ;
67- h2 . style . fontWeight = "600" ;
68- h2 . style . margin = "0" ;
70+ h2 . style . lineHeight = "28px"
71+ h2 . style . fontSize = "20px"
72+ h2 . style . fontWeight = "600"
73+ h2 . style . margin = "0"
6974 }
7075 for ( const h3 of figure . querySelectorAll ( "h3" ) ) {
71- h3 . style . lineHeight = "24px" ;
72- h3 . style . fontSize = "16px" ;
73- h3 . style . fontWeight = "400" ;
74- h3 . style . margin = "0" ;
76+ h3 . style . lineHeight = "24px"
77+ h3 . style . fontSize = "16px"
78+ h3 . style . fontWeight = "400"
79+ h3 . style . margin = "0"
7580 }
7681 for ( const figcaption of figure . querySelectorAll ( "figcaption" ) ) {
77- figcaption . style . lineHeight = "20px" ;
78- figcaption . style . fontSize = "12px" ;
79- figcaption . style . fontWeight = "500" ;
80- figcaption . style . color = caption [ theme ] ;
82+ figcaption . style . lineHeight = "20px"
83+ figcaption . style . fontSize = "12px"
84+ figcaption . style . fontWeight = "500"
85+ figcaption . style . color = caption [ theme ]
8186 }
8287 }
8388
84- return el . outerHTML ;
89+ return el . outerHTML
8590}
8691
8792// Request listener for http server
8893const requestListener = function ( req , res ) {
8994 // Send back plain text
90- res . setHeader ( "Content-Type" , "text/plain" ) ;
95+ res . setHeader ( "Content-Type" , "text/plain" )
9196 switch ( req . url ) {
9297 // plot entry point
9398 case "/plot" :
94- let body = "" ;
99+ let body = ""
95100 req . on ( "data" , ( chunk ) => {
96- body += chunk . toString ( ) ;
97- } ) ;
101+ body += chunk . toString ( )
102+ } )
98103 req . on ( "end" , ( ) => {
99- let output ;
104+ let output
100105 try {
101- output = jsdom_plot ( body ) ;
106+ output = jsdom_plot ( body )
102107 } catch ( error ) {
103- res . writeHead ( 500 ) ;
104- res . end ( `Server error: ${ error . message } .` ) ;
105- return ;
108+ res . writeHead ( 500 )
109+ res . end ( `Server error: ${ error . message } .` )
110+ return
106111 }
107- res . writeHead ( 200 ) ;
108- res . end ( output ) ;
109- } ) ;
110- break ;
112+ res . writeHead ( 200 )
113+ res . end ( output )
114+ } )
115+ break
111116 // status entry point
112117 case "/status" :
113- res . writeHead ( 200 ) ;
114- res . end ( "pyobsplot" ) ;
115- break ;
118+ res . writeHead ( 200 )
119+ res . end ( "pyobsplot" )
120+ break
116121 // else
117122 default :
118- res . writeHead ( 404 ) ;
119- res . end ( "Resource not found" ) ;
123+ res . writeHead ( 404 )
124+ res . end ( "Resource not found" )
120125 }
121- } ;
126+ }
122127
123128// let OS find a free port
124- const port = 0 ;
125- const host = "localhost" ;
129+ const port = 0
130+ const host = "localhost"
126131// Launch server
127- const server = http . createServer ( requestListener ) ;
132+ const server = http . createServer ( requestListener )
128133server . listen ( port , host , ( ) => {
129134 // send selected port to stdout
130- const port = server . address ( ) . port ;
131- process . stdout . write ( port + "\n" ) ;
132- } ) ;
135+ const port = server . address ( ) . port
136+ process . stdout . write ( port + "\n" )
137+ } )
0 commit comments