include/epubutils/epub_utils.h

Go to the documentation of this file.
00001 
00002 // Name:        epub_utils.h
00003 // Purpose:     Epub utilities
00004 // Author:      Julian Smart
00005 // Modified by:
00006 // Created:     2008-11-18
00007 // RCS-ID:      $Id$
00008 // Copyright:   (c) Julian Smart
00009 // Licence:     New BSD License
00011 
00012 #ifndef _EPUB_UTILS_H_
00013 #define _EPUB_UTILS_H_
00014 
00015 #include "wx/dynarray.h"
00016 #include "wx/variant.h"
00017 
00018 #include "imageutils/imageblock.h"
00019 #include "datautils/stringtable.h"
00020 #include "xmlutils/xmlparser.h"
00021 
00022 #include "epubutils/epub_property.h"
00023 
00024 class ebFileLocations;
00025 
00026 /* Allow editing of multiple attributes and multiple instances of the same element type */
00027 #define ebUSE_COMPLEX_ATTRIBUTES 1
00028 
00029 #if ebUSE_COMPLEX_ATTRIBUTES
00030 
00031 WX_DECLARE_OBJARRAY(wxVariant, wxVariantArray);
00032 
00037 class ebElementInstance
00038 {
00039 public:
00040 
00041     ebElementInstance() { Init(); }
00042 
00043     ebElementInstance(const ebElementInstance& inst) { Copy(inst); }
00044 
00045     void Init();
00046     void Clear() { m_attributes.Clear(); }
00047 
00048     void Copy(const ebElementInstance& inst);
00049     
00050     void operator=(const ebElementInstance& inst) { Init(); Copy(inst); }
00051     bool operator==(const ebElementInstance& inst) const;
00052 
00053     const wxVariant& operator[](size_t i) const { return GetAttribute(i); }
00054     wxVariant& operator[](size_t i) { return GetAttribute(i); }
00055 
00056     void SetAttributeValue(const wxString& name, const wxString& value, const wxArrayString& choices = wxArrayString());
00057     wxVariant GetAttributeValue(const wxString& name) const;
00058 
00059     size_t GetCount() const { return m_attributes.GetCount(); }
00060     const wxVariant& GetAttribute(size_t i) const { return m_attributes.GetProperty(i)->GetVariant(); }
00061     wxVariant& GetAttribute(size_t i) { return m_attributes.GetProperty(i)->GetVariant(); }
00062 
00063     bool RemoveAttribute(size_t i);
00064 
00065     EpProperty* GetProperty(int i) const { return m_attributes.GetProperty(i); }
00066 
00067     wxVariant* FindAttribute(const wxString& name) const;
00068     wxVariant* FindOrCreateAttribute(const wxString& name);
00069 
00070     EpProperties& GetProperties() { return m_attributes; }
00071 
00072 protected:
00073 
00074     // wxVariantArray m_attributes; // always has at least Value
00075     EpProperties m_attributes;
00076 };
00077 
00078 WX_DECLARE_OBJARRAY(ebElementInstance, ebElementInstanceArray);
00079 
00084 class ebElement
00085 {
00086 public:
00087 
00088     ebElement(const wxString& name = wxEmptyString) { Init(); m_name = name; }
00089 
00090     ebElement(const ebElement& element) { Copy(element); }
00091 
00092     void Init();
00093 
00095     void InitializeInstances();
00096 
00098     wxString GetDefaultValue() const;
00099 
00101     wxArrayString GetValues() const;
00102 
00104     wxString GetCombinedValue(const wxString& separator = wxT(","), bool useAnd = true) const;
00105 
00107     wxVariant* GetDefaultValueVariant();
00108 
00110     void SetDefaultValue(const wxString& value);
00111 
00113     wxString GetAttributeValue(const wxString& name) const;
00114 
00116     void SetAttributeValue(const wxString& name, const wxString& value);
00117 
00118     void Clear() { m_instances.Clear(); }
00119 
00120     void Copy(const ebElement& element);
00121     
00122     void operator=(const ebElement& element) { Init(); Copy(element); }
00123     bool operator==(const ebElement& element) const;
00124 
00125     size_t GetCount() const { return m_instances.GetCount(); }
00126     const ebElementInstance& GetInstance(size_t i) const { return m_instances[i]; }
00127     ebElementInstance& GetInstance(size_t i) { return m_instances[i]; }
00128 
00129     void AddInstance(const ebElementInstance& instance);
00130     bool RemoveInstance(size_t i);
00131 
00133     void SetName(const wxString& name) { m_name = name; }
00134     const wxString& GetName() const { return m_name; }
00135 
00137     void SetDisplayName(const wxString& name) { m_displayName = name; }
00138     const wxString& GetDisplayName() const { return m_displayName; }
00139 
00141     wxString GenerateXML(const wxString& indent, const wxString& defaultValue = wxEmptyString) const;
00142 
00143 protected:
00144 
00145     ebElementInstanceArray  m_instances;    // always has at least Value
00146     wxString                m_name;         // actual element name
00147     wxString                m_displayName;  // display name
00148 };
00149 
00150 WX_DEFINE_ARRAY_PTR(ebElement*, ebElementPtrArray);
00151 
00152 #endif
00153     // ebUSE_COMPLEX_ATTRIBUTES
00154 
00155 
00160 class ebBasicInfo: public wxObject
00161 {
00162 public:
00163     ebBasicInfo();
00164     ebBasicInfo(const ebBasicInfo& info) { Copy(info); }
00165 
00166     void Copy(const ebBasicInfo& info);
00167     void operator=(const ebBasicInfo& info) { Copy(info); }
00168 
00169     wxString GetAuthor() const;
00170     void SetAuthor(const wxString& author);
00171 
00172     wxString GetTitle() const;
00173     void SetTitle(const wxString& value);
00174 
00175     wxString GetId() const;
00176     void SetId(const wxString& value);
00177 
00178     wxString GetPublisher() const;
00179     void SetPublisher(const wxString& value);
00180 
00181     wxString GetLanguage() const;
00182     void SetLanguage(const wxString& value);
00183 
00184     wxString GetDescription() const;
00185     void SetDescription(const wxString& value);
00186 
00187     wxString GetContributors() const;
00188     void SetContributors(const wxString& value);
00189 
00190     wxString GetUrl() const;
00191     void SetUrl(const wxString& value);
00192 
00193     wxString GetSubject() const;
00194     void SetSubject(const wxString& value);
00195 
00196     wxString GetDate() const;
00197     void SetDate(const wxString& value);
00198 
00199     wxString GetRights() const;
00200     void SetRights(const wxString& value);
00201 
00202     wxString GetCoverage() const;
00203     void SetCoverage(const wxString& value);
00204 
00205     wxString GetFormat() const;
00206     void SetFormat(const wxString& value);
00207 
00208     wxString GetType() const;
00209     void SetType(const wxString& value);
00210 
00211     wxString GetSource() const;
00212     void SetSource(const wxString& value);
00213 
00214     wxString GetRelation() const;
00215     void SetRelation(const wxString& value);
00216 
00217 #if ebUSE_COMPLEX_ATTRIBUTES
00218     ebElement       m_title;
00219     ebElement       m_id;
00220     ebElement       m_language;
00221     ebElement       m_author;
00222     ebElement       m_contributors;
00223     ebElement       m_publisher;
00224     ebElement       m_url;
00225     ebElement       m_subject;
00226     ebElement       m_description;
00227     ebElement       m_date;
00228     ebElement       m_rights;
00229     ebElement       m_coverage;
00230     ebElement       m_type;
00231     ebElement       m_format;
00232     ebElement       m_source;
00233     ebElement       m_relation;
00234 #else
00235     wxString        m_title;
00236     wxString        m_id;
00237     wxString        m_language;
00238     wxString        m_author;
00239     wxString        m_contributors;
00240     wxString        m_publisher;
00241     wxString        m_url;
00242     wxString        m_subject;
00243     wxString        m_description;
00244     wxString        m_date;
00245     wxString        m_rights;
00246     wxString        m_coverage;
00247     wxString        m_type;
00248     wxString        m_format;
00249     wxString        m_source;
00250     wxString        m_relation;
00251 #endif
00252 };
00253 
00258 class ebEpubOptions: public wxObject
00259 {
00260 public:
00261     ebEpubOptions() { Init(); }
00262     ebEpubOptions(const ebEpubOptions& options) { Copy(options); }
00263 
00264     void Init();
00265     void Copy(const ebEpubOptions& options);
00266     void operator=(const ebEpubOptions& options) { Copy(options); }
00267 
00268     bool            m_generateEpub;
00269     bool            m_generateMobiPocket;
00270 
00271     bool            m_generateTitlePage; // not currently used; may be used if we eventually allow
00272                                          // the user to add title, author etc.
00273     bool            m_generateTOC;
00274     bool            m_generateGuide;
00275     bool            m_generateCoverPage;
00276     bool            m_generateCSS;
00277     bool            m_expandCoverImage;
00278     bool            m_allowHtmlInText;
00279     bool            m_takeTitleFromFirstLine;
00280     wxString        m_importEncoding; // For text files
00281 
00283     int             m_firstLineIndent; // in mm
00284     int             m_paragraphSpacing; // in pt
00285 
00286     int             m_compressionLevel;
00287 };
00288 
00293 class ebHtmlFile: public wxObject
00294 {
00295 public:
00296     ebHtmlFile() { Init(); }
00297     ebHtmlFile(const ebHtmlFile& f) { Copy(f); }
00298 
00299     void Init();
00300     void Copy(const ebHtmlFile& f);
00301     void operator=(const ebHtmlFile& f) { Copy(f); }
00302 
00304     bool IsHtmlFile() const;
00305 
00307     bool IsTextFile() const;
00308 
00310     wxString GetAbsoluteHtmlFile(const ebFileLocations& locations) const;
00311 
00313     wxString GetAbsoluteFile(const ebFileLocations& locations) const;
00314 
00316     wxString GetRelativeHtmlFile(const ebFileLocations& locations) const;
00317 
00319     wxString GetRelativeFile(const ebFileLocations& locations) const;
00320 
00321     wxString        m_fileLocation;
00322     wxString        m_guideType;
00323     wxString        m_tocTitle;
00324     bool            m_showInTOC;
00325     int             m_level;
00326 };
00327 
00328 WX_DECLARE_OBJARRAY(ebHtmlFile, ebHtmlFileArray);
00329 
00334 class ebFileLocations: public wxObject
00335 {
00336 public:
00337     ebFileLocations();
00338     ebFileLocations(const ebFileLocations& info) { Copy(info); }
00339 
00340     void Copy(const ebFileLocations& info, bool exceptHtmlFiles = false);
00341     void operator=(const ebFileLocations& info) { Copy(info); }
00342 
00343     wxString GetAbsoluteLocation(const wxString& path) const;
00344 
00346     int FindHtmlFileByFilename(const wxString& filename);
00347 
00348     wxString        m_projectFolder;
00349     wxString        m_workingFolder;
00350     wxString        m_destinationFile;  // The .epub
00351     wxString        m_cssFile;          // Source for custom CSS file
00352     wxString        m_coverImageFile;   // Source for cover image file
00353     ebHtmlFileArray m_htmlFiles;        // HTML content files
00354 };
00355 
00362 class ebGeneratedContentInfo: public wxObject
00363 {
00364 public:
00365     ebGeneratedContentInfo() { Init(); }
00366     ebGeneratedContentInfo(const ebGeneratedContentInfo& content) { Copy(content); }
00367 
00368     void Init();
00369     void Copy(const ebGeneratedContentInfo& content);
00370     void operator=(const ebGeneratedContentInfo& content) { Copy(content); }
00371 
00372     wxString    m_tocFilename;
00373     wxString    m_tocTitle;
00374 
00375     wxString    m_coverPageFilename;
00376     wxString    m_coverPageTitle;
00377 
00378     wxString    m_titlePageFilename;
00379     wxString    m_titlePageTitle;
00380 };
00381 
00382 
00387 #define ebEPUB_OUTPUT_ERRORS_ONLY           0
00388 #define ebEPUB_OUTPUT_ERRORS_AND_WARNINGS   1
00389 #define ebEPUB_OUTPUT_VERBOSE               2
00390 
00391 class ebEpubGenerator: public wxObject
00392 {
00393 public:
00394 
00395     ebEpubGenerator() { Init(); }
00396     ebEpubGenerator(const ebEpubGenerator& gen) { Copy(gen); }
00397     ebEpubGenerator(const ebBasicInfo& info, const ebFileLocations& locations, const ebEpubOptions& options)
00398     { Init(); m_basicInfo = info; m_locations = locations; m_options = options; }
00399 
00400     void Init();
00401     void operator=(const ebEpubGenerator& gen) { Copy(gen); }
00402     void Copy(const ebEpubGenerator& gen);
00403 
00404     ebFileLocations& GetFileLocations() { return m_locations; }
00405     const ebFileLocations& GetFileLocations() const { return m_locations; }
00406 
00407     ebBasicInfo& GetBasicInfo() { return m_basicInfo; }
00408     const ebBasicInfo& GetBasicInfo() const { return m_basicInfo; }
00409 
00410     ebEpubOptions& GetOptions() { return m_options; }
00411     const ebEpubOptions& GetOptions() const { return m_options; }
00412 
00413     const wxString& GetMobigenCommand() const { return m_mobigenCommand; }
00414     wxString& GetMobigenCommand() { return m_mobigenCommand; }
00415     void SetMobigenCommand(const wxString& cmd) { m_mobigenCommand = cmd; }
00416 
00418     void SetVerbosity(int level) { m_verbosity = level; }
00419     int GetVerbosity() const { return m_verbosity; }
00420 
00422     bool GenerateEpub();
00423 
00425     const wxString& GetLog() const { return m_log; }
00426 
00428     bool DoGenerate();
00429 
00431     void Log(const wxString& msg, int minLevel = ebEPUB_OUTPUT_ERRORS_ONLY);
00432 
00434     void ClearLog() { m_log = wxEmptyString; }
00435 
00437     void Clear();
00438 
00440     bool InitialChecks();
00441 
00443     bool CreateMimeTypeFile();
00444 
00446     bool CreateContainerFile();
00447 
00449     bool CreateContentFile();
00450 
00452     bool CreateNCXFile();
00453 
00455     bool CreateHTMLTOCFile();
00456 
00458     bool CreateTitlePageFile();
00459 
00461     bool CreateCoverPageFile();
00462 
00464     bool CreateCSSFile();
00465 
00467     bool CreateHtmlFiles();
00468 
00470     bool CopyImageFiles();
00471 
00473     bool GetImageFiles();
00474 
00476     bool CopyOtherFiles();
00477 
00479     bool CreateEpubFile();
00480 
00482     bool CreateMobiPocketFile();
00483 
00485     wxString GetContentFolder() const;
00486 
00488     wxString GetCoverPageFilename() const;
00489 
00491     wxString AppendPaths(const wxString& path1, const wxString& path2) const;
00492 
00494     bool CreateZipArchive(const wxString& zipArchiveName, const wxArrayString& filenames, const wxArrayString& archiveNames, int compressionLevel);
00495 
00497     bool ConvertTextFilesToHTML(int flags, wxMBConv& conv);
00498 
00500     bool CanGenerateCoverPage() const;
00501 
00503     bool ConvertFromExistingEbook(const wxString& ebookFilename, const wxString& utilityDir, wxArrayString& msgs);
00504 
00506     void SetStringTable(wxStringTable* table) { m_stringTable = table; }
00507     wxStringTable* GetStringTable() const { return m_stringTable; }
00508 
00510     wxString GetStringFromTable(const wxString& name) const;
00511 
00513     static int GetTitleHeadingSize() ;
00514 
00516     static wxString GetHeadingString(int size);
00517 
00519     static bool WriteHTMLHead(wxTextOutputStream& textStream, const wxString& title, const wxString& cssFilename = wxEmptyString);
00520 
00522 
00523     #define ebHTML_CONVERSION_FIRST_LINE_IS_TITLE   0x01
00524     #define ebHTML_CONVERSION_ALLOW_HTML_IN_TEXT    0x02
00525 
00526     static bool ConvertFromTextToHTML(const wxString& textFile, const wxString& htmlFile, wxString& title, const wxString& cssFile, int flags = ebHTML_CONVERSION_FIRST_LINE_IS_TITLE, wxMBConv& conv = wxConvUTF8);
00527 
00529     static bool GetTextFileTitle(const wxString& textFile, wxString& title, wxMBConv& conv);
00530 
00532     static wxString IndentString(const wxString& str, int level, int spacesPerLevel = 4);
00533 
00535     static bool CopyFileWithDirCreation(const wxString& file1, const wxString& file2);
00536 
00539     static bool GetFilesUnder(wxArrayString& files, const wxString& dir, const wxString& excludingDir, const wxString& exts);
00540     static bool GetFilesUnder(wxArrayString& files, const wxString& topDir, const wxString& dir, const wxString& excludingDir, const wxString& exts);
00541 
00543     static wxString MakeIdFromFilename(const wxString& filename);
00544 
00546     static void AddCreatorAttributes(ebElement& element, bool onlyResetChoices = false);
00547 
00549     static void AddContributorAttributes(ebElement& element, bool onlyResetChoices = false);
00550 
00552     static void AddIdentifierAttributes(ebElement& element, bool onlyResetChoices = false);
00553 
00555     static void AddPublisherAttributes(ebElement& element, bool onlyResetChoices = false);
00556 
00558     static void AddDateAttributes(ebElement& element, bool onlyResetChoices = false);
00559 
00561     static void AddLanguageAttributes(ebElement& element, bool onlyResetChoices = false);
00562 
00563 private:
00564     ebFileLocations m_locations;
00565     ebBasicInfo     m_basicInfo;
00566     ebEpubOptions   m_options;
00567 
00568     wxString        m_log;
00569     int             m_verbosity;
00570     
00571     wxArrayString   m_relativeZipFiles;
00572     wxArrayString   m_absoluteZipFiles;
00573 
00574     wxArrayString   m_imageFiles;
00575 
00576     wxString        m_mobigenCommand;
00577 
00578     wxStringTable*  m_stringTable;
00579 };
00580 
00585 class wxXmlNode;
00586 
00587 class ebEpubParser: public wxXMLParser
00588 {
00589 public:
00590 
00591     ebEpubParser() { Init(); }
00592     void Init();
00593 
00595     bool ReadBasicInfo(const wxString& filename, ebBasicInfo& info, wxString& coverImageZipName);
00596 
00598     bool ReadAdvancedInfo(const wxString& filename, ebBasicInfo& info, ebFileLocations& locations, wxString& opfLocation, wxString& coverImageFilename, wxString& coverPageFilename);
00599 
00601     bool ReadOPF(wxInputStream& stream, ebBasicInfo& info, ebFileLocations& locations, wxString& coverImageFilename, wxString& coverPageFilename);
00602     bool ReadOPF(const wxString& filename, ebBasicInfo& info, ebFileLocations& locations, wxString& coverImageFilename, wxString& coverPageFilename);
00603 
00605     bool ExtractFileFromArchive(const wxString& archiveName, const wxString& sourceFilename, const wxString& destFilename);
00606 
00608     bool CreateImageBlockFromZipImage(const wxString& archiveName, const wxString& sourceFilename, wxImageBlock& imageBlock, wxImage& image);
00609 
00611     bool ReadElement(ebElement& element, const wxString& elementName, wxXmlNode* node);
00612 
00614     bool ExtractOPFToFile(const wxString& epubFilename, wxString& opfFilename, wxString& opfLocationInZip);
00615 };
00616 
00617 // Returns the string between start and end tags if found,
00618 // setting 'str' to the remaining part.
00619 wxString ebFindTag(const wxString& str, const wxString& tag);
00620 wxString ebFindTag(wxString& str, const wxString& tag);
00621 
00622 // Find title for HTML file
00623 wxString ebFindTitle(const wxString& htmlFilename);
00624 
00626 wxString ebMakeRelative(const wxString& filename, const wxString& relativeTo);
00627 
00628 #endif
00629     // _EPUB_UTILS_H_

Generated on Wed May 6 19:20:19 2009 for AxTk by  doxygen 1.5.1