GetMetaValue() public method

public GetMetaValue ( string name, string defaultValue ) : string
name string
defaultValue string
return string
        /// <summary>
        /// we update these so that the file continues to look the same when you just open it in firefox
        /// </summary>
        private void UpdateSupportFiles()
        {
            if (IsPathReadonly(_folderPath))
            {
                Logger.WriteEvent("Not updating files in folder {0} because the directory is read-only.", _folderPath);
            }
            else
            {
                UpdateIfNewer("placeHolder.png");
                UpdateIfNewer("basePage.css");
                UpdateIfNewer("previewMode.css");
                UpdateIfNewer("origami.css");

                foreach (var path in Directory.GetFiles(_folderPath, "*.css"))
                {
                    var file = Path.GetFileName(path);
                    //if (file.ToLower().Contains("portrait") || file.ToLower().Contains("landscape"))
                    UpdateIfNewer(file);
                }
            }

            //by default, this comes from the collection, but the book can select one, inlucing "null" to select the factory-supplied empty xmatter
            var nameOfXMatterPack = _dom.GetMetaValue("xMatter", _collectionSettings.XMatterPackName);
            var helper            = new XMatterHelper(_dom, nameOfXMatterPack, _fileLocator);

            UpdateIfNewer(Path.GetFileName(helper.PathToStyleSheetForPaperAndOrientation), helper.PathToStyleSheetForPaperAndOrientation);
        }
