Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        private static PersonFieldElement GetPreviousPersonFieldElement(PersonFieldElement thisPersonFieldElement, Template template, Citation citation)
        {
            if (thisPersonFieldElement == null)
            {
                return(null);
            }
            if (template == null || template.ComponentParts == null || !template.ComponentParts.Any())
            {
                return(null);
            }

            IEnumerable <PersonFieldElement> allPersonFieldElements = template.ComponentParts.SelectMany(part => part.Elements).OfType <PersonFieldElement>().ToList();

            if (allPersonFieldElements == null || !allPersonFieldElements.Any())
            {
                return(null);
            }

            int thisIndex = allPersonFieldElements.FindIndex <PersonFieldElement>(item => item.Id == thisPersonFieldElement.Id);

            if (thisIndex == -1 || thisIndex == 0)
            {
                return(null);
            }

            for (int i = thisIndex - 1; i >= 0; i--)
            {
                PersonFieldElement previousPersonFieldElement = allPersonFieldElements.ElementAtOrDefault <PersonFieldElement>(i) as PersonFieldElement;
                if (previousPersonFieldElement == null)
                {
                    continue;
                }

                IEnumerable <Person> previousPersons = previousPersonFieldElement.GetPersons(citation);
                if (previousPersons == null || !previousPersons.Any())
                {
                    continue;
                }

                return(previousPersonFieldElement);
            }

            return(null);
        }
