Commit bbd365ab authored by Lyza Danger Gardner's avatar Lyza Danger Gardner Committed by Lyza Gardner

Add PDFJS scripts and template

parent 18c74abf
This diff is collapsed.
#!/usr/bin/env python
# This is a helper used by `update-pdfjs` to update the Mustache template for
# serving PDFs with PDFJS with the local dev server.
import os
import sys
# Header to insert at the top of the generated PDF.js viewer template
FILE_HEADER = """
<!-- AUTO-GENERATED BY {}. DO NOT EDIT. -->
""".format(
sys.argv[0]
)
# Header to insert after the original `<title>` tag in the PDF viewer HTML
# mustache template.
#
# This header is responsible for:
#
# - Adding a `<base>` tag so that relative URLs in the pre-built viewer HTML
# resolve to the right URL.
# - Injecting custom PDF.js viewer configuration
# - Injecting the Hypothesis client entry point and configuration
#
# The header needs to be inserted after the `<title>` tag so we can override it,
# but before any relative asset links which will be affected by the `<base>`
# tag.
#
HYPOTHESIS_HEADER = """
<!-- Begin Hypothesis modifications -->
<base href="/scripts/pdfjs-2/web/">
<title>via Hypothesis</title>
{#
It's worth noting that this link tag is *not* currently used by the
Hypothesis client to determine the URL of this page. For consistency with
how these pages are served on via, however, we serve it with the PDF.js
viewer application.
-#}
<link rel="canonical" href="{{{ documentUrl }}}"/>
<script>
window.DOCUMENT_URL = '{{{documentUrl}}}';
window.PDF_URL = '{{{ url }}}';
window.CLIENT_URL = '{{{clientUrl}}}'.replace('{current_host}', document.location.hostname);
</script>
<script src="/scripts/pdfjs-init.js"></script>
{# Configure Hypothesis client. #}
<script>
window.hypothesisConfig = function() {
return {
openSidebar: true,
};
};
</script>
<!-- End Hypothesis modifications -->
"""
def insert_after(str_, search_str, insert_str):
return str_.replace(search_str, search_str + insert_str)
input_file_path = sys.argv[1]
output_file_path = sys.argv[2]
input_file = open(input_file_path, "r")
output_file = open(output_file_path, "w")
base_dir = os.path.dirname(input_file_path)
viewer_html = input_file.read()
viewer_html = insert_after(viewer_html, "<!DOCTYPE html>", FILE_HEADER)
viewer_html = insert_after(
viewer_html, "</title>", HYPOTHESIS_HEADER.replace("$BASEDIR", base_dir)
)
output_file.write(viewer_html)
\ No newline at end of file
#!/bin/sh
set -eu
# This script fetches the latest build of PDF.js from the viewer demo
# page. It also regenerates the Mustache-templated HTML viewer for use
# by the local devserver.
#
# See https://github.com/mozilla/pdf.js/wiki/Setup-pdf.js-in-a-website#from-examples
#
# To update PDF.js to the latest version:
#
# 1. Create a new branch and run this script.
# 2. Re-start `make dev` and verify that PDFs in the `dev-server/documents/pdf`
# directory work correctly.
# 3. Commit the changes to the `static/` and `templates/` directories inside of
# `dev-server`
#
# If you make changes to the PDF.js viewer template generator only and want to
# regenerate the HTML template, run this script with the `--no-download` flag.
DEST_DIR=dev-server/static/scripts/pdfjs-2
PREFIX=pdf.js-gh-pages
COMPONENTS="$PREFIX/build $PREFIX/web $PREFIX/LICENSE"
download_pdfjs=1
for arg in "$@"
do
if [ "$arg" = "--no-download" ]; then
download_pdfjs=""
fi
done
# Download the latest version of the PDF.js library and viewer.
if [ $download_pdfjs ]; then
rm -rf $DEST_DIR
mkdir -p $DEST_DIR
# Get the latest build of the viewer
curl -L https://github.com/mozilla/pdf.js/archive/gh-pages.tar.gz \
| tar -xz --directory $DEST_DIR --strip-components=1 $COMPONENTS
# Remove example content from viewer
rm $DEST_DIR/web/*.pdf
# Remove the check that the PDF being loaded is from the same origin as the
# viewer.
sed -i '' -e 's/HOSTED_VIEWER_ORIGINS.includes(viewerOrigin)/true \/* Hypothesis *\//' $DEST_DIR/web/viewer.js
fi
# Generate the PDF viewer mustache (HTML) template.
BASEDIR=$(dirname $0)
python \
$BASEDIR/generate-pdfjs-template.py \
$DEST_DIR/web/viewer.html \
dev-server/templates/pdfjs-viewer.mustache
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment