parseFile method
Constructs a Publication.Builder
to build a Publication
from a publication file.
@param asset Digital medium (e.g. a file) used to access the publication. @param fetcher Initial leaf fetcher which should be used to read the publication's resources. This can be used to:
- support content protection technologies
- parse exploded archives or in archiving formats unknown to the parser, e.g. RAR If the file is not an archive, it will be reachable at the HREF /<file.name>, e.g. with a PDF. @param warnings Used to report non-fatal parsing warnings, such as publication authoring mistakes. This is useful to warn users of potential rendering issues or help authors debug their publications.
Implementation
@override
Future<PublicationBuilder?> parseFile(
PublicationAsset asset, Fetcher fetcher) async {
if (!await _accepts(asset, fetcher)) {
return null;
}
List<Link> readingOrder = (await fetcher.links())
.where((it) => !File(it.href).isHiddenOrThumbs && it.mediaType.isBitmap)
.toList()
..sort((a, b) => a.href.compareTo(b.href));
if (readingOrder.isEmpty) {
throw Exception("No bitmap found in the publication.");
}
String title = (await fetcher.guessTitle()) ?? asset.name;
// First valid resource is the cover.
readingOrder[0] = readingOrder[0].copy(rels: {"cover"});
Manifest manifest = Manifest(
metadata: Metadata(localizedTitle: LocalizedString.fromString(title)),
readingOrder: readingOrder,
subcollections: {
"pageList": [PublicationCollection(links: readingOrder)]
});
return PublicationBuilder(
manifest: manifest,
fetcher: fetcher,
servicesBuilder: ServicesBuilder.create(
positions: PerResourcePositionsService.createFactory(
fallbackMediaType: "image/*")));
}