Ejemplo n.º 1
0
if __name__ == '__main__':

    # If the caret is right next to or between a word, then we show the
    # documentation for that word using the the shell command `texdoc`
    tm_current_word = getenv('TM_CURRENT_WORD')
    if tm_current_word:
        output = check_output("texdoc {}".format(shellquote(tm_current_word)),
                              shell=True).strip()
        # Close the html output window on success
        if not output:
            exit(200)

    # Find all the packages included in the file or its inputs
    master_file, master_dir = find_file_to_typeset(
        find_tex_directives(getenv("TM_FILEPATH")))
    chdir(master_dir)
    packages = find_tex_packages(master_file)

    texmf_directory = check_output("kpsewhich --expand-path '$TEXMFMAIN'",
                                   shell=True,
                                   universal_newlines=True).strip()
    docdbpath = "{}/Library/Caches/TextMate".format(expanduser('~'))
    docdbfile = "{}/latexdocindex".format(docdbpath)

    if exists(docdbfile) and getmtime(docdbfile) > getmtime(texmf_directory):
        # Read from cache
        with open(docdbfile, 'rb') as cache:
            paths, descriptions, headings = load(cache)
    else:
        # Parse the texdoctk database
Ejemplo n.º 2
0
def get_typesetting_data(filepath, tm_engine,
                         tm_bundle_support=getenv('TM_BUNDLE_SUPPORT'),
                         ignore_warnings=False):
    """Return a dictionary containing up-to-date typesetting data.

    This function changes the current directory to the location of
    ``filepath``!

    Arguments:

        filepath

            The filepath of the file we want to typeset.

        tm_engine

            A string representing the current default latex engine.

        tm_bundle_support

            The location of the “LaTeX Bundle” support folder.

        ignore_warnings

            Specifies if this function exits with an error status if there are
            any problems.

    Returns: ``{str: str}``

    Examples:

        >>> current_directory = getcwd()
        >>> data = get_typesetting_data('Tests/TeX/lualatex.tex', 'pdflatex')
        >>> print(data['engine'])
        lualatex
        >>> data['synctex']
        True
        >>> chdir(current_directory)

    """
    def get_cached_data():
        """Get current data and update cache."""
        cache_read = False
        typesetting_data = {}

        try:
            with open(cache_filename, 'rb') as storage:
                typesetting_data = load(storage)
                cache_read = True

            cache_data_outdated = (getmtime(file_path) <
                                   getmtime(cache_filename) >
                                   getmtime(filepath))

            # Write new cache data if the current data does not contain
            # the necessary up to date information - This might be the case if
            # only `texparser` has written to the cache file
            if 'engine' not in typesetting_data or cache_data_outdated:
                raise Exception()

        except:
            # Get data and save it in the cache
            packages = find_tex_packages(filename, ignore_warnings)
            engine = construct_engine_command(typesetting_directives,
                                              tm_engine, packages)
            synctex = not(bool(call("{} --help | grep -q synctex".format(
                                    engine), shell=True)))
            typesetting_data.update({'engine': engine,
                                     'packages': packages,
                                     'synctex': synctex})
            if not cache_read:
                typesetting_data['files_with_guttermarks'] = {filename}

        try:
            with open(cache_filename, 'wb') as storage:
                dump(typesetting_data, storage)
        except:
            print('<p class="warning"> Could not write cache file!</p>')

        return typesetting_data

    filepath = normpath(realpath(filepath))
    typesetting_directives = find_tex_directives(filepath, ignore_warnings)
    filename, file_path = find_file_to_typeset(typesetting_directives,
                                               tex_file=filepath)
    file_without_suffix = splitext(filename)[0]
    chdir(file_path)
    cache_filename = '.{}.lb'.format(file_without_suffix)
    typesetting_data = get_cached_data()

    # We add the tex files in the bundle directory to the possible input
    # files. If `TEXINPUTS` was not set before then we also add the current
    # directory `.` and the central default repository `::` to the start
    # of `TEXINPUTS`
    texinputs = "{}:{}/tex//".format(
        getenv('TEXINPUTS') if getenv('TEXINPUTS') else '.::',
        tm_bundle_support)
    putenv('TEXINPUTS', texinputs)

    typesetting_data.update({'cache_filename': cache_filename,
                             'filename': filename,
                             'file_path': file_path,
                             'file_without_suffix': file_without_suffix,
                             'typesetting_directives': typesetting_directives})

    return typesetting_data
Ejemplo n.º 3
0
# -- Main ---------------------------------------------------------------------

