
    rh,0                       S r SSKJr  SSKrSSKJr  SSKJr  SSKJr  / SQr	SS	S
.S jr
 " S S5      r " S S\5      r " S S\R                  5      r " S S\R                  5      r\S:X  a  SSKr\R"                  " \5        gg)zT
Convert a music21 object into JSON and send it to the browser for music21j to use.
    )annotationsN)Music21Exception)
freezeThaw)stream)htmljsbodyjsbodyScriptjsonr   F)modelocalc               P    [        5       nXl        X#l        UR                  U 5      $ )a$  
returns a string of data for a given Music21Object such as a Score, Note, etc. that
can be displayed in a browser using the music21j package.  Called by .show('vexflow').

>>> n = note.Note('C#4')
>>> #_DOCS_SHOW print(vexflow.toMusic21j.fromObject(n))
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <!-- for MSIE 10 on Windows 8 -->
    <meta http-equiv="X-UA-Compatible" content="requiresActiveX=true"/>
    <title>Music21 Fragment</title>
    <script data-main='https://www.music21.org/music21j/src/music21'
            src='https://www.music21.org/music21j/ext/require/require.js'></script>
    <script>
        require(['music21'], function() {
            var pickleIn = '{"m21Version": {"py/tuple": [1, 9, 2]}, "stream":
{"_mutable": true, "_activeSite": null, "' +
'_priority": 0, "_elements": [], "_cache": {}, "definesExplicitPageBreaks":
false, "_unlinkedDuration": null, "' +
'id": ..., "_duration": null, "py/object": "music21.stream.Stream",
"streamStatus": {"py/object": "music' +
'21.stream.streamStatus.StreamStatus", "_enharmonics": null,
"_dirty": null, "_concertPitch": null, "_accidentals"' +
': null, "_ties": null, "_rests": null, "_ornaments": null,
"_client": null, "_beams": null, "_measures": nu' +
...
'd": null}, "definesExplicitSystemBreaks": false, ...}}';
            var jpc = new music21.fromPython.Converter();
            streamObj = jpc.run(pickleIn);
            streamObj.renderOptions.events.resize = "reflow";
            streamObj.appendNewCanvas();
        });
    </script>
<BLANKLINE>
</head>
<body>
</body>
</html>

* Changed in v8: mode and useLocal are keyword only.
)VexflowPicklerr   useLocal
fromObject)
thisObjectr   r   convs       T/home/james-whalen/.local/lib/python3.13/site-packages/music21/vexflow/toMusic21j.pyr   r      s&    Z DIM??:&&    c                  p    \ rS rSrSrSrSrSrS rSS jr	SS	 jr
SS
 jrSS jrSS jrSS jrSS jrSrg)r   R   a  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <!-- for MSIE 10 on Windows 8 -->
        <meta http-equiv="X-UA-Compatible" content="requiresActiveX=true"/>
        <title>{title}</title>
        {loadM21Template}
        {jsBodyScript}
    </head>
    <body>
    </body>
    </html>
    z<script>
{jsBody}
</script>zrequire(['music21'], function() {{
                var pickleIn = {pickleOutput};
                var jpc = new music21.fromPython.Converter();
                streamObj = jpc.run(pickleIn);
                {callback}
            }});z9<script data-main='{m21URI}' src='{requireURI}'></script>c                <    SSSSSSS.U l         SU l        S	U l        g )
Nz{"py/object": "hello"}z,https://www.music21.org/music21j/src/music21z7https://www.music21.org/music21j/ext/require/require.jszPstreamObj.renderOptions.events.resize = "reflow";
		streamObj.appendNewCanvas();/file:///Users/Cuthbert/git/music21j/src/music21:file:///Users/Cuthbert/git/music21j/ext/require/require.js)pickleOutputm21URI
requireURIcallbackm21URIlocalrequireURIlocalr   Fdefaultsr   r   )selfs    r   __init__VexflowPickler.__init__k   s2    4DS1 M[
 	r   Nc                    Uc  U R                   nUR                  SL a'  [        R                  " 5       nUR	                  U5        OUnU R                  X2S9$ )NF)r   )r   isStreamr   Streamappend
fromStream)r"   r   r   	retStreams       r   r   VexflowPickler.fromObject{   sM    <99D%'IZ("Iy44r   c                    / n[        S[        U5      U5       H  nUR                  SXXB-    -   S-   5        M      SR                  U5      $ )Nr   'z + 
    )rangelenr(   join)r"   
jsonString	chunkSizeallJSONListis        r   splitLongJSONVexflowPickler.splitLongJSON   sN    q#j/95AsZ!-%@@3FG 6,,r   c                    Uc  U R                   nU R                  SL a"  U R                  R                  US   US   S9nU$ U R                  R                  US   US   S9nU$ )a  
Gets the <script> tag for loading music21 from require.js

>>> vfp = vexflow.toMusic21j.VexflowPickler()
>>> vfp.getLoadTemplate()
"<script data-main='https://www.music21.org/music21j/src/music21'
    src='https://www.music21.org/music21j/ext/require/require.js'></script>"

>>> d = {'m21URI': 'file:///tmp/music21', 'requireURI': 'http://requirejs.com/require.js'}
>>> vfp.getLoadTemplate(d)
"<script data-main='file:///tmp/music21' src='http://requirejs.com/require.js'></script>"
Fr   r   )r   r   r   r   )r!   r   loadM21Templateformat)r"   urlsloadM21formatteds      r   getLoadTemplateVexflowPickler.getLoadTemplate   s     <==D==E!#33::$x.FJ<FX  ;  [    $33::$}BUFJK\F]  ;  `  r   c                x    Uc  U R                   nU R                  X5      nU R                  R                  US9nU$ )a  
Get the <script>...</script> tag to render the JSON