Esempio n. 3
0
        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 (citation == null || citation.Reference == null)
            {
                return(null);
            }
            if (componentPart == null || componentPart.Elements == null || !componentPart.Elements.Any())
            {
                return(null);
            }

            Reference reference = citation.Reference;

            if (reference == null)
            {
                return(null);
            }

            #region ThisPersonFieldElement

            PersonFieldElement thisPersonFieldElement = GetFirstPersonFieldElement(componentPart);
            if (thisPersonFieldElement == null || thisPersonFieldElement.SuppressOutput)
            {
                return(null);
            }

            #endregion

            #region ThesePersons

            IEnumerable <Person> thesePersons = thisPersonFieldElement.GetPersons(citation);
            if (thesePersons == null || !thesePersons.Any())
            {
                return(null);
            }

            #endregion

            #region PreviousPersonFieldElement

            PersonFieldElement previousPersonFieldElement = GetPreviousPersonFieldElement(thisPersonFieldElement, template, citation);
            if (previousPersonFieldElement == null)
            {
                return(null);
            }

            #endregion

            #region PreviousPersons

            IEnumerable <Person> previousPersons = previousPersonFieldElement.GetPersons(citation);
            if (previousPersons == null || !previousPersons.Any())
            {
                return(null);
            }

            #endregion

            PersonTeamsCompareResult compareResult = GetPersonTeamsIdentityAndGenderStructure(thesePersons, previousPersons);
            if (compareResult.Identity == PersonTeamsIdentity.None)
            {
                return(null);
            }

            bool handleAlsoPartialIdentity = true;  //if true, both of the following cases (1) and (2) will be treated. Otherwise,
            //if false, only case (1) will be treated
            //(1) Watson, Mary / Smith, Emma, »An interesting contribution«, in: iidem (Eds.), Edited book with 2 editors, London 2012.
            //(2) Watson, Mary / Smith, Emma, »A rather boring contribution«, in: iidem / Green, Peter (Eds.), Edited book with 3 editors, London 2012.
            if (compareResult.Identity == PersonTeamsIdentity.Partial && !handleAlsoPartialIdentity)
            {
                return(null);
            }

            //if you want "ders."/"dies." written in italics or other font styles, choose the desired option, for example FontStyle.Italic, FontStyle.Bold or FontStyle.SmallCaps
            LiteralTextUnit idemSingularMaleLiteral    = new LiteralTextUnit("ders.", FontStyle.Neutral);
            LiteralTextUnit idemSingularFemaleLiteral  = new LiteralTextUnit("dies.", FontStyle.Neutral);
            LiteralTextUnit idemSingularNeutralLiteral = new LiteralTextUnit("dass.", FontStyle.Neutral);
            LiteralTextUnit idemPluralLiteral          = new LiteralTextUnit("dies.", FontStyle.Neutral);

            //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


            //CAUTION: This script will get more complex, if the separators between persons differ from group to group of for the last person
            LiteralTextUnit personSeparator = new LiteralTextUnit(thisPersonFieldElement.FirstGroupPersonSeparator.Text, thisPersonFieldElement.FirstGroupPersonSeparator.FontStyle);

            //we remove all separators, because some person names will have to be suppressed and we want to avoid excess separators such as the /'s in idem///John Smith
            thisPersonFieldElement.FirstGroupPersonSeparator.Text        = "";
            thisPersonFieldElement.FirstGroupLastPersonSeparator.Text    = "";
            thisPersonFieldElement.FirstGroupToSecondGroupSeparator.Text = "";
            thisPersonFieldElement.SecondGroupPersonSeparator.Text       = "";
            thisPersonFieldElement.SecondGroupLastPersonSeparator.Text   = "";


            AfterFormatPersonEventArgs afp;
            thisPersonFieldElement.PersonFormatter.AfterFormatPerson +=
                (sender, e) =>
            {
                afp = (AfterFormatPersonEventArgs)e;

                #region Full Identity

                if (compareResult.Identity == PersonTeamsIdentity.Full)
                {
                    if (afp.Index == 0)
                    {
                        afp.TextUnits.Clear();
                        switch (compareResult.GenderStructure)
                        {
                        case PersonTeamGenderStructure.SingleMale:
                        {
                            afp.TextUnits.Add(idemSingularMaleLiteral);
                        }
                        break;

                        case PersonTeamGenderStructure.SingleFemale:
                        {
                            afp.TextUnits.Add(idemSingularFemaleLiteral);
                        }
                        break;

                        case PersonTeamGenderStructure.SingleNeuter:
                        {
                            afp.TextUnits.Add(idemSingularNeutralLiteral);
                        }
                        break;

                        default:
                        {
                            afp.TextUnits.Add(idemPluralLiteral);
                        }
                        break;
                        }
                    }
                    else
                    {
                        afp.TextUnits.Clear();
                    }
                }

                #endregion

                #region Partial Identity

                else
                {
                    if (afp.Index == 0)
                    {
                        afp.TextUnits.Clear();
                        switch (compareResult.GenderStructure)
                        {
                        case PersonTeamGenderStructure.SingleMale:
                        {
                            afp.TextUnits.Add(idemSingularMaleLiteral);
                        }
                        break;

                        case PersonTeamGenderStructure.SingleFemale:
                        {
                            afp.TextUnits.Add(idemSingularFemaleLiteral);
                        }
                        break;

                        case PersonTeamGenderStructure.SingleNeuter:
                        {
                            afp.TextUnits.Add(idemSingularNeutralLiteral);
                        }
                        break;

                        default:
                        {
                            afp.TextUnits.Add(idemPluralLiteral);
                        }
                        break;
                        }
                    }
                    else if (afp.Index < compareResult.IdenticalPersonsCount)
                    {
                        afp.TextUnits.Clear();
                    }
                    else
                    {
                        afp.TextUnits.Insert(0, personSeparator);
                    }
                }

                #endregion
            };


            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;

            var useNonBreakingSpacesInAndBetweenFirstAndMiddleNames = false;                    //if true, then e.g. Meyers, J.°R.
            var useNonBreakingSpaceBetweenLastAndFirstName          = false;                    //if true, then e.g. Meyers,°John Richard
            var useNonBreakingSpaceBetweenPrefixAndName             = false;                    //if true, then e.g. von°Bülow, V.
            var useNonBreakingHyphenInFirstAndMiddleName            = false;                    //if true, then e.g. Ewing, J.-R.

            if (citation == null || citation.Reference == null)
            {
                return(null);
            }
            if (componentPart == null || componentPart.Elements == null || !componentPart.Elements.Any())
            {
                return(null);
            }

            Reference reference = citation.Reference;

            if (reference == null)
            {
                return(null);
            }

            #region ThisPersonFieldElement

            PersonFieldElement thisPersonFieldElement = GetFirstPersonFieldElement(componentPart);
            if (thisPersonFieldElement == null || thisPersonFieldElement.SuppressOutput)
            {
                return(null);
            }

            #endregion

            #region ThesePersons

            IEnumerable <Person> thesePersons = thisPersonFieldElement.GetPersons(citation);
            if (thesePersons == null || !thesePersons.Any())
            {
                return(null);
            }

            #endregion

            #region PreviousPersonFieldElement

            PersonFieldElement previousPersonFieldElement = GetPreviousPersonFieldElement(thisPersonFieldElement, template, citation);
            if (previousPersonFieldElement == null)
            {
                return(null);
            }

            #endregion

            #region PreviousPersons

            IEnumerable <Person> previousPersons = previousPersonFieldElement.GetPersons(citation);
            if (previousPersons == null || !previousPersons.Any())
            {
                return(null);
            }

            #endregion

            IEnumerable <PersonFieldElement> personFieldElements = componentPart.Elements.OfType <PersonFieldElement>();
            if (personFieldElements == null || personFieldElements.Count() == 0)
            {
                return(null);
            }

            PersonTeamsCompareResult compareResult = GetPersonTeamsIdentityAndGenderStructure(thesePersons, previousPersons);
            if (compareResult.Identity == PersonTeamsIdentity.None)             //return null; [old version without useNonBreakingSpace...]
            {
                foreach (PersonFieldElement element in personFieldElements)
                {
                    if (useNonBreakingSpacesInAndBetweenFirstAndMiddleNames)
                    {
                        element.FirstGroupUseNonBreakingSpaceInAndBetweenFirstAndMiddleNames  = true;
                        element.SecondGroupUseNonBreakingSpaceInAndBetweenFirstAndMiddleNames = true;
                        element.LastPersonUseNonBreakingSpaceInAndBetweenFirstAndMiddleNames  = true;
                    }

                    if (useNonBreakingSpaceBetweenLastAndFirstName)
                    {
                        element.FirstGroupUseNonBreakingSpaceBetweenLastAndFirstName  = true;
                        element.SecondGroupUseNonBreakingSpaceBetweenLastAndFirstName = true;
                        element.LastPersonUseNonBreakingSpaceBetweenLastAndFirstName  = true;
                    }

                    if (useNonBreakingSpaceBetweenPrefixAndName)
                    {
                        element.FirstGroupUseNonBreakingSpaceBetweenPrefixAndName  = true;
                        element.SecondGroupUseNonBreakingSpaceBetweenPrefixAndName = true;
                        element.LastPersonUseNonBreakingSpaceBetweenPrefixAndName  = true;
                    }

                    if (useNonBreakingHyphenInFirstAndMiddleName)
                    {
                        element.FirstGroupUseNonBreakingHyphenInFirstAndMiddleNames  = true;
                        element.SecondGroupUseNonBreakingHyphenInFirstAndMiddleNames = true;
                        element.LastPersonUseNonBreakingHyphenInFirstAndMiddleNames  = true;
                    }
                }

                //for downward compatibility of this script we make sure the separator is just a comma and not "comma + space"
                var currentVersion          = SwissAcademic.Environment.InformationalVersion;
                var maxVersionForWorkaround = new Version(6, 3, 5, 0);
                if (currentVersion <= maxVersionForWorkaround)
                {
                    foreach (PersonFieldElement element in personFieldElements)
                    {
                        element.FirstGroupLastNameFirstNameSeparator.Text  = ",";
                        element.SecondGroupLastNameFirstNameSeparator.Text = ",";
                        element.LastPersonLastNameFirstNameSeparator.Text  = ",";
                    }
                }

                //Citavi will do the rest, no need to return any output, since PersonTeamsIdentity.None = true
                return(null);
            }

            bool handleAlsoPartialIdentity = true;                         //if true, all of the following cases (1) - (3) will be treated. Otherwise,
            //if false, only case (2) will be treated
            bool confineReplacementToSingleAuthorOnPartialIdentity = true; //ignored if handleAlsoPartialIdentity = false;
            //if true, only cases (1) and (2) will be treated
            //(1) Watson, Mary, »An interesting contribution«, in: eadem / Smith, Emma (Eds.), Edited book with 2 editors, London 2012.
            //(2) Watson, Mary / Smith, Emma, »An interesting contribution«, in: iidem (Eds.), Edited book with 2 editors, London 2012.
            //(3) Watson, Mary / Smith, Emma, »A rather boring contribution«, in: iidem / Green, Peter (Eds.), Edited book with 3 editors, London 2012.
            if (compareResult.Identity == PersonTeamsIdentity.Partial && !handleAlsoPartialIdentity)
            {
                return(null);
            }
            if (compareResult.Identity == PersonTeamsIdentity.Partial && previousPersons.Count() > 1 && confineReplacementToSingleAuthorOnPartialIdentity)
            {
                return(null);
            }

            //if you want "ders."/"dies." written in italics or other font styles, choose the desired option, for example FontStyle.Italic, FontStyle.Bold or FontStyle.SmallCaps
            LiteralTextUnit idemSingularMaleLiteral    = new LiteralTextUnit("Ders.", FontStyle.Neutral);
            LiteralTextUnit idemSingularFemaleLiteral  = new LiteralTextUnit("Dies.", FontStyle.Neutral);
            LiteralTextUnit idemSingularNeutralLiteral = new LiteralTextUnit("Dass.", FontStyle.Neutral);
            LiteralTextUnit idemPluralLiteral          = new LiteralTextUnit("Dies.", FontStyle.Neutral);

            //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


            //CAUTION: This script will get more complex, if the separators between persons differ from group to group of for the last person
            LiteralTextUnit personSeparator = new LiteralTextUnit(thisPersonFieldElement.FirstGroupPersonSeparator.Text, thisPersonFieldElement.FirstGroupPersonSeparator.FontStyle);

            //we remove all separators, because some person names will have to be suppressed and we want to avoid excess separators such as the /'s in idem///John Smith
            thisPersonFieldElement.FirstGroupPersonSeparator.Text        = "";
            thisPersonFieldElement.FirstGroupLastPersonSeparator.Text    = "";
            thisPersonFieldElement.FirstGroupToSecondGroupSeparator.Text = "";
            thisPersonFieldElement.SecondGroupPersonSeparator.Text       = "";
            thisPersonFieldElement.SecondGroupLastPersonSeparator.Text   = "";


            AfterFormatPersonEventArgs afp;
            thisPersonFieldElement.PersonFormatter.AfterFormatPerson +=
                (sender, e) =>
            {
                afp = (AfterFormatPersonEventArgs)e;

                #region Full Identity

                if (compareResult.Identity == PersonTeamsIdentity.Full)
                {
                    if (afp.Index == 0)
                    {
                        afp.TextUnits.Clear();
                        switch (compareResult.GenderStructure)
                        {
                        case PersonTeamGenderStructure.SingleMale:
                        {
                            afp.TextUnits.Add(idemSingularMaleLiteral);
                        }
                        break;

                        case PersonTeamGenderStructure.SingleFemale:
                        {
                            afp.TextUnits.Add(idemSingularFemaleLiteral);
                        }
                        break;

                        case PersonTeamGenderStructure.SingleNeuter:
                        {
                            afp.TextUnits.Add(idemSingularNeutralLiteral);
                        }
                        break;

                        default:
                        {
                            afp.TextUnits.Add(idemPluralLiteral);
                        }
                        break;
                        }
                    }
                    else
                    {
                        afp.TextUnits.Clear();
                    }
                }

                #endregion

                #region Partial Identity

                else
                {
                    if (afp.Index == 0)
                    {
                        afp.TextUnits.Clear();
                        switch (compareResult.GenderStructure)
                        {
                        case PersonTeamGenderStructure.SingleMale:
                        {
                            afp.TextUnits.Add(idemSingularMaleLiteral);
                        }
                        break;

                        case PersonTeamGenderStructure.SingleFemale:
                        {
                            afp.TextUnits.Add(idemSingularFemaleLiteral);
                        }
                        break;

                        case PersonTeamGenderStructure.SingleNeuter:
                        {
                            afp.TextUnits.Add(idemSingularNeutralLiteral);
                        }
                        break;

                        default:
                        {
                            afp.TextUnits.Add(idemPluralLiteral);
                        }
                        break;
                        }
                    }
                    else if (afp.Index < compareResult.IdenticalPersonsCount)
                    {
                        afp.TextUnits.Clear();
                    }
                    else
                    {
                        afp.TextUnits.Insert(0, personSeparator);
                    }
                }

                #endregion
            };


            return(null);
        }