Example #2
0
        /// <summary>
        /// Constructs by finding the file and folder of the xmatter pack, given the its key name e.g. "Factory", "SILIndonesia".
        /// The default key name is provided as a method parameter, but that can be overridden by a value from inside the book.
        /// The name of the file should be (key)-XMatter.htm. The name and the location of the folder is not our problem...
        /// we leave it to the supplied fileLocator to find it.
        /// </summary>
        /// <param name="bookDom">The book's DOM</param>
        /// <param name="xmatterNameFromCollectionSettings">e.g. "Factory", "SILIndonesia".  This can be overridden inside the bookDom.</param>
        /// <param name="fileLocator">The locator needs to be able tell us the path to an xmatter html file, given its name</param>
        /// <param name="useDeviceVersionIfAvailable">If true, use a pack-specific device xmatter, or "Device" if none found. E.g. ABC => ABC-Device</param>
        public XMatterHelper(HtmlDom bookDom, string xmatterNameFromCollectionSettings, IFileLocator fileLocator, bool useDeviceVersionIfAvailable = false)
        {
            string directoryPath = null;

            _bookDom = bookDom;
            var bookSpecificXMatterPack = bookDom.GetMetaValue("xmatter", null);

            if (!String.IsNullOrWhiteSpace(bookSpecificXMatterPack))
            {
                bookSpecificXMatterPack = MigrateXMatterName(bookSpecificXMatterPack);
                _nameOfXMatterPack      = bookSpecificXMatterPack;
                if (useDeviceVersionIfAvailable)
                {
                    _nameOfXMatterPack = GetBestDeviceXMatterAvailable(_nameOfXMatterPack, fileLocator);
                }
                var errorTemplate = LocalizationManager.GetString("Errors.XMatterSpecifiedByBookNotFound",
                                                                  "This book called for a Front/Back Matter pack named '{0}', but this version of Bloom does not have it, and Bloom could not find it on this computer. The book has been changed to use the Front/Back Matter pages from the Collection Settings.");
                directoryPath = GetXMatterDirectory(_nameOfXMatterPack, fileLocator, String.Format(errorTemplate, bookSpecificXMatterPack), false);
                if (directoryPath == null)
                {
                    // Remove the xmatter specification from the DOM since it couldn't be found.
                    _bookDom.RemoveMetaElement("xmatter");
                }
            }
            if (directoryPath == null)
            {
                _nameOfXMatterPack = xmatterNameFromCollectionSettings;
                if (useDeviceVersionIfAvailable)
                {
                    _nameOfXMatterPack = GetBestDeviceXMatterAvailable(_nameOfXMatterPack, fileLocator);
                }
                directoryPath = GetXMatterDirectory(_nameOfXMatterPack, fileLocator, "It should not be possible to get an error here, because the collection verifies its xmatter name in CheckAndFixDependencies()", true);
            }
            var htmName = _nameOfXMatterPack + "-XMatter.html";

            PathToXMatterHtml = directoryPath.CombineForPath(htmName);
            if (!RobustFile.Exists(PathToXMatterHtml))
            {
                htmName           = _nameOfXMatterPack + "-XMatter.htm";       // pre- Bloom 3.7
                PathToXMatterHtml = directoryPath.CombineForPath(htmName);
            }
            if (!RobustFile.Exists(PathToXMatterHtml))
            {
                ErrorReport.NotifyUserOfProblem(new ShowOncePerSessionBasedOnExactMessagePolicy(), "Could not locate the file {0} in {1} (also checked .html)", htmName, directoryPath);
                throw new ApplicationException();
            }
            PathToXMatterStylesheet = directoryPath.CombineForPath(GetStyleSheetFileName());
            if (!RobustFile.Exists(PathToXMatterStylesheet))
            {
                ErrorReport.NotifyUserOfProblem(new ShowOncePerSessionBasedOnExactMessagePolicy(), "Could not locate the file {0} in {1}", GetStyleSheetFileName(), directoryPath);
                throw new ApplicationException();
            }
            XMatterDom = XmlHtmlConverter.GetXmlDomFromHtmlFile(PathToXMatterHtml, false);
        }
        private static void AddLocalizedHintContentsToDictionary(HtmlDom singlePageHtmlDom, Dictionary <string, string> dictionary, CollectionSettings collectionSettings)
        {
            var nameOfXMatterPack = singlePageHtmlDom.GetMetaValue("xMatter", collectionSettings.XMatterPackName);


            string idPrefix    = "";
            var    pageElement = singlePageHtmlDom.RawDom.SelectSingleNode("//div") as XmlElement;

            if (XMatterHelper.IsFrontMatterPage(pageElement))
            {
                idPrefix = "FrontMatter." + nameOfXMatterPack + ".";
            }
            else if (XMatterHelper.IsBackMatterPage(pageElement))
            {
                idPrefix = "BackMatter." + nameOfXMatterPack + ".";
            }
            foreach (XmlElement element in singlePageHtmlDom.RawDom.SelectNodes("//*[@data-hint]"))
            {
                //why aren't we just doing: element.SetAttribute("data-hint", translation);  instead of bothering to write out a dictionary?
                //because (especially since we're currently just assuming it is in english), we would later save it with the translation, and then next time try to translate that, and poplute the
                //list of strings that we tell people to translate
                var key = element.GetAttribute("data-hint");
                if (!dictionary.ContainsKey(key))
                {
                    string translation;
                    var    id = idPrefix + key;
                    if (key.Contains("{lang}"))
                    {
                        translation = LocalizationManager.GetDynamicString("Bloom", id, key, "Put {lang} in your translation, so it can be replaced by the language name.");
                    }
                    else
                    {
                        translation = LocalizationManager.GetDynamicString("Bloom", id, key);
                    }
                    dictionary.Add(key, translation);
                }
            }
        }
        public static string GetMessageIfVersionIsIncompatibleWithThisBloom(HtmlDom dom)
        {
            //var dom = new HtmlDom(XmlHtmlConverter.GetXmlDomFromHtmlFile(path, false));//with throw if there are errors

            var versionString = dom.GetMetaValue("BloomFormatVersion", "").Trim();

            if (string.IsNullOrEmpty(versionString))
            {
                return("");               // "This file lacks the following required element: <meta name='BloomFormatVersion' content='x.y'>";
            }
            float versionFloat = 0;

            if (!float.TryParse(versionString, out versionFloat))
            {
                return("This file claims a version number that isn't really a number: " + versionString);
            }

            if (versionFloat > float.Parse(kBloomFormatVersion))
            {
                return(string.Format("This book or template was made with a newer version of Bloom. Download the latest version of Bloom from bloomlibrary.org (format {0} vs. {1})", versionString, kBloomFormatVersion));
            }

            return(null);
        }
