/// ------------------------------------------------------------------------------------ /// <summary> /// Gets or creates a mapping for the given marker. /// </summary> /// <param name="marker">The marker.</param> /// ------------------------------------------------------------------------------------ private ImportMappingInfo GetOrCreateMarkerMapping(ref string marker) { ImportMappingInfo markerMapping; if (m_scanInlineBackslashMarkers) { bool fMapEndMarker = false; if (marker.EndsWith("*")) { string beginMarker = marker.Substring(0, marker.Length - 1); if (m_mappingList[beginMarker] != null) { marker = beginMarker; fMapEndMarker = true; } } else if (m_mappingList[marker + "*"] != null) { // If the corresponding end marker is present in the mapping list // (as a begin marker), we need to remove it and instead map it as // an end marker. This is unlikely, but can happen if the data has // an errant end marker before the begin marker. m_mappingList.Delete(m_mappingList[marker + "*"]); fMapEndMarker = true; } markerMapping = m_mappingList.AddDefaultMappingIfNeeded(marker, m_domain, true); if (fMapEndMarker) { markerMapping.EndMarker = marker + "*"; } } else { markerMapping = m_mappingList.AddDefaultMappingIfNeeded(marker, m_domain, true); } return(markerMapping); }
public bool LoadProjectMappings(string project, ScrMappingList mappingList, ImportDomain domain) { // If the new project ID is null, then do not load mappings. if (project == null) { return(false); } // Load the tags from the paratext project and create mappings for them. ScrText scParatextText; try { // REVIEW (EberhardB): I'm not sure if ScrTextCollection.Get() returns a // reference to a ScrText or a new object (in which case we would have to // call Dispose() on it) scParatextText = ScrTextCollection.Get(project); } catch (Exception ex) { Logger.WriteError(ex); return(false); } mappingList.ResetInUseFlags(domain); try { foreach (ScrTag tag in scParatextText.DefaultStylesheet.Tags) { if (tag == null) { break; } string marker = @"\" + tag.Marker; string endMarker = string.Empty; if (!String.IsNullOrEmpty(tag.Endmarker)) { endMarker = @"\" + tag.Endmarker; } // When the nth marker has an end marker, the nth + 1 marker will be // that end marker. Therefore, we have to skip those "end style" markers. if (tag.StyleType == ScrStyleType.scEndStyle) { continue; } // Create a new mapping for this marker. mappingList.AddDefaultMappingIfNeeded(marker, endMarker, domain, false, false); } ScrParser parser = scParatextText.Parser(); foreach (int bookNum in scParatextText.BooksPresentSet.SelectedBookNumbers()) { foreach (UsfmToken token in parser.GetUsfmTokens(new VerseRef(bookNum, 0, 0), false, true)) { if (token.Marker == null) { continue; // Tokens alternate between text and marker types } ImportMappingInfo mapping = mappingList[@"\" + token.Marker]; if (mapping != null) { mapping.SetIsInUse(domain, true); } // ENHANCE (TE-4408): Consider Detecting markers that occur in the data but are missing // from the STY file. How can we write a test for this? //else if (ScrImportFileInfo.IsValidMarker(sMarker)) //{ // mappingList.AddDefaultMappingIfNeeded(sMarker,domain, false, true); //} //else //{ // throw new ScriptureUtilsException(SUE_ErrorCode.InvalidCharacterInMarker, null, 0, // sMarker + sText, new ScrReference(scParatextTextSegment.FirstReference.BBCCCVVV)); //} } } } catch (Exception ex) { Logger.WriteError(ex); return(false); } return(true); }
public void AddDefaultMappingIfNeeded_btNotFromTeStyle() { IVwStylesheet stylesheet = MockRepository.GenerateStrictMock<IVwStylesheet>(); ScrMappingList list = new ScrMappingList(MappingSet.Main, stylesheet); list.Add(new ImportMappingInfo(@"\h", null, false, MappingTargetType.TitleShort, MarkerDomain.Default, null, null)); list.AddDefaultMappingIfNeeded(@"\bth", ImportDomain.Main, true); list.Add(new ImportMappingInfo(@"\vt", null, false, MappingTargetType.DefaultParaChars, MarkerDomain.Default, null, null)); list.AddDefaultMappingIfNeeded(@"\btvt", ImportDomain.Main, true); Assert.AreEqual(4, list.Count); // Test that \bth maps automatically to the corresponding vernacular import property // in the Back-trans marker domain. ImportMappingInfo info = list[@"\bth"]; Assert.AreEqual(MarkerDomain.BackTrans, info.Domain); Assert.IsFalse(info.IsExcluded); Assert.AreEqual(MappingTargetType.TitleShort, info.MappingTarget); // Test that \btvt maps automatically to Default Paragraph Characters // in the Back-trans marker domain. info = list[@"\btvt"]; Assert.AreEqual(MarkerDomain.BackTrans, info.Domain); Assert.IsFalse(info.IsExcluded); Assert.AreEqual(MappingTargetType.DefaultParaChars, info.MappingTarget); }
public void AddDefaultMappingIfNeeded_btMappingsWithNonDefaultMappings() { IVwStylesheet stylesheet = MockRepository.GenerateStub<IVwStylesheet>(); stylesheet.Stub(x => x.GetContext("Emphasis")).Return((int)ContextValues.General); stylesheet.Stub(x => x.GetType("Emphasis")).Return((int)StyleType.kstCharacter); ScrMappingList list = new ScrMappingList(MappingSet.Main, stylesheet); list.Add(new ImportMappingInfo(@"\p", null, "Emphasis")); list.AddDefaultMappingIfNeeded(@"\btp", ImportDomain.Main, true); Assert.AreEqual(2, list.Count); // Test that \btp maps automatically to the corresponding vernacular style ("Emphasis") // but does not map into the Back-trans marker domain because Emphasis is a character style. ImportMappingInfo info = list[@"\btp"]; Assert.AreEqual(MarkerDomain.Default, info.Domain, @"\btp should not map automatically as a Back-trans marker"); Assert.IsFalse(info.IsExcluded); Assert.AreEqual(MappingTargetType.TEStyle, info.MappingTarget); Assert.AreEqual("Emphasis", info.StyleName); }
public void AddDefaultMappingIfNeeded_btMappings() { IVwStylesheet stylesheet = MockRepository.GenerateStub<IVwStylesheet>(); stylesheet.Stub(x => x.GetContext(ScrStyleNames.NormalFootnoteParagraph)).Return((int)ContextValues.Note); stylesheet.Stub(x => x.GetContext(ScrStyleNames.NormalParagraph)).Return((int)ContextValues.Text); stylesheet.Stub(x => x.GetContext("Emphasis")).Return((int)ContextValues.General); stylesheet.Stub(x => x.GetContext(ScrStyleNames.Remark)).Return((int)ContextValues.Annotation); stylesheet.Stub(x => x.GetType(ScrStyleNames.NormalFootnoteParagraph)).Return((int)StyleType.kstParagraph); stylesheet.Stub(x => x.GetType(ScrStyleNames.NormalParagraph)).Return((int)StyleType.kstParagraph); stylesheet.Stub(x => x.GetType("Emphasis")).Return((int)StyleType.kstCharacter); stylesheet.Stub(x => x.GetType(ScrStyleNames.Remark)).Return((int)StyleType.kstParagraph); ScrMappingList list = new ScrMappingList(MappingSet.Main, stylesheet); list.AddDefaultMappingIfNeeded(@"\bt", ImportDomain.Main, true); list.AddDefaultMappingIfNeeded(@"\btc", ImportDomain.Main, true); list.AddDefaultMappingIfNeeded(@"\btf", ImportDomain.Main, true); list.AddDefaultMappingIfNeeded(@"\btp", ImportDomain.Main, true); list.Add(new ImportMappingInfo(@"\emph", null, "Emphasis")); list.AddDefaultMappingIfNeeded(@"\btemph", ImportDomain.Main, true); list.AddDefaultMappingIfNeeded(@"\btrem", ImportDomain.Main, true); list.AddDefaultMappingIfNeeded(@"\bty", ImportDomain.Main, true); Assert.AreEqual(8, list.Count); // Test that \bt does not map automatically as a Back-trans marker. ImportMappingInfo info = list[@"\bt"]; Assert.AreEqual(MarkerDomain.Default, info.Domain, @"\bt should not map automatically as a Back-trans marker"); Assert.IsFalse(info.IsExcluded); Assert.AreEqual(MappingTargetType.TEStyle, info.MappingTarget); Assert.IsNull(info.StyleName); // Test that \btc does not map automatically as a Back-trans marker (this is a special exception to the rul). info = list[@"\btc"]; Assert.AreEqual(MarkerDomain.Default, info.Domain, @"\btc should not map automatically as a Back-trans marker"); Assert.IsFalse(info.IsExcluded); Assert.AreEqual(MappingTargetType.TEStyle, info.MappingTarget); Assert.IsNull(info.StyleName); // Test that \btf maps automatically as a Back-trans marker. info = list[@"\btf"]; Assert.AreEqual(MarkerDomain.BackTrans | MarkerDomain.Footnote, info.Domain, @"\btf should map automatically as a Back-trans marker"); Assert.IsFalse(info.IsExcluded); Assert.AreEqual(MappingTargetType.TEStyle, info.MappingTarget); Assert.AreEqual(ScrStyleNames.NormalFootnoteParagraph, info.StyleName); // Test that \btp maps automatically as a Back-trans marker. info = list[@"\btp"]; Assert.AreEqual(MarkerDomain.BackTrans, info.Domain, @"\btp should map automatically as a Back-trans marker"); Assert.IsFalse(info.IsExcluded); Assert.AreEqual(MappingTargetType.TEStyle, info.MappingTarget); Assert.AreEqual(ScrStyleNames.NormalParagraph, info.StyleName); // Test that \btemph maps automatically to the corresponding vernacular style but does not map // into the Back-trans marker domain because \emph is a character style. info = list[@"\btemph"]; Assert.AreEqual(MarkerDomain.Default, info.Domain, @"\btemph should not map automatically as a Back-trans marker"); Assert.IsFalse(info.IsExcluded); Assert.AreEqual(MappingTargetType.TEStyle, info.MappingTarget); Assert.AreEqual("Emphasis", info.StyleName); // Test that \btrem does not map automatically as a Back-trans marker (because \rem is a Note style). info = list[@"\btrem"]; Assert.AreEqual(MarkerDomain.Default, info.Domain, @"\btrem should not map automatically as a Back-trans marker"); Assert.IsFalse(info.IsExcluded); Assert.AreEqual(MappingTargetType.TEStyle, info.MappingTarget); Assert.IsNull(info.StyleName); // Test that \bty does not map automatically as a Back-trans marker (because \y has no default mapping). info = list[@"\bty"]; Assert.AreEqual(MarkerDomain.Default, info.Domain, @"\bty should not map automatically as a Back-trans marker"); Assert.IsFalse(info.IsExcluded); Assert.AreEqual(MappingTargetType.TEStyle, info.MappingTarget); Assert.IsNull(info.StyleName); }
public void AddDefaultMappingIfNeeded_FigureMarkers() { IVwStylesheet stylesheet = MockRepository.GenerateMock<IVwStylesheet>(); ScrMappingList list = new ScrMappingList(MappingSet.Main, stylesheet); list.AddDefaultMappingIfNeeded(@"\cap", ImportDomain.Main, true); list.AddDefaultMappingIfNeeded(@"\cat", ImportDomain.Main, true); list.AddDefaultMappingIfNeeded(@"\gmb", ImportDomain.Main, true); list.AddDefaultMappingIfNeeded(@"\gmbj", ImportDomain.Main, true); Assert.AreEqual(4, list.Count); ImportMappingInfo info = list[@"\cap"]; Assert.AreEqual(MarkerDomain.Default, info.Domain); Assert.IsFalse(info.IsExcluded); Assert.AreEqual(MappingTargetType.FigureCaption, info.MappingTarget); Assert.IsNull(info.StyleName); info = list[@"\cat"]; Assert.AreEqual(MarkerDomain.Default, info.Domain); Assert.IsFalse(info.IsExcluded); Assert.AreEqual(MappingTargetType.FigureFilename, info.MappingTarget); Assert.IsNull(info.StyleName); info = list[@"\gmb"]; Assert.AreEqual(MarkerDomain.Default, info.Domain); Assert.IsFalse(info.IsExcluded); Assert.AreEqual(MappingTargetType.FigureFilename, info.MappingTarget); Assert.IsNull(info.StyleName); info = list[@"\gmbj"]; Assert.AreEqual(MarkerDomain.Default, info.Domain); Assert.IsFalse(info.IsExcluded); Assert.AreEqual(MappingTargetType.FigureCaption, info.MappingTarget); Assert.IsNull(info.StyleName); }
private bool LoadProjectMappings(string project, ScrMappingList mappingList, ImportDomain domain) { // If the new project ID is null, then do not load mappings. if (string.IsNullOrEmpty(project)) return false; // Load the tags from the paratext project and create mappings for them. ScrText scParatextText; try { // REVIEW (EberhardB): I'm not sure if ScrTextCollection.Get() returns a // reference to a ScrText or a new object (in which case we would have to // call Dispose() on it) scParatextText = ScrTextCollection.Get(project); } catch (Exception ex) { Logger.WriteError(ex); m_IsParatextInitialized = false; return false; } foreach (ImportMappingInfo mapping in mappingList) mapping.SetIsInUse(domain, false); try { foreach (ScrTag tag in scParatextText.DefaultStylesheet.Tags) { if (tag == null) break; string marker = @"\" + tag.Marker; string endMarker = string.Empty; if (!String.IsNullOrEmpty(tag.Endmarker)) endMarker = @"\" + tag.Endmarker; // When the nth marker has an end marker, the nth + 1 marker will be // that end marker. Therefore, we have to skip those "end style" markers. if (tag.StyleType == ScrStyleType.scEndStyle) continue; // Create a new mapping for this marker. mappingList.AddDefaultMappingIfNeeded(marker, endMarker, domain, false, false); } ScrParser parser = scParatextText.Parser; foreach (int bookNum in scParatextText.BooksPresentSet.SelectedBookNumbers) { foreach (UsfmToken token in parser.GetUsfmTokens(new VerseRef(bookNum, 0, 0), false, true)) { if (token.Marker == null) continue; // Tokens alternate between text and marker types ImportMappingInfo mapping = mappingList[@"\" + token.Marker]; if (mapping != null) mapping.SetIsInUse(domain, true); } } } catch (Exception ex) { Logger.WriteError(ex); // A lot goes on in the try block, so this exception doesn't necessarily mean Paratext is inaccessible, // so don't mark Paratext as uninitialized return false; } return true; }