>>> vfp = vexflow.toMusic21j.VexflowPickler()
>>> print(vfp.getJSBodyScript('{"hi": "hello"}'))
   <script>
        require(['music21'], function() {
            var pickleIn = {"hi": "hello"};
            var jpc = new music21.fromPython.Converter();
            streamObj = jpc.run(pickleIn);
            streamObj.renderOptions.events.resize = "reflow";
        streamObj.appendNewCanvas();
        });
    </script>
)jsBody)r!   	getJSBodyjsBodyScriptr9   )r"   	dataSplitr!   r?   rA   s        r   getJSBodyScriptVexflowPickler.getJSBodyScript   s@      }}H	4((//v/>r   c                d    Uc  U R                   nOUnU R                  R                  UUS   S9nU$ )a  
Get the javascript code without the <script> tags to render the JSON

>>> vfp = vexflow.toMusic21j.VexflowPickler()
>>> print(vfp.getJSBody('{"hi": "hello"}'))
        require(['music21'], function() {
            var pickleIn = {"hi": "hello"};
            var jpc = new music21.fromPython.Converter();
            streamObj = jpc.run(pickleIn);
            streamObj.renderOptions.events.resize = "reflow";
        streamObj.appendNewCanvas();
        });
r   )r   r   )r!   r?   r9   )r"   rB   r!   dr?   s        r   r@   VexflowPickler.getJSBody   s?     AA##-.z] $ <r   c                    Uc  U R                   nOUnU R                  U5      nU R                  X5      nU R                  R	                  UUUS9nU$ )ap  
Get the complete HTML page to pass to the browser:

>>> vfp = vexflow.toMusic21j.VexflowPickler()
>>> print(vfp.getHTML('{"hi": "hello"}', 'myPiece'))
   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <!-- for MSIE 10 on Windows 8 -->
    <meta http-equiv="X-UA-Compatible" content="requiresActiveX=true"/>
    <title>myPiece</title>
    <script data-main='https://www.music21.org/music21j/src/music21'
            src='https://www.music21.org/music21j/ext/require/require.js'></script>
    <script>
    require(['music21'], function() {
                    var pickleIn = {"hi": "hello"};
                    var jpc = new music21.fromPython.Converter();
                    streamObj = jpc.run(pickleIn);
                    streamObj.renderOptions.events.resize = "reflow";
                streamObj.appendNewCanvas();
                });
    </script>
    </head>
    <body>
    </body>
    </html>
)titler8   rA   )r!   r<   rC   templateHtmlr9   )r"   rB   rI   r!   rF   loadM21FormattedrA   	formatteds           r   getHTMLVexflowPickler.getHTML   se    < AA//2++I9%%,,5AQ>J - L	 r   c                   Uc  U R                   nUR                  b1  UR                  R                  S:w  a  UR                  R                  nOSn[        R                  " U5      nUR                  SS9nU R                  U5      nUS:X  a  U$ US:X  a  U$ US:X  a  U R                  U5      $ US:X  a  U R                  U5      $ US	:X  a  U R                  Xc5      $ [        S
U< 35      e)N zMusic21 Fragment
jsonpickle)fmtr
   	jsonSplitr   r	   r   zCannot deal with mode: )r   metadatarI   r   StreamFreezerwriteStrr5   r@   rC   rM   VexflowToM21JException)r"   
thisStreamr   rI   sfdatarB   s          r   r)   VexflowPickler.fromStream   s    <99D*z/B/B/H/HB/N''--E&E%%j1 {{|{,&&t,	6>K[ X>>),,^#''	22V^<<	11(+B4()KLLr   r    )N)n   )NN)__name__
__module____qualname____firstlineno__rJ   rA   r?   r8   r#   r   r5   r<   rC   r@   rM   r)   __static_attributes__ r   r   r   r   R   sI    L 7LF VO 5- 2,,'RMr   r   c                      \ rS rSrSrg)rW   i  rb   Nr]   r^   r_   r`   ra   rb   r   r   rW   rW         r   rW   c                      \ rS rSrSrg)Testi  rb   Nrd   rb   r   r   rg   rg     re   r   rg   c                      \ rS rSrS rSrg)TestCuthberti  c                ~   SSK Jn  SSK Jn  UR                  5       nUR	                  S5      R                  SS5      n[        5       nSUR                  S'   S	UR                  S
'   UR                  U5      nUR                  S5      n[        USSS9 nUR                  U5        SSS5        g! , (       d  f       g= f)z#
test a local version of this mess
r   )corpus)environmentzluca/gloria      r   r   r   r   z.htmlwzutf-8)encodingN)music21rk   rl   Environmentparsemeasuresr   r!   r   getTempFileopenwrite)	r"   rk   rl   environLocalsvfprZ   fpfs	            r   testCuthbertLocalTestCuthbert.testCuthbertLocal  s     	#'"..0LL'00B7 !RX%a\"~~a %%g."cG,GGDM -,,s   B..
B<rb   N)r]   r^   r_   r`   r}   ra   rb   r   r   ri   ri     s    r   ri   __main__)__doc__
__future__r   unittestmusic21.exceptions21r   rq   r   r   supportedDisplayModesr   r   rW   TestCaserg   ri   r]   mainTestrb   r   r   <module>r      s    #  1    $* 0'f@M @MF	- 		8 	8$$ . zT r   