Esempio n. 5
0
        //Version 1.0a Add initials if there are other authors with the same last name (FIRST PERSON ONLY)
        public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled)
        {
            PersonNameOrder nameOrderForAmbiguityResolution       = PersonNameOrder.LastNameFirstName;
            NameFormat      firstNameFormatForAmbiguityResolution = NameFormat.Abbreviated;

            handled = false;

            if (componentPart == null)
            {
                return(null);
            }
            if (citation == null || citation.CitationManager == null)
            {
                return(null);
            }

            Reference reference = citation.Reference;

            if (reference == null)
            {
                return(null);
            }

            PersonFieldElement personFieldElement = componentPart.GetFieldElements().OfType <PersonFieldElement>().FirstOrDefault();

            if (personFieldElement == null)
            {
                return(null);
            }

            IEnumerable <Person> persons = personFieldElement.GetPersons(reference);

            if (persons == null || !persons.Any())
            {
                return(null);
            }

            Guid firstPersonId = persons.First <Person>().Id;

            #region BeforeFormatPerson

            BeforeFormatPersonEventArgs b;
            personFieldElement.PersonFormatter.BeforeFormatPerson +=
                (sender, e) =>
            {
                b = (BeforeFormatPersonEventArgs)e;
                if (b.Person == null)
                {
                    return;
                }
                if (!b.Person.Id.Equals(firstPersonId))
                {
                    return;
                }

                if (citation.CitationManager.IsCitedPersonLastNameAmbiguous(b.Person.LastName))
                {
                    b.NameOrder       = nameOrderForAmbiguityResolution;
                    b.FirstNameFormat = firstNameFormatForAmbiguityResolution;
                }
            };

            #endregion

            return(null);
        }