Exemplo n.º 1
0
        private static void CopyItemToFieldsInPages(HtmlDom dom, string key, string valueAttribute = null, string[] languagePreferences = null)
        {
            if (languagePreferences == null)
            {
                languagePreferences = new[] { "*", "en" }
            }
            ;

            MultiTextBase source = dom.GetBookSetting(key);

            if (key == "copyright")
            {
                // For CC0, we store the "copyright", but don't display it in the text of the book.
                var licenseUrl = dom.GetBookSetting("licenseUrl").GetExactAlternative("*");
                if (licenseUrl == CreativeCommonsLicense.CC0Url)
                {
                    source = new MultiTextBase();
                }
            }

            foreach (XmlElement target in dom.SafeSelectNodes("//*[@data-derived='" + key + "']"))
            {
                //just put value into the text of the element
                if (string.IsNullOrEmpty(valueAttribute))
                {
                    //clear out what's there now
                    target.RemoveAttribute("lang");
                    target.InnerText = "";

                    var form = source.GetBestAlternative(languagePreferences);
                    if (form != null && !string.IsNullOrWhiteSpace(form.Form))
                    {
                        // HtmlDom.GetBookSetting(key) returns the result of XmlNode.InnerXml which will be Html encoded (& < etc).
                        // HtmlDom.SetElementFromUserStringPreservingLineBreaks() calls XmlNode.InnerText, which Html encodes if necessary.
                        // So we need to decode here to prevent double encoding.  See http://issues.bloomlibrary.org/youtrack/issue/BL-4585.
                        // Note that HtmlDom.SetElementFromUserStringPreservingLineBreaks() handles embedded <br/> elements, but makes no
                        // effort to handle p or div elements.
                        var decoded = System.Web.HttpUtility.HtmlDecode(form.Form);
                        HtmlDom.SetElementFromUserStringSafely(target, decoded);
                        target.SetAttribute("lang", form.WritingSystemId);                         //this allows us to set the font to suit the language
                    }
                }
                else                 //Put the value into an attribute. The license image goes through this path.
                {
                    target.SetAttribute(valueAttribute, source.GetBestAlternativeString(languagePreferences));
                    if (source.Empty)
                    {
                        //if the license image is empty, make sure we don't have some alternative text
                        //about the image being missing or slow to load
                        target.SetAttribute("alt", "");
                        //over in javascript land, @alt will get set appropriately when the image url is not empty.
                    }
                }
            }
        }
Exemplo n.º 2
0
 private static void CopyStringToFieldsInPages(HtmlDom dom, string key, string val, string lang)
 {
     foreach (XmlElement target in dom.SafeSelectNodes("//*[@data-derived='" + key + "']"))
     {
         if (target == null)                 // don't think this can happen, but something like it seemed to in one test...
         {
             continue;
         }
         if (string.IsNullOrEmpty(val))
         {
             target.RemoveAttribute("lang");
             target.InnerText = "";
         }
         else
         {
             HtmlDom.SetElementFromUserStringSafely(target, val);
             target.SetAttribute("lang", lang);
         }
     }
 }