doc2json/tex2json/tex_to_xml.py
def extract_latex(zip_file: str, latex_dir: str, cleanup=True):
    """
    Unzip latex zip into temp directory
    :param zip_file:
    :param latex_dir:
    :param cleanup:
    :return:
    """
    assert os.path.exists(zip_file)
    assert zip_file.endswith('.gz') or zip_file.endswith('.zip') or zip_file.endswith('.tar')

    # get name of zip file
    file_id = os.path.splitext(zip_file)[0].split('/')[-1]

    # check if tar file -> untar
    tar_dir = os.path.join(latex_dir, file_id)
    os.makedirs(tar_dir, exist_ok=True)
    if tarfile.is_tarfile(zip_file):
        with tarfile.open(zip_file) as tar:
            tar.extractall(tar_dir)
    # check if gzip file -> un-gz and/or untar
    elif _is_gzip_file(zip_file):
        tar_file = os.path.join(latex_dir, f'{file_id}.tar')
        with gzip.open(zip_file, 'rb') as in_f, open(tar_file, 'wb') as out_f:
            s = in_f.read()
            out_f.write(s)
        if os.path.exists(tar_file):
            # check if tarfile
            if tarfile.is_tarfile(tar_file):
                with tarfile.open(tar_file) as tar:
                    tar.extractall(tar_dir)
                os.remove(tar_file)
            # else, copy to tex file
            else:
                tex_file = os.path.join(latex_dir, file_id, f'{file_id}.tex')
                os.makedirs(tar_dir, exist_ok=True)
                os.rename(tar_file, tex_file)
    # check if zip file -> unzip
    elif zipfile.is_zipfile(zip_file):
        with zipfile.ZipFile(zip_file, 'r') as in_f:
            in_f.extractall(tar_dir)
    else:
        return None

    # clean up if needed
    if cleanup:
        os.remove(zip_file)

    # returns directory
    if os.path.exists(tar_dir):
        return tar_dir

    Unzip latex zip into temp directory
    :param zip_file:
    :param latex_dir:
    :param cleanup:
    :return:
    
Unzip latex zip into temp directory
Unzip latex zip into temp directory
def extract_latex(zip_file: str, latex_dir: str, cleanup=True):
    assert os.path.exists(zip_file)
    assert zip_file.endswith('.gz') or zip_file.endswith('.zip') or zip_file.endswith('.tar')
    file_id = os.path.splitext(zip_file)[0].split('/')[-1]
    tar_dir = os.path.join(latex_dir, file_id)
    os.makedirs(tar_dir, exist_ok=True)
    if tarfile.is_tarfile(zip_file):
        with tarfile.open(zip_file) as tar:
            tar.extractall(tar_dir)
    elif _is_gzip_file(zip_file):
        tar_file = os.path.join(latex_dir, f'{file_id}.tar')
        with gzip.open(zip_file, 'rb') as in_f, open(tar_file, 'wb') as out_f:
            s = in_f.read()
            out_f.write(s)
        if os.path.exists(tar_file):
            if tarfile.is_tarfile(tar_file):
                with tarfile.open(tar_file) as tar:
                    tar.extractall(tar_dir)
                os.remove(tar_file)
            else:
                tex_file = os.path.join(latex_dir, file_id, f'{file_id}.tex')
                os.makedirs(tar_dir, exist_ok=True)
                os.rename(tar_file, tex_file)
    elif zipfile.is_zipfile(zip_file):
        with zipfile.ZipFile(zip_file, 'r') as in_f:
            in_f.extractall(tar_dir)
    else:
        return None
    if cleanup:
        os.remove(zip_file)
    if os.path.exists(tar_dir):
        return tar_dir
def extract_latex ( zip_file : str , latex_dir : str , cleanup = True ) : assert os . path . exists ( zip_file ) assert zip_file . endswith ( '.gz' ) or zip_file . endswith ( '.zip' ) or zip_file . endswith ( '.tar' ) file_id = os . path . splitext ( zip_file ) [ 0 ] . split ( '/' ) [ - 1 ] tar_dir = os . path . join ( latex_dir , file_id ) os . makedirs ( tar_dir , exist_ok = True ) if tarfile . is_tarfile ( zip_file ) : with tarfile . open ( zip_file ) as tar : tar . extractall ( tar_dir ) elif _is_gzip_file ( zip_file ) : tar_file = os . path . join ( latex_dir , f'{file_id}.tar' ) with gzip . open ( zip_file , 'rb' ) as in_f , open ( tar_file , 'wb' ) as out_f : s = in_f . read ( ) out_f . write ( s ) if os . path . exists ( tar_file ) : if tarfile . is_tarfile ( tar_file ) : with tarfile . open ( tar_file ) as tar : tar . extractall ( tar_dir ) os . remove ( tar_file ) else : tex_file = os . path . join ( latex_dir , file_id , f'{file_id}.tex' ) os . makedirs ( tar_dir , exist_ok = True ) os . rename ( tar_file , tex_file ) elif zipfile . is_zipfile ( zip_file ) : with zipfile . ZipFile ( zip_file , 'r' ) as in_f : in_f . extractall ( tar_dir ) else : return None if cleanup : os . remove ( zip_file ) if os . path . exists ( tar_dir ) : return tar_dir
Unzip latex zip into temp directory
Unzip latex zip into temp directory
"""
    Unzip latex zip into temp directory
    :param zip_file:
    :param latex_dir:
    :param cleanup:
    :return:
    """ # get name of zip file # check if tar file -> untar # check if gzip file -> un-gz and/or untar # check if tarfile # else, copy to tex file # check if zip file -> unzip # clean up if needed # returns directory
None
zip_file
str
None
latex_dir
str
None
cleanup
None