private TextUnitCollection SeparateIdentifyingLetterFromND(TextUnitCollection input, string identifyingLetter) { string ndRegexPattern = @"(?<nd>n\.{0,1} *d\.{0,1} *)(?<identifyingLetter>" + identifyingLetter + @")$"; string ndMatch = ""; var index = -1; bool found = false; foreach (ITextUnit textUnit in input) { index++; var match = Regex.Match(textUnit.Text, ndRegexPattern); if (match.Success) { ndMatch = match.Groups["nd"].Value; found = true; break; } } if (found) { input.ElementAt(index).Text = ndMatch; LiteralTextUnit identifyingLetterTextUnit = new LiteralTextUnit(" " + identifyingLetter, Drawing.FontStyle.Neutral); input.Insert(index + 1, identifyingLetterTextUnit); } //input.Insert(0, new LiteralTextUnit("(", Drawing.FontStyle.Neutral)); //we put parentheses on component part level again //input.Add(new LiteralTextUnit(")", Drawing.FontStyle.Neutral)); return(input); }
public static string NewShortTitle(Reference reference) { try { if (reference == null) { return(null); } string newShortTitle = string.Empty; Project project = Program.ActiveProjectShell.Project; string styleName = "ShortTitleGenerator.ccs"; string folder = project.Addresses.GetFolderPath(CitaviFolder.UserData).ToString(); string fullPath = folder + @"\Custom Citation Styles\" + styleName; if (!System.IO.File.Exists(fullPath)) { System.Windows.Forms.MessageBox.Show(ShortTitleGeneratorLocalization.CitationStyleNotFound); return(null); } Uri uri = new Uri(fullPath); CitationStyle citationStyle = CitationStyle.Load(uri.AbsoluteUri); List <Reference> references = new List <Reference>(); references.Add(reference); CitationManager citationManager = new CitationManager(Program.ActiveProjectShell.Project.Engine, citationStyle, references); if (citationManager == null) { return(null); } BibliographyCitation bibliographyCitation = citationManager.BibliographyCitations.FirstOrDefault(); if (bibliographyCitation == null) { return(null); } List <ITextUnit> textUnits = bibliographyCitation.GetTextUnits(); if (textUnits == null) { return(null); } var output = new TextUnitCollection(); foreach (ITextUnit textUnit in textUnits) { output.Add(textUnit); } newShortTitle = output.ToString(); return(newShortTitle); } catch { return(null); } }
public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { handled = false; if (citation == null) { return(null); } if (citation.Reference == null) { return(null); } if (componentPart == null) { return(null); } if (componentPart.Elements == null || componentPart.Elements.Count == 0) { return(null); } TextFieldElement textFieldElement = componentPart.Elements.OfType <TextFieldElement>().FirstOrDefault(); //SwissAcademic.Citavi.Citations.TextFieldElement if (textFieldElement == null) { return(null); } bool found = false; TextUnitCollection textUnits = textFieldElement.GetTextUnits(citation, template); if (textUnits == null) { return(null); } foreach (ITextUnit textUnit in textUnits) { if (textUnit.Text.Contains("-")) { found = true; textUnit.Text = textUnit.Text.Replace("-", "\u2011"); } } if (found) { componentPart.Elements.ReplaceItem(textFieldElement, textUnits.TextUnitsToLiteralElements(componentPart)); } return(null); }
public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { handled = false; if (componentPart == null) { return(null); } if (citation == null || citation.Reference == null) { return(null); } var dateString = citation.Reference.Date; if (string.IsNullOrWhiteSpace(dateString)) { return(null); } var dateFieldElement = componentPart.GetFieldElements().FirstOrDefault <FieldElement>(item => item.PropertyId == ReferencePropertyId.Date); if (dateFieldElement == null) { return(null); } var output = new TextUnitCollection(); DateTime dateValue; if (!DateTime.TryParse(dateString, out dateValue)) { return(null); } var day = dateValue.Day; //int var month = dateValue.Month; //ditto var year = dateValue.Year; //ditto var monthStringRoman = NumeralSystemConverter.ToRomanNumber(arabicNumber: month.ToString(), lowerCase: false); var dayString = day.ToString("D2"); //2-digit day, padded with leading 0 if necessary, so 08 instead of 8 var yearString = dateValue.ToString("yyyy"); //4-digit year var newDatePattern = "{0}. {1} {2}"; dateString = string.Format(newDatePattern, dayString, monthStringRoman, yearString); var dateStringTextUnit = new LiteralTextUnit(dateString); output.Add(dateStringTextUnit); handled = true; return(output); }
public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { handled = false; if (componentPart == null) { return(null); } if (citation == null || citation.Reference == null) { return(null); } PersonFieldElement personFieldElement = componentPart.GetFieldElements().OfType <PersonFieldElement>().FirstOrDefault(); if (personFieldElement == null) { return(null); } IEnumerable <Person> persons = personFieldElement.GetPersons(citation.Reference); if (persons == null || !persons.Any()) { return(null); } bool found = false; TextUnitCollection textUnits = personFieldElement.GetTextUnits(citation, template); if (textUnits == null) { return(null); } foreach (ITextUnit textUnit in textUnits) { if (textUnit.Text.Contains(" ")) { found = true; textUnit.Text = textUnit.Text.Replace(" ", "\u00A0"); } } if (found) { componentPart.Elements.ReplaceItem(personFieldElement, textUnits.TextUnitsToLiteralElements(componentPart)); } return(null); }
IEnumerable <IElement> TextUnitsToLiteralElements(TextUnitCollection textUnits, ComponentPart componentPart) { if (componentPart == null) { yield break; } if (textUnits == null || textUnits.Count == 0) { yield break; } foreach (ITextUnit textUnit in textUnits) { LiteralElement element = new LiteralElement(componentPart, textUnit.Text, ElementApplyCondition.Always); element.FontStyle = textUnit.FontStyle; yield return(element); } }
public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { //return handled = true if this macro generates the output (as an IEnumerable<ITextUnit>); the standard output will be suppressed //return handled = false if you want Citavi to produce the standard output; handled = true; TextUnitCollection output = new TextUnitCollection(); LiteralTextUnit text; string referenceType = citation.Reference.ReferenceType.ToString(); text = new LiteralTextUnit(referenceType, FontStyle.Neutral); // text = new LiteralTextUnit(referenceType, FontStyle.Bold | FontStyle.SmallCaps); output.Add(new LiteralTextUnit("[", FontStyle.Neutral)); output.Add(text); output.Add(new LiteralTextUnit("]", FontStyle.Neutral)); return(output); }
public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { handled = true; var output = new TextUnitCollection(); if (citation == null) { return(output); } if (citation.Reference == null) { return(output); } if (!citation.Reference.HasCoreField(ReferenceTypeCoreFieldId.Editors)) { return(output); //empty string } var editors = citation.Reference.Editors; if (editors == null || editors.Count == 0) { return(output); //empty string } if (editors.Count == 1) { output.Add(new LiteralTextUnit("ed.")); } else { output.Add(new LiteralTextUnit("eds.")); } return(output); }
public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { handled = false; if (componentPart == null) { return(null); } Reference currentReference = citation.Reference; if (currentReference == null) { return(null); } Reference currentParentReference = currentReference.ParentReference; // can be null; if (componentPart == null) { return(null); } if (componentPart.Elements == null || !componentPart.Elements.Any()) { return(null); } Reference referenceInScope = null; if (componentPart.Scope == ComponentPartScope.ParentReference && currentParentReference != null) { referenceInScope = currentParentReference; } else if (componentPart.Scope == ComponentPartScope.Reference) { referenceInScope = currentReference; } SeriesTitle seriesTitle = referenceInScope.SeriesTitle; if (seriesTitle == null) { return(null); } SeriesTitleFieldElement seriesTitleFieldElement = componentPart.Elements.OfType <SeriesTitleFieldElement>().FirstOrDefault() as SeriesTitleFieldElement; if (seriesTitleFieldElement == null) { return(null); } bool found = false; TextUnitCollection textUnits = seriesTitleFieldElement.GetTextUnits(citation, template); if (textUnits == null) { return(null); } foreach (ITextUnit textUnit in textUnits) { if (textUnit.Text.Contains(" ")) { found = true; textUnit.Text = textUnit.Text.Replace(" ", "\u00A0"); } } if (found) { componentPart.Elements.ReplaceItem(seriesTitleFieldElement, textUnits.TextUnitsToLiteralElements(componentPart)); } return(null); }
public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { var titleAsFallback = true; //if titleAsFallback = true, the title will be displayed, if the citation key is NOT manually set //if titleAsFallback = false, nothing will be displayed, if the citation key is NOT manually set //in both cases, the citation key will be displayed, if it is manually set //suppresses the output of the component, if a CitationKey field element is present and if the current citation key was automatically created handled = false; if (componentPart == null) { return(null); } if (componentPart.Elements == null) { return(null); } if (citation == null || citation.Reference == null) { return(null); } if (componentPart.Scope == ComponentPartScope.ParentReference && citation.Reference.ParentReference == null) { return(null); } var citationKeyFieldElement = componentPart.Elements.OfType <CitationKeyFieldElement>().FirstOrDefault(); if (citationKeyFieldElement == null) { return(null); } TextUnitCollection output = new TextUnitCollection(); LiteralTextUnit text; string citationKeyResolved; UpdateType citationKeyUpdateTypeResolved; if (componentPart.Scope == ComponentPartScope.ParentReference) { citationKeyResolved = citation.Reference.ParentReference.CitationKey; citationKeyUpdateTypeResolved = citation.Reference.ParentReference.CitationKeyUpdateType; } else { citationKeyResolved = citation.Reference.CitationKey; citationKeyUpdateTypeResolved = citation.Reference.CitationKeyUpdateType; } if (citationKeyUpdateTypeResolved == UpdateType.Automatic || string.IsNullOrWhiteSpace(citationKeyResolved)) { if (titleAsFallback) { handled = true; text = new LiteralTextUnit(citation.Reference.Title, FontStyle.Neutral); output.Add(text); return(output); } else { handled = true; return(null); } } return(null); }
public IEnumerable<ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { handled = false; if (citation == null) return null; if (citation.Reference == null) return null; if (componentPart == null) return null; if (componentPart.Elements == null || componentPart.Elements.Count == 0) return null; bool ambiguityFound = false; BibliographyCitation bibliographyCitation = citation as BibliographyCitation; if (bibliographyCitation == null) { PlaceholderCitation placeholderCitation = citation as PlaceholderCitation; if (placeholderCitation != null) { bibliographyCitation = placeholderCitation.CorrespondingBibliographyCitation; } } if (bibliographyCitation != null && bibliographyCitation.AmbiguityFound && !string.IsNullOrEmpty(bibliographyCitation.IdentifyingLetter) ) ambiguityFound = true; #region Find field elements of type DateTime IEnumerable<DateTimeFieldElement> dateTimeFieldElements = componentPart.Elements.OfType<DateTimeFieldElement>(); if (dateTimeFieldElements == null || dateTimeFieldElements.Count() != 1) return null; DateTimeFieldElement dateTimeFieldElement = dateTimeFieldElements.ElementAt(0); if (dateTimeFieldElement == null) return null; #endregion #region Determine reference to look at Reference reference; if (componentPart.Scope == ComponentPartScope.ParentReference) { if (citation.Reference.ParentReference == null) return null; reference = citation.Reference.ParentReference as Reference; } else { reference = citation.Reference as Reference; } if (reference == null) return null; #endregion Determine reference to look at #region Determine reference language Language language; if (String.Equals(reference.LanguageCode, CultureInfo.GetCultureInfo("en").TwoLetterISOLanguageName, StringComparison.OrdinalIgnoreCase)) { language = Language.English; } else if (String.Equals(reference.LanguageCode, CultureInfo.GetCultureInfo("de").TwoLetterISOLanguageName, StringComparison.OrdinalIgnoreCase)) { language = Language.German; } else { language = Language.Other; } #endregion Determine reference language var propertyId = dateTimeFieldElement.PropertyId; var dateTimeStringValue = reference.GetValue(propertyId) as string; if (string.IsNullOrEmpty(dateTimeStringValue)) return null; //das folgende geht nicht, da DateTimeFieldFormatter leider "internal" ist //TextUnitCollection textUnits = DateTimeFieldFormatter.Format(citation, dateTimeFieldElement, dateTimeStringValue); List<Segment> segments = GetSegments(dateTimeStringValue); List<LiteralElement> literalElements = new List<LiteralElement>(); TextUnitCollection debug = new TextUnitCollection(); //int counter = 1; foreach (Segment segment in segments) { switch (segment.type) { case SegmentType.Text: { var literalElement = new LiteralElement(componentPart, segment.text); literalElement.FontStyle = dateTimeFieldElement.FontStyle; literalElements.Add(literalElement); } break; case SegmentType.DateTime: { string newDateString; #region YEAR information only if (!segment.ContainsMonthInformation && !segment.ContainsDayInformation) { switch (language) { default: case (Language.English): { newDateString = segment.dateTime.ToString("yyyy", new CultureInfo("en-US")); //or "en-UK" } break; case (Language.German): { newDateString = segment.dateTime.ToString("yyyy", new CultureInfo("de-DE")); // or "de-CH" or "de-AT" } break; case (Language.Other): { newDateString = segment.dateTime.ToString("yyyy", new CultureInfo("en-US")); } break; } } #endregion #region YEAR and MONTH else if (!segment.ContainsDayInformation) { switch (language) { default: case (Language.English): { newDateString = segment.dateTime.ToString("MM/yyyy", new CultureInfo("en-US")); //or "en-UK" } break; case (Language.German): { newDateString = segment.dateTime.ToString("MMM yyyy", new CultureInfo("de-DE")); // or "de-CH" or "de-AT" } break; case (Language.Other): { newDateString = segment.dateTime.ToString("MM/yyyy", new CultureInfo("en-US")); } break; } } #endregion #region YEAR and MONTH and DAY else { switch (language) { default: case (Language.English): { newDateString = segment.dateTime.ToString("d", new CultureInfo("en-US")); //or "en-UK" } break; case (Language.German): { newDateString = segment.dateTime.ToString("dd. MM yyyy", new CultureInfo("de-DE")); // or "de-CH" or "de-AT" } break; case (Language.Other): { newDateString = segment.dateTime.ToString("MM/dd/yyyy", new CultureInfo("en-US")); } break; } } #endregion var literalElement = new LiteralElement(componentPart, newDateString); literalElement.FontStyle = dateTimeFieldElement.FontStyle; literalElements.Add(literalElement); } break; } } if (ambiguityFound) { var literalElement = new LiteralElement(componentPart, bibliographyCitation.IdentifyingLetter); literalElement.FontStyle = dateTimeFieldElement.FontStyle; literalElements.Add(literalElement); } //replace the DateTimeFieldElement by the LiteralElements componentPart.Elements.ReplaceItem(dateTimeFieldElement, literalElements); //and Citavi handles the rest, therefore we say handled = false && return null handled = false; return null; }
public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { handled = false; if (citation == null) { return(null); } Reference reference = citation.Reference; if (reference == null) { return(null); } if (componentPart == null) { return(null); } if (componentPart.Elements == null || !componentPart.Elements.Any() || componentPart.Elements.Count != 1) { return(null); } TextFieldElement firstTextFieldElement = componentPart.Elements.OfType <TextFieldElement>().FirstOrDefault() as TextFieldElement; if (firstTextFieldElement == null) { return(null); } ComponentPartScope scope = componentPart.Scope; Reference referenceInScope = null; if (scope == ComponentPartScope.Reference) { referenceInScope = reference; } else { referenceInScope = reference.ParentReference; } if (referenceInScope == null) { return(null); } ReferencePropertyId propertyTagged = ReferencePropertyId.None; switch (firstTextFieldElement.PropertyId) { case ReferencePropertyId.Title: { propertyTagged = ReferencePropertyId.TitleTagged; } break; case ReferencePropertyId.Subtitle: { propertyTagged = ReferencePropertyId.SubtitleTagged; } break; case ReferencePropertyId.TitleSupplement: { propertyTagged = ReferencePropertyId.TitleSupplementTagged; } break; } if (propertyTagged == ReferencePropertyId.None) { return(null); } string stringTagged = referenceInScope.GetValue(propertyTagged) as string; if (string.IsNullOrEmpty(stringTagged)) { return(null); } if (!HasTags(stringTagged)) { return(null); } bool italicFound = false; TextUnitCollection textUnitCollection = TextUnitCollectionUtility.TaggedTextToTextUnits(firstTextFieldElement, stringTagged); foreach (ITextUnit textUnit in textUnitCollection) { //Flip bits where required if (firstTextFieldElement.FontStyle.HasFlag(FontStyle.Italic)) { textUnit.TemporaryFontStyle ^= FontStyle.Italic; } if (firstTextFieldElement.FontStyle.HasFlag(FontStyle.Bold)) { textUnit.TemporaryFontStyle ^= FontStyle.Bold; } } handled = true; return(textUnitCollection); }
public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { handled = false; if (citation == null) { return(null); } if (citation.Reference == null) { return(null); } if (componentPart == null) { return(null); } if (componentPart.Elements == null || componentPart.Elements.Count == 0) { return(null); } foreach (TextFieldElement textFieldElement in componentPart.Elements.OfType <TextFieldElement>().ToList()) { if (textFieldElement == null) { return(null); } bool found = false; TextUnitCollection textUnits = textFieldElement.GetTextUnits(citation, template); if (textUnits == null) { return(null); } foreach (ITextUnit textUnit in textUnits) { if (textUnit.Text.Contains("“")) //EN, öffnend { found = true; textUnit.Text = textUnit.Text.Replace("“", "‘"); } if (textUnit.Text.Contains("”")) //EN, schließend { found = true; textUnit.Text = textUnit.Text.Replace("”", "’"); } if (textUnit.Text.Contains("„")) //DE, öffnend { found = true; textUnit.Text = textUnit.Text.Replace("„", "‚"); } if (textUnit.Text.Contains("“")) //DE, schließend { found = true; textUnit.Text = textUnit.Text.Replace("“", "‘"); } if (textUnit.Text.Contains("»")) //FR/CH, öffnend { found = true; textUnit.Text = textUnit.Text.Replace("»", "›"); } if (textUnit.Text.Contains("«")) //FR/CH, schließend { found = true; textUnit.Text = textUnit.Text.Replace("«", "‹"); } } if (found) { componentPart.Elements.ReplaceItem(textFieldElement, textUnits.TextUnitsToLiteralElements(componentPart)); } } return(null); }
public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { //Name of filter: Outputs a dash "---" instead of names in case of repetition //Version 3.2: Refactored for C6 to make use of AfterFormatPerson event handler //Version 3.1: //- set font style of dashes to neutral //Version 3.0: //- allow either one dash if all persons are the same (lookForRepetitionOfIndividualPersons = false) or various dashes for each repeated person (lookForRepetitionOfIndividualPersons = true) //- allow mix of dashes and written names and again dashes (exitLookForRepetitionIfFailedOnce = false) or not (exitLookForRepetitionIfFailedOnce = true) //Version 2.2: //- GetPreviousVisibleCitation() method gets first previous citation where nobib = false //Version 2.1: //- filter deactivates itself, if the bibliography is NOT YET completely sorted (see below) //Version 2: //- filter deactivates itself, if the person field component it is attached to is NOT the first inside the template //- filter works on ALL kinds of person fields (Authors, Editors, Organizations, AuthorsEditorsOrOrganizations etc.) //- filter compares ALL kinds of person fields of this citation with the ones of its predecessor (e.g. authors with authors, authors with editors etc.) //- filter considers group prefices for singular and plural already defined on the field element itself: // --- (Hrsg.) or ---, eds. etc. I.e. it checks, if ", eds." has already been defined and uses it with the dash. //- you can customize the dash in line 34 below //NOTE: Set the following to true, if you want one dash if all persons are repeated, or none, //set it to false, if you want to compare person by person and set individual dashes per repeated person bool lookForRepetitionOfIndividualPersons = true; bool exitLookForRepetitionIfFailedOnce = true; //only applicable if previous was set to true /* * lookForRepetitionOfIndividualPersons = FALSE: * * A, B (2010) * - (2010) * A, B, C (2010) * A, B, D (2010) * A, B, C (2010) * A, D, C (2010) * * lookForRepetitionOfIndividualPersons = TRUE, exitLookForRepetitionIfFailedOnce = TRUE * * A, B (2010) * -, - (2010) * A, B, C (2010) * -, -, D (2010) * A, B, C (2010) * -, D, C (2010) * * lookForRepetitionOfIndividualPersons = TRUE, exitLookForRepetitionIfFailedOnce = FALSE * * A, B (2010) * -, - (2010) * A, B, C (2010) * -, -, D (2010) * A, B, C (2010) * -, D, - (2010) * */ handled = false; if (citation == null || citation.Reference == null || citation.CitationManager == null) { return(null); } if (template == null) { return(null); } if (componentPart == null || componentPart.Elements == null || !componentPart.Elements.Any()) { return(null); } //define the dashes string emdashes = "———"; LiteralTextUnit emDashesTextUnit = new LiteralTextUnit(emdashes); //filter deactivates itself, if the bibliography is NOT YET completely sorted //this is necessary to avoid that this filter in turn changes the sort order, that it depends upon if (citation.CitationManager.BibliographyCitations.IsSorted == false) { return(null); } //make sure the current componentPart is the FIRST inside the template if (template.ComponentParts == null || template.ComponentParts.Count == 0) { return(null); } if (template.ComponentParts[0].Id != componentPart.Id) { return(null); } #region ThisBibliographyCitation var thisBibliographyCitation = citation as BibliographyCitation; if (thisBibliographyCitation == null) { return(null); } #endregion ThisBibliographyCitation #region PreviousBibliographyCitation var previousBibliographyCitation = GetPreviousVisibleBibliographyCitation(thisBibliographyCitation); if (previousBibliographyCitation == null) { return(null); } if (previousBibliographyCitation.Reference == null) { return(null); } if (previousBibliographyCitation.NoBib == true) { return(null); } #endregion PreviousBibliographyCitation #region ThisTemplate var thisTemplate = thisBibliographyCitation.Template; if (thisTemplate == null) { return(null); } #endregion ThisTemplate #region PreviousTemplate var previousTemplate = previousBibliographyCitation.Template; if (previousTemplate == null) { return(null); } #endregion PreviousTemplate #region ThisPersonFieldElement var thisPersonFieldElement = ( componentPart.Elements != null && componentPart.Elements.Count > 0 ? componentPart.Elements[0] : null ) as PersonFieldElement; if (thisPersonFieldElement == null) { return(null); } #endregion ThisPersonFieldElement #region PreviousPersonFieldElement var previousPersonFieldElement = ( previousTemplate.ComponentParts != null && previousTemplate.ComponentParts.Count > 0 && previousTemplate.ComponentParts[0].Elements != null && previousTemplate.ComponentParts[0].Elements.Count > 0 ? previousTemplate.ComponentParts[0].Elements[0] : null ) as PersonFieldElement; if (previousPersonFieldElement == null) { return(null); } #endregion PreviousPersonFieldElement #region ThesePersons //we DO have a valid citation/reference a previous citation/reference, so we can compare their persons IEnumerable <Person> thesePersons = thisBibliographyCitation.Reference.GetValue(thisPersonFieldElement.PropertyId) as IEnumerable <Person>; if (thesePersons == null || thesePersons.Count() == 0) { return(null); } #endregion ThesePersons #region PreviousPersons IEnumerable <Person> previousPersons = previousBibliographyCitation.Reference.GetValue(previousPersonFieldElement.PropertyId) as IEnumerable <Person>; if (previousPersons == null || previousPersons.Count() == 0) { return(null); } bool failedOnce = false; #endregion PreviousPersons //we DO have authors in both cases to compare #region LookForRepetitionOfIndividualPersons = TRUE if (lookForRepetitionOfIndividualPersons) { /* * A, B (2010) * -, - (2010) * A, B, C (2010) * -, -, D (2010) */ AfterFormatPersonEventArgs afp; thisPersonFieldElement.PersonFormatter.AfterFormatPerson += (sender, e) => { if (exitLookForRepetitionIfFailedOnce && failedOnce) { return; } afp = (AfterFormatPersonEventArgs)e; Person thisPerson = afp.Person; if (thisPerson == null) { failedOnce = true; return; } Person previousPerson = previousPersons.ElementAtOrDefault(afp.Index); if (previousPerson == null) { failedOnce = true; return; } if (!thisPerson.Equals(previousPerson)) { failedOnce = true; return; } //same person afp.TextUnits.Clear(); afp.TextUnits.Add(emDashesTextUnit); }; } #endregion LookForRepetitionOfIndividualPersons = TRUE #region LookForRepetitionOfIndividualPersons = FALSE else { if (!thesePersons.SequenceEqual(previousPersons)) { return(null); } //check if there are group suffixe defined LiteralTextUnit thisGroupSuffixSingularTextUnit = thisPersonFieldElement.GroupSuffixSingular != null ? new LiteralTextUnit(thisPersonFieldElement.GroupSuffixSingular) : null; LiteralTextUnit thisGroupSuffixPluralTextUnit = thisPersonFieldElement.GroupSuffixPlural != null ? new LiteralTextUnit(thisPersonFieldElement.GroupSuffixPlural) : null; //we are dealing the the same author(s), so we handle the output now: TextUnitCollection output = new TextUnitCollection(); if (thesePersons.Count() > 1 && thisGroupSuffixPluralTextUnit != null) { output.Add(emDashesTextUnit); output.Add(thisGroupSuffixSingularTextUnit); } else if (thesePersons.Count() == 1 && thisGroupSuffixSingularTextUnit != null) { output.Add(emDashesTextUnit); output.Add(thisGroupSuffixSingularTextUnit); } else { output.Add(emDashesTextUnit); } //Set dashes to neutral font style foreach (ITextUnit unit in output) { if (!unit.Text.Equals(emdashes)) { continue; } unit.FontStyle = SwissAcademic.Drawing.FontStyle.Neutral; } handled = true; return(output); } #endregion LookForRepetitionOfIndividualPersons = FALSE return(null); }
public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { //if the following is set to true a placeholder citation's output will not show ders./dies. although this would normally be the case //as e.g. the second one of the following two: {Meier 2010 #2} and {Meier 2010 #2:17-19 /opt1} var deactivateFilterWithOption1Switch = true; //default: true //if the following is set to true, the citation collapsing can take place (if the style is set to omit author names) //true: (Mueller 2010, S. 10; 2011, S. 12f.; 2012, S. 17)o //false: (Mueller 2010, S. 10; ders. 2011, S. 12 f; ders. 2012, S. 17) var deactivateFilterInsideMultipleCitations = true; //default: true var deactivateFilterForFirstInsideMultipleCitations = true; //default: true; only applicable when deactivateFilterInsideMultipleCitations = false var deactivateFilterForFirstAfterMultipleCitations = true; //default: true var deactivateFilterInIbidemIdemSequence = false; //default: false var deactivateFilterAcrossFootnotes = false; //default: false var deactivateFilterAcrossFootnotesIfSeparatedByMoreThanOneIndexNo = true; //default: true; only applicable when deactivateFilterAcrossFootnotes = false var deactivateFilterAcrossFootnotesIfPreviousCitationNotSolitair = true; //default: true; ditto var outputInItalics = true; //default: true var outputInSmallCaps = false; //default: false var outputInBold = false; //default: false var outputUnderlined = false; //default: false var missingPersonsInfo = "o.A."; //can be set to e.g. "o.A."/"ohne Autor"; leave empty otherwise var outputMissingPersonsInfoInItalics = false; //default: false var outputMissingPersonsInfoInSmallCaps = false; //default: false var outputMissingPersonsInfoInBold = false; //default: false var outputMissingPersonsInfoUnderlined = false; //default: false var showGroupPrefixIfPresent = false; //default: false var showGroupSuffixIfPresent = true; //default: true var comparePersonsAgainstLastPersonField = true; handled = false; var thisCitationIsPartOfMultipleCitation = false; if (citation == null) { return(null); } if (citation.Reference == null) { return(null); } if (componentPart == null) { return(null); } if (componentPart.Elements == null || !componentPart.Elements.Any()) { return(null); } PersonFieldElement personFieldElement = componentPart.Elements.OfType <PersonFieldElement>().FirstOrDefault(); if (personFieldElement == null) { return(null); } //determine current persons to compare IEnumerable <Person> thesePersons = GetPersonsOfPersonFieldElement(citation, personFieldElement); if (thesePersons == null || !thesePersons.Any()) { return(null); } bool usePlural = thesePersons.Count() > 1; PlaceholderCitation thisPlaceholderCitation = citation as PlaceholderCitation; #region deactivateFilterWithOption1Switch //SPECIAL: if this citation has the /opt1 switch set, this filter should be deactivated if (deactivateFilterWithOption1Switch && thisPlaceholderCitation != null && thisPlaceholderCitation.FormatOption1) { return(null); } #endregion deactivateFilterWithOption1Switch //handle missing persons #region MissingPersonsOutput var text = string.Empty; var output = new TextUnitCollection(); //SwissAcademic.Drawing.FontStyle fontStyle = outputInItalics ? SwissAcademic.Drawing.FontStyle.Italic : SwissAcademic.Drawing.FontStyle.Neutral; SwissAcademic.Drawing.FontStyle fontStyle; var personsMissing = (thesePersons == null || !thesePersons.Any()); var outputMissingPersonsInfo = personsMissing && !string.IsNullOrEmpty(missingPersonsInfo); if (personsMissing && outputMissingPersonsInfo) { text = missingPersonsInfo; fontStyle = SwissAcademic.Drawing.FontStyle.Neutral; if (outputMissingPersonsInfoInItalics) { fontStyle |= SwissAcademic.Drawing.FontStyle.Italic; } if (outputMissingPersonsInfoInSmallCaps) { fontStyle |= SwissAcademic.Drawing.FontStyle.SmallCaps; } if (outputMissingPersonsInfoInBold) { fontStyle |= SwissAcademic.Drawing.FontStyle.Bold; } if (outputMissingPersonsInfoUnderlined) { fontStyle |= SwissAcademic.Drawing.FontStyle.Underline; } output.Add(new LiteralTextUnit(text, fontStyle)); handled = true; return(output); } else if (personsMissing) { return(null); } #endregion MissingPersonsOutput var previousVisibleCitation = GetPreviousVisibleCitation(citation); if (previousVisibleCitation == null) { return(null); } if (previousVisibleCitation.Reference == null) { return(null); } var secondPreviousVisibleCitation = GetPreviousVisibleCitation(previousVisibleCitation); #region MultipleCitation if (thisPlaceholderCitation != null) { var printingEntries = thisPlaceholderCitation.Entry.Placeholder.GetPrintingEntries(); if (printingEntries == null) { return(null); } if (printingEntries.Count() > 1) { thisCitationIsPartOfMultipleCitation = true; } if (thisCitationIsPartOfMultipleCitation) { if (deactivateFilterInsideMultipleCitations) { //We switch off "ders./dies." completely ... or ... return(null); } else { //... at least for the very first printing entry in a multiple citation var index = printingEntries.IndexOf(thisPlaceholderCitation.Entry); if (index == 0 && deactivateFilterForFirstInsideMultipleCitations) { return(null); } } } } #endregion MultipleCitation #region deactivateFilterInIbidemIdemSequence //avoiding a sequence such as e.g.: [2] Ebd. -> [3] Ders. if (deactivateFilterInIbidemIdemSequence) { if (secondPreviousVisibleCitation != null && secondPreviousVisibleCitation.Reference != null) { if (previousVisibleCitation.Reference == secondPreviousVisibleCitation.Reference) { return(null); } } } #endregion deactivateFilterInIbidemIdemSequence #region FootnoteCitation var thisFootnoteCitation = citation as FootnoteCitation; if (thisFootnoteCitation != null && !thisCitationIsPartOfMultipleCitation) { var previousVisibleFootnoteCitation = previousVisibleCitation as FootnoteCitation; if (previousVisibleFootnoteCitation == null) { return(null); } var printingEntries = previousVisibleFootnoteCitation.Entry.Placeholder.GetPrintingEntries(); if (printingEntries != null && printingEntries.Count() > 1 && deactivateFilterForFirstAfterMultipleCitations) { //previousVisibleFootnoteCitation IS part of a multiple citation return(null); } int thisFootnoteIndex = thisFootnoteCitation.FootnoteIndex; int previousFootnoteIndex = previousVisibleFootnoteCitation.FootnoteIndex; var secondPreviousVisibleFootnoteCitation = secondPreviousVisibleCitation as FootnoteCitation; int secondPreviousFootnoteIndex = secondPreviousVisibleFootnoteCitation == null ? 0 : secondPreviousVisibleFootnoteCitation.FootnoteIndex; #region deactivateFilterAcrossFootnotes //enforce distance rules as given by user settings above if ( ( deactivateFilterAcrossFootnotes && thisFootnoteIndex != previousFootnoteIndex ) || ( !deactivateFilterAcrossFootnotes && deactivateFilterAcrossFootnotesIfSeparatedByMoreThanOneIndexNo && thisFootnoteIndex - previousFootnoteIndex > 1 ) || ( !deactivateFilterAcrossFootnotes && deactivateFilterAcrossFootnotesIfPreviousCitationNotSolitair && thisFootnoteIndex - previousFootnoteIndex == 1 && secondPreviousFootnoteIndex == previousFootnoteIndex ) ) { return(null); } #endregion deactivateFilterAcrossFootnotes } #endregion FootnoteCitation #region InTextCitation var thisInTextCitation = citation as InTextCitation; //if this citations predecessor is part of a multiple citation, but THIS is NOT, switch off filter if (thisInTextCitation != null && !thisCitationIsPartOfMultipleCitation) { var previousVisibleInTextCitation = previousVisibleCitation as InTextCitation; if (previousVisibleInTextCitation == null) { return(null); } var printingEntries = previousVisibleInTextCitation.Entry.Placeholder.GetPrintingEntries(); if (printingEntries != null && printingEntries.Count() > 1 && deactivateFilterForFirstAfterMultipleCitations) { //previousVisibleInTextCitation IS part of a multiple citation return(null); } } #endregion InTextCitation //determine previous persons IEnumerable <Person> previousPersons = null; if (comparePersonsAgainstLastPersonField) { previousPersons = GetPersonsOfLastPersonFieldElement(previousVisibleCitation); } else { previousPersons = previousVisibleCitation.GetPersonsOfPersonFieldElement(0); } if (previousPersons == null || !previousPersons.Any()) { return(null); } var equality = CheckPersonEquality(thesePersons, previousPersons); if (equality == PersonEquality.None) { return(null); } #region Equality detected - generate output //we DO have some equality, so let's check what we need to output instead of the person's name(s) switch (equality) { case PersonEquality.M: text = "ders."; break; case PersonEquality.N: text = "dass."; break; default: //all others text = "dies."; break; } fontStyle = SwissAcademic.Drawing.FontStyle.Neutral; if (outputInItalics) { fontStyle |= SwissAcademic.Drawing.FontStyle.Italic; } if (outputInSmallCaps) { fontStyle |= SwissAcademic.Drawing.FontStyle.SmallCaps; } if (outputInBold) { fontStyle |= SwissAcademic.Drawing.FontStyle.Bold; } if (outputUnderlined) { fontStyle |= SwissAcademic.Drawing.FontStyle.Underline; } #region GroupPrefix if (showGroupPrefixIfPresent && !string.IsNullOrEmpty(personFieldElement.GroupPrefixPlural.Text) && usePlural) { output.Add(new LiteralTextUnit(personFieldElement.GroupPrefixPlural.Text, personFieldElement.GroupPrefixPlural.FontStyle)); } else if (showGroupPrefixIfPresent && !string.IsNullOrEmpty(personFieldElement.GroupPrefixSingular.Text) && !usePlural) { output.Add(new LiteralTextUnit(personFieldElement.GroupPrefixSingular.Text, personFieldElement.GroupPrefixSingular.FontStyle)); } #endregion GroupPrefix output.Add(new LiteralTextUnit(text, fontStyle)); #region GroupSuffix if (showGroupSuffixIfPresent && !string.IsNullOrEmpty(personFieldElement.GroupSuffixPlural.Text) && usePlural) { output.Add(new LiteralTextUnit(personFieldElement.GroupSuffixPlural.Text, personFieldElement.GroupSuffixPlural.FontStyle)); } else if (showGroupSuffixIfPresent && !string.IsNullOrEmpty(personFieldElement.GroupSuffixSingular.Text) && !usePlural) { output.Add(new LiteralTextUnit(personFieldElement.GroupSuffixSingular.Text, personFieldElement.GroupSuffixSingular.FontStyle)); } #endregion GroupSuffix handled = true; return(output); #endregion Equality detected - generate output }
public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { //change the following to "true", if you want "id.", "eid.", "ead.", "eaed." written in italics or other font styles bool outputInItalics = true; bool outputInSmallCaps = false; bool outputInBold = false; bool outputUnderlined = false; //NOTE: If you want a prefix such as "In: " and a suffix " (Hrsg)", you can define them as group prefix and suffix on the field element inside the component part editor handled = false; if (componentPart == null) { return(null); } if (componentPart.Elements == null || componentPart.Elements.Count == 0) { return(null); } if (citation == null) { return(null); } Reference reference = citation.Reference; if (reference == null) { return(null); } Reference parentReference = reference.ParentReference; if (parentReference == null) { return(null); } //get editor person field elements (or authors in case of CollectedWorks) and make it a separate List<>, since we are going to change that collection below List <PersonFieldElement> editorPersonFieldElements = parentReference.ReferenceType == ReferenceType.CollectedWorks ? componentPart.Elements.OfType <PersonFieldElement>().Where(element => element.PropertyId == ReferencePropertyId.Authors).ToList() : componentPart.Elements.OfType <PersonFieldElement>().Where(element => element.PropertyId == ReferencePropertyId.Editors).ToList(); if (editorPersonFieldElements == null || editorPersonFieldElements.Count() == 0) { return(null); } //we DO have a valid citation/reference that is a parent's child ReferencePersonCollection childAuthorsCollection = reference.Authors; if (childAuthorsCollection == null || childAuthorsCollection.Count == 0) { return(null); } List <Person> childAuthors = new List <Person>(childAuthorsCollection); ReferencePersonCollection parentEditorsCollection = parentReference.ReferenceType == ReferenceType.CollectedWorks ? parentReference.Authors : parentReference.Editors; if (parentEditorsCollection == null || parentEditorsCollection.Count == 0) { return(null); } List <Person> parentEditors = new List <Person>(parentEditorsCollection); bool usePlural = parentEditors.Count() > 1; PersonEquality equality = CheckPersonEquality(childAuthors, parentEditors); if (equality == PersonEquality.None) { return(null); } //we DO have some equality, so let's check what we need to output instead of the person's name(s) var textIdem = string.Empty; switch (equality) { //see http://en.wiktionary.org/wiki/idem#Inflection case PersonEquality.M: case PersonEquality.N: textIdem = "id."; break; case PersonEquality.F: textIdem = "ead."; break; case PersonEquality.NN: textIdem = "ead."; break; case PersonEquality.MM: textIdem = "iid."; break; case PersonEquality.MN: case PersonEquality.FM: case PersonEquality.FMN: textIdem = "eid."; break; case PersonEquality.FF: case PersonEquality.FN: textIdem = "eaed."; break; } foreach (PersonFieldElement editors in editorPersonFieldElements) { TextUnitCollection output = new TextUnitCollection(); #region GroupPrefix if (usePlural && !string.IsNullOrEmpty(editors.GroupPrefixPlural.Text)) { output.Add(new LiteralTextUnit(editors.GroupPrefixPlural.Text, editors.GroupPrefixPlural.FontStyle)); } else if (!usePlural & !string.IsNullOrEmpty(editors.GroupPrefixSingular.Text)) { output.Add(new LiteralTextUnit(editors.GroupPrefixSingular.Text, editors.GroupPrefixSingular.FontStyle)); } #endregion GroupPrefix SwissAcademic.Drawing.FontStyle fontStyle; fontStyle = SwissAcademic.Drawing.FontStyle.Neutral; if (outputInItalics) { fontStyle |= SwissAcademic.Drawing.FontStyle.Italic; } if (outputInSmallCaps) { fontStyle |= SwissAcademic.Drawing.FontStyle.SmallCaps; } if (outputInBold) { fontStyle |= SwissAcademic.Drawing.FontStyle.Bold; } if (outputUnderlined) { fontStyle |= SwissAcademic.Drawing.FontStyle.Underline; } var fontStyleNeutral = SwissAcademic.Drawing.FontStyle.Neutral; output.Add(new LiteralTextUnit(textIdem, fontStyle)); #region GroupSuffix if (usePlural && !string.IsNullOrEmpty(editors.GroupSuffixPlural.Text)) { output.Add(new LiteralTextUnit(editors.GroupSuffixPlural.Text, editors.GroupSuffixPlural.FontStyle)); } else if (!usePlural && !string.IsNullOrEmpty(editors.GroupSuffixSingular.Text)) { output.Add(new LiteralTextUnit(editors.GroupSuffixSingular.Text, editors.GroupSuffixSingular.FontStyle)); } #endregion GroupSuffix //inject this as LiteralElements into the componentPart, replacing the editors person field element componentPart.Elements.ReplaceItem(editors, TextUnitsToLiteralElements(output, componentPart)); //for some reason this does not work } handled = false; return(null); }
//Version 3.0: complete overhaul, script considers different output for placehoder citations and bibliography citations //Version 2.0: script can be attached to both date/time field element as well as text field element public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { //enter the culture the date info has been formatted and entered in, e.g. 12/05/2017 would be December, 12th in en-UK and May, 5th in en-US CultureInfo targetCulture = CultureInfo.CreateSpecificCulture("en-US"); //list all possible date formats for this script to check; the scripts tries to parse the date beginning from left to right string[] formats = new string[] { "yyyy-MM-dd", "yyyy/MM/dd", "dd/MM/yyyy", "yyyy/dd/MM", "dd.MM.yyyy", "d.M.yyyy", "d.MM.yyyy", "dd.M.yyyy", "dd.MM.yy", "d.M.yy", "d.MM.yy", "dd.M.yy" }; bool usePeriodAfterAbbreviatedMonthName = true; //if true, month names will be: Jan. Feb. Mar. Apr. May (!) Jun. Jul. Aug. Sept. Oct. Nov. Dec. ///IMPORTANT: Use the following indexed placeholders {n} and format strings xxxx as in {n:xxxx} for the templates. ///You can ommit placeholders and/or place them freely inside the templates below. Yet, it is not recommended to use the same placeholder more than once, ///because this script is not optimized for this. /// ///{0}: letter for ambiguity resolving ///{1}: year of start or single date ///{2}: month of start or single date ///{3}: day of start or single date ///{4}: year of end date ///{5}: month of end date ///{6}: day of end date ///use the following formatting for "6 June 2018" ///YEAR: yyyy = 2018, yy = 18 ///MONTH: MMMM = June, MMM = Jun, MM = 06, %M = 6 ///DAY: dd = 06, %d = 6, %do = 6th //SINGLE DATE - output format templates string outputFormatSingleDatePlaceholder = "{1:yyyy}{0}"; //e.g. 2013a string outputFormatSingleDateBibliography = "{1:yyyy}{0}, {2:MMMM} {3:%do}"; //e.g. 2013a, January 6th //DATE RANGE - output format templates //same year, same month string outputFormatDateRangeSameYearSameMonthPlaceholder = "{1:yyyy}{0}"; //e.g. 2013a string outputFormatDateRangeSameYearSameMonthBibliography = "{1:yyyy}{0}, {2:MMMM} {3:%do} - {6:%do}"; //e.g. 2013a, January 6th - 9th //same year, different month string outputFormatDateRangeSameYearDifferentMonthPlaceholder = "{1:yyyy}{0}"; //e.g. 2013a string outputFormatDateRangeSameYearDifferentMonthBibliography = "{1:yyyy}{0}, {2:MMMM} {3:%do} - {5:MMMM} {6:%do}"; //e.g. 2013a, September 28th - October 3rd //different years string outputFormatDateRangeDifferentYearsPlaceholder = "{1:yyyy}/{4:yyyy}{0}"; //e.g. 2013/2014a string outputFormatDateRangeDifferentYearsBibliography = "{1:yyyy}/{4:yyyy}{0}; {1:yyyy}, {2:MMMM} {3:%do} - {4:yyyy}, {5:MMMM} {6:%do}"; //e.g. 2013/2014a; 2013, December 29th - 2014, January 4th handled = false; if (citation == null) { return(null); } Reference referenceInScope = GetReferenceInScope(componentPart, citation); if (referenceInScope == null) { return(null); } FieldElement dateFieldElement = GetDateFieldElement(componentPart); if (dateFieldElement == null) { return(null); } ReferencePropertyId referencePropertyId = dateFieldElement.PropertyId; string dateString = referenceInScope.GetValue(referencePropertyId) as string; if (string.IsNullOrEmpty(dateString)) { return(null); } TextUnitCollection output = null; PlaceholderCitation placeholderCitation = citation as PlaceholderCitation; bool isPlaceholderCitation = placeholderCitation != null; PreviewCitation previewCitation = citation as PreviewCitation; bool isPreviewBibliographyCitation = previewCitation != null && citation.CitationType == CitationType.Bibliography; BibliographyCitation bibliographyCitation = citation as BibliographyCitation; bool isBibliographyCitation = bibliographyCitation != null; if (bibliographyCitation == null && placeholderCitation != null) { bibliographyCitation = placeholderCitation.CorrespondingBibliographyCitation; } if (bibliographyCitation == null && !isPreviewBibliographyCitation) { return(null); } string identifyingLetter = bibliographyCitation != null ? bibliographyCitation.IdentifyingLetter : string.Empty; LiteralTextUnit identifyingLetterTextUnit = new LiteralTextUnit(identifyingLetter, Drawing.FontStyle.Neutral); bool hasIdentifyingLetter = !string.IsNullOrEmpty(identifyingLetter); #region Tread n.d. + letter for disambiguation ("IdentifyingLetter") if (hasIdentifyingLetter && ContainsND(dateString)) { //we make sure the IdentifyingLetter is separated from n.d. by a space char or hyphen: Smith n.d.-a, Smith n.d.-b //go to method SeparateIdentifyingLetterFromND below to customize output = componentPart.GetTextUnitsUnfiltered(citation, template); if (output == null || !output.Any()) { return(null); } handled = true; return(SeparateIdentifyingLetterFromND(output, identifyingLetter)); } #endregion FontStyle fontStyle = dateFieldElement is DateTimeFieldElement ? ((DateTimeFieldElement)dateFieldElement).FontStyle : ((TextFieldElement)dateFieldElement).FontStyle; DateTime dateSingle; DateTime dateStart; DateTime dateEnd; string outputText = string.Empty; #region Check for Single Date if (TryParseSingleDate(dateString, formats, targetCulture, out dateSingle)) { #region BibliographyCitation if (isBibliographyCitation || isPreviewBibliographyCitation) { outputText = FormatDate(dateSingle, outputFormatSingleDateBibliography, targetCulture, identifyingLetter, usePeriodAfterAbbreviatedMonthName); } #endregion #region PlaceholderCitation else if (isPlaceholderCitation) { outputText = FormatDate(dateSingle, outputFormatSingleDatePlaceholder, targetCulture, identifyingLetter, usePeriodAfterAbbreviatedMonthName); } #endregion #region Other else { handled = false; return(null); } #endregion } #endregion #region Check for Date Range else if (TryParseDateRange(dateString, formats, targetCulture, out dateStart, out dateEnd)) { #region BibliographyCitation if (isBibliographyCitation || isPreviewBibliographyCitation) { #region same year, same month if (dateStart.Year == dateEnd.Year && dateStart.Month == dateEnd.Month && dateStart.Day != dateEnd.Day) { outputText = FormatDateRange(dateStart, dateEnd, outputFormatDateRangeSameYearSameMonthBibliography, targetCulture, identifyingLetter, usePeriodAfterAbbreviatedMonthName); } #endregion #region same year, different months else if (dateStart.Year == dateEnd.Year && dateStart.Month != dateEnd.Month) { outputText = FormatDateRange(dateStart, dateEnd, outputFormatDateRangeSameYearDifferentMonthBibliography, targetCulture, identifyingLetter, usePeriodAfterAbbreviatedMonthName); } #endregion #region different years else { outputText = FormatDateRange(dateStart, dateEnd, outputFormatDateRangeDifferentYearsBibliography, targetCulture, identifyingLetter, usePeriodAfterAbbreviatedMonthName); } #endregion } #endregion #region PlaceholderCitation else if (isPlaceholderCitation) { #region same year, same month if (dateStart.Year == dateEnd.Year && dateStart.Month == dateEnd.Month && dateStart.Day != dateEnd.Day) { outputText = FormatDateRange(dateStart, dateEnd, outputFormatDateRangeSameYearSameMonthPlaceholder, targetCulture, identifyingLetter, usePeriodAfterAbbreviatedMonthName); } #endregion #region same year, different months else if (dateStart.Year == dateEnd.Year && dateStart.Month != dateEnd.Month) { outputText = FormatDateRange(dateStart, dateEnd, outputFormatDateRangeSameYearDifferentMonthPlaceholder, targetCulture, identifyingLetter, usePeriodAfterAbbreviatedMonthName); } #endregion #region different years else { outputText = FormatDateRange(dateStart, dateEnd, outputFormatDateRangeDifferentYearsPlaceholder, targetCulture, identifyingLetter, usePeriodAfterAbbreviatedMonthName); } #endregion } #endregion #region Other else { handled = false; return(null); } #endregion } #endregion #region Do the output if (!string.IsNullOrEmpty(outputText)) { var outputTextUnits = new TextUnitCollection(); outputTextUnits = TextUnitCollectionUtility.TaggedTextToTextUnits(dateFieldElement, outputText, fontStyle); if (outputTextUnits.Any()) { List <ITextUnit> componentPartOutput = new List <ITextUnit>(); foreach (IElement element in componentPart.Elements) { if (element == dateFieldElement) { componentPartOutput.AddRange(outputTextUnits); } else { componentPartOutput.AddRange(element.GetTextUnits(citation, template)); } } handled = true; return(componentPartOutput); } } #endregion handled = false; return(null); }
public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { //return handled = true if this macro generates the output (as an IEnumerable<ITextUnit>); the standard output will be suppressed //return handled = false if you want Citavi to produce the standard output; handled = false; if (componentPart == null) { return(null); } if (componentPart.Elements == null) { return(null); } if (citation == null) { return(null); } if (citation.Reference == null) { return(null); } PageRangeFieldElement pageRangeFieldElement = componentPart.Elements.OfType <PageRangeFieldElement>().FirstOrDefault(); if (pageRangeFieldElement == null) { return(null); } PageRange pageRangeResolved = PageRange.Empty; if (pageRangeFieldElement is QuotationPageRangeFieldElement) { PlaceholderCitation placeholderCitation = citation as PlaceholderCitation; if (placeholderCitation == null) { return(null); } if (placeholderCitation.Entry == null) { return(null); } pageRangeResolved = placeholderCitation.Entry.PageRange; } else { pageRangeResolved = citation.Reference.PageRange; } if (pageRangeResolved == null) { return(null); } if (pageRangeResolved == PageRange.Empty) { return(null); } if (pageRangeResolved.NumeralSystem != NumeralSystem.Omit) { return(null); } LiteralElement prefix = null; LiteralElement suffix = null; switch (pageRangeResolved.NumberingType) { case NumberingType.Page: prefix = pageRangeFieldElement.PageMultiPrefix; suffix = pageRangeFieldElement.PageMultiSuffix; break; case NumberingType.Column: prefix = pageRangeFieldElement.ColumnMultiPrefix; suffix = pageRangeFieldElement.ColumnMultiSuffix; break; case NumberingType.Paragraph: prefix = pageRangeFieldElement.ParagraphMultiPrefix; suffix = pageRangeFieldElement.ParagraphMultiSuffix; break; case NumberingType.Margin: prefix = pageRangeFieldElement.MarginMultiPrefix; suffix = pageRangeFieldElement.MarginMultiSuffix; break; case NumberingType.Other: prefix = pageRangeFieldElement.OtherMultiPrefix; suffix = pageRangeFieldElement.OtherMultiSuffix; break; } bool hasPrefix = prefix != null && !string.IsNullOrEmpty(prefix.Text); bool hasSuffix = suffix != null && !string.IsNullOrEmpty(suffix.Text); TextUnitCollection pageRangeTextUnits = pageRangeFieldElement.GetTextUnits(citation, template); if (pageRangeTextUnits == null || !pageRangeTextUnits.Any()) { return(null); } foreach (ITextUnit unit in pageRangeTextUnits) { unit.Text = unit.Text.Replace(System.StringUtility.Divis, System.StringUtility.EnDash); } TextUnitCollection output = new TextUnitCollection(); if (hasPrefix && pageRangeTextUnits.First().Text != prefix.Text) { TextUnitCollection prefixTextUnits = prefix.GetTextUnits(citation, template); if (prefixTextUnits != null && prefixTextUnits.Any()) { output.AddRange(prefixTextUnits); } } output.AddRange(pageRangeTextUnits); if (hasSuffix && pageRangeTextUnits.Last().Text != suffix.Text) { TextUnitCollection suffixTextUnits = suffix.GetTextUnits(citation, template); if (suffixTextUnits != null && suffixTextUnits.Any()) { output.AddRange(suffixTextUnits); } } componentPart.Elements.ReplaceItem(pageRangeFieldElement, TextUnitCollectionUtility.TextUnitsToLiteralElements(output, componentPart)); handled = false; //all literal elements should always be output: foreach (LiteralElement literalElement in componentPart.Elements.OfType <LiteralElement>()) { literalElement.ApplyCondition = ElementApplyCondition.Always; } return(null); }
public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { handled = true; if (citation == null) { return(null); } if (citation.Reference == null) { return(null); } if (componentPart == null) { return(null); } if (componentPart.Elements == null || componentPart.Elements.Count == 0) { return(null); } #region Determine reference to look at & reference language Reference reference = citation.Reference; if (reference == null) { return(null); } Reference parentReference = reference.ParentReference; string language = string.Empty; if (componentPart.Scope == ComponentPartScope.Reference) { language = reference.Language.ToUpperInvariant(); } else if (componentPart.Scope == ComponentPartScope.ParentReference) { if (parentReference == null) { return(null); } language = parentReference.Language.ToUpperInvariant(); } if (string.IsNullOrEmpty(language)) { return(null); } #endregion Determine reference to look at string outputString = string.Empty; TextUnitCollection output = new TextUnitCollection(); LiteralTextUnit text; #region German if (language.Contains("DE")) { outputString = "ebd."; handled = true; } #endregion #region English else if (language.Contains("EN")) { outputString = "ibid."; handled = true; } #endregion #region French else if (language.Contains("FR")) { outputString = "ibid."; handled = true; } #endregion #region Other else { outputString = "ebd."; handled = true; } #endregion text = new LiteralTextUnit(outputString, FontStyle.Neutral); // text = new LiteralTextUnit(outputString, FontStyle.Bold | FontStyle.SmallCaps); // output.Add(new LiteralTextUnit("[", FontStyle.Neutral)); output.Add(text); // output.Add(new LiteralTextUnit("]", FontStyle.Neutral)); return(output); }
//Version 2.0 script can be attached to both date/time field element as well as text field element //Format date range (if applicable) //Enter date range as: dd.MM.yyyy - dd.MM.yyyy //Format it to: dd.-dd. MMMM yyyy e.g. public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { CultureInfo targetCulture = CultureInfo.CreateSpecificCulture("de-DE"); handled = false; if (citation == null) { return(null); } if (citation.Reference == null) { return(null); } if (componentPart == null) { return(null); } if (componentPart.Elements == null || componentPart.Elements.Count != 1) { return(null); } FieldElement firstDateContainingFieldElement = componentPart.Elements.Where(element => { DateTimeFieldElement dateTimeFieldElement = element as DateTimeFieldElement; if (dateTimeFieldElement != null) { return(true); } TextFieldElement textFieldElement = element as TextFieldElement; if (textFieldElement != null) { return(true); } return(false); }).FirstOrDefault() as FieldElement; if (firstDateContainingFieldElement == null) { return(null); } ReferencePropertyId referencePropertyId = firstDateContainingFieldElement.PropertyId; Reference referenceResolved = componentPart.Scope == ComponentPartScope.ParentReference ? citation.Reference.ParentReference : citation.Reference; if (referenceResolved == null) { return(null); } string date = referenceResolved.GetValue(referencePropertyId) as string; if (string.IsNullOrEmpty(date)) { return(null); } FontStyle fontStyle = firstDateContainingFieldElement is DateTimeFieldElement ? ((DateTimeFieldElement)firstDateContainingFieldElement).FontStyle : ((TextFieldElement)firstDateContainingFieldElement).FontStyle; DateTime dateSingle; DateTime dateA; DateTime dateB; CultureInfo deDE = new CultureInfo("de-DE"); string[] formats = new string[] { "dd.MM.yyyy", "d.M.yyyy", "d.MM.yyyy", "dd.M.yyyy", "dd.MM.yy", "d.M.yy", "d.MM.yy", "dd.M.yy" }; //try single date first var found = DateTime.TryParseExact(date, formats, deDE, DateTimeStyles.None, out dateSingle); if (found) { var monthStringShort = dateSingle.ToString("MMM", targetCulture); var monthStringLong = dateSingle.ToString("MMMM", targetCulture); var monthString = monthStringShort == monthStringLong ? monthStringShort : monthStringShort + "."; var yearString = dateSingle.ToString("yyyy"); var dayString = dateSingle.Day.ToString("D2"); var outputFormatSingle = "{0} {1}, {2}"; var dateSingleText = string.Format(outputFormatSingle, monthString, dayString, yearString); var outputSingleDate = new TextUnitCollection(); var textSingleDate = new LiteralTextUnit(dateSingleText); textSingleDate.FontStyle = fontStyle; outputSingleDate.Add(textSingleDate); handled = true; return(outputSingleDate); } //then try date range List <string> dates = date.Split('-').Select(d => d.Trim()).ToList(); if (dates.Count != 2) { return(null); } var foundA = DateTime.TryParseExact(dates.ElementAt(0), formats, deDE, DateTimeStyles.None, out dateA); var foundB = DateTime.TryParseExact(dates.ElementAt(1), formats, deDE, DateTimeStyles.None, out dateB); if (!foundA || !foundB) { return(null); } var monthAStringShort = dateA.ToString("MMM", targetCulture); var monthAStringLong = dateA.ToString("MMMM", targetCulture); var monthAString = monthAStringShort == monthAStringLong ? monthAStringShort : monthAStringShort + "."; var monthBStringShort = dateB.ToString("MMM", targetCulture); var monthBStringLong = dateB.ToString("MMMM", targetCulture); var monthBString = monthBStringShort == monthBStringLong ? monthBStringShort : monthBStringShort + "."; var yearAString = dateA.ToString("yyyy"); var yearBString = dateB.ToString("yyyy"); var dayAString = dateA.Day.ToString("D2"); var dayBString = dateB.Day.ToString("D2"); string outputFormat = string.Empty; string dateRangeText = string.Empty; //same year, same month if (dateA.Year == dateB.Year && dateA.Month == dateB.Month && dateA.Day != dateB.Day) { outputFormat = "{0}.-{1}. {2} {3}"; //e.g. 08.-11. September 2013 dateRangeText = string.Format(outputFormat, dayAString, dayBString, monthAStringLong, yearAString); } //same year, different months else if (dateA.Year == dateB.Year && dateA.Month != dateB.Month) { outputFormat = "{0}. {1} - {2}. {3} {4}"; //e.g. 27. September - 04. Oktober 2013 dateRangeText = string.Format(outputFormat, dayAString, monthAStringLong, dayBString, monthBStringLong, yearAString); } //different years else { outputFormat = "{0}. {1} {2} - {3}. {4} {5}"; //e.g. 27. Dezember 2013 - 04. Januar 2014 dateRangeText = string.Format(outputFormat, dayAString, monthAStringLong, yearAString, dayBString, monthBStringLong, yearBString); } var output = new TextUnitCollection(); var text = new LiteralTextUnit(dateRangeText); text.FontStyle = fontStyle; output.Add(text); handled = true; return(output); }
public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { handled = false; if (template == null) { return(null); } if (citation == null) { return(null); } if (citation.Reference == null) { return(null); } if (componentPart == null) { return(null); } if (componentPart.Elements == null) { return(null); } //let Citavi do the apply conditions on elements inside the component part var originalTextUnits = componentPart.GetTextUnitsUnfiltered(citation, template); if (originalTextUnits == null) { return(null); } var outputTextUnits = new TextUnitCollection(); foreach (var textUnit in originalTextUnits) { var literalTextUnit = textUnit as LiteralTextUnit; if (literalTextUnit != null) { var text = literalTextUnit.Text; if (!string.IsNullOrEmpty(text)) { var originalFontStyle = literalTextUnit.FontStyle; if (literalTextUnit.HasTemporaryFontStyle()) { originalFontStyle |= literalTextUnit.TemporaryFontStyle; } var newTextUnits = literalTextUnit.LiteralElement.TaggedTextToTextUnits(text, originalFontStyle); if (newTextUnits != null && newTextUnits.Count > 0) { outputTextUnits.AddRange(newTextUnits); } } continue; } var fieldTextUnit = textUnit as FieldTextUnit; if (fieldTextUnit != null) { var text = fieldTextUnit.Text; if (!string.IsNullOrEmpty(text)) { var originalFontStyle = fieldTextUnit.FontStyle; if (fieldTextUnit.HasTemporaryFontStyle()) { originalFontStyle |= fieldTextUnit.TemporaryFontStyle; } var newTextUnits = fieldTextUnit.FieldElement.TaggedTextToTextUnits(text, originalFontStyle); if (newTextUnits != null && newTextUnits.Count > 0) { outputTextUnits.AddRange(newTextUnits); } } } } handled = true; return(outputTextUnits); }
public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { handled = false; //make sure, the important variables have data if (citation == null) { return(null); } Reference reference = citation.Reference; if (reference == null || reference.Project == null) { return(null); } if (reference.ReferenceType != ReferenceType.JournalArticle) { return(null); } if (!string.IsNullOrEmpty(reference.TitleSupplement)) { return(null); //Diese Zeile ggf. durch // auskommentieren, falls auch bereits gefüllte Titelzusätze-Felder überschrieben werden sollen } if (reference.Periodical == null) { return(null); } //find the other reference via entity links if (reference.Project.EntityLinks == null || !reference.Project.EntityLinks.Any()) { return(null); } List <Reference> referencesLinkedFromThis = (from link in reference.Project.EntityLinks.FindLinksForSource((ICitaviEntity)reference) where link.Target is Reference select(Reference) link.Target).ToList(); if (referencesLinkedFromThis == null || !referencesLinkedFromThis.Any()) { return(null); } List <Reference> referencesLinkingToThis = (from link in reference.Project.EntityLinks.FindLinksForTarget((ICitaviEntity)reference) where link.Source is Reference select(Reference) link.Source).ToList(); if (referencesLinkingToThis == null || !referencesLinkingToThis.Any()) { return(null); } //find first reference with a bidirectional link and treat that as the parrallel publication Reference otherReference = referencesLinkedFromThis.Intersect(referencesLinkingToThis).FirstOrDefault(); if (otherReference == null) { return(null); } if (otherReference.Periodical == null || string.IsNullOrEmpty(otherReference.Periodical.FullName)) { return(null); } //prepare this component's output var output = new TextUnitCollection(); StringBuilder outputTagged = new StringBuilder(); #region title of the article //wird für den Stil "Angewandte Chemie" nicht benötigt /** if (!string.IsNullOrEmpty(otherReference.Title)) * { * if (output.Count > 0) * { * outputTagged.Append(", "); * output.Add(new LiteralTextUnit("; ", FontStyle.Neutral)); * } * outputTagged.Append("<i>"); * outputTagged.Append(otherReference.Title); * outputTagged.Append("</i>"); * output.Add(new LiteralTextUnit(otherReference.Title, FontStyle.Neutral)); * } **/ #endregion #region periodical name if (!string.IsNullOrEmpty(otherReference.Periodical.StandardAbbreviation)) { /** if (output.Count > 0) //wird benötigt, falls zuvor noch der Aufsatztitel ausgegeben wird * { * outputTagged.Append(", "); * output.Add(new LiteralTextUnit(", ", FontStyle.Neutral)); * }**/ outputTagged.Append("<i>"); outputTagged.Append(otherReference.Periodical.StandardAbbreviation); outputTagged.Append("</i>"); output.Add(new LiteralTextUnit(otherReference.Periodical.StandardAbbreviation, FontStyle.Italic)); } else if (!string.IsNullOrEmpty(otherReference.Periodical.Name)) { /** if (output.Count > 0) //wird benötigt, falls zuvor noch der Aufsatztitel ausgegeben wird * { * outputTagged.Append(", "); * output.Add(new LiteralTextUnit(", ", FontStyle.Neutral)); * }**/ outputTagged.Append("<i>"); outputTagged.Append(otherReference.Periodical.Name); outputTagged.Append("</i>"); output.Add(new LiteralTextUnit(otherReference.Periodical.Name, FontStyle.Italic)); } #endregion #region year if (!string.IsNullOrEmpty(otherReference.YearResolved)) { if (output.Count > 0) { outputTagged.Append(", "); output.Add(new LiteralTextUnit(", ", FontStyle.Neutral)); // output.Add(new LiteralTextUnit(" (", FontStyle.Bold)); } outputTagged.Append("<b>"); // outputTagged.Append(" ("); outputTagged.Append(otherReference.YearResolved); // outputTagged.Append(")"); outputTagged.Append("</b>"); output.Add(new LiteralTextUnit(otherReference.YearResolved, FontStyle.Bold)); // output.Add(new LiteralTextUnit(")", FontStyle.Bold)); } #endregion #region volumne number if (!string.IsNullOrEmpty(otherReference.Volume)) { if (output.Count > 0) { outputTagged.Append(", "); output.Add(new LiteralTextUnit(", ", FontStyle.Neutral)); } outputTagged.Append("<i>"); outputTagged.Append(otherReference.Volume); outputTagged.Append("</i>"); output.Add(new LiteralTextUnit(otherReference.Volume, FontStyle.Italic)); } #endregion #region page range if (!string.IsNullOrEmpty(otherReference.PageRange.ToString())) { if (output.Count > 0) { outputTagged.Append(", "); output.Add(new LiteralTextUnit(", ", FontStyle.Neutral)); } outputTagged.Append(otherReference.PageRange.ToString()); output.Add(new LiteralTextUnit(otherReference.PageRange.ToString(), FontStyle.Neutral)); } #endregion #region DOI //wird für den Stil "Angewandte Chemie" nicht benötigt /** if (!string.IsNullOrEmpty(otherReference.Doi)) * { * if (output.Count > 0) * { * outputTagged.Append(", https://doi.org/"); * output.Add(new LiteralTextUnit(", https://doi.org/", FontStyle.Neutral)); * } * // outputTagged.Append("<i>"); * outputTagged.Append(otherReference.Doi); * // outputTagged.Append("</i>"); * output.Add(new LiteralTextUnit(otherReference.Doi, FontStyle.Neutral)); * } **/ #endregion if (reference.Project.DesktopProjectConfiguration != null && reference.Project.DesktopProjectConfiguration.Permissions != null && reference.Project.DesktopProjectConfiguration.Permissions.DbPermission != null && reference.Project.DesktopProjectConfiguration.Permissions.DbPermission == ProjectDbPermission.Write) { reference.TitleSupplementTagged = outputTagged.ToString(); } handled = true; return(output); }
public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { //change the following to "true", if you want "ders."/"dies." written in italics or other font styles var outputInItalics = true; var outputInSmallCaps = false; var outputInBold = false; var outputUnderlined = false; //NOTE: If you want a prefix such as "In: " and a suffix " (Hrsg)", you can define them as group prefix and suffix on the field element inside the component part editor handled = false; if (componentPart == null) { return(null); } if (componentPart.Elements == null || !componentPart.Elements.Any()) { return(null); } if (citation == null) { return(null); } Reference reference = citation.Reference; if (reference == null) { return(null); } Reference parentReference = citation.Reference.ParentReference; #region ThisPersonFieldElement PersonFieldElement thisPersonFieldElement = GetPersonFieldElement(componentPart); if (thisPersonFieldElement == null) { return(null); } #endregion #region ThesePersons List <Person> thesePersons = GetPersonsDisplayed(thisPersonFieldElement, reference); #endregion #region PreviousPersonFieldElement PersonFieldElement previousPersonFieldElement = GetPreviousPersonFieldElement(thisPersonFieldElement, template, reference); if (previousPersonFieldElement == null) { return(null); } #endregion #region PreviousPersons List <Person> previousPersons = GetPersonsDisplayed(previousPersonFieldElement, reference); if (previousPersons == null || !previousPersons.Any()) { return(null); } #endregion bool usePlural = thesePersons.Count > 1; PersonEquality equality = CheckPersonEquality(thesePersons, previousPersons); if (equality == PersonEquality.None) { return(null); } //we DO have some equality, so let's check what we need to output instead of the person's name(s) var textIdem = string.Empty; switch (equality) { case PersonEquality.M: case PersonEquality.N: textIdem = "ders."; break; default: //all others textIdem = "dies."; break; } TextUnitCollection output = new TextUnitCollection(); #region GroupPrefix if (usePlural && !string.IsNullOrEmpty(thisPersonFieldElement.GroupPrefixPlural.Text)) { output.Add(new LiteralTextUnit(thisPersonFieldElement.GroupPrefixPlural.Text, thisPersonFieldElement.GroupPrefixPlural.FontStyle)); } else if (!usePlural & !string.IsNullOrEmpty(thisPersonFieldElement.GroupPrefixSingular.Text)) { output.Add(new LiteralTextUnit(thisPersonFieldElement.GroupPrefixSingular.Text, thisPersonFieldElement.GroupPrefixSingular.FontStyle)); } #endregion GroupPrefix SwissAcademic.Drawing.FontStyle fontStyle; fontStyle = SwissAcademic.Drawing.FontStyle.Neutral; if (outputInItalics) { fontStyle |= SwissAcademic.Drawing.FontStyle.Italic; } if (outputInSmallCaps) { fontStyle |= SwissAcademic.Drawing.FontStyle.SmallCaps; } if (outputInBold) { fontStyle |= SwissAcademic.Drawing.FontStyle.Bold; } if (outputUnderlined) { fontStyle |= SwissAcademic.Drawing.FontStyle.Underline; } var fontStyleNeutral = SwissAcademic.Drawing.FontStyle.Neutral; output.Add(new LiteralTextUnit(textIdem, fontStyle)); #region GroupSuffix if (usePlural && !string.IsNullOrEmpty(thisPersonFieldElement.GroupSuffixPlural.Text)) { output.Add(new LiteralTextUnit(thisPersonFieldElement.GroupSuffixPlural.Text, thisPersonFieldElement.GroupSuffixPlural.FontStyle)); } else if (!usePlural && !string.IsNullOrEmpty(thisPersonFieldElement.GroupSuffixSingular.Text)) { output.Add(new LiteralTextUnit(thisPersonFieldElement.GroupSuffixSingular.Text, thisPersonFieldElement.GroupSuffixSingular.FontStyle)); } #endregion GroupSuffix //inject this as LiteralElements into the componentPart, replacing the editors person field element componentPart.Elements.ReplaceItem(thisPersonFieldElement, TextUnitsToLiteralElements(output, componentPart)); //for some reason this does not work //all literal elements should always be output: foreach (LiteralElement literalElement in componentPart.Elements.OfType <LiteralElement>()) { literalElement.ApplyCondition = ElementApplyCondition.Always; } handled = false; return(null); }