if __name__ == "__main__":

    # If the caret is right next to or between a word, then we show the
    # documentation for that word using the the shell command `texdoc`
    tm_current_word = getenv("TM_CURRENT_WORD")
    if tm_current_word:
        output = check_output("texdoc {}".format(shellquote(tm_current_word)), shell=True).strip()
        # Close the html output window on success
        if not output:
            exit(200)

    # Find all the packages included in the file or its inputs
    master_file, master_dir = find_file_to_typeset(find_tex_directives(getenv("TM_FILEPATH")))
    chdir(master_dir)
    packages = find_tex_packages(master_file)

    texmf_directory = check_output("kpsewhich --expand-path '$TEXMFMAIN'", shell=True, universal_newlines=True).strip()
    docdbpath = "{}/Library/Caches/TextMate".format(expanduser("~"))
    docdbfile = "{}/latexdocindex".format(docdbpath)

    if exists(docdbfile) and getmtime(docdbfile) > getmtime(texmf_directory):
        # Read from cache
        with open(docdbfile, "rb") as cache:
            paths, descriptions, headings = load(cache)
    else:
        # Parse the texdoctk database
        docfiles = get_documentation_files(texmf_directory)
        paths, descriptions, headings = parse_texdoctk_data(docfiles, texmf_directory)
Ejemplo n.º 4
0
def get_typesetting_data(filepath,
                         tm_engine,
                         tm_bundle_support=getenv('TM_BUNDLE_SUPPORT'),
                         ignore_warnings=False):
    """Return a dictionary containing up-to-date typesetting data.

    This function changes the current directory to the location of
    ``filepath``!

    Arguments:

        filepath

            The filepath of the file we want to typeset.

        tm_engine

            A string representing the current default latex engine.

        tm_bundle_support

            The location of the “LaTeX Bundle” support folder.

        ignore_warnings

            Specifies if this function exits with an error status if there are
            any problems.

    Returns: ``{str: str}``

    Examples:

        >>> current_directory = getcwd()
        >>> data = get_typesetting_data('Tests/TeX/lualatex.tex', 'pdflatex')
        >>> print(data['engine'])
        lualatex
        >>> data['synctex']
        True
        >>> chdir(current_directory)

    """
    def get_cached_data():
        """Get current data and update cache."""
        cache_read = False
        typesetting_data = {}

        try:
            with open(cache_filename, 'rb') as storage:
                typesetting_data = load(storage)
                cache_read = True

            cache_data_outdated = (getmtime(file_path) <
                                   getmtime(cache_filename) >
                                   getmtime(filepath))

            # Write new cache data if the current data does not contain
            # the necessary up to date information - This might be the case if
            # only `texparser` has written to the cache file
            if 'engine' not in typesetting_data or cache_data_outdated:
                raise Exception()

        except:
            # Get data and save it in the cache
            packages = find_tex_packages(filename, ignore_warnings)
            engine = construct_engine_command(typesetting_directives,
                                              tm_engine, packages)
            synctex = not (bool(
                call("{} --help | grep -q synctex".format(engine),
                     shell=True)))
            typesetting_data.update({
                'engine': engine,
                'packages': packages,
                'synctex': synctex
            })
            if not cache_read:
                typesetting_data['files_with_guttermarks'] = {filename}

        try:
            with open(cache_filename, 'wb') as storage:
                dump(typesetting_data, storage)
        except:
            print('<p class="warning"> Could not write cache file!</p>')

        return typesetting_data

    filepath = normpath(realpath(filepath))
    typesetting_directives = find_tex_directives(filepath, ignore_warnings)
    filename, file_path = find_file_to_typeset(typesetting_directives,
                                               tex_file=filepath)
    file_without_suffix = splitext(filename)[0]
    chdir(file_path)
    cache_filename = '.{}.lb'.format(file_without_suffix)
    typesetting_data = get_cached_data()

    # We add the tex files in the bundle directory to the possible input
    # files. If `TEXINPUTS` was not set before then we also add the current
    # directory `.` and the central default repository `::` to the start
    # of `TEXINPUTS`
    texinputs = "{}:{}/tex//".format(
        getenv('TEXINPUTS') if getenv('TEXINPUTS') else '.::',
        tm_bundle_support)
    putenv('TEXINPUTS', texinputs)

    typesetting_data.update({
        'cache_filename': cache_filename,
        'filename': filename,
        'file_path': file_path,
        'file_without_suffix': file_without_suffix,
        'typesetting_directives': typesetting_directives
    })

    return typesetting_data
Ejemplo n.º 5
0
if __name__ == '__main__':

    # If the caret is right next to or between a word, then we show the
    # documentation for that word using the the shell command `texdoc`
    tm_current_word = getenv('TM_CURRENT_WORD')
    if tm_current_word:
        output = check_output("texdoc {}".format(shellquote(tm_current_word)),
                              shell=True).strip()
        # Close the html output window on success
        if not output:
            exit(200)

    # Find all the packages included in the file or its inputs
    master_file, master_dir = find_file_to_typeset(
        find_tex_directives(getenv("TM_FILEPATH")))
    chdir(master_dir)
    packages = find_tex_packages(master_file)

    texmf_directory = check_output("kpsewhich --expand-path '$TEXMFMAIN'",
                                   shell=True, universal_newlines=True).strip()
    docdbpath = "{}/Library/Caches/TextMate".format(expanduser('~'))
    docdbfile = "{}/latexdocindex".format(docdbpath)

    if exists(docdbfile) and getmtime(docdbfile) > getmtime(texmf_directory):
        # Read from cache
        with open(docdbfile, 'rb') as cache:
            paths, descriptions, headings = load(cache)
    else:
        # Parse the texdoctk database
        docfiles = get_documentation_files(texmf_directory)