B
    £'=[_
  ã               @   sb   d Z ddlmZmZmZmZmZmZmZ ddlm	Z	 dd„ Z
ddd	„Zd
d„ Zdd„ Zdd„ ZdS )z8Code for converting notebooks to and from the v2 format.é   )Únew_code_cellÚnew_text_cellÚnew_worksheetÚnew_notebookÚ
new_outputÚnbformatÚnbformat_minoré    )Úv2c             C   st   t | tƒr.xd|  ¡ D ]\}}t|ƒ| |< qW nBt | tƒr\x6t| ƒD ]\}}t|ƒ| |< qBW nt | tƒrp|  d¡} | S )z^There should be no bytes objects in a notebook

    v2 stores png/jpeg as b64 ascii bytes
    Úascii)Ú
isinstanceÚdictÚitemsÚ_unbytesÚlistÚ	enumerateÚbytesÚdecode)ÚobjÚkÚvÚi© r   ú2lib/python3.7/site-packages/nbformat/v3/convert.pyr      s    



r   é   c             C   s†   |dkrVt | _ t| _d| _t| ƒ} x.| d D ]"}x|d D ]}| di ¡ q:W q,W | S |dkrv|tkrl|| _t| _| S td| ƒ‚dS )aP  Convert a notebook to v3.

    Parameters
    ----------
    nb : NotebookNode
        The Python representation of the notebook to convert.
    from_version : int
        The original version of the notebook to convert.
    from_minor : int
        The original minor version of the notebook to convert (only relevant for v >= 3).
    r   Ú
worksheetsÚcellsZmetadataé   zZCannot convert a notebook directly from v%s to v3.  Try using the nbformat.convert module.N)r   r   Zorig_nbformatr   Ú
setdefaultZorig_nbformat_minorÚ
ValueError)ÚnbZfrom_versionZ
from_minorÚwsÚcellr   r   r   Úupgrade   s     r#   c             C   s*   d| _ |  dd¡}d| d | j | _dS )z-turn heading cell into corresponding markdownÚmarkdownÚlevelr   ú#ú N)Ú	cell_typeÚpopÚsource)r"   r%   r   r   r   Úheading_to_md>   s    r+   c             C   s
   d| _ dS )zlet raw passthrough as markdownr$   N)r(   )r"   r   r   r   Ú	raw_to_mdE   s    r,   c             C   s^   | j dkr| S d| _ xD| jD ]:}x4|jD ]*}|jdkr@t|ƒ q(|jdkr(t|ƒ q(W qW | S )z”Convert a v3 notebook to v2.

    Parameters
    ----------
    nb : NotebookNode
        The Python representation of the notebook to convert.
    r   r   ZheadingÚraw)r   r   r   r(   r+   r,   )r    r!   r"   r   r   r   Ú	downgradeJ   s    



r.   N)r   r	   )Ú__doc__Znbbaser   r   r   r   r   r   r   r
   r   r#   r+   r,   r.   r   r   r   r   Ú<module>   s   $
!