public Set ( string key, string value, bool isCollectionValue ) : void | ||
key | string | |
value | string | |
isCollectionValue | bool | |
return | void |
/// <summary> /// Copy the copyright & license info to the originalCopyrightAndLicense, /// then remove the copyright so the translator can put in their own if they /// want. We retain the license, but the translator is allowed to change that. /// If the source is already a translation (already has original copyright or license) /// we keep them unchanged. /// </summary> public static void SetOriginalCopyrightAndLicense(HtmlDom dom, BookData bookData, CollectionSettings collectionSettings) { // At least one of these should exist if the source was a derivative, since we don't allow a // book to have no license, nor to be uploaded without copyright...unless of course it was derived // before 3.9, when we started doing this. In that case the best we can do is record the earliest // information we have for this and later adaptations. if (bookData.GetMultiTextVariableOrEmpty("originalLicenseUrl").Count > 0 || bookData.GetMultiTextVariableOrEmpty("originalLicenseNotes").Count > 0 || bookData.GetMultiTextVariableOrEmpty("originalCopyright").Count > 0) { return; //leave the original there. } // If there's no copyright information in a source-collection book, we're presumably making // a new original book, and shouldn't try to record any original copyright and license information. // This is somewhat redundant with the check in BookStarter.SetupNewDocumentContents(), the one // non-unit-test current caller of this method, that doesn't call this at all if the source is // a template book. I was trying for a minimal reasonable change for BL-5131, and therefore // put in this extra check, since previously this method was simply NEVER called in a source // collection. var copyrightNotice = GetMetadata(dom).CopyrightNotice; if (string.IsNullOrEmpty(copyrightNotice) && collectionSettings.IsSourceCollection) { return; } bookData.Set("originalLicenseUrl", GetLicenseUrl(dom), "*"); bookData.Set("originalCopyright", System.Web.HttpUtility.HtmlEncode(copyrightNotice), "*"); bookData.Set("originalLicenseNotes", dom.GetBookSetting("licenseNotes").GetFirstAlternative(), "*"); bookData.RemoveAllForms("copyright"); // RemoveAllForms does modify the dom }
/// <summary> /// This is used when a book is first created from a source; without it, if the shell maker left the book as trilingual when working on it, /// then everytime someone created a new book based on it, it too would be trilingual. /// </summary> public static void SetInitialMultilingualSetting(BookData bookData, int oneTwoOrThreeContentLanguages, CollectionSettings collectionSettings) { //var multilingualClass = new string[]{"bloom-monolingual", "bloom-bilingual","bloom-trilingual"}[oneTwoOrThreeContentLanguages-1]; if (oneTwoOrThreeContentLanguages < 3) bookData.RemoveAllForms("contentLanguage3"); if (oneTwoOrThreeContentLanguages < 2) bookData.RemoveAllForms("contentLanguage2"); bookData.Set("contentLanguage1", collectionSettings.Language1Iso639Code, false); if (oneTwoOrThreeContentLanguages > 1) bookData.Set("contentLanguage2", collectionSettings.Language2Iso639Code, false); if (oneTwoOrThreeContentLanguages > 2 && !string.IsNullOrEmpty(collectionSettings.Language3Iso639Code)) bookData.Set("contentLanguage3", collectionSettings.Language3Iso639Code, false); }
private static void SetBookTitle(BookStorage storage, BookData bookData) { //NB: no multi-lingual name suggestion ability yet //otherwise, the case where there is no defaultNameForDerivedBooks, we just want to use the names //that the shell used, e.g. "Vaccinations". //We don't have to do anything special to get that. string kdefaultName = null; var nameSuggestion = storage.Dom.GetMetaValue("defaultNameForDerivedBooks", kdefaultName); // var nameSuggestion = storage.Dom.SafeSelectNodes("//head/meta[@name='defaultNameForDerivedBooks']"); if (nameSuggestion != null) { bookData.Set("bookTitle", nameSuggestion, "en"); storage.MetaData.Title = nameSuggestion; } storage.Dom.RemoveMetaElement("defaultNameForDerivedBooks"); // //var name = "New Book"; //shouldn't rarel show up, because it will be overriden by the meta tag // if (nameSuggestion.Count > 0) // { // var metaTag = (XmlElement) nameSuggestion[0]; // var name = metaTag.GetAttribute("content"); // bookData.SetDataDivBookVariable("bookTitle", name, "en"); // metaTag.ParentNode.RemoveChild(metaTag); // } // else // { // // } }
/// <summary> /// Copy the copyright & license info to the originalCopyrightAndLicense, /// then remove the copyright so the translator can put in their own if they /// want. We retain the license, but the translator is allowed to change that. /// </summary> public static void SetOriginalCopyrightAndLicense(HtmlDom dom, BookData bookData, CollectionSettings collectionSettings) { if (bookData.GetMultiTextVariableOrEmpty("originalCopyrightAndLicense").Count > 0) { return; //leave the original there. } var metadata = BookCopyrightAndLicense.GetMetadata(dom); string idOfLanguageUsed; var languagePriorityIds = collectionSettings.LicenseDescriptionLanguagePriorities; //TODO HOW DO I GET THESE IN THE NATIONAL LANGUAGE INSTEAD OF THE UI LANGUAGE? var license = metadata.License.GetMinimalFormForCredits(languagePriorityIds, out idOfLanguageUsed); string originalLicenseSentence; var preferredLanguageIds = new[] { collectionSettings.Language2Iso639Code, LocalizationManager.UILanguageId, "en" }; if (metadata.License is CustomLicense) { // I can imagine being more fancy... something like "Licensed under custom license:", and get localizations // for that... but sheesh, these are even now very rare in Bloom-land and should become more rare. // So for now, let's just print the custom license contents. originalLicenseSentence = license; } else { var licenseSentenceTemplate = LocalizationManager.GetString("EditTab.FrontMatter.OriginalLicenseSentence", "Licensed under {0}.", "On the Credits page of a book being translated, Bloom puts texts like 'Licensed under CC-BY', so that we have a record of what the license was for the original book. Put {0} in the translation, where the license should go in the sentence.", preferredLanguageIds, out idOfLanguageUsed); originalLicenseSentence = string.IsNullOrWhiteSpace(license) ? "" : string.Format(licenseSentenceTemplate, license); originalLicenseSentence = originalLicenseSentence.Replace("..", "."); // in case had notes which also had a period. } Console.WriteLine(originalLicenseSentence); var copyrightNotice = ""; if (string.IsNullOrWhiteSpace(metadata.CopyrightNotice)) { var noCopyrightSentence = LocalizationManager.GetString("EditTab.FrontMatter.OriginalHadNoCopyrightSentence", "Adapted from original without a copyright notice.", "On the Credits page of a book being translated, Bloom shows this if the original book did not have a copyright notice.", preferredLanguageIds, out idOfLanguageUsed); copyrightNotice = noCopyrightSentence + " " + originalLicenseSentence; } else { var originalCopyrightSentence = LocalizationManager.GetString("EditTab.FrontMatter.OriginalCopyrightSentence", "Adapted from original, {0}.", "On the Credits page of a book being translated, Bloom shows the original copyright. Put {0} in the translation where the copyright notice should go. For example in English, 'Adapted from original, {0}.' comes out like 'Adapted from original, Copyright 2011 SIL'.", preferredLanguageIds, out idOfLanguageUsed); copyrightNotice = String.Format(originalCopyrightSentence, metadata.CopyrightNotice.Trim()) + " " + originalLicenseSentence; } Console.WriteLine(copyrightNotice); bookData.Set("originalCopyrightAndLicense", copyrightNotice, "*"); bookData.RemoveAllForms("copyright"); // RemoveAllForms does modify the dom }
/// <summary> /// This is used when a book is first created from a source; without it, if the shell maker left the book as trilingual when working on it, /// then every time someone created a new book based on it, it too would be trilingual. /// </summary> /// <remarks> /// This method explicitly used the CollectionSettings languages in creating a new book. /// </remarks> public static void SetInitialMultilingualSetting(BookData bookData, int oneTwoOrThreeContentLanguages) { //var multilingualClass = new string[]{"bloom-monolingual", "bloom-bilingual","bloom-trilingual"}[oneTwoOrThreeContentLanguages-1]; if (oneTwoOrThreeContentLanguages < 3) { bookData.RemoveAllForms("contentLanguage3"); } if (oneTwoOrThreeContentLanguages < 2) { bookData.RemoveAllForms("contentLanguage2"); } var language1 = bookData.CollectionSettings.Language1; bookData.Set("contentLanguage1", XmlString.FromUnencoded(language1.Iso639Code), false); bookData.Set("contentLanguage1Rtl", XmlString.FromUnencoded(language1.IsRightToLeft.ToString()), false); if (oneTwoOrThreeContentLanguages > 1) { var language2 = bookData.CollectionSettings.Language2; bookData.Set("contentLanguage2", XmlString.FromUnencoded(language2.Iso639Code), false); bookData.Set("contentLanguage2Rtl", XmlString.FromUnencoded(language2.IsRightToLeft.ToString()), false); } var language3 = bookData.CollectionSettings.Language3; if (oneTwoOrThreeContentLanguages > 2 && !String.IsNullOrEmpty(language3.Iso639Code)) { bookData.Set("contentLanguage3", XmlString.FromUnencoded(language3.Iso639Code), false); bookData.Set("contentLanguage3Rtl", XmlString.FromUnencoded(language3.IsRightToLeft.ToString()), false); } }
/// <summary> /// This is used when a book is first created from a source; without it, if the shell maker left the book as trilingual when working on it, /// then everytime someone created a new book based on it, it too would be trilingual. /// </summary> public static void SetInitialMultilingualSetting(BookData bookData, int oneTwoOrThreeContentLanguages, CollectionSettings collectionSettings) { //var multilingualClass = new string[]{"bloom-monolingual", "bloom-bilingual","bloom-trilingual"}[oneTwoOrThreeContentLanguages-1]; if (oneTwoOrThreeContentLanguages < 3) { bookData.RemoveAllForms("contentLanguage3"); } if (oneTwoOrThreeContentLanguages < 2) { bookData.RemoveAllForms("contentLanguage2"); } bookData.Set("contentLanguage1", collectionSettings.Language1Iso639Code, false); bookData.Set("contentLanguage1Rtl", collectionSettings.IsLanguage1Rtl.ToString(), false); if (oneTwoOrThreeContentLanguages > 1) { bookData.Set("contentLanguage2", collectionSettings.Language2Iso639Code, false); bookData.Set("contentLanguage2Rtl", collectionSettings.IsLanguage2Rtl.ToString(), false); } if (oneTwoOrThreeContentLanguages > 2 && !string.IsNullOrEmpty(collectionSettings.Language3Iso639Code)) { bookData.Set("contentLanguage3", collectionSettings.Language3Iso639Code, false); bookData.Set("contentLanguage3Rtl", collectionSettings.IsLanguage3Rtl.ToString(), false); } }
/// <summary> /// Copy the copyright & license info to the originalCopyrightAndLicense, /// then remove the copyright so the translator can put in their own if they /// want. We retain the license, but the translator is allowed to change that. /// If the source is already a translation (already has original copyright or license) /// we keep them unchanged. /// </summary> public static void SetOriginalCopyrightAndLicense(HtmlDom dom, BookData bookData, CollectionSettings collectionSettings) { // If it already has some of this information, just keep it. if (bookData.BookIsDerivative()) { return; //leave the original there. } // If there's no copyright information in a source-collection book, we're presumably making // a new original book, and shouldn't try to record any original copyright and license information. // This is somewhat redundant with the check in BookStarter.SetupNewDocumentContents(), the one // non-unit-test current caller of this method, that doesn't call this at all if the source is // a template book. I was trying for a minimal reasonable change for BL-5131, and therefore // put in this extra check, since previously this method was simply NEVER called in a source // collection. var copyrightNotice = BookCopyrightAndLicense.GetMetadata(dom).CopyrightNotice; if (String.IsNullOrEmpty(copyrightNotice) && collectionSettings.IsSourceCollection) { return; } bookData.Set("originalLicenseUrl", BookCopyrightAndLicense.GetLicenseUrl(dom), "*"); bookData.Set("originalCopyright", System.Web.HttpUtility.HtmlEncode(copyrightNotice), "*"); bookData.Set("originalLicenseNotes", dom.GetBookSetting("licenseNotes").GetFirstAlternative(), "*"); }
private static void SetBookTitle(BookStorage storage, BookData bookData) { //NB: no multi-lingual name suggestion ability yet //otherwise, the case where there is no defaultNameForDerivedBooks, we just want to use the names //that the shell used, e.g. "Vaccinations". //We don't have to do anything special to get that. string kdefaultName = null; var nameSuggestion = storage.Dom.GetMetaValue("defaultNameForDerivedBooks", kdefaultName); // var nameSuggestion = storage.Dom.SafeSelectNodes("//head/meta[@name='defaultNameForDerivedBooks']"); if(nameSuggestion!=null) bookData.Set("bookTitle",nameSuggestion,"en"); storage.Dom.RemoveMetaElement("defaultNameForDerivedBooks"); // //var name = "New Book"; //shouldn't rarel show up, because it will be overriden by the meta tag // if (nameSuggestion.Count > 0) // { // var metaTag = (XmlElement) nameSuggestion[0]; // var name = metaTag.GetAttribute("content"); // bookData.SetDataDivBookVariable("bookTitle", name, "en"); // metaTag.ParentNode.RemoveChild(metaTag); // } // else // { // // } }
public void UpdateVariablesAndDataDivThroughDOM_VariableIsNull_DataDivForItRemoved() { var htmlDom = new HtmlDom(); var data = new BookData(htmlDom, new CollectionSettings(), null); data.Set("1","one","en"); data.Set("1", null, "es"); data.UpdateVariablesAndDataDivThroughDOM(); AssertThatXmlIn.Dom(htmlDom.RawDom).HasSpecifiedNumberOfMatchesForXpath("html/body/div/div[@lang='en']",1); AssertThatXmlIn.Dom(htmlDom.RawDom).HasSpecifiedNumberOfMatchesForXpath("html/body/div/div[@lang='es']", 0); }
public void Set_Null_Removes() { var htmlDom = new HtmlDom(); var data = new BookData(htmlDom, new CollectionSettings(), null); data.Set("1", "one", "en"); data.Set("1", null, "en"); Assert.AreEqual(null, data.GetVariableOrNull("1", "en")); AssertThatXmlIn.Dom(htmlDom.RawDom).HasSpecifiedNumberOfMatchesForXpath("//div[@lang='en']", 0); var roundTripData = new BookData(htmlDom, new CollectionSettings(), null); Assert.IsNull(roundTripData.GetVariableOrNull("1", "en")); }
public void Set_DidNotHaveForm_Added() { var htmlDom = new HtmlDom(); var data = new BookData(htmlDom, new CollectionSettings(), null); data.Set("1", "one", "en"); Assert.AreEqual("one", data.GetVariableOrNull("1", "en")); AssertThatXmlIn.Dom(htmlDom.RawDom).HasSpecifiedNumberOfMatchesForXpath("//div[@lang='en']",1); var roundTripData = new BookData(htmlDom, new CollectionSettings(), null); var t = roundTripData.GetVariableOrNull("1", "en"); Assert.AreEqual("one", t); }
public void Set_CalledTwiceWithDIfferentLangs_HasBoth() { var htmlDom = new HtmlDom(); var data = new BookData(htmlDom, new CollectionSettings(), null); data.Set("1", "one", "en"); data.Set("1", "uno", "es"); Assert.AreEqual(2,data.GetMultiTextVariableOrEmpty("1").Forms.Count()); }
public void Set_AddTwoForms_BothAdded() { var htmlDom = new HtmlDom(); var data = new BookData(htmlDom, new CollectionSettings(), null); data.Set("1", "one", "en"); data.Set("1", "uno", "es"); var roundTripData = new BookData(htmlDom, new CollectionSettings(), null); Assert.AreEqual("one", roundTripData.GetVariableOrNull("1", "en")); Assert.AreEqual("uno", roundTripData.GetVariableOrNull("1", "es")); }
public void RemoveSingleForm_HasForm_Removed() { var htmlDom = new HtmlDom(); var data = new BookData(htmlDom, new CollectionSettings(), null); data.Set("1","one","en"); var data2 = new BookData(htmlDom, new CollectionSettings(), null); data2.RemoveSingleForm("1","en"); Assert.IsNull(data2.GetVariableOrNull("1", "en")); }
public void RemoveDataDivVariableForOneLanguage_WasTwoForms_OtherRemains() { var htmlDom = new HtmlDom(); var data = new BookData(htmlDom, new CollectionSettings(), null); data.Set("1", "one", "en"); data.Set("1", "uno", "es"); var roundTripData = new BookData(htmlDom, new CollectionSettings(), null); roundTripData.RemoveSingleForm("1", "en"); Assert.IsNull(roundTripData.GetVariableOrNull("1", "en")); Assert.AreEqual("uno",roundTripData.GetVariableOrNull("1","es")); }
public void RemoveDataDivVariableForOneLanguage_WasLastForm_WholeElementRemoved() { var htmlDom = new HtmlDom(); var data = new BookData(htmlDom, new CollectionSettings(), null); data.Set("1","one","en"); var roundTripData = new BookData(htmlDom, new CollectionSettings(), null); roundTripData.RemoveSingleForm("1", "en"); Assert.IsNull(roundTripData.GetVariableOrNull("1", "en")); }