Skip to content

Commit

Permalink
Add a REST end point to invoke the dtbook2html script from the pipeline2
Browse files Browse the repository at this point in the history
  • Loading branch information
egli committed Nov 21, 2024
1 parent 24b2b80 commit be9f451
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/clj/daisyproducer2/documents/preview.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[daisyproducer2.config :refer [env]]
[daisyproducer2.db.core :as db]
[daisyproducer2.documents.images :as images]
[daisyproducer2.documents.preview.dtbook2html :as dtbook2html]
[daisyproducer2.documents.preview.dtbook2pdf :as dtbook2pdf]
[daisyproducer2.documents.preview.dtbook2sbsform :as dtbook2sbsform]
[daisyproducer2.documents.versions :as versions]
Expand Down Expand Up @@ -88,8 +89,24 @@
(scripts/dtbook-to-odt dtbook images path opts)
[name path])))

(defn html
"Generate an HTML file for given `document-id` and return a tuple
containing the name and the path of the generated file. An exception
is thrown if the document has no versions."
[document-id]
(let [dtbook (-> (versions/get-latest document-id)
(versions/get-content))
images (->> (images/get-images document-id)
(map images/image-path))
name (format "%s.html" document-id)
target-dir (fs/path (env :spool-dir))
path (str (fs/path target-dir name))]
(dtbook2html/dtbook2html dtbook images path)
[name path]))

(prometheus/instrument! metrics/registry #'epub)
(prometheus/instrument! metrics/registry #'sbsform)
(prometheus/instrument! metrics/registry #'large-print)
(prometheus/instrument! metrics/registry #'open-document)
(prometheus/instrument! metrics/registry #'html)

19 changes: 19 additions & 0 deletions src/clj/daisyproducer2/documents/preview/dtbook2html.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(ns daisyproducer2.documents.preview.dtbook2html
(:require
[babashka.fs :as fs]
[clojure.java.io :as io]
[daisyproducer2.documents.utils :refer [with-tempfile]]
[daisyproducer2.pipeline2.scripts :as scripts]
[sigel.xslt.core :as xslt]))

(defn- filter-braille-and-add-image-refs
[xml target]
(let [xslt [(xslt/compile-xslt (io/resource "xslt/filterBrlContractionhints.xsl"))
(xslt/compile-xslt (io/resource "xslt/addImageRefs.xsl"))]]
(xslt/transform-to-file xslt (fs/file xml) (fs/file target))))

(defn dtbook2html
[input images output]
(with-tempfile [clean-xml {:prefix "daisyproducer-" :suffix "-clean.xml"}]
(filter-braille-and-add-image-refs input clean-xml)
(scripts/dtbook-to-html clean-xml images output)))
13 changes: 13 additions & 0 deletions src/clj/daisyproducer2/pipeline2/scripts.clj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@
out (io/output-stream epub)]
(io/copy in out)))))

(defn dtbook-to-html [dtbook images html]
(pipeline2/with-job [job (pipeline2/job-create "dtbook-to-html" [dtbook] images {})]
(let [completed (pipeline2/wait-for job)
results (pipeline2/get-results completed)
_ (println results)
epub-stream (->> results
(filter #(str/ends-with? % ".xhtml"))
first
pipeline2/get-stream)]
(with-open [in epub-stream
out (io/output-stream html)]
(io/copy in out)))))

(def ^:private odt-defaults
{:asciimath :both
:phonetics true
Expand Down
17 changes: 17 additions & 0 deletions src/clj/daisyproducer2/routes/services.clj
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,23 @@
(catch java.nio.file.FileSystemException e
(log/error (str e))
(internal-server-error {:status-text (str e)})))
(not-found)))}}]

["/html"
{:get {:summary "Get an HTML for a document by ID"
:parameters {:path {:id int?}}
:handler (fn [{{{:keys [id]} :path} :parameters}]
(if-let [doc (documents/get-document id)]
(try
(let [[name _] (preview/html id)
url (format "/download/%s/html/%s" id name)]
(created url {:location url}))
(catch clojure.lang.ExceptionInfo e
(log/error (ex-message e))
(internal-server-error {:status-text (ex-message e)}))
(catch java.nio.file.FileSystemException e
(log/error (str e))
(internal-server-error {:status-text (str e)})))
(not-found)))}}]]

["/versions"
Expand Down
6 changes: 5 additions & 1 deletion src/clj/daisyproducer2/routes/static.clj
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,9 @@
["/open-document/:file" {:parameters {:path {:id int?
:file string?}}
:handler (created-assets-handler
(fn [title _] (format "%s.odt" title)))}]])
(fn [title _] (format "%s.odt" title)))}]
["/html/:file" {:parameters {:path {:id int?
:file string?}}
:handler (created-assets-handler
(fn [title _] (format "%s.html" title)))}]])

6 changes: 6 additions & 0 deletions test/rest-api.http
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,12 @@ GET http://localhost:3000/api/documents/644/preview/open-document?asciimath=asci
# Get the actual ODT artifact
GET http://localhost:3000/download/644.odt

# Get HTML
GET http://localhost:3000/api/documents/644/preview/html

# Get the actual html artifact
GET http://localhost:3000/download/644/html/644.html

#########
# Words #
#########
Expand Down

0 comments on commit be9f451

Please sign in to comment.