Modernize @B bookmark namer
This commit is contained in:
parent
697ee3d412
commit
8119e6eb3f
|
@ -2,22 +2,31 @@ open Pdfutil
|
|||
open Pdfio
|
||||
open Cpdferror
|
||||
|
||||
(* Remove characters which might not make good filenames. *)
|
||||
let remove_unsafe_characters encoding s =
|
||||
if encoding = Cpdfmetadata.UTF8 then Pdftext.utf8_of_pdfdocstring s else (* For @B bookmarks splitting. *)
|
||||
if encoding = Cpdfmetadata.Raw then s else
|
||||
let chars =
|
||||
lose
|
||||
(function x ->
|
||||
match x with
|
||||
'/' | '?' | '<' | '>' | '\\' | ':' | '*' | '|' | '\"' | '^' | '+' | '=' -> true
|
||||
| x when int_of_char x < 32 || (int_of_char x > 126 && encoding <> Cpdfmetadata.Stripped) -> true
|
||||
| _ -> false)
|
||||
(explode s)
|
||||
in
|
||||
match chars with
|
||||
| '.'::more -> implode more
|
||||
| chars -> implode chars
|
||||
(* Remove characters which might not make good filenames. In, UTF8, out UTF8. *)
|
||||
let remove_unsafe_characters s =
|
||||
let codepoints = Pdftext.codepoints_of_utf8 s in
|
||||
let codepoints =
|
||||
lose
|
||||
(function x ->
|
||||
x = int_of_char '/'
|
||||
|| x = int_of_char '?'
|
||||
|| x = int_of_char '<'
|
||||
|| x = int_of_char '>'
|
||||
|| x = int_of_char '\\'
|
||||
|| x = int_of_char ':'
|
||||
|| x = int_of_char '*'
|
||||
|| x = int_of_char '|'
|
||||
|| x = int_of_char '\"'
|
||||
|| x = int_of_char '^'
|
||||
|| x = int_of_char '+'
|
||||
|| x = int_of_char '='
|
||||
|| x < 32
|
||||
|| x = 127)
|
||||
codepoints
|
||||
in
|
||||
match codepoints with
|
||||
| 46::more -> Pdftext.utf8_of_codepoints codepoints (* Don't produce a dotfile *)
|
||||
| chars -> Pdftext.utf8_of_codepoints codepoints
|
||||
|
||||
(* Attaching files *)
|
||||
let attach_file ?memory keepversion topage pdf file =
|
||||
|
@ -257,7 +266,7 @@ let dump_attachment out pdf (_, embeddedfile) =
|
|||
| _ -> error "Bad embedded file stream"
|
||||
end
|
||||
in
|
||||
let s = remove_unsafe_characters Cpdfmetadata.UTF8 s in
|
||||
let s = remove_unsafe_characters (Pdftext.utf8_of_pdfdocstring s) in
|
||||
let filename = if out = "" then s else out ^ Filename.dir_sep ^ s in
|
||||
begin try
|
||||
let fh = open_out_bin filename in
|
||||
|
|
|
@ -5,9 +5,8 @@ type attachment =
|
|||
pagenumber : int;
|
||||
data : unit -> Pdfio.bytes}
|
||||
|
||||
(** Remove characters which might not make good filenames. If the encoding is
|
||||
[Cpdfmetadata.Stripped] we in addition lose any character > 126. *)
|
||||
val remove_unsafe_characters : Cpdfmetadata.encoding -> string -> string
|
||||
(** Remove characters which might not make good filenames from a UTF8 string. *)
|
||||
val remove_unsafe_characters : string -> string
|
||||
|
||||
(** [attach_file keepversion topage pdf filename] attaches the file in
|
||||
[filename] to the pdf, optionally to a page (rather than document-level).
|
||||
|
|
|
@ -265,7 +265,7 @@ let get_bookmark_name encoding pdf marks splitlevel n _ =
|
|||
let refnums = Pdf.page_reference_numbers pdf in
|
||||
let fastrefnums = hashtable_of_dictionary (combine refnums (indx refnums)) in
|
||||
match keep (function m -> n = Pdfpage.pagenumber_of_target ~fastrefnums pdf m.Pdfmarks.target && m.Pdfmarks.level <= splitlevel) marks with
|
||||
| {Pdfmarks.text = title}::_ -> Cpdfattach.remove_unsafe_characters encoding title
|
||||
| {Pdfmarks.text = title}::_ -> Cpdfattach.remove_unsafe_characters (Pdftext.utf8_of_pdfdocstring title)
|
||||
| _ -> ""
|
||||
|
||||
(* @F means filename without extension *)
|
||||
|
|
BIN
cpdfmanual.pdf
BIN
cpdfmanual.pdf
Binary file not shown.
|
@ -1303,7 +1303,7 @@ the result is unspecified. The following format operators may be used:
|
|||
\texttt{@N} & Sequence number without padding zeroes \\
|
||||
\texttt{@S} & Start page of this chunk \\
|
||||
\texttt{@E} & End page of this chunk \\
|
||||
\texttt{@B} & Bookmark name at this page \\
|
||||
\texttt{@B} & Bookmark name at this page, if any. \\
|
||||
\end{tabular}
|
||||
\end{center}
|
||||
|
||||
|
@ -1340,14 +1340,12 @@ one of the output files.
|
|||
\noindent Split \texttt{a.pdf} on bookmark boundaries, using the bookmark text as the filename.
|
||||
|
||||
\end{framed}
|
||||
\noindent The bookmark text used for a name is converted from unicode to 7 bit ASCII, and the following characters are removed, in addition to any character with ASCII code less than 32:
|
||||
\noindent The bookmark text used for a name has the following characters are removed, in addition to any character with ASCII code less than 32 or equal to 126. In addition, names beginning with \texttt{.} are not produced.
|
||||
\begin{framed}
|
||||
\centering
|
||||
\verb! / ? < > \ : * | " ^ + =!
|
||||
\end{framed}
|
||||
|
||||
\noindent To prevent this process, and convert bookmark names to UTF8 instead, add \texttt{-utf8} to the command.
|
||||
|
||||
\section{Splitting to Maximum Size}
|
||||
|
||||
The \texttt{-split-max} operation splits a file into chunks of no more than the given size, starting at the beginning. The suffixes kB, KiB, MB, MiB, GB, and GiB may be used to give the size. For example:
|
||||
|
|
Loading…
Reference in New Issue