Ejemplo n.º 1
0
def save_css(soup, fo, assets_dir):
    """
        Extracts <style> elements in <head> from the
        file and replaces them with <link>.
    """
    n = 0
    for style in soup.select('head style'):
        data = style.text.encode(errors='ignore')
        md5 = get_md5(data)
        basename = 'style-%s.css' % md5
        dest_abs = os.path.join(assets_dir, basename)
        dest_rel = os.path.relpath(dest_abs, os.path.dirname(fo))
        if not os.path.exists(dest_abs):
            make_sure_dir_exists(dest_abs)
            with open(dest_abs, 'w') as f:
                f.write(data)

        link = Tag(name='link')
        link.attrs['rel'] = 'stylesheet'
        link.attrs['type'] = 'text/css'
        link.attrs['href'] = dest_rel

        style.replace_with(link)
        n += 1
    return n
Ejemplo n.º 2
0
def save_images_locally(soup, fo, assets_dir):
    n = 0
    for tag in soup.select('img[src]'):
        src = tag.attrs['src']
        # check if not relative path
        if src.startswith('/') or src.startswith('..'):

            if not os.path.exists(src):
                msg = 'Could not find resource %s' % src
                logger.error(msg)
                continue

            with open(src) as f:
                data = f.read()
                md5 = get_md5(data)

            b0, ext = os.path.splitext(os.path.basename((src)))
            basename = 'data-from-img-%s-%s%s' % (b0, md5[:8], ext)
            dest_abs = os.path.join(assets_dir, basename)
            dest_rel = os.path.relpath(dest_abs, os.path.dirname(fo))

            if not os.path.exists(dest_abs):
                shutil.copy(src, dest_abs)
            #            print('new link: %s' % dest_rel)
            #            print('new res: %s' % dest_abs)
            tag.attrs['src'] = dest_rel
            # print('image points to %s' % dest_rel)
            n += 1
    return n
Ejemplo n.º 3
0
    def subfloat_replace(args, opts):
        contents = args[0]
        caption = opts[0]
        check_isinstance(contents, str)

        if caption is None:
            label = None
        else:
            caption, label = get_s_without_label(caption, labelprefix="fig:")
            if label is None:
                caption, label = get_s_without_label(
                    caption, labelprefix="subfig:")
            if label is not None and not label.startswith('subfig:'):
                msg = 'Subfigure labels should start with "subfig:"; found %r.' % (
                    label)
                label = 'sub' + label
                msg += 'I will change to %r.' % label
                logger.debug(msg)

        # we need to make up an ID
        if label is None:
            label = 'subfig:' + get_md5(contents)
#             print('making up label %r' % label)
#         if label is not None:
        idpart = ' id="%s"' % label
#         else:
#             idpart = ""

        if caption is None:
            caption = 'no subfloat caption'
        res = '<figure class="subfloat"%s>%s<figcaption>%s</figcaption></figure>' % (
            idpart, contents, caption)
        return res
Ejemplo n.º 4
0
def graphvizgen_plot(ggraph, output, prog='dot'):
    gg = gg_deepcopy(ggraph)
    with tmpfile(".dot") as filename_dot:
        with open(filename_dot, 'w') as f:
            gg.dot(f)
        try:
            graphviz_run(filename_dot, output, prog=prog)
        except:
            contents = open(filename_dot).read()
            s = get_md5(contents)
            filename = 'out-%s.dot' % s
            with open(filename, 'w') as f:
                f.write(contents)
            print('Saved problematic dot as %r.' % filename)
            raise
Ejemplo n.º 5
0
def manual_jobs(context,
                src_dir,
                output_file,
                generate_pdf,
                bibfile,
                stylesheet,
                remove=None,
                filter_soup=None,
                extra_css=None):
    manual_contents = list(get_manual_contents(src_dir))

    if not manual_contents:
        msg = 'Could not find any file for composing the book.'
        raise Exception(msg)

    # check that all the docnames are unique
    pnames = [_[1] for _ in manual_contents]
    if len(pnames) != len(set(pnames)):
        msg = 'Repeated names detected: %s' % pnames
        raise ValueError(msg)

    local_files = list(locate_files(src_dir, '*.md'))
    basename2filename = dict((os.path.basename(_), _) for _ in local_files)

    files_contents = []
    for i, (_, docname) in enumerate(manual_contents):
        libname = 'unused'
        logger.info('adding document %s - %s' % (libname, docname))
        out_part_basename = '%02d%s' % (i, docname)

        # read the file to get hash
        basename = '%s.md' % docname
        fn = basename2filename[basename]
        contents = open(fn).read()
        contents_hash = get_md5(contents)[:8]
        # job will be automatically erased if the source changes
        job_id = '%s-%s' % (docname, contents_hash)
        res = context.comp(render_book,
                           src_dir,
                           docname,
                           generate_pdf,
                           main_file=output_file,
                           out_part_basename=out_part_basename,
                           filter_soup=filter_soup,
                           extra_css=extra_css,
                           job_id=job_id)

        #         source = '%s.md' % docname
        #         if source in basename2filename:
        #             filenames = [basename2filename[source]]
        #             erase_job_if_files_updated(context.cc, promise=res,
        #                                        filenames=filenames)
        #         else:
        #             logger.debug('Could not find file %r for date check' % source)

        files_contents.append(res)

    fn = os.path.join(src_dir, MCDPManualConstants.main_template)
    if not os.path.exists(fn):
        msg = 'Could not find template %s' % fn
        raise ValueError(msg)

    template = open(fn).read()

    d = context.comp(manual_join,
                     template=template,
                     files_contents=files_contents,
                     bibfile=bibfile,
                     stylesheet=stylesheet,
                     remove=remove)
    context.comp(write, d, output_file)

    if os.path.exists(MCDPManualConstants.pdf_metadata_template):
        context.comp(generate_metadata, src_dir)
Ejemplo n.º 6
0
def h(filename):
    data = open(filename).read()
    return get_md5(data)
Ejemplo n.º 7
0
def makefigure(inside, opt, asterisk):  # @UnusedVariable
    align = opt  # @UnusedVariable
#     print('makefigure inside = %r' % inside)

    def subfloat_replace(args, opts):
        contents = args[0]
        caption = opts[0]
        check_isinstance(contents, str)

        if caption is None:
            label = None
        else:
            caption, label = get_s_without_label(caption, labelprefix="fig:")
            if label is None:
                caption, label = get_s_without_label(
                    caption, labelprefix="subfig:")
            if label is not None and not label.startswith('subfig:'):
                msg = 'Subfigure labels should start with "subfig:"; found %r.' % (
                    label)
                label = 'sub' + label
                msg += 'I will change to %r.' % label
                logger.debug(msg)

        # we need to make up an ID
        if label is None:
            label = 'subfig:' + get_md5(contents)
#             print('making up label %r' % label)
#         if label is not None:
        idpart = ' id="%s"' % label
#         else:
#             idpart = ""

        if caption is None:
            caption = 'no subfloat caption'
        res = '<figure class="subfloat"%s>%s<figcaption>%s</figcaption></figure>' % (
            idpart, contents, caption)
        return res

    inside = substitute_command_ext(
        inside, 'subfloat', subfloat_replace, nargs=1, nopt=1)

    class Tmp:
        label = None

    def sub_caption(args, opts):
        assert not opts and len(args) == 1
        x, Tmp.label = get_s_without_label(args[0], labelprefix="fig:")
        res = '<figcaption>' + x + "</figcaption>"
#         print('caption args: %r, %r' % (args, opts))
        return res

    inside = substitute_command_ext(
        inside, 'caption', sub_caption, nargs=1, nopt=0)

#     print('makefigure inside without caption = %r'  % inside)
    assert not '\\caption' in inside

    if Tmp.label is None:
        Tmp.label = 'fig:' + get_md5(inside)
        #print('making up label %r' % Tmp.label)
#     if Tmp.label is not None:
    idpart = ' id="%s"' % Tmp.label
#     else:
#         idpart = ""

    res = '<figure%s>%s</figure>' % (idpart, inside)
    return res