Example #5
0
        public static string GetHtmlMessageIfVersionIsIncompatibleWithThisBloom(HtmlDom dom,string path)
        {
            var versionString = dom.GetMetaValue("BloomFormatVersion", "").Trim();
            if (string.IsNullOrEmpty(versionString))
                return "";// "This file lacks the following required element: <meta name='BloomFormatVersion' content='x.y'>";

            float versionFloat = 0;
            if (!float.TryParse(versionString, NumberStyles.Float, CultureInfo.InvariantCulture, out versionFloat))
                return "This file claims a version number that isn't really a number: " + versionString;

            if (versionFloat > float.Parse(kBloomFormatVersion, CultureInfo.InvariantCulture))
            {
                var msg = LocalizationManager.GetString("Errors.NeedNewerVersion",
                    "{0} requires a newer version of Bloom. Download the latest version of Bloom from {1}","{0} will get the name of the book, {1} will give a link to open the Bloom Library Web page.");
                msg = string.Format(msg, path, string.Format("<a href='{0}'>BloomLibrary.org</a>", UrlLookup.LookupUrl(UrlType.LibrarySite)));
                msg += string.Format(". (Format {0} vs. {1})",versionString, kBloomFormatVersion);
                return msg;
            }

            return null;
        }
        private static void AddLocalizedHintContentsToDictionary(HtmlDom singlePageHtmlDom, Dictionary<string, string> dictionary, CollectionSettings collectionSettings)
        {
            var nameOfXMatterPack = singlePageHtmlDom.GetMetaValue("xMatter", collectionSettings.XMatterPackName);

            string idPrefix = "";
            var pageElement = singlePageHtmlDom.RawDom.SelectSingleNode("//div") as XmlElement;
            if (XMatterHelper.IsFrontMatterPage(pageElement))
            {
                idPrefix = "FrontMatter." + nameOfXMatterPack + ".";
            }
            else if (XMatterHelper.IsBackMatterPage(pageElement))
            {
                idPrefix = "BackMatter." + nameOfXMatterPack + ".";
            }
            foreach (XmlElement element in singlePageHtmlDom.RawDom.SelectNodes("//*[@data-hint]"))
            {
                //why aren't we just doing: element.SetAttribute("data-hint", translation);  instead of bothering to write out a dictionary?
                //because (especially since we're currently just assuming it is in english), we would later save it with the translation, and then next time try to translate that, and poplute the
                //list of strings that we tell people to translate
                var key = element.GetAttribute("data-hint");
                if (!dictionary.ContainsKey(key))
                {
                    string translation;
                    var id = idPrefix + key;
                    if (key.Contains("{lang}"))
                    {
                        translation = LocalizationManager.GetDynamicString("Bloom", id, key, "Put {lang} in your translation, so it can be replaced by the language name.");
                    }
                    else
                    {
                        translation = LocalizationManager.GetDynamicString("Bloom", id, key);
                    }
                    dictionary.Add(key, translation);
                }
            }
        }
Example #7
0
        private static void FixBookIdAndLineageIfNeeded(HtmlDom bookDOM)
        {
            //at version 0.9.71, we introduced this book lineage for real. At that point almost all books were from Basic book,
            //so let's get further evidence by looking at the page source and then fix the lineage
            if (bookDOM.GetMetaValue("bloomBookLineage", "") == "")
                if (bookDOM.GetMetaValue("pageTemplateSource", "") == "Basic Book")
                {
                    bookDOM.UpdateMetaElement("bloomBookLineage", kIdOfBasicBook);
                }

            //there were a number of books in version 0.9 that just copied the id of the basic book from which they were created
            if (bookDOM.GetMetaValue("bloomBookId", "") == kIdOfBasicBook)
            {
                if (bookDOM.GetMetaValue("title", "") != "Basic Book")
                {
                    bookDOM.UpdateMetaElement("bloomBookId", Guid.NewGuid().ToString());
                }
            }
        }