@@ -44,6 +44,26 @@ export const withDocxContent = async <T>(
4444 }
4545} ;
4646
47+ export const withEpubDirectory = async < T > (
48+ file : string ,
49+ k : ( path : string ) => Promise < T >
50+ ) => {
51+ const [ _dir , stem ] = dirAndStem ( file ) ;
52+ const temp = await Deno . makeTempDir ( ) ;
53+ try {
54+ // Move the docx to a temp dir and unzip it
55+ const zipFile = join ( temp , stem + ".zip" ) ;
56+ await Deno . copyFile ( file , zipFile ) ;
57+ await unzip ( zipFile ) ;
58+
59+ // Open the core xml document and match the matches
60+ const result = await k ( temp ) ;
61+ return result ;
62+ } finally {
63+ await Deno . remove ( temp , { recursive : true } ) ;
64+ }
65+ } ;
66+
4767export const withPptxContent = async < T > (
4868 file : string ,
4969 slideNumber : number ,
@@ -569,6 +589,18 @@ export const verifyDocXDocument = (
569589 } ) ;
570590} ;
571591
592+ export const verifyEpubDocument = (
593+ callback : ( path : string ) => Promise < void > ,
594+ name ?: string ,
595+ ) : ( file : string ) => Verify => {
596+ return ( file : string ) => ( {
597+ name : name ?? "Inspecting Epub" ,
598+ verify : async ( _output : ExecuteOutput [ ] ) => {
599+ return await withEpubDirectory ( file , callback ) ;
600+ } ,
601+ } ) ;
602+ }
603+
572604export const verifyPptxDocument = (
573605 callback : ( doc : string , docFile : string ) => Promise < void > ,
574606 name ?: string ,
@@ -730,6 +762,52 @@ export const ensureDocxRegexMatches = (
730762 } , "Inspecting Docx for Regex matches" ) ( file ) ;
731763} ;
732764
765+ export const ensureEpubFileRegexMatches = (
766+ epubFile : string ,
767+ pathsAndRegexes : {
768+ path : string ;
769+ regexes : ( string | RegExp ) [ ] [ ] ;
770+ } [ ]
771+ ) : Verify => {
772+ return verifyEpubDocument ( async ( epubDir ) => {
773+ for ( const { path, regexes } of pathsAndRegexes ) {
774+ const file = join ( epubDir , path ) ;
775+ assert (
776+ existsSync ( file ) ,
777+ `File ${ file } doesn't exist in Epub` ,
778+ ) ;
779+ const content = await Deno . readTextFile ( file ) ;
780+ const mustMatch : ( RegExp | string ) [ ] = [ ] ;
781+ const mustNotMatch : ( RegExp | string ) [ ] = [ ] ;
782+ if ( regexes . length ) {
783+ mustMatch . push ( ...regexes [ 0 ] ) ;
784+ }
785+ if ( regexes . length > 1 ) {
786+ mustNotMatch . push ( ...regexes [ 1 ] ) ;
787+ }
788+
789+ mustMatch . forEach ( ( regex ) => {
790+ if ( typeof regex === "string" ) {
791+ regex = new RegExp ( regex ) ;
792+ }
793+ assert (
794+ regex . test ( content ) ,
795+ `Required match ${ String ( regex ) } is missing from file ${ file } .` ,
796+ ) ;
797+ } ) ;
798+ mustNotMatch . forEach ( ( regex ) => {
799+ if ( typeof regex === "string" ) {
800+ regex = new RegExp ( regex ) ;
801+ }
802+ assert (
803+ ! regex . test ( content ) ,
804+ `Illegal match ${ String ( regex ) } was found in file ${ file } .` ,
805+ ) ;
806+ } ) ;
807+ }
808+ } , "Inspecting Epub for Regex matches" ) ( epubFile ) ;
809+ }
810+
733811// export const ensureDocxRegexMatches = (
734812// file: string,
735813// regexes: (string | RegExp)[],
0 commit comments