From e3abffb21cc51e3f63e2e97c146a540b5b096342 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Wed, 27 Oct 2021 11:44:30 +0100 Subject: [PATCH] Beginning -add-bookmarks-json --- cpdf.ml | 21 ++++++++++++++++++--- cpdf.mli | 2 +- cpdfcommand.ml | 2 +- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/cpdf.ml b/cpdf.ml index 615dc8b..a3cd9af 100644 --- a/cpdf.ml +++ b/cpdf.ml @@ -631,6 +631,20 @@ let bookmark_of_data pdf i s i' isopen optionaldest = Pdfmarks.target = target; Pdfmarks.isopen = isopen} +let parse_bookmark_file_json verify pdf input = + try + let marks = + [] + in + if verify then + if verify_bookmarks pdf 0 (Pdfpage.endpage pdf) marks then marks else + error "Bad bookmark file (References non-existant pages or is malformed)" + else + marks + with + e -> + error (Printf.sprintf "Malformed JSON bookmark file (%s)" (Printexc.to_string e)) + let parse_bookmark_file verify pdf input = let currline = ref 0 in try @@ -669,8 +683,9 @@ let parse_bookmark_file verify pdf input = (Printexc.to_string e)) -let add_bookmarks verify input pdf = - let parsed = parse_bookmark_file verify pdf input in +let add_bookmarks ~json verify input pdf = + let parsed = + (if json then parse_bookmark_file_json else parse_bookmark_file) verify pdf input in (*iter (fun b -> flprint (Pdfmarks.string_of_bookmark b); flprint "\n") parsed;*) Pdfmarks.add_bookmarks parsed pdf @@ -855,7 +870,7 @@ let output_json_marks ch calculate_page_number pdf fastrefnums marks = let json = `List (map json_of_mark marks) in J.pretty_to_channel ch json -(* List the bookmarks, optionally deunicoding the text, in the given range to the given output *) +(* List the bookmarks in the given range to the given output *) let list_bookmarks ~json encoding range pdf output = let process_stripped escaped = let b = Buffer.create 200 in diff --git a/cpdf.mli b/cpdf.mli index 09aabcc..d28ac69 100644 --- a/cpdf.mli +++ b/cpdf.mli @@ -97,7 +97,7 @@ val parse_bookmark_file : bool -> Pdf.t -> Pdfio.input -> Pdfmarks.t list (** [add_bookmarks verify input pdf] adds bookmarks from the bookmark file give. If [verify] is given, bookmarks will be verified to ensure, for example, that they are not out of the page range. *) -val add_bookmarks : bool -> Pdfio.input -> Pdf.t -> Pdf.t +val add_bookmarks : json:bool -> bool -> Pdfio.input -> Pdf.t -> Pdf.t (** [list_bookmarks encoding range pdf output] lists the bookmarks to the given output in the format specified in cpdfmanual.pdf *) diff --git a/cpdfcommand.ml b/cpdfcommand.ml index e7a2ded..efe0eb6 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -3967,7 +3967,7 @@ let go () = args.relative_to_cropbox args.underneath range pdf) | Some (AddBookmarks file) -> write_pdf false - (Cpdf.add_bookmarks true (Pdfio.input_of_channel (open_in_bin file)) + (Cpdf.add_bookmarks ~json:args.bookmarks_json true (Pdfio.input_of_channel (open_in_bin file)) (get_single_pdf args.op false)) | Some RemoveBookmarks -> write_pdf false (Pdfmarks.remove_bookmarks (get_single_pdf args.op false))