FunctionalPerl

 view release on metacpan or  search on metacpan

examples/pdf-to-html  view on Meta::CPAN


my $insert = list(undef);

sub navigation_html ($svgpaths, $for_svgpath, $is_single) {
    my $is_selected = sub($path) {
        $path eq $for_svgpath
    };

    my $possibly_shortened_svgpaths
        = possibly_shortened($svgpaths, svgpath_to_pageno($for_svgpath),
        $nav_window_sidelen, $insert, $insert);

    my $ul = UL(
        { class => "menu" },
        $possibly_shortened_svgpaths->map_with_islast(
            sub ($is_last, $maybe_svgpath) {
                if (defined $maybe_svgpath) {
                    my $svgpath = $maybe_svgpath;

                    my $pageno = svgpath_to_pageno($svgpath);

                    my $href
                        = $is_single
                        ? "#p$pageno"
                        : basename svgpath_to_htmlpath($svgpath);

                    LI(
                        { class => ($is_last ? "menu_last" : "menu") },
                        (
                            &$is_selected($svgpath)
                            ? SPAN({ class => "menu_selected" }, $pageno)
                            : A({ href => $href }, $pageno)
                        )
                    )
                } else {

                    # never the last
                    LI({ class => "menu" }, "...")
                }
            }
        )
    );

    $is_single
        ? A({ name => "p" . svgpath_to_pageno($for_svgpath) }, $ul)
        : $ul
}

# pure function that returns the actions to be taken (this allows us
# to inspect them before their execution, for debugging or testing):

sub _svgpaths_to_html_actions ($svgpaths, $title, $outdir) {

    # (No need to protect $svgpaths with `Keep` here since it's a
    # purearray because of the sorting)

    # the html fragment for one page from the pdf
    my $page_htmlfragment = sub ($is_last, $for_svgpath) {

        # sub needed to work around destruction of document by
        # weakening done in serializer (ugly, really replace all
        # weakening and Keep stuff with a fixed perl?)
        my $TR_TD_nav = sub {
            TR TD { align => "center" },
                navigation_html($svgpaths, $for_svgpath, $opt_single)
        };
        [
            &$TR_TD_nav,
            TR(TD(IMG { src => basename($for_svgpath), width => "100%" })),
            $opt_single ? ($is_last ? (TR TD HR) : ()) : &$TR_TD_nav
        ]
    };

    my $html = sub ($title, $body, $maybe_for_svgpath) {
        HTML(
            { lang => 'en' },    # XX should not assume 'en' (use HTML5)
            HEAD(
                TITLE($title), css_link($css_src),
                paging_js($svgpaths, $maybe_for_svgpath)
            ),
            BODY(TABLE({ width => "100%", border => 0 }, $body))
        )
    };

    cons(
        [\&xputfile_utf8, "$outdir/$css_src", $css_code],

        $opt_single
        ?

            # all PDF pages in a single HTML page
            list(
            [\&possibly_unlink, "$outdir/index.html"],
            [
                \&puthtmlfile,
                "$outdir/index.html",
                &$html(
                    $title, $svgpaths->map_with_islast($page_htmlfragment),
                    undef
                )
            ]
            )
        :

            # one HTML page per PDF page
            cons(
            [
                \&possibly_symlink,
                basename(svgpath_to_htmlpath($svgpaths->first)),
                "$outdir/index.html"
            ],
            $svgpaths->map_with_index(
                sub ($i, $svgpath) {
                    [
                        \&puthtmlfile,
                        svgpath_to_htmlpath($svgpath),
                        &$html(
                            "$title - page " . svgpath_to_pageno($svgpath),
                            &$page_htmlfragment(0, $svgpath), $i
                        ),
                    ]
                }



( run in 0.808 second using v1.01-cache-2.11-cpan-39bf76dae61 )