EBook::Tools(3pm) | User Contributed Perl Documentation | EBook::Tools(3pm) |
EBook::Tools - Object class for manipulating and generating E-books
This module provides an object interface and a number of related procedures intended to create or modify documents centered around the International Digital Publishing Forum (IDPF) standards, currently both OEBPS v1.2 and OPS/OPF v2.0.
use EBook::Tools qw(split_metadata system_tidy_xml); $EBook::Tools::tidysafety = 2; my $opffile = split_metadata('ebook.html'); my $otheropffile = 'alternate.opf'; my $retval = system_tidy_xml($opffile,'tidy-backup.xml'); my $ebook = EBook::Tools->new($opffile); $ebook->fix_opf20; $ebook->fix_misc; $ebook->print; $ebook->save; $ebook->init($otheropffile); $ebook->fix_oeb12; $ebook->gen_epub;
http://search.cpan.org/perldoc?Date::Manip#TIME_ZONES
Running the unit tests will cause this to happen automatically.
Order is preserved and significant -- fix_oeb12 will output DC metadata elements in the same order as in this hash, though order for tags of the same name is preserved from the input file.
Order is preserved and significant -- fix_opf20 will output DC metadata elements in the same order as in this hash, though order for tags of the same name is preserved from the input file.
This MUST NOT contain mappings from BISAC codes or descriptors to Library of Congress subjects. Use %bisactolc for that.
Keys should be entered in lowercase. The hash can also be set empty to prevent fix_publisher() from taking any action at all.
Unlike the other mappings, this one is *not* lowercased, and the keys are case-sensitive. The hash maps only to the canonical base code, but subject normalization will also add a prefix, defaulting to the BISAC format of 'FICTION / Erotica / '.
This setting is DANGEROUS!
Instantiates a new EBook::Tools object. If $filename is specified, it will also immediately initialize itself via the "init" method.
Initializes the object from an existing OPF file. If $filename is specified and exists, the OEB object will be set to read and write to that file before attempting to initialize. Otherwise, if the object currently points to an OPF file it will use that name. If there is no OPF filename data, and $filename was not specified, it will make a last-ditch attempt to find an OPF file first by looking in META-INF/container.xml, and if nothing is found there, by looking in the current directory for a single OPF file.
If no such files or found (or more than one is found), the initialization croaks.
Initializes an object containing nothing more than the basic OPF framework, suitable for adding new documents when creating an e-book from scratch.
Arguments
"init_blank" takes up to three optional named arguments:
Example
init_blank('opffile' => 'newfile.opf', 'title' => 'The Great Unknown');
The following methods return data deeper in the structure than the auto-accessors, but still do not modify any object data or files.
Returns the text of the Mobipocket-specific <Adult> element, if it exists. Expected values are 'yes' and undef.
Returns a list containing the text of all dc:contributor elements (case-insensitive) or undef if none are found.
In scalar context, returns the first contributor, not the last.
Returns the href to the cover image, or undef if none is found.
Checks the following in order:
Returns the text of all dc:date elements (case-insensitive) matching the specified attributes.
In scalar context, returns the first match, not the last.
Returns undef if no matches are found.
Arguments
If both arguments are specified a value is added to the list if it matches either one (i.e. the logic is OR).
Returns the description of the e-book, if set, or undef otherwise.
Returns a list containing the text values of all elements matching the specified criteria.
Arguments
The XML::Twig search condition used to find the elements. Typically this is just the GI (tag) of the element you wish to find, but it can also be a "qr//" expression, coderef, or anything else that XML::Twig can work with. See the XML::Twig documentation for details.
If this is not specified, an error is added and the method returns undef.
'id' attribute that must be matched exactly for the result to be added to the list
'opf:scheme' or 'scheme' attribute that must be matched exactly for the result to be added to the list
'opf:event' or 'event' attribute that must be matched exactly for the result to be added to the list
If more than one of the arguments "id", "scheme", or "event" are specified a value is added to the list if it matches any one (i.e. the logic is OR).
Returns an arrayref containing any generated error messages.
Returns the text of the dc:identifier element pointed to by the 'unique-identifier' attribute of the root 'package' element, or undef if it could not be located.
Returns a list of all ISBNs matching the specified attributes. See "twigelt_is_isbn()" for a detailed description of how the ISBN elements are found.
Returns undef if no matches are found.
In scalar context returns the first match, not the last.
See also "isbns(%args)".
Arguments
'id' attribute that must be matched exactly for the result to be added to the list
'opf:scheme' or 'scheme' attribute that must be matched exactly for the result to be added to the list
If both arguments are specified a value is added to the list if it matches either one (i.e. the logic is OR).
Returns all of the ISBN identifiers matching the specificied attributes as a list of hashrefs, with one hash per ISBN identifier presented in the order that the identifiers are found. The hash keys are 'id' (containing the value of the 'id' attribute), 'scheme' (containing the value of either the 'opf:scheme' or 'scheme' attribute, whichever is found first), and 'isbn' (containing the text of the element).
If no entries are found, returns undef.
In scalar context returns the first match, not the last.
See also "isbn_list(%args)".
Arguments
"isbns()" takes two optional named arguments:
If both arguments are specified a value is added to the list if it matches either one (i.e. the logic is OR).
Returns a list containing the text of all dc:language (case-insensitive) entries, or undef if none are found.
In scalar context returns the first match, not the last.
Returns all of the items in the manifest as a list of hashrefs, with one hash per manifest item in the order that they appear, where the hash keys are 'id', 'href', and 'media-type', each returning the appropriate attribute, if any.
In scalar context, returns the first match, not the last.
Arguments
"manifest()" takes four optional named arguments:
If any of the named arguments are specified, "manifest()" will return only items matching the specified criteria. This is an exact case-sensitive match, but it can (especially in the case of mtype) still return multiple elements.
Return values
Returns undef if there is no <manifest> element directly underneath <package>, or if <manifest> contains no items.
See also
"manifest_hrefs()", "spine()"
Example
@manifest = $ebook->manifest(id => 'ncx', mtype => 'text/xml', logic => 'or');
Returns a list of all of the hrefs in the current OPF manifest, or the empty list if none are found.
In scalar context returns the first href, not the last.
See also: "manifest()", "spine_idrefs()"
Some OPF generators explicity assign 'opf:' in the gi as a prefix on OPF elements. This makes later parsing more complex and is unnecessary, so this is stripped before any parsing takes place.
Returns the full filesystem path to the directory where the OPF metadata file will be stored, or undef if either the top-level directory or the OPF subdirectory is not found.
Returns the name of the file where the OPF metadata will be stored or undef if no value is found..
Returns the full filesystem path to the file where the OPF metadata will be stored or undef if either the top level directory or the OPF filename is not found.
Finds the primary author of the book, defined as the first 'dc:creator' entry (case-insensitive) where either the attribute opf:role="aut" or role="aut", or the first 'dc:creator' entry if no entries with either attribute can be found. Entries must actually have text to be considered.
In list context, returns a two-item list, the first of which is the text of the entry (the author name), and the second element of which is the value of the 'opf:file-as' or 'file-as' attribute (where 'opf:file-as' is given precedence if both are present).
In scalar context, returns the text of the entry (the author name).
If no entries are found, returns undef.
Uses "twigelt_is_author()" in the first half of the search.
Example
my ($fileas, $author) = $ebook->primary_author; my $author = $ebook->primary_author;
Prints the current list of errors to STDERR.
Prints the current list of warnings to STDERR.
Prints the OPF file to the default filehandle
Returns a list containing the text of all dc:publisher (case-insensitive) entries, or undef if none are found.
In scalar context returns the first match, not the last.
Returns a two-scalar list, the first scalar being the text of the Mobipocket-specific <SRP> element, if it exists, and the second being the 'Currency' attribute of that element, if it exists.
In scalar context, returns just the text (price).
Returns undef if the SRP element is not found.
Returns the text of the Mobipocket-specific <Review> element, if it exists. Returns undef if one is not found.
Returns a list containing the text of all of dc:rights or dc:copyrights (case-insensitive) entries in the e-book, or undef if none are found.
In scalar context returns the first match, not the last.
If the optional named argument 'id' is specified, it will only return entries where the id attribute matches the specified identifier. Although this still returns a list, if more than one entry is found, a warning is logged.
Note that dc:copyrights is not a valid Dublin Core element -- it is included only because some broken Mobipocket books use it.
Searches the OPF twig for the first dc:identifier (case-insensitive) element with an ID matching known UID IDs.
Returns the ID if a match is found, undef otherwise
Searches descendants of the OPF twig element for the first <dc:identifier> or <dc:Identifier> subelement with the attribute 'scheme' or 'opf:scheme' matching a known list of schemes for unique IDs
NOTE: this is NOT a case-insensitive search! If you have to deal with really bizarre input, make sure that you run "fix_oeb12()" or "fix_opf20()" before calling "fix_packageid()" or "fix_misc()".
Returns the ID if a match is found, undef otherwise.
Returns the version of the OEB specification currently in use. Valid values are "OEB12" and "OPF20". This value will default to undef until "fix_oeb12" or "fix_opf20" is called, as there is no way for the object to know what specification is being conformed to (if any) until it attempts to enforce it.
Returns all of the manifest items referenced in the spine as a list of hashrefs, with one hash per manifest item in the order that they appear, where the hash keys are 'id', 'href', and 'media-type', each returning the appropriate attribute, if any.
In scalar context, returns the first item, not the last.
Returns undef if there is no <spine> element directly underneath <package>, or if <spine> contains no itemrefs. If <spine> exists, but <manifest> does not, or a spine itemref exists but points an ID not found in the manifest, spine() logs an error and returns undef.
See also: "spine_idrefs()", "manifest()"
Returns a list of all of the idrefs in the current OPF spine, or the empty list if none are found.
In scalar context, returns the first idref, not the last.
See also: "spine()", "manifest_hrefs()"
Returns a list containing the text of all dc:subject elements or undef if none are found.
In scalar context, returns the first subject, not the last.
Returns the title of the e-book, or undef if no dc:title element (case-insensitive) exists. If a dc:title element exists, but contains no text, returns an empty string.
Returns the raw XML::Twig object used to store the OPF metadata.
Although this twig can be manipulated via the standard XML::Twig methods, doing so requires caution and is not recommended. In particular, changing the root element from here will cause the EBook::Tools internal twig and twigroot attributes to become unlinked and the result of any subsequent action is not defined.
Croaks showing the calling location unless $self has both a twig and a twigroot, and the twigroot is <package>. Used as a sanity check for methods that use twig or twigroot.
Returns the raw XML::Twig root element used to store the OPF metadata.
This twig element can be manipulated via the standard XML::Twig::Elt methods, but care should be taken not to attempt to cut this element from its twig as doing so will cause the EBook::Tools internal twig and twigroot attributes to become unlinked and the result of any subsequent action is not defined.
Returns an arrayref containing any generated warning messages.
Unless otherwise specified, all modifier methods return undef if an error was added to the error list, and true otherwise (even if a warning was added to the warning list).
Adds a document to the OPF manifest and spine, creating <manifest> and <spine> if necessary. To add an item only to the OPF manifest, see add_item().
Arguments
The method returns undef if $href is not defined or empty.
This must be unique not only to the manifest list, but to every element in the OPF file. If a duplicate ID exists, the method sets an error and returns undef.
Adds @errors to the list of object errors. Each member of @errors should be a string containing the entire text of the error, with no ending newline.
SEE ALSO: "add_warning()", "clear_errors()", "clear_warnerr()"
Creates a new dc:identifier element containing the specified text, id, and scheme.
If a <dc-metadata> element exists underneath <metadata>, the identifier element will be created underneath the <dc-metadata> in OEB 1.2 format, otherwise the title element is created underneath <metadata> in OPF 2.0 format.
Returns the twig element containing the new identifier.
Arguments
"add_identifier()" takes three named arguments, one mandatory, two optional.
Adds a document to the OPF manifest (but not spine), creating <manifest> if necessary. To add an item only to both the OPF manifest and spine, see add_document().
Arguments
This must be unique not only to the manifest list, but to every element in the OPF file. If a duplicate ID exists, the method sets an error and returns undef.
Creates a metadata element with the specified text, attributes, and parent.
If a <dc-metadata> element exists underneath <metadata>, the language element will be created underneath the <dc-metadata> and any standard attributes will be created in OEB 1.2 format, otherwise the element is created underneath <metadata> in OPF 2.0 format.
Returns 1 on success, returns undef if no gi or if no text was specified.
Arguments
A newly created element will be created under the first element found with this gi. A modified element will be moved under the first element found with this gi.
Newly created elements will use OPF 2.0 attribute names if the parent is 'metadata' and OEB 1.2 attribute names otherwise.
Example
$retval = $ebook->add_metadata(gi => 'AuthorNonstandard', text => 'Element Text', id => 'customid', fileas => 'Text, Element', role => 'xxx', scheme => 'code');
Creates a new dc:subject element containing the specified text, code, and id.
If a <dc-metadata> element exists underneath <metadata>, the subject element will be created underneath the <dc-metadata> in OEB 1.2 format, otherwise the title element is created underneath <metadata> in OPF 2.0 format.
Returns the twig element containing the new subject.
Arguments
"add_subject()" takes four named arguments, one mandatory, three optional.
Joins @newwarning to a single string and adds it to the list of object warnings. The warning should not end with a newline newline.
SEE ALSO: "add_error()", "clear_warnings()", "clear_warnerr()"
Clear the current list of errors
Clear both the error and warning lists
Clear the current list of warnings
Deletes metadata elements with the attribute 'filepos' underneath the given parent element
These are secondary metadata elements included in the output from mobi2html may that are not used.
Deletes dc:subject and dc:Subject elements based on text content or the id, scheme, or basiccode attributes. Matches are case-sensitive.
Specifying multiple arguments will delete subject matching any of them.
This has the same potential arguments as add_subject.
Returns the count of elements deleted.
Normalizes creator and contributor names and file-as attributes
Names are normalized to 'First Last' format, while file-as attributes are normalized to 'Last, First' format.
This can damage some unusual names that do not match standard capitalization formats, so it is not made part of "fix_misc()".
Standardizes all <dc:date> elements via fix_datestring(). Adds a warning to the object for each date that could not be fixed.
Called from "fix_misc()".
Fixes problems related to the OPF guide elements, specifically:
Logs a warning if a reference href is found that does not appear in the manifest.
Checks through the <dc:language> elements (case-insensitive) and removes any duplicates. If no <dc:language> elements are found, one is created.
TODO: Also convert language names to IANA language and region codes.
Arguments
The default language string to use when creating a new language element. If not specified, defaults to 'en'.
Checks through the links in the manifest and checks them for anything they might link to, adding anything missing to the manifest.
A warning is added for every manifest item missing a href.
If no <manifest> element exists directly underneath the <package> root, or <manifest> contains no items, the method logs a warning and returns undef. Otherwise, it returns 1.
Finds all <item> elements and moves them underneath <manifest>, creating <manifest> if necessary.
Logs a warning but continues if it finds an <item> with a missing id or href attribute. If both id and href attributes are missing, logs a warning, skips moving the item entirely (unless it was already underneath <manifest>, in which case it is moved to preserve its sort order along all other items under <manifest>), but otherwise continues.
Verifies that <metadata> exists (creating it if necessary), and moves it to be the first child of <package>. If additional <metadata> elements exist, their children are moved into the first one found and then the extras are deleted.
Used in "fix_metastructure_oeb12()", "fix_packageid()", and "set_primary_author(%args)".
Verifies the existence of <metadata>, <dc-metadata>, and <x-metadata>, creating them as needed, and making sure that <metadata> is a child of <package>, while <dc-metadata> and <x-metadata> are children of <metadata>.
Used in "fix_oeb12()" and "fix_mobi()".
Fixes miscellaneous potential problems in OPF data. Specifically, this is a shortcut to calling "delete_meta_filepos()", "fix_packageid()", "fix_dates()", "fix_languages()", "fix_publisher()", "fix_manifest()", "fix_spine()", "fix_subjects()", "fix_type()", "fix_guide()", and "fix_links()".
"fix_creators()" is not run from this, as it carries a risk of taking a correct name and making it incorrect.
The objective here is that you can run either "fix_misc()" and either "fix_oeb12()" or "fix_opf20()" and a perfectly valid OPF file will result from only two calls.
Manipulates the twig to fix Mobipocket-specific issues
Note that the forced creation of <output> will cause the OPF file to become noncompliant with IDPF specifications.
Modifies the OPF data to conform to the OEB 1.2 standard
Specifically, this involves:
Makes a case-insensitive search for tags matching a known list of DC metadata elements and corrects the capitalization to the OEB 1.2 standard. Also corrects 'dc:Copyrights' to 'dc:Rights'. See global variable $dcelements12.
The "fix_oeb12()" method does this also, but fix_oeb12_dcmetatags() is usable separately for the case where you want DC metadata elements with consistent tag names, but don't want them moved from wherever they are.
Modifies the OPF data to conform to the OPF 2.0 standard
Specifically, this involves:
Makes a case-insensitive search for tags matching a known list of DC metadata elements and corrects the capitalization to the OPF 2.0 standard. Also corrects 'dc:copyrights' to 'dc:rights'. See package variable %dcelements20.
The "fix_opf20()" method does this also, but "fix_opf20_dcmetatags()" is usable separately for the case where you want DC metadata elements with consistent tag names, but don't want them moved from wherever they are.
Checks the <package> element for the attribute 'unique-identifier', makes sure that it is mapped to a valid dc:identifier subelement, and if not, searches those subelements for an identifier to assign, or creates one if nothing can be found.
Requires that <metadata> exist. Croaks if it doesn't. Run "fix_oeb12()" or "fix_opf20()" before calling this if the input might be very broken.
Standardizes publisher names in all dc:publisher entities, mapping known variants of a publisher's name to a canonical form via package variable %publishermap.
Publisher entries with no text are deleted.
Fixes problems with the OPF spine, specifically:
Deletes empty and duplicate subject elements and normalizes existing subject text against the known Library of Congress mappings.
If $self->{erotic} is true, then the book will be treated as a work of erotic fiction and the subjects will go through preprocessing against the %sexcodes package variable, normalizing matches and prepending 'FICTION / Erotica / ' (with a trailing space).
This method is called as a component of "fix_misc()".
Normalizes <dc:type> elements against a limited list based on book types listed in Wikipedia.
Creates a .epub format e-book. This will create (or overwrite) the files 'mimetype' and 'META-INF/container.xml' in the current directory, creating the subdirectory META-INF as needed.
A NCX file will also be created if missing.
Arguments
This method can take two optional named arguments.
Example
gen_epub(filename => 'mybook.epub', dir => '../epub_books');
Generates the "mimetype" and "META-INF/container.xml" files expected by a .epub container, but does not actually generate the .epub file itself. This will be called automatically by "gen_epub".
The OPF will be normalized to the OPF 2.0 format.
If no NCX element exists, it will also be created.
Creates a NCX-format table of contents from the package unique-identifier, the dc:title, dc:creator, and spine elements, and then add the NCX entry to the manifest if it is not already referenced.
Adds an error and fails if any of those cannot be found. The first available dc:title is taken, but will prioritize dc:creator elements with opf:role="aut" over those with no role attribute (see twigelt_is_author() for details).
WARNING: This method REQUIRES that the e-book be in OPF 2.0 format to function correctly. Call fix_opf20() before calling gen_ncx(). gen_ncx() will log an error and fail if $self{spec} is not set to OPF20.
Arguments
This method will overwrite any existing file.
Returns a twig containing the NCX XML, or undef on failure.
Saves the OPF file to disk. Existing files are backed up to filename.backup.
Sets the Mobipocket-specific <Adult> element, creating or deleting it as necessary. If $bool is true, the text is set to 'yes'. If it is defined but false, any existing elements are deleted. If it is undefined, the method immediately returns.
If a new element has to be created, "fix_metastructure_oeb12" is called to ensure that <x-metadata> exists and the element is created under <x-metadata>, as Mobipocket elements are not recognized by Mobipocket's software when placed directly under <metadata>
Sets a cover image
In OPF 2.0, this is done by setting both a <meta name="cover"> element and a guide <reference type="other.ms-coverimage-standard"> element (though some readers will also extract the first image found in the HTML of the <reference type="cover"> element, which this method will not handle).
In OEB 1.2, this is done by setting the <EmbeddedCover> tag.
If the filename is not currently listed as an item in the manifest, it is added.
Arguments
Sets the date metadata for a given event. If more than one dc:date or dc:Date element is present with the specified event attribute, sets the first. If no dc:date element is present with the specified event attribute, a new element is created.
If a <dc-metadata> element exists underneath <metadata>, the date element will be created underneath the <dc-metadata> in OEB 1.2 format, otherwise the title element is created underneath <metadata> in OPF 2.0 format.
Returns 1 on success, logs an error and returns undef if no text or event was specified.
Arguments
Sets the text and optionally ID of the first dc:description element found (case-insensitive). Creates the element if one did not exist. If a <dc-metadata> element exists underneath <metadata>, the description element will be created underneath the <dc-metadata> in OEB 1.2 format, otherwise the title element is created underneath <metadata> in OPF 2.0 format.
Returns 1 on success, returns undef if no publisher was specified.
Arguments
"set_description()" takes one required and one optional named argument
Example
$retval = $ebook->set_description('text' => 'A really good book', 'id' => 'mydescid');
If $bool is true, "$self-"{erotic}> is set to 1, otherwise this is set to 0.
This will enable or disable special handling for erotic books, most notably in subject normalization.
This is not related in any way to "set_adult" which is a Mobipocket-specific flag.
Returns 1 if no argument is given, 0 otherwise.
Sets the text and optionally the ID of the first dc:language element found (case-insensitive). Creates the element if one did not exist. If a <dc-metadata> element exists underneath <metadata>, the language element will be created underneath the <dc-metadata> in OEB 1.2 format, otherwise the title element is created underneath <metadata> in OPF 2.0 format.
Returns 1 on success, returns undef if no text was specified.
Arguments
Example
$retval = $ebook->set_language('text' => 'en-us', 'id' => 'langid');
Sets a <meta> element in the <metadata> element area.
Arguments
Sets the text and optionally the ID of the first specified element type found (case-insensitive). Creates the element if one did not exist (with the exact capitalization specified).
If a <dc-metadata> element exists underneath <metadata>, the language element will be created underneath the <dc-metadata> and any standard attributes will be created in OEB 1.2 format, otherwise the element is created underneath <metadata> in OPF 2.0 format.
Returns 1 on success, returns undef if no gi or if no text was specified.
Arguments
A newly created element will be created under the first element found with this gi. A modified element will be moved under the first element found with this gi.
Newly created elements will use OPF 2.0 attribute names if the parent is 'metadata' and OEB 1.2 attribute names otherwise.
Example
$retval = $ebook->set_metadata(gi => 'AuthorNonstandard', text => 'Element Text', id => 'customid', fileas => 'Text, Element', role => 'xxx', scheme => 'code');
Sets the filename used to store the OPF metadata.
Returns 1 on success; sets an error message and returns undef if no filename was specified.
Sets the Mobipocket-specific <SRP> element (Suggested Retail Price), creating or deleting it as necessary.
If a new element has to be created, "fix_metastructure_oeb12" is called to ensure that <x-metadata> exists and the element is created under <x-metadata>, as Mobipocket elements are not recognized by Mobipocket's software when placed directly under <metadata>
Arguments
The price to set as the text of the element. If this is undefined, the method sets an error and returns undef. If it is set but false, any existing <SRP> element is deleted.
The value to set on the 'Currency' attribute. If not provided, defaults to 'USD' (US Dollars)
Sets the text, id, file-as, and role attributes of the primary author element (see "primary_author()" for details on how this is found), or if no primary author exists, creates a new element containing the information.
This method calls "fix_metastructure_basic()" to enforce the presence of the <metadata> element. When creating a new element, the method will use the OEB 1.2 element name and create the element underneath <dc-metadata> if an existing <dc-metadata> element is found underneath <metadata>. If no existing <dc-metadata> element is found, the new element will be created with the OPF 2.0 element name directly underneath <metadata>. Regardless, it is probably a good idea to call "fix_oeb12()" or "fix_opf20()" after calling this method to ensure a consistent scheme.
Arguments
Three optional named arguments can be passed:
Specifies the author text to set. If omitted and a primary author element exists, the text will be left as is; if omitted and a primary author element cannot be found, an error message will be generated and the method will return undef.
Specifies the 'file-as' attribute to set. If omitted and a primary author element exists, any existing attribute will be left untouched; if omitted and a primary author element cannot be found, the newly created element will not have this attribute.
Specifies the 'id' attribute to set. If this is specified, and the id is already in use, a warning will be added but the method will continue, removing the id attribute from the element that previously contained it.
If this is omitted and a primary author element exists, any existing id will be left untouched; if omitted and a primary author element cannot be found, the newly created element will not have an id set.
If called with no arguments, the only effect this method has is to enforce that either an 'opf:role' or 'role' attribute is set to 'aut' on the primary author element.
Return values
Returns 1 if successful, returns undef and sets an error message if the author argument is missing and no primary author element was found.
Sets the text and optionally the ID of the first dc:publisher element found (case-insensitive). Creates the element if one did not exist. If a <dc-metadata> element exists underneath <metadata>, the publisher element will be created underneath the <dc-metadata> in OEB 1.2 format, otherwise the title element is created underneath <metadata> in OPF 2.0 format.
Returns 1 on success, returns undef if no publisher was specified.
Arguments
"set_publisher()" takes one required and one optional named argument
Example
$retval = $ebook->set_publisher('text' => 'My Publishing House', 'id' => 'mypubid');
Sets the text and optionally ID of the first <Review> element found (case-insensitive), creating the element if one did not exist.
This is a Mobipocket-specific element and if it needs to be created it will always be created under <x-metadata> with "fix_metastructure_oeb12()" called to ensure that <x-metadata> exists.
Returns 1 on success, returns undef if no review text was specified
Arguments
Example
$retval = $ebook->set_review('text' => 'This book is perfect!', 'id' => 'revid');
Sets the text of the first dc:rights or dc:copyrights element found (case-insensitive). If the element found has the gi of dc:copyrights, it will be changed to dc:rights. This is to correct certain noncompliant Mobipocket files.
Creates the element if one did not exist. If a <dc-metadata> element exists underneath <metadata>, the title element will be created underneath the <dc-metadata> in OEB 1.2 format, otherwise the title element is created underneath <metadata> in OPF 2.0 format.
Returns 1 on success, returns undef if no rights string was specified.
Arguments
This specifies the text of the element. If not specified, the method returns undef.
This specifies the ID to set on the element. If set and the ID is already in use, a warning is logged but the method continues anyway.
Sets the OEB specification to match when modifying OPF data. Allowable values are 'OEB12', 'OPF20', and 'MOBI12'.
Returns 1 if successful; returns undef and sets an error message if an unknown specification was set.
Sets the <meta property="dcterms:modified"> element to the current timestamp and removes duplicate or nonstandard timestamps.
Sets the text or id of the first dc:title element found (case-insensitive). Creates the element if one did not exist. If a <dc-metadata> element exists underneath <metadata>, the title element will be created underneath the <dc-metadata> in OEB 1.2 format, otherwise the title element is created underneath <metadata> in OPF 2.0 format.
Arguments
set_title() takes two optional named arguments. If neither is specified, the method will do nothing.
This specifies the text of the element. If not specified, and no title element is found, an error will be set and the method will return undef -- set_title() will refuse to create a dc:title element with no text.
This specifies the ID to set on the element. If set and the ID is already in use, a warning is logged but the method continues anyway.
Sets the text and optionally the ID of the first dc:type element found (case-insensitive). Creates the element if one did not exist. If a <dc-metadata> element exists underneath <metadata>, the publisher element will be created underneath the <dc-metadata> in OEB 1.2 format, otherwise the title element is created underneath <metadata> in OPF 2.0 format.
Returns 1 on success, returns undef if no publisher was specified.
Arguments
"set_type()" takes one required and one optional named argument
Example
$retval = $ebook->set_type('text' => 'Short Story', 'id' => 'mytypeid');
All procedures are exportable, but none are exported by default. All procedures can be exported by using the ":all" tag.
Wrapper for CORE::lc to get around the fact that builtins can't be used in dispatch tables prior to Perl 5.16.
WARNING: this procedure may disappear once Perl 5.16 is standard on all systems in common use! For that reason, this is not exportable.
Wrapper for CORE::uc to get around the fact that builtins can't be used in dispatch tables prior to Perl 5.16.
WARNING: this procedure may disappear once Perl 5.16 is standard on all systems in common use! For that reason, this is not exportable.
Capitalizes the first letter of each word in $string.
Returns the corrected string.
Takes an input string and cleans out any characters that would not be valid in a filename.
Returns the cleaned string.
Creates the XML file META-INF/container.xml pointing to the specified OPF file.
Creates the META-INF directory if necessary. Will destroy any non-directory file named 'META-INF' in the current directory. If META-INF/container.xml already exists, it will rename that file to META-INF/container.xml.backup.
Arguments
Return values
Creates a file named 'mimetype' in the current working directory containing 'application/epub+zip' (no trailing newline)
Destroys and overwrites that file if it exists.
Returns the mimetype string if successful, undef otherwise.
Prints a debugging message to "STDERR" if package variable $debug is greater than or equal to $level. A trailing newline is appended, and should not be part of @message.
Returns true or dies.
Takes as an argument a list of text pieces that will be joined. If the joined length is less than 70, all of the joined text is returned.
If the joined length is greater than 70, the return string is the first 30 characters followed by ' [...] ' followed by the last 30 characters.
Searches through $ENV{PATH} (and optionally any additional directories specified in @extradirs) for the first regular file matching $pattern. $pattern itself can take two forms: if passed a "qr//" regular expression, that expression is used directly. If passed any other string, that string will be used for a case-insensitive exact match where the extension '.bat', '.com', or '.exe' is optional (i.e. the final pattern will be "qr/^ $pattern (\.bat|\.com|\.exe)? $/ix").
Returns the first match found, or undef if there were no matches or if no pattern was specified.
Searches through a file for href and src attributes, and returns a list of unique links with any named anchors removed (e.g. 'myfile.html#part7' returns as just 'myfile.html'). If no links are found, or the file does not exist, returns undef.
Does not check to see if the links are local. Requires that links be surrounded by double quotes, not single or left bare. Assumes that any link will not be broken across multiple lines, so it will (for example) fail to find:
<img src= "myfile.jpg">
though it can find:
<img src="myfile.jpg">
This also does not distinguish between local files and remote links.
Attempts to locate an OPF file, first by calling "get_container_rootfile()" to check the contents of "META-INF/container.xml", and then by looking for a single file with the extension ".opf" in the current working directory.
Returns the filename of the OPF file, or undef if nothing was found.
Takes a date string and attempts to convert it to the limited subset of ISO8601 allowed by the OPF standard (YYYY, YYYY-MM, or YYYY-MM-DD).
In the special case of finding MM/DD/YYYY, it assumes that it was a Mobipocket-mangled date, and not only converts it, but will strip the day information if the day is '01', and both the month and day information if both month and day are '01'. This is because Mobipocket Creator enforces a complete MM/DD/YYYY even if the month and day aren't known, and it is common practice to use 01 for an unknown value.
Arguments
Returns $fixeddate
Opens and parses an OPS/epub container, extracting the 'full-path' attribute of element 'rootfile'
Arguments
Return values
Takes as an argument a hash reference and an optional modifier and inserts a new key for every value in that hash if no such key already exists.
If the modifer is set to 'lc' or 'uc', the value is either lowercased or uppercased respectively before it is used as a key.
Croaks if the first argument is not a hashref, or if an invalid modifier string is used.
Takes as an argument a scalar containing a sequence of binary bytes. Returns a string converting each octet of the data to its two-digit hexadecimal equivalent. There is no leading "0x" on the string.
Checks /proc/$PID/statm and prints out a line to STDERR showing the current memory usage. This is a debugging tool that will likely fail to do anything useful on a system without a /proc system compatible with Linux.
Arguments
Returns 1 on success, undef otherwise.
Takes a psuedo-HTML containing one or more <metadata>...</metadata> blocks and splits out the metadata blocks into an XML file ready to be used as an OPF document. The input HTML file is rewritten without the metadata.
If $metafile (or the temporary HTML-only file created during the split) already exists, it will be moved to filename.backup.
Arguments
Returns the filename the metadata was written to, or undef if no metadata was found.
Splits <pre>...</pre> blocks out of a source HTML file into their own separate HTML files including required headers. Each block will be written to its own file following the naming format "$outfilebase-###.html", where ### is a three-digit number beginning at 001 and incrementing for each block found. If $outfilebase is not specified, it defaults to the basename of $htmlfile with "-pre-###.html" appended. The
Returns a list containing all filenames created.
Strips any <script>...</script> blocks out of a HTML file.
Arguments
Checks the result of a system call and croak on failure with an appropriate message. For this to work, it MUST be used as the line immediately following the system command.
Arguments
Return Values
Returns 0 on success
Croaks on failure.
Runs tidy on a XHTML file semi-safely (using a secondary file)
Converts HTML to XHTML if necessary
Arguments
Defaults to "infile-tidy.ext" if not specified.
Global variables used
Return Values
Returns the return value from tidy
Runs tidy on an XML file semi-safely (using a secondary file)
Arguments
Defaults to "infile-tidy.ext" if not specified.
Global variables used
Return values
Returns the return value from tidy
Removes any whitespace characters from the beginning or end of every string in @list (also works on scalars).
trim; # trims $_ inplace $new = trim; # trims (and returns) a copy of $_ trim $str; # trims $str inplace $new = trim $str; # trims (and returns) a copy of $str trim @list; # trims @list inplace @new = trim @list; # trims (and returns) a copy of @list
This was shamelessly copied from japhy's example at perlmonks.org:
http://www.perlmonks.org/?node_id=36684
If needed for large lists, it would probably be better to use String::Strip.
Creates an unlinked element with the specified gi (tag), and then assigns it the id and scheme attributes 'UUID'.
Arguments
Returns the element.
Takes two twig elements and returns 1 if they have the same GI (tag), text, and attributes, but are not actually the same element. The GI comparison is case-insensitive. The others are case-sensitive.
Returns 0 otherwise.
Croaks if passed anything but twig elements.
Checks the attributes in a twig element to see if they match OPF names with an opf: namespace, and if so, removes the namespace. Used by the fix_oeb12() method.
Takes as a sole argument a twig element.
Returns that element with the modified attributes, or undef if the element didn't exist. Returns an unmodified element if both att and opf:att exist.
Checks the attributes in a twig element to see if they match OPF names, and if so, prepends the OPF namespace. Used by the fix_opf20() method.
Takes as a sole argument a twig element.
Returns that element with the modified attributes, or undef if the element didn't exist.
Takes as an argument a twig element. Returns true if the element is a dc:creator (case-insensitive) with either a opf:role="aut" or role="aut" attribute defined. Returns undef otherwise, and also if the element has no text.
Croaks if fed no argument, or fed an argument that isn't a twig element.
Intended to be used as a twig search condition.
Example
my @elements = $ebook->twigroot->descendants(\&twigelt_is_author);
Takes as an argument a twig element. Returns true if the element is a dc:identifier (case-insensitive) where any of the id, opf:scheme, or scheme attributes start with 'isbn', '-isbn', 'eisbn', or 'e-isbn' (again case-insensitive).
Returns undef otherwise, and also if the element has no text.
Croaks if fed no argument, or fed an argument that isn't a twig element.
Intended to be used as a twig search condition.
Example
my @elements = $ebook->twigroot->descendants(\&twigelt_is_isbn);
Takes as an argument a twig element. Returns true if the element is a dc:identifier (case-insensitive) element with an "id" attribute matching the known IDs of proper unique identifiers suitable for a package-id (also case-insensitive). Returns undef otherwise.
Croaks if fed no argument, or fed an argument that isn't a twig element.
Intended to be used as a twig search condition.
Example
my @elements = $ebook->twigroot->descendants(\&twigelt_is_knownuid);
Changes the current working directory to the one specified, creating it if necessary.
Returns the current working directory before the change. If no directory is specified, returns the current working directory without changing anything.
Croaks on any failure.
Returns the directory in which user configuration files and helper programs are expected to be found, creating that directory if it does not exist. Typically, this directory is "$ENV{HOME}/.ebooktools", but on MSWin32 systems if that directory does not already exist, "$ENV{USERPROFILE}/ApplicationData/EBook-Tools" is returned (and potentially created) instead.
If $ENV{HOME} (and $ENV{USERPROFILE} on MSWin32) are either not set or do not point to a directory, the sub returns undef.
Make sure month and day have valid values. Return the passed values if they are, return 3 undefs if not. Testing of month or day can be skipped by passing undef in that spot.
This may not be worth the effort, since the average size for most books is less than 500k, and the largest books are rarely over 10M.
Zed Pobre <zed@debian.org>
Copyright 2008-2013 Zed Pobre
Licensed to the public under the terms of the GNU GPL, version 2
2019-08-08 | perl v5.28